|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { resolveNpxInvocation } from "./npx-sync.mjs"; |
| 4 | + |
| 5 | +// Coverage parity with tts-local-provider.test.mjs, for the whisper.cpp |
| 6 | +// fallback's call-site shape (transcribe.mjs runWhisper): the same three |
| 7 | +// branches, but with the hard-fail contract — no fallback provider exists, |
| 8 | +// so an unresolvable npx must throw actionably instead of returning null. |
| 9 | + |
| 10 | +const envWithNpxCli = { |
| 11 | + npm_execpath: "C:/Program Files/nodejs/node_modules/npm/bin/npm-cli.js", |
| 12 | + npm_node_execpath: "C:/Program Files/nodejs/node.exe", |
| 13 | +}; |
| 14 | +const npxCliPath = "C:/Program Files/nodejs/node_modules/npm/bin/npx-cli.js"; |
| 15 | +const pathExists = (path) => path === npxCliPath; |
| 16 | + |
| 17 | +const WHISPER_ARGV = ["hyperframes", "transcribe", "C:\\media\\input.wav", "--dir", "C:\\work"]; |
| 18 | +const WHISPER_OPTS = { stdio: ["ignore", "pipe", "pipe"], timeout: 1_800_000 }; |
| 19 | + |
| 20 | +test("win32: routes the whisper fallback through node + npx-cli, never bare npx", () => { |
| 21 | + const resolved = resolveNpxInvocation( |
| 22 | + WHISPER_ARGV, |
| 23 | + WHISPER_OPTS, |
| 24 | + "win32", |
| 25 | + envWithNpxCli, |
| 26 | + pathExists, |
| 27 | + ); |
| 28 | + |
| 29 | + assert.equal(resolved.cmd, envWithNpxCli.npm_node_execpath); |
| 30 | + assert.equal(resolved.args[0], npxCliPath); |
| 31 | + assert.deepEqual(resolved.args.slice(1, 3), ["hyperframes", "transcribe"]); |
| 32 | + // execFileSync options survive the rerouting (the timeout bounds the |
| 33 | + // whisper build/model download; the pipes surface its errors). |
| 34 | + assert.deepEqual(resolved.opts.stdio, ["ignore", "pipe", "pipe"]); |
| 35 | + assert.equal(resolved.opts.timeout, 1_800_000); |
| 36 | +}); |
| 37 | + |
| 38 | +test("win32 without a resolvable npx-cli: throws the actionable install hint", () => { |
| 39 | + assert.throws( |
| 40 | + () => resolveNpxInvocation(WHISPER_ARGV, WHISPER_OPTS, "win32", {}, () => false), |
| 41 | + /npx-cli\.js was not found/, |
| 42 | + ); |
| 43 | +}); |
| 44 | + |
| 45 | +test("non-win32: spawns plain npx unchanged", () => { |
| 46 | + const resolved = resolveNpxInvocation(WHISPER_ARGV, WHISPER_OPTS, "darwin", {}, () => false); |
| 47 | + |
| 48 | + assert.equal(resolved.cmd, "npx"); |
| 49 | + assert.deepEqual(resolved.args, WHISPER_ARGV); |
| 50 | +}); |
0 commit comments