|
| 1 | +import { describe, it, expect, mock } from "bun:test"; |
| 2 | + |
| 3 | +let baselineJson: unknown = []; |
| 4 | +mock.module("./fetch-retry", () => ({ |
| 5 | + fetchWithRetry: async (_url: string): Promise<Response> => ({ json: async () => baselineJson }) as Response, |
| 6 | +})); |
| 7 | + |
| 8 | +import { entryKey, fingerprint, diffCatalog, fetchBaseline, type DiffableEntry } from "./catalog-diff"; |
| 9 | + |
| 10 | +describe("entryKey", () => { |
| 11 | + it("combines sourceId and name", () => { |
| 12 | + expect(entryKey({ sourceId: "com.kapeli.dash", name: "Bash" })).toBe("com.kapeli.dash/Bash"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("distinguishes same name across sources", () => { |
| 16 | + const a = entryKey({ sourceId: "com.kapeli.dash", name: "Vim" }); |
| 17 | + const b = entryKey({ sourceId: "com.kapeli.cheatsheet", name: "Vim" }); |
| 18 | + expect(a).not.toBe(b); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("fingerprint", () => { |
| 23 | + it("dash: changes with revision or version list", () => { |
| 24 | + const base: DiffableEntry = { name: "Bash", sourceId: "com.kapeli.dash", revision: "9", versions: ["9"] }; |
| 25 | + expect(fingerprint(base)).toBe(fingerprint({ ...base })); |
| 26 | + expect(fingerprint({ ...base, revision: "10" })).not.toBe(fingerprint(base)); |
| 27 | + expect(fingerprint({ ...base, versions: ["9", "8"] })).not.toBe(fingerprint(base)); |
| 28 | + }); |
| 29 | + |
| 30 | + it("contrib: changes with archive or specific versions", () => { |
| 31 | + const base: DiffableEntry = { |
| 32 | + name: "Jest", |
| 33 | + sourceId: "com.kapeli.contrib", |
| 34 | + versions: ["29.0"], |
| 35 | + archive: "Jest.tgz", |
| 36 | + specificVersions: { "29.0": "versions/29.0/Jest.tgz" }, |
| 37 | + }; |
| 38 | + expect(fingerprint(base)).toBe(fingerprint({ ...base })); |
| 39 | + expect(fingerprint({ ...base, archive: "Jest-new.tgz" })).not.toBe(fingerprint(base)); |
| 40 | + expect(fingerprint({ ...base, specificVersions: { "29.0": "x", "28.0": "y" } })).not.toBe(fingerprint(base)); |
| 41 | + }); |
| 42 | + |
| 43 | + it("contrib: specific-version order does not matter", () => { |
| 44 | + const a: DiffableEntry = { |
| 45 | + name: "Jest", |
| 46 | + sourceId: "com.kapeli.contrib", |
| 47 | + versions: ["29.0"], |
| 48 | + archive: "Jest.tgz", |
| 49 | + specificVersions: { "29.0": "a", "28.0": "b" }, |
| 50 | + }; |
| 51 | + const b: DiffableEntry = { ...a, specificVersions: { "28.0": "b", "29.0": "a" } }; |
| 52 | + expect(fingerprint(a)).toBe(fingerprint(b)); |
| 53 | + }); |
| 54 | + |
| 55 | + it("contrib: changes when a specific-version archive path changes", () => { |
| 56 | + const a: DiffableEntry = { |
| 57 | + name: "Jest", |
| 58 | + sourceId: "com.kapeli.contrib", |
| 59 | + versions: ["29.0"], |
| 60 | + archive: "Jest.tgz", |
| 61 | + specificVersions: { "29.0": "versions/29.0/Jest.tgz" }, |
| 62 | + }; |
| 63 | + const b: DiffableEntry = { ...a, specificVersions: { "29.0": "versions/29.0/Jest-v2.tgz" } }; |
| 64 | + expect(fingerprint(a)).not.toBe(fingerprint(b)); |
| 65 | + }); |
| 66 | + |
| 67 | + it("cheatsheet: changes only with version", () => { |
| 68 | + const base: DiffableEntry = { name: "Vim", sourceId: "com.kapeli.cheatsheet", versions: ["1"] }; |
| 69 | + expect(fingerprint(base)).toBe(fingerprint({ ...base })); |
| 70 | + expect(fingerprint({ ...base, versions: ["2"] })).not.toBe(fingerprint(base)); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("diffCatalog", () => { |
| 75 | + const baseline: DiffableEntry[] = [ |
| 76 | + { name: "Bash", sourceId: "com.kapeli.dash", revision: "9", versions: ["9"] }, |
| 77 | + { name: "Go", sourceId: "com.kapeli.dash", revision: "1", versions: ["1.26"] }, |
| 78 | + { name: "Vim", sourceId: "com.kapeli.cheatsheet", versions: ["1"] }, |
| 79 | + ]; |
| 80 | + |
| 81 | + it("classifies unchanged, changed, added, removed", () => { |
| 82 | + const current: DiffableEntry[] = [ |
| 83 | + { name: "Bash", sourceId: "com.kapeli.dash", revision: "9", versions: ["9"] }, // unchanged |
| 84 | + { name: "Go", sourceId: "com.kapeli.dash", revision: "2", versions: ["1.27"] }, // changed |
| 85 | + { name: "Rust", sourceId: "com.kapeli.dash", revision: "1", versions: ["1.0"] }, // added |
| 86 | + // Vim cheatsheet dropped -> removed |
| 87 | + ]; |
| 88 | + const diff = diffCatalog(current, baseline); |
| 89 | + expect(diff.unchanged).toEqual(["com.kapeli.dash/Bash"]); |
| 90 | + expect(diff.changed).toEqual(["com.kapeli.dash/Go"]); |
| 91 | + expect(diff.added).toEqual(["com.kapeli.dash/Rust"]); |
| 92 | + expect(diff.removed).toEqual(["com.kapeli.cheatsheet/Vim"]); |
| 93 | + }); |
| 94 | + |
| 95 | + it("treats an empty baseline as all-added", () => { |
| 96 | + const diff = diffCatalog(baseline, []); |
| 97 | + expect(diff.added).toHaveLength(3); |
| 98 | + expect(diff.unchanged).toHaveLength(0); |
| 99 | + expect(diff.changed).toHaveLength(0); |
| 100 | + expect(diff.removed).toHaveLength(0); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("fetchBaseline", () => { |
| 105 | + it("drops malformed entries missing name or sourceId", async () => { |
| 106 | + baselineJson = [ |
| 107 | + { name: "Bash", sourceId: "com.kapeli.dash" }, |
| 108 | + { name: "NoSource" }, |
| 109 | + { sourceId: "com.kapeli.dash" }, |
| 110 | + null, |
| 111 | + ]; |
| 112 | + const entries = await fetchBaseline("https://example/catalog.json"); |
| 113 | + expect(entries).toEqual([{ name: "Bash", sourceId: "com.kapeli.dash" }]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("throws when the payload is not an array", async () => { |
| 117 | + baselineJson = { not: "an array" }; |
| 118 | + await expect(fetchBaseline("https://example/catalog.json")).rejects.toThrow(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments