import { describe, expect, it } from "vitest"; import { isPermanent, parsePatch, parseTicket, pullPipeline } from "../src/pulls"; import type { Patch } from "../src/pulls"; import { XrpcError } from "../src/xrpc"; import { repoDid as repo } from "./helpers"; const fork = "did:plc:ffffffffffffffffffffffff"; const sender = "did:plc:ssssssssssssssssssssssss"; const head = "a".repeat(40); function patch(overrides: object = {}): Record { return { $type: "org.tangled.review.patch", ticket: "3tik", base: { oid: "b".repeat(40) }, head: { oid: head }, source: { repo, branch: "feature" }, createdAt: "2026-11-07T00:00:00Z", "x-tngl-editor": sender, ...overrides, }; } describe("parsePatch", () => { it("reads the head, ticket, sender, and source", () => { expect(parsePatch(patch())).toEqual({ head, ticket: "3tik", sender, source: { repo, branch: "feature" }, }); }); it.each([ ["a 64-character head", { head: { oid: "c".repeat(64) } }, { head: "c".repeat(64) }], ["no source", { source: undefined }, { source: undefined }], ["a malformed source", { source: { repo: "nope", branch: "x" } }, { source: undefined }], ])("reads %s", (_, overrides, expected) => { expect(parsePatch(patch(overrides))).toMatchObject(expected); }); it.each([ ["no head", { head: undefined }], ["a short head", { head: { oid: "a".repeat(39) } }], ["a non-hex head", { head: { oid: "g".repeat(40) } }], ["no ticket", { ticket: undefined }], ["no editor", { "x-tngl-editor": undefined }], ["a malformed editor", { "x-tngl-editor": "nope" }], ])("refuses a patch with %s", (_, overrides) => { expect(typeof parsePatch(patch(overrides))).toBe("string"); }); }); describe("parseTicket", () => { it.each([ ["an open ticket", { state: "open", targetBranch: "main" }, "main"], ["a ticket without a state", { targetBranch: "main" }, "main"], ])("returns the target branch of %s", (_, ticket, expected) => { expect(parseTicket(ticket)).toBe(expected); }); it.each([ ["a closed ticket", { state: "closed", targetBranch: "main" }], ["a merged ticket", { state: "completed", targetBranch: "main" }], ["an issue", { state: "open" }], ["an empty target branch", { targetBranch: "" }], ])("refuses %s", (_, ticket) => { expect(parseTicket(ticket)).toBeUndefined(); }); }); describe("pullPipeline", () => { const base: Patch = { head, ticket: "3tik", sender, source: { repo, branch: "feature" } }; const pull = `at://${repo}/org.tangled.track.ticket/3tik`; it("builds a same-repo pull request on its source branch", () => { expect(pullPipeline(repo, "bafyp", base, "main", "opened")).toEqual({ trigger: { $type: "org.tangled.event.pullRequest", action: "opened", pull, sourceBranch: "feature", targetBranch: "main", sourceSha: head, ref: "refs/heads/feature", }, pushKey: `pull ${repo} bafyp`, commit: head, branch: "feature", sender, }); }); it.each<[string, Patch["source"], object]>([ ["a fork", { repo: fork, branch: "feature" }, { sourceRepo: fork }], ["no source", undefined, {}], ])("builds a pull request from %s on a pull branch", (_, source, extra) => { expect( pullPipeline(repo, "bafyp", { ...base, source }, "main", "synchronize"), ).toMatchObject({ trigger: { $type: "org.tangled.event.pullRequest", action: "synchronize", pull, targetBranch: "main", sourceSha: head, ref: "refs/heads/pull/3tik", ...extra, }, branch: "pull/3tik", }); expect( pullPipeline(repo, "bafyp", { ...base, source }, "main", "opened").trigger, ).not.toHaveProperty("sourceBranch"); }); }); describe("isPermanent", () => { it.each([ ["a refused ref", new XrpcError(422, "PushRefused", "x"), true], ["a missing record", new XrpcError(404, "NotFound", "x"), true], ["an upstream failure", new XrpcError(500, "InternalError", "x"), false], ["an unavailable upstream", new XrpcError(503, "UpstreamUnavailable", "x"), false], ["a network error", new TypeError("network"), false], ])("classifies %s", (_, error, expected) => { expect(isPermanent(error)).toBe(expected); }); });