|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { main, MANIFEST_PATH, syncManifestVersions } from "../../scripts/sync-release-manifest.mjs"; |
| 3 | + |
| 4 | +const SAMPLE_MANIFEST = JSON.stringify( |
| 5 | + { |
| 6 | + "packages/loopover-mcp": "3.1.0", |
| 7 | + "packages/loopover-engine": "3.2.0", |
| 8 | + "packages/loopover-miner": "3.1.0", |
| 9 | + }, |
| 10 | + null, |
| 11 | + 2, |
| 12 | +); |
| 13 | + |
| 14 | +describe("syncManifestVersions (#release-manifest-drift)", () => { |
| 15 | + it("rewrites a stale entry to match the real package.json version", () => { |
| 16 | + const result = syncManifestVersions(SAMPLE_MANIFEST, { "packages/loopover-mcp": "3.1.1" }); |
| 17 | + expect(result.changed).toBe(true); |
| 18 | + expect(result.stale).toEqual([{ workspacePath: "packages/loopover-mcp", from: "3.1.0", to: "3.1.1" }]); |
| 19 | + expect(JSON.parse(result.content)["packages/loopover-mcp"]).toBe("3.1.1"); |
| 20 | + // Untouched entries survive the round-trip unchanged. |
| 21 | + expect(JSON.parse(result.content)["packages/loopover-engine"]).toBe("3.2.0"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("reports no drift and leaves content byte-identical when every version already matches", () => { |
| 25 | + const result = syncManifestVersions(SAMPLE_MANIFEST, { |
| 26 | + "packages/loopover-mcp": "3.1.0", |
| 27 | + "packages/loopover-engine": "3.2.0", |
| 28 | + "packages/loopover-miner": "3.1.0", |
| 29 | + }); |
| 30 | + expect(result.changed).toBe(false); |
| 31 | + expect(result.stale).toEqual([]); |
| 32 | + expect(result.content).toBe(SAMPLE_MANIFEST); |
| 33 | + }); |
| 34 | + |
| 35 | + it("ignores a package.json version for a workspace the manifest doesn't track", () => { |
| 36 | + const result = syncManifestVersions(SAMPLE_MANIFEST, { "packages/loopover-ui-kit": "9.9.9" }); |
| 37 | + expect(result.changed).toBe(false); |
| 38 | + expect(result.stale).toEqual([]); |
| 39 | + expect(JSON.parse(result.content)).not.toHaveProperty("packages/loopover-ui-kit"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("collects every stale entry, not just the first", () => { |
| 43 | + const result = syncManifestVersions(SAMPLE_MANIFEST, { |
| 44 | + "packages/loopover-mcp": "3.1.1", |
| 45 | + "packages/loopover-miner": "3.1.1", |
| 46 | + }); |
| 47 | + expect(result.stale.map((entry) => entry.workspacePath)).toEqual([ |
| 48 | + "packages/loopover-mcp", |
| 49 | + "packages/loopover-miner", |
| 50 | + ]); |
| 51 | + expect(JSON.parse(result.content)["packages/loopover-mcp"]).toBe("3.1.1"); |
| 52 | + expect(JSON.parse(result.content)["packages/loopover-miner"]).toBe("3.1.1"); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +function fakeIo(packageVersions: Record<string, string>) { |
| 57 | + const written = new Map<string, string>(); |
| 58 | + const readFileSync = vi.fn((path: string) => { |
| 59 | + if (path === MANIFEST_PATH) return SAMPLE_MANIFEST; |
| 60 | + const match = /^(.+)\/package\.json$/.exec(path); |
| 61 | + const key = match?.[1]; |
| 62 | + if (key !== undefined && key in packageVersions) { |
| 63 | + return JSON.stringify({ version: packageVersions[key] }); |
| 64 | + } |
| 65 | + throw new Error(`unexpected read: ${path}`); |
| 66 | + }); |
| 67 | + const writeFileSync = vi.fn((path: string, content: string) => { |
| 68 | + written.set(path, content); |
| 69 | + }); |
| 70 | + const log = vi.fn(); |
| 71 | + const error = vi.fn(); |
| 72 | + const exit = vi.fn(); |
| 73 | + return { readFileSync, writeFileSync, log, error, exit, written }; |
| 74 | +} |
| 75 | + |
| 76 | +describe("sync-release-manifest main (#release-manifest-drift)", () => { |
| 77 | + it("--check exits non-zero and never writes when the manifest is stale", () => { |
| 78 | + const io = fakeIo({ |
| 79 | + "packages/loopover-mcp": "3.1.1", |
| 80 | + "packages/loopover-engine": "3.2.0", |
| 81 | + "packages/loopover-miner": "3.1.1", |
| 82 | + }); |
| 83 | + |
| 84 | + const code = main(["--check"], io); |
| 85 | + |
| 86 | + expect(code).toBe(1); |
| 87 | + expect(io.exit).toHaveBeenCalledWith(1); |
| 88 | + expect(io.error).toHaveBeenCalledWith( |
| 89 | + expect.stringContaining("packages/loopover-mcp is 3.1.0, package.json says 3.1.1"), |
| 90 | + ); |
| 91 | + expect(io.error).toHaveBeenCalledWith(expect.stringContaining("run npm run release-manifest:sync")); |
| 92 | + expect(io.writeFileSync).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("--check exits 0 and logs a clean summary when everything already matches", () => { |
| 96 | + const io = fakeIo({ |
| 97 | + "packages/loopover-mcp": "3.1.0", |
| 98 | + "packages/loopover-engine": "3.2.0", |
| 99 | + "packages/loopover-miner": "3.1.0", |
| 100 | + }); |
| 101 | + |
| 102 | + const code = main(["--check"], io); |
| 103 | + |
| 104 | + expect(code).toBe(0); |
| 105 | + expect(io.exit).not.toHaveBeenCalled(); |
| 106 | + expect(io.writeFileSync).not.toHaveBeenCalled(); |
| 107 | + expect(io.log).toHaveBeenCalledWith( |
| 108 | + expect.stringContaining("checked 3 package version(s), all in sync"), |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it("without --check, writes the synced manifest and reports how many entries changed", () => { |
| 113 | + const io = fakeIo({ |
| 114 | + "packages/loopover-mcp": "3.1.1", |
| 115 | + "packages/loopover-engine": "3.2.0", |
| 116 | + "packages/loopover-miner": "3.1.1", |
| 117 | + }); |
| 118 | + |
| 119 | + const code = main([], io); |
| 120 | + |
| 121 | + expect(code).toBe(0); |
| 122 | + expect(io.exit).not.toHaveBeenCalled(); |
| 123 | + expect(io.writeFileSync).toHaveBeenCalledOnce(); |
| 124 | + const [writtenPath, writtenContent] = io.writeFileSync.mock.calls[0]!; |
| 125 | + expect(writtenPath).toBe(MANIFEST_PATH); |
| 126 | + expect(JSON.parse(writtenContent as string)).toEqual({ |
| 127 | + "packages/loopover-mcp": "3.1.1", |
| 128 | + "packages/loopover-engine": "3.2.0", |
| 129 | + "packages/loopover-miner": "3.1.1", |
| 130 | + }); |
| 131 | + expect(io.log).toHaveBeenCalledWith(expect.stringContaining("synced 2 of 3 package version(s)")); |
| 132 | + }); |
| 133 | + |
| 134 | + it("without --check, never writes when nothing is stale", () => { |
| 135 | + const io = fakeIo({ |
| 136 | + "packages/loopover-mcp": "3.1.0", |
| 137 | + "packages/loopover-engine": "3.2.0", |
| 138 | + "packages/loopover-miner": "3.1.0", |
| 139 | + }); |
| 140 | + |
| 141 | + const code = main([], io); |
| 142 | + |
| 143 | + expect(code).toBe(0); |
| 144 | + expect(io.writeFileSync).not.toHaveBeenCalled(); |
| 145 | + expect(io.log).toHaveBeenCalledWith(expect.stringContaining("checked of 3 package version(s)")); |
| 146 | + }); |
| 147 | +}); |
0 commit comments