| 1 | const assert = require("node:assert/strict"); |
| 2 | const test = require("node:test"); |
| 3 | |
| 4 | const { run, _internal } = require("../scripts/run"); |
| 5 | |
| 6 | test("version fallback handles only version flags", () => { |
| 7 | assert.equal(_internal.isVersionFlag(["--version"]), true); |
| 8 | assert.equal(_internal.isVersionFlag(["-V"]), true); |
| 9 | assert.equal(_internal.isVersionFlag(["-v"]), false); |
| 10 | assert.equal(_internal.isVersionFlag(["--verbose"]), false); |
| 11 | }); |
| 12 | |
| 13 | test("version flags prefer the installed binary over package metadata", async () => { |
| 14 | let spawned = false; |
| 15 | const exits = []; |
| 16 | |
| 17 | await run("codewhale", { |
| 18 | args: ["--version"], |
| 19 | getBinaryPath: async () => "/tmp/codewhale-test-binary", |
| 20 | spawnSync: (binary, args, options) => { |
| 21 | spawned = true; |
| 22 | assert.equal(binary, "/tmp/codewhale-test-binary"); |
| 23 | assert.deepEqual(args, ["--version"]); |
| 24 | assert.deepEqual(options, { stdio: "inherit" }); |
| 25 | return { status: 0 }; |
| 26 | }, |
| 27 | exit: (status) => { |
| 28 | exits.push(status); |
| 29 | }, |
| 30 | }); |
| 31 | |
| 32 | assert.equal(spawned, true); |
| 33 | assert.deepEqual(exits, [0]); |
| 34 | }); |
| 35 | |
| 36 | test("codew wrapper dispatches the native shortcut binary", async () => { |
| 37 | const resolvedNames = []; |
| 38 | const spawned = []; |
| 39 | |
| 40 | await run("codew", { |
| 41 | args: ["--version"], |
| 42 | getBinaryPath: async (name) => { |
| 43 | resolvedNames.push(name); |
| 44 | return "/tmp/codew-test-binary"; |
| 45 | }, |
| 46 | spawnSync: (binary, args) => { |
| 47 | spawned.push({ binary, args }); |
| 48 | return { status: 0 }; |
| 49 | }, |
| 50 | exit: () => {}, |
| 51 | }); |
| 52 | |
| 53 | assert.deepEqual(resolvedNames, ["codew"]); |
| 54 | assert.deepEqual(spawned, [ |
| 55 | { binary: "/tmp/codew-test-binary", args: ["--version"] }, |
| 56 | ]); |
| 57 | }); |
| 58 | |
| 59 | test("version flags fall back to package metadata when the binary is unavailable", async () => { |
| 60 | const originalLog = console.log; |
| 61 | const lines = []; |
| 62 | const exits = []; |
| 63 | console.log = (line) => lines.push(line); |
| 64 | try { |
| 65 | await run("codewhale", { |
| 66 | args: ["--version"], |
| 67 | getBinaryPath: async () => { |
| 68 | throw new Error("download unavailable"); |
| 69 | }, |
| 70 | spawnSync: () => { |
| 71 | throw new Error("spawn should not run without a binary"); |
| 72 | }, |
| 73 | exit: (status) => { |
| 74 | exits.push(status); |
| 75 | }, |
| 76 | }); |
| 77 | } finally { |
| 78 | console.log = originalLog; |
| 79 | } |
| 80 | |
| 81 | assert.deepEqual(exits, [0]); |
| 82 | assert.match(lines.join("\n"), /codewhale \(npm wrapper\) v/); |
| 83 | assert.match(lines.join("\n"), /binary version: v/); |
| 84 | }); |
| 85 |