|
| 1 | +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 5 | +import { findMissingDeps, parseLockfileDeps } from "./check-lockfile-sync.ts"; |
| 6 | + |
| 7 | +function makeLockfile(deps: Record<string, string>): string { |
| 8 | + const depsEntries = Object.entries(deps) |
| 9 | + .map(([k, v]) => ` "${k}": "${v}",`) |
| 10 | + .join("\n"); |
| 11 | + return [ |
| 12 | + "{", |
| 13 | + ' "lockfileVersion": 1,', |
| 14 | + ' "workspaces": {', |
| 15 | + ' "": {', |
| 16 | + ' "dependencies": {', |
| 17 | + depsEntries, |
| 18 | + " },", |
| 19 | + " },", |
| 20 | + " },", |
| 21 | + ' "packages": {}', |
| 22 | + "}", |
| 23 | + ].join("\n"); |
| 24 | +} |
| 25 | + |
| 26 | +describe("parseLockfileDeps", () => { |
| 27 | + test("extracts dependency names from lockfile content", () => { |
| 28 | + const content = makeLockfile({ |
| 29 | + "@r_masseater/cc-plugin-lib": "0.0.3", |
| 30 | + "cc-hooks-ts": "latest", |
| 31 | + yaml: "^2.8.3", |
| 32 | + }); |
| 33 | + const deps = parseLockfileDeps(content); |
| 34 | + expect(deps).toContain("@r_masseater/cc-plugin-lib"); |
| 35 | + expect(deps).toContain("cc-hooks-ts"); |
| 36 | + expect(deps).toContain("yaml"); |
| 37 | + }); |
| 38 | + |
| 39 | + test("handles scoped packages", () => { |
| 40 | + const content = makeLockfile({ "@google/genai": "^1.38.0" }); |
| 41 | + expect(parseLockfileDeps(content)).toContain("@google/genai"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("returns empty array for invalid content", () => { |
| 45 | + expect(parseLockfileDeps("not json at all {{{")).toStrictEqual([]); |
| 46 | + }); |
| 47 | + |
| 48 | + test("returns empty array for empty dependencies", () => { |
| 49 | + const content = makeLockfile({}); |
| 50 | + expect(parseLockfileDeps(content)).toStrictEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + test("handles lockfile without packages section", () => { |
| 54 | + const content = [ |
| 55 | + "{", |
| 56 | + ' "lockfileVersion": 1,', |
| 57 | + ' "workspaces": {', |
| 58 | + ' "": {', |
| 59 | + ' "dependencies": {', |
| 60 | + ' "yaml": "^2.8.3",', |
| 61 | + " },", |
| 62 | + " },", |
| 63 | + " }", |
| 64 | + "}", |
| 65 | + ].join("\n"); |
| 66 | + expect(parseLockfileDeps(content)).toContain("yaml"); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("findMissingDeps", () => { |
| 71 | + let pluginsDir: string; |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + pluginsDir = join(tmpdir(), `lockfile-sync-test-${Date.now()}`); |
| 75 | + mkdirSync(pluginsDir, { recursive: true }); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + rmSync(pluginsDir, { recursive: true, force: true }); |
| 80 | + }); |
| 81 | + |
| 82 | + test("returns empty when all deps are in lockfile", () => { |
| 83 | + const pluginDir = join(pluginsDir, "my-plugin"); |
| 84 | + mkdirSync(pluginDir); |
| 85 | + writeFileSync( |
| 86 | + join(pluginDir, "package.json"), |
| 87 | + JSON.stringify({ dependencies: { yaml: "^2.8.3" } }), |
| 88 | + ); |
| 89 | + writeFileSync(join(pluginDir, "bun.lock"), makeLockfile({ yaml: "^2.8.3" })); |
| 90 | + |
| 91 | + expect(findMissingDeps(pluginsDir)).toStrictEqual([]); |
| 92 | + }); |
| 93 | + |
| 94 | + test("reports missing dep", () => { |
| 95 | + const pluginDir = join(pluginsDir, "my-plugin"); |
| 96 | + mkdirSync(pluginDir); |
| 97 | + writeFileSync( |
| 98 | + join(pluginDir, "package.json"), |
| 99 | + JSON.stringify({ dependencies: { yaml: "^2.8.3", valibot: "^1.0.0" } }), |
| 100 | + ); |
| 101 | + writeFileSync(join(pluginDir, "bun.lock"), makeLockfile({ yaml: "^2.8.3" })); |
| 102 | + |
| 103 | + const missing = findMissingDeps(pluginsDir); |
| 104 | + expect(missing).toHaveLength(1); |
| 105 | + expect(missing[0]).toStrictEqual({ plugin: "my-plugin", dep: "valibot" }); |
| 106 | + }); |
| 107 | + |
| 108 | + test("skips plugins without lockfile", () => { |
| 109 | + const pluginDir = join(pluginsDir, "no-lock"); |
| 110 | + mkdirSync(pluginDir); |
| 111 | + writeFileSync( |
| 112 | + join(pluginDir, "package.json"), |
| 113 | + JSON.stringify({ dependencies: { yaml: "^2.8.3" } }), |
| 114 | + ); |
| 115 | + // No bun.lock |
| 116 | + |
| 117 | + expect(findMissingDeps(pluginsDir)).toStrictEqual([]); |
| 118 | + }); |
| 119 | + |
| 120 | + test("skips plugins without dependencies", () => { |
| 121 | + const pluginDir = join(pluginsDir, "no-deps"); |
| 122 | + mkdirSync(pluginDir); |
| 123 | + writeFileSync(join(pluginDir, "package.json"), JSON.stringify({})); |
| 124 | + writeFileSync(join(pluginDir, "bun.lock"), makeLockfile({})); |
| 125 | + |
| 126 | + expect(findMissingDeps(pluginsDir)).toStrictEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + test("detects scoped package missing from lockfile", () => { |
| 130 | + const pluginDir = join(pluginsDir, "scoped"); |
| 131 | + mkdirSync(pluginDir); |
| 132 | + writeFileSync( |
| 133 | + join(pluginDir, "package.json"), |
| 134 | + JSON.stringify({ dependencies: { "@scope/pkg": "^1.0.0" } }), |
| 135 | + ); |
| 136 | + writeFileSync(join(pluginDir, "bun.lock"), makeLockfile({})); |
| 137 | + |
| 138 | + const missing = findMissingDeps(pluginsDir); |
| 139 | + expect(missing).toHaveLength(1); |
| 140 | + expect(missing[0]).toStrictEqual({ plugin: "scoped", dep: "@scope/pkg" }); |
| 141 | + }); |
| 142 | + |
| 143 | + test("skips plugin with unparseable lockfile", () => { |
| 144 | + const pluginDir = join(pluginsDir, "broken"); |
| 145 | + mkdirSync(pluginDir); |
| 146 | + writeFileSync( |
| 147 | + join(pluginDir, "package.json"), |
| 148 | + JSON.stringify({ dependencies: { yaml: "^2.8.3" } }), |
| 149 | + ); |
| 150 | + writeFileSync(join(pluginDir, "bun.lock"), "completely invalid {{{"); |
| 151 | + |
| 152 | + // parseLockfileDeps returns [] → all deps appear missing |
| 153 | + const missing = findMissingDeps(pluginsDir); |
| 154 | + expect(missing).toHaveLength(1); |
| 155 | + expect(missing[0]!.dep).toBe("yaml"); |
| 156 | + }); |
| 157 | + |
| 158 | + test("returns empty for non-existent plugins directory", () => { |
| 159 | + expect(findMissingDeps("/nonexistent/path")).toStrictEqual([]); |
| 160 | + }); |
| 161 | +}); |
0 commit comments