import { afterEach, describe, expect, test } from "bun:test"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; import { commandExtensions, findNestedWorkspaceShadows, findSkcOnPath, isApprovedWorkspaceSource, isLocalWindowsBunShim, isLocalWorkspaceWrapper, pathDirs, resolvePackageFrom, smokeTest, } from "./dev-link"; const tempRoots: string[] = []; const shimFlags = (5478 << 3) | 0b101; async function makeExecutable(file: string, content: string): Promise { await fs.mkdir(path.dirname(file), { recursive: true }); await Bun.write(file, content); await fs.chmod(file, 0o755); } function bunMetadata(target = "@sayknow-cli\\coding-agent\\bin\\skc.js", command = "bun "): Buffer { const pathBytes = Buffer.from(target, "utf16le"); const framing = Buffer.from('"\0', "utf16le"); const shebang = Buffer.from(command, "utf16le"); const lengths = Buffer.alloc(8); lengths.writeUInt32LE(pathBytes.length, 0); lengths.writeUInt32LE(shebang.length, 4); const flags = Buffer.alloc(2); flags.writeUInt16LE(shimFlags, 0); return Buffer.concat([pathBytes, framing, shebang, lengths, flags]); } function windowsShimExecutable(): Buffer { const executable = Buffer.alloc(0x200); executable.write("MZ", 0); executable.writeUInt32LE(0x80, 0x3c); executable.write("PE\0\0", 0x80); executable.writeUInt16LE(1, 0x86); executable.writeUInt16LE(0, 0x94); executable.writeUInt32LE(0x140, 0x98 + 16); executable.writeUInt32LE(0xc0, 0x98 + 20); return executable; } function fixtureBun(root: string): string { return path.join(root, "bun.exe"); } function isFixtureShim(file: string, root: string): boolean { return isLocalWindowsBunShim(file, root, fixtureBun(root)); } function isFixtureSource(file: string, real: string | null, root: string, platform: NodeJS.Platform): boolean { return isApprovedWorkspaceSource(file, real, root, platform, fixtureBun(root)); } async function workspaceFixture(): Promise { const root = await fs.mkdtemp(path.join(os.tmpdir(), "skc-dev-link-windows-")); tempRoots.push(root); const packageRoot = path.join(root, "packages", "coding-agent"); await fs.mkdir(path.join(packageRoot, "bin"), { recursive: true }); await fs.mkdir(path.join(packageRoot, "src"), { recursive: true }); await Bun.write( path.join(packageRoot, "package.json"), JSON.stringify({ bin: { skc: "bin/skc.js" }, exports: { "./cli": "./src/cli.ts" } }), ); await Bun.write( path.join(packageRoot, "bin", "skc.js"), '#!/usr/bin/env bun\nimport { runCli } from "@sayknow-cli/coding-agent/cli";\n\nawait runCli(process.argv.slice(2));\n', ); await Bun.write(path.join(packageRoot, "src", "cli.ts"), "export {};\n"); await fs.mkdir(path.join(root, "node_modules", "@sayknow-cli"), { recursive: true }); await fs.symlink(packageRoot, path.join(root, "node_modules", "@sayknow-cli", "coding-agent"), "dir"); await fs.mkdir(path.join(root, "node_modules", ".bin"), { recursive: true }); await Bun.write(path.join(root, "node_modules", ".bin", "skc.exe"), windowsShimExecutable()); await Bun.write(path.join(root, "node_modules", ".bin", "skc.bunx"), bunMetadata()); await Bun.write(fixtureBun(root), Buffer.concat([Buffer.from("bun-runtime"), windowsShimExecutable(), Buffer.from("tail")])); return root; } afterEach(async () => { await Promise.all(tempRoots.splice(0).map(root => fs.rm(root, { force: true, recursive: true }))); }); describe("dev:link command discovery", () => { test("uses Windows PATH directory order and PATHEXT order with case-insensitive deduplication", async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "skc-dev-link-path-")); tempRoots.push(root); const first = path.join(root, "first"); const second = path.join(root, "second"); await makeExecutable(path.join(first, "skc.CMD"), ""); await makeExecutable(path.join(first, "skc.exe"), ""); await makeExecutable(path.join(second, "skc.exe"), ""); expect(pathDirs(`${first};${second}`, "win32")).toEqual([first, second]); expect(commandExtensions("win32", ".CMD;.exe;.CMD;.EXE")).toEqual([".CMD", ".exe"]); expect(findSkcOnPath(`${first};${second}`, "win32", ".CMD;.exe;.CMD;.EXE").map(hit => hit.file)).toEqual([ path.join(first, "skc.CMD"), path.join(first, "skc.exe"), path.join(second, "skc.exe"), ]); }); test("uses the documented Windows PATHEXT fallback and keeps Unix extensionless", () => { expect(commandExtensions("win32", "")).toEqual([".COM", ".EXE", ".BAT", ".CMD"]); expect(commandExtensions("linux", ".EXE")).toEqual([""]); }); }); describe.skipIf(process.platform === "win32")("dev:link workspace wrapper provenance", () => { test("accepts this checkout's bun-linked bin/skc.js wrapper on any platform", async () => { const root = await workspaceFixture(); const wrapper = path.join(root, "packages", "coding-agent", "bin", "skc.js"); expect(isLocalWorkspaceWrapper(wrapper, root)).toBe(true); expect(isFixtureSource(path.join(root, "global-bin", "skc"), wrapper, root, "linux")).toBe(true); }); test("rejects a wrapper whose contents drifted from the expected workspace wrapper", async () => { const root = await workspaceFixture(); const wrapper = path.join(root, "packages", "coding-agent", "bin", "skc.js"); await Bun.write(wrapper, '#!/usr/bin/env bun\nimport { runCli } from "sayknow-cli/cli";\n\nawait runCli([]);\n'); expect(isLocalWorkspaceWrapper(wrapper, root)).toBe(false); expect(isFixtureSource("ignored", wrapper, root, "linux")).toBe(false); }); test("rejects an identical wrapper that belongs to a different checkout", async () => { const root = await workspaceFixture(); const other = await workspaceFixture(); const foreignWrapper = path.join(other, "packages", "coding-agent", "bin", "skc.js"); expect(isLocalWorkspaceWrapper(foreignWrapper, root)).toBe(false); }); test("rejects the wrapper when the workspace no longer resolves the CLI to its own source", async () => { const root = await workspaceFixture(); const wrapper = path.join(root, "packages", "coding-agent", "bin", "skc.js"); await fs.rm(path.join(root, "node_modules", "@sayknow-cli", "coding-agent")); expect(isLocalWorkspaceWrapper(wrapper, root)).toBe(false); }); }); describe.skipIf(process.platform === "win32")("dev:link Windows Bun workspace shim provenance", () => { test("accepts a direct canonical source link and the exact valid Bun workspace shim", async () => { const root = await workspaceFixture(); const source = path.join(root, "packages", "coding-agent", "src", "cli.ts"); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); expect(isFixtureSource("ignored", source, root, "win32")).toBe(true); expect(isFixtureShim(shim, root)).toBe(true); expect(isFixtureSource(shim, null, root, "win32")).toBe(true); }); test("keeps smoke health independent from accepted provenance", async () => { const root = await workspaceFixture(); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); const failedSmoke = path.join(root, "failed-smoke"); await makeExecutable(failedSmoke, "#!/usr/bin/env sh\necho smoke-test: failed\nexit 1\n"); expect(isFixtureSource(shim, null, root, "win32")).toBe(true); expect(smokeTest(failedSmoke).ok).toBe(false); }); test("fails closed for missing, corrupt, foreign, or substituted shim metadata", async () => { const root = await workspaceFixture(); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); const metadata = path.join(root, "node_modules", ".bin", "skc.bunx"); await fs.rm(metadata); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, Buffer.from("corrupt")); expect(isFixtureShim(shim, root)).toBe(false); await fs.mkdir(path.join(root, "node_modules", "foreign", "bin"), { recursive: true }); await Bun.write(path.join(root, "node_modules", "foreign", "bin", "skc.js"), "#!/usr/bin/env bun\n"); await Bun.write(metadata, bunMetadata("foreign\\bin\\skc.js")); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, bunMetadata()); const trusted = windowsShimExecutable(); for (const fragment of [ trusted.subarray(0, 1), Buffer.from("MZ"), trusted.subarray(0, -1), Buffer.concat([trusted, Buffer.from("suffix")]), trusted.subarray(32, 96), ]) { await Bun.write(shim, fragment); expect(isFixtureShim(shim, root)).toBe(false); } const substituted = Buffer.from(trusted); substituted[0x1f0] = 1; Buffer.from("smoke-test: ok").copy(substituted, 0x1d0); await Bun.write(shim, substituted); expect(isFixtureShim(shim, root)).toBe(false); expect(substituted.includes(Buffer.from("smoke-test: ok"))).toBe(true); }); test("rejects inconsistent metadata lengths, trailing bytes, versions, flags, commands, and oversized files", async () => { const root = await workspaceFixture(); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); const metadata = path.join(root, "node_modules", ".bin", "skc.bunx"); const valid = bunMetadata(); const badLength = Buffer.from(valid); badLength.writeUInt32LE(badLength.readUInt32LE(badLength.length - 6) + 2, badLength.length - 6); await Bun.write(metadata, badLength); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, Buffer.concat([valid.subarray(0, -2), Buffer.from([0, 0]), valid.subarray(-2)])); expect(isFixtureShim(shim, root)).toBe(false); const wrongVersion = Buffer.from(valid); wrongVersion.writeUInt16LE((5477 << 3) | 0b101, wrongVersion.length - 2); await Bun.write(metadata, wrongVersion); expect(isFixtureShim(shim, root)).toBe(false); const wrongFlags = Buffer.from(valid); wrongFlags.writeUInt16LE((5478 << 3) | 0b111, wrongFlags.length - 2); await Bun.write(metadata, wrongFlags); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, bunMetadata(undefined, "node ")); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, Buffer.alloc(64 * 1024)); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, Buffer.alloc(64 * 1024 + 1)); expect(isFixtureShim(shim, root)).toBe(false); await Bun.write(metadata, valid); await Bun.write(shim, Buffer.alloc(1024 * 1024 + 1)); expect(isFixtureShim(shim, root)).toBe(false); }); test("rejects bin and canonical export mismatches", async () => { const root = await workspaceFixture(); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); await Bun.write(path.join(root, "packages", "coding-agent", "package.json"), JSON.stringify({ bin: { skc: "bin/other.js" } })); expect(isFixtureShim(shim, root)).toBe(false); const exportRoot = await workspaceFixture(); const exportShim = path.join(exportRoot, "node_modules", ".bin", "skc.exe"); await Bun.write( path.join(exportRoot, "packages", "coding-agent", "package.json"), JSON.stringify({ bin: { skc: "bin/skc.js" }, exports: { "./cli": "./src/other.ts" } }), ); await Bun.write(path.join(exportRoot, "packages", "coding-agent", "src", "other.ts"), "export {};\n"); expect(isFixtureShim(exportShim, exportRoot)).toBe(false); const markerRoot = await workspaceFixture(); const markerShim = path.join(markerRoot, "node_modules", ".bin", "skc.exe"); await Bun.write( path.join(markerRoot, "packages", "coding-agent", "bin", "skc.js"), '#!/usr/bin/env bun\n// from "@sayknow-cli/coding-agent/cli"\nconsole.log("smoke-test: ok");\n', ); expect(isFixtureShim(markerShim, markerRoot)).toBe(false); }); test("rejects wrong-extension, compiled, and published targets even at an allowlisted path", async () => { const root = await workspaceFixture(); const shim = path.join(root, "node_modules", ".bin", "skc.exe"); expect(isFixtureSource(path.join(root, "node_modules", ".bin", "skc.cmd"), null, root, "win32")).toBe(false); expect(isFixtureSource(path.join(root, "dist", "skc.exe"), path.join(root, "dist", "skc.exe"), root, "win32")).toBe(false); expect( isFixtureSource( path.join(root, "node_modules", "sayknow-cli", "bin", "skc.js"), path.join(root, "node_modules", "sayknow-cli", "bin", "skc.js"), root, "win32", ), ).toBe(false); expect(isFixtureSource(shim, null, root, "linux")).toBe(false); }); }); describe("dev:link", () => { test.skipIf(process.platform === "win32")("fails when a shadow skc earlier on PATH would make smoke-test validate the wrong command", async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "skc-dev-link-shadow-")); tempRoots.push(root); const shadowDir = path.join(root, "shadow-bin"); const targetDir = path.join(root, "managed-bin"); await makeExecutable( path.join(shadowDir, "skc"), `#!/usr/bin/env sh\nif [ "$1" = "--smoke-test" ]; then echo "smoke-test: ok"; exit 0; fi\necho shadow\nexit 0\n`, ); const result = Bun.spawnSync([process.execPath, "scripts/dev-link.ts"], { env: { ...process.env, SKC_DEV_LINK_DIR: targetDir, PATH: `${shadowDir}:${targetDir}` }, stderr: "pipe", stdout: "pipe", }); expect(result.exitCode).not.toBe(0); expect(result.stdout.toString()).toContain(`Linked ${path.join(targetDir, "skc")}`); expect(result.stderr.toString()).toContain("still resolves to a different command earlier on PATH"); expect(result.stderr.toString()).toContain(path.join(shadowDir, "skc")); }); }); describe("nested workspace shadows", () => { async function shadowFixture(): Promise { const root = await fs.mkdtemp(path.join(os.tmpdir(), "skc-dev-link-nested-")); tempRoots.push(root); for (const [pkg, version] of [ ["coding-agent", "0.5.21"], ["natives", "0.5.21"], ] as const) { await fs.mkdir(path.join(root, "packages", pkg), { recursive: true }); await Bun.write( path.join(root, "packages", pkg, "package.json"), JSON.stringify({ name: `@sayknow-cli/${pkg}`, version }), ); } return root; } test("reports nothing for a clean workspace", async () => { expect(findNestedWorkspaceShadows(await shadowFixture())).toEqual([]); }); test("reports a nested install that shadows a workspace package", async () => { const root = await shadowFixture(); const nested = path.join(root, "packages", "coding-agent", "node_modules", "@sayknow-cli", "natives"); await fs.mkdir(nested, { recursive: true }); await Bun.write(path.join(nested, "package.json"), JSON.stringify({ name: "@sayknow-cli/natives", version: "0.5.13" })); expect(findNestedWorkspaceShadows(root)).toEqual([ { dir: nested, packageName: "@sayknow-cli/natives", version: "0.5.13", workspaceVersion: "0.5.21" }, ]); }); test.skipIf(process.platform === "win32")("accepts a nested symlink back into the checkout", async () => { const root = await shadowFixture(); const scope = path.join(root, "packages", "coding-agent", "node_modules", "@sayknow-cli"); await fs.mkdir(scope, { recursive: true }); await fs.symlink(path.join(root, "packages", "natives"), path.join(scope, "natives")); expect(findNestedWorkspaceShadows(root)).toEqual([]); }); test("ignores nested installs of packages this workspace does not own", async () => { const root = await shadowFixture(); const nested = path.join(root, "packages", "coding-agent", "node_modules", "some-dependency"); await fs.mkdir(nested, { recursive: true }); await Bun.write(path.join(nested, "package.json"), JSON.stringify({ name: "some-dependency", version: "1.0.0" })); expect(findNestedWorkspaceShadows(root)).toEqual([]); }); }); describe("package resolution parity", () => { test("reads the version of the package that resolution actually picks", async () => { const repoRoot = path.join(import.meta.dir, ".."); const runtime = JSON.parse( await Bun.file(path.join(repoRoot, "packages", "coding-agent", "package.json")).text(), ) as { version: string }; const natives = resolvePackageFrom("@sayknow-cli/natives", path.join(repoRoot, "packages", "coding-agent")); expect(natives?.version).toBe(runtime.version); }); test("returns null for an unresolvable specifier", () => { expect(resolvePackageFrom("@sayknow-cli/does-not-exist", import.meta.dir)).toBeNull(); }); });