Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import { describe, expect, it } from "vitest";import { activeCommitId, activeVersion, isRedirect, parseDiffRoute, parseInterdiffRoute, parseRange, resolveChange, seeAll, type PullView} from "./pullRoute";
const versions = [ { base: "b0", head: "h0" }, { base: "b1", head: "h1" }];
// dispatches on `..` the way the loader doesconst view = (versionParam: string, rangeParam?: string): PullView => { const result = versionParam.includes("..") ? parseInterdiffRoute(versions, versionParam, rangeParam) : parseDiffRoute(versions, versionParam, rangeParam); if (isRedirect(result)) throw new Error(`unexpected redirect: ${result.redirect}`); return result;};
describe("parseRange", () => { it("accepts every shape the appview accepts", () => { expect(parseRange("")).toEqual({ base: "", head: "" }); expect(parseRange(" ")).toEqual({ base: "", head: "" }); expect(parseRange("abc")).toEqual({ base: "", head: "abc" }); expect(parseRange("a..b")).toEqual({ base: "a", head: "b" }); expect(parseRange("..b")).toEqual({ base: "", head: "b" }); expect(parseRange("a..")).toEqual({ base: "a", head: "" }); });
it("rejects empty and multi ranges", () => { expect(parseRange("..")).toBeNull(); expect(parseRange("a...b")).toBeNull(); expect(parseRange("a..b..c")).toBeNull(); });});
describe("parseDiffRoute", () => { it("resolves latest and numbers to the version's own ends", () => { expect(view("latest")).toEqual({ mode: "diff", version: 1, base: "b1", head: "h1", isDiffBase: true, isDiffHead: true }); expect(view("0")).toMatchObject({ version: 0, base: "b0", head: "h0" }); });
it("takes an explicit range, and the base/head magic words", () => { expect(view("1", "c1..c2")).toMatchObject({ base: "c1", head: "c2", isDiffBase: false, isDiffHead: false }); // a bare rev is a head, and the words fall back to the version's ends expect(view("1", "c2")).toMatchObject({ base: "b1", head: "c2", isDiffBase: true }); expect(view("1", "base..head")).toMatchObject({ base: "b1", head: "h1" }); });
it("marks see-all only when both sides defaulted", () => { expect(seeAll(view("1"))).toBe(true); expect(seeAll(view("1", "base..head"))).toBe(true); expect(seeAll(view("1", "c1..c2"))).toBe(false); expect(seeAll(view("1", "..c2"))).toBe(false); });
it("redirects a bad version to latest and a bad range to the version", () => { expect(parseDiffRoute(versions, "9")).toEqual({ redirect: "latest" }); expect(parseDiffRoute(versions, "foo")).toEqual({ redirect: "latest" }); expect(parseDiffRoute(versions, "-1")).toEqual({ redirect: "latest" }); expect(parseDiffRoute(versions, "1", "a..b..c")).toEqual({ redirect: "version" }); });});
describe("parseInterdiffRoute", () => { it("parses both versions, resolves their ends, and takes the change filter", () => { expect(view("0..1")).toEqual({ mode: "interdiff", version1: 0, version2: 1, from: { base: "b0", head: "h0" }, to: { base: "b1", head: "h1" }, changeId: "" }); expect(view("0..1", "all")).toMatchObject({ changeId: "" }); expect(view("0..1", "kmzwvxqo")).toMatchObject({ changeId: "kmzwvxqo" }); });
it("bounds-checks both sides instead of panicking", () => { expect(parseInterdiffRoute(versions, "0..9")).toEqual({ redirect: "latest" }); expect(parseInterdiffRoute(versions, "foo..1")).toEqual({ redirect: "latest" }); expect(parseInterdiffRoute(versions, "..1")).toEqual({ redirect: "latest" }); });
it("reports the newer version as active, and see-all without a filter", () => { expect(activeVersion(view("0..1"))).toBe(1); expect(activeCommitId(view("0..1", "kmzwvxqo"))).toBe("kmzwvxqo"); expect(seeAll(view("0..1"))).toBe(true); expect(seeAll(view("0..1", "kmzwvxqo"))).toBe(false); });});
describe("resolveChange", () => { const bases = { from: "v1base", to: "v2base" }; const older = [{ hash: "aaa", parent: "aaaparent", changeId: "kmzwvxqo" }]; const newer = [ { hash: "bbb", parent: "bbbparent", changeId: "kmzwvxqo" }, { hash: "ccc", parent: "cccparent", changeId: "nvtsyrpq" } ];
it("interdiffs the two commits carrying the change", () => { expect(resolveChange("kmzwvxqo", older, newer, bases)).toEqual({ kind: "interdiff", from: { base: "aaaparent", head: "aaa" }, to: { base: "bbbparent", head: "bbb" } }); });
it("plain-diffs a change that only exists in the newer version", () => { expect(resolveChange("nvtsyrpq", older, newer, bases)).toEqual({ kind: "diff", spec: { base: "cccparent", head: "ccc" } }); });
it("reports a change the newer version does not have", () => { expect(resolveChange("zzzzzzzz", older, newer, bases)).toEqual({ kind: "missing" }); // a repo without jj change ids can never match expect(resolveChange("kmzwvxqo", older, [{ hash: "bbb" }], bases)).toEqual({ kind: "missing" }); });
it("falls back to the version base for a root commit", () => { expect( resolveChange("kmzwvxqo", [{ hash: "aaa", changeId: "kmzwvxqo" }], newer, bases) ).toEqual({ kind: "interdiff", from: { base: "v1base", head: "aaa" }, to: { base: "bbbparent", head: "bbb" } }); });});