import { describe, expect, it } from "vitest"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { saveConfigPatch, type OssclipConfig } from "@ossclip/core"; import { MODELS, WHISPER_BUILD_HINT, ffmpegAsset, whisperAsset } from "../src/setup/manifest"; import { formatPlan, planSetup, type SetupProbes } from "../src/setup/plan"; import { promptForProvider } from "../src/setup/provider"; import { PinnedAssetGoneError, download } from "../src/setup/download"; import { manualInstall } from "../src/setup/setup"; import { tarCandidates } from "../src/setup/extract"; import { openCommand, revealCommand } from "../src/open"; const CFG: OssclipConfig = { ffmpegPath: "ffmpeg", ffprobePath: "ffprobe", whisperPath: "whisper-cli", modelDir: "/home/u/.ossclip/models", model: "small.en", }; const OPTS = { configDir: "/home/u/.ossclip", model: "small.en", force: false, skipLlm: false }; /** Everything present — individual tests break exactly one thing (doctor's pattern). */ const healthy = (over: Partial = {}): SetupProbes => ({ binRuns: async () => true, exists: () => true, platform: "linux", arch: "x64", env: { GEMINI_API_KEY: "k" }, ...over, }); const byKind = (steps: Awaited>, kind: string) => { const hit = steps.find((s) => s.kind === kind); if (!hit) throw new Error(`no step ${kind}`); return hit; }; describe("setup manifest (§90: the install cliff is the adoption ceiling)", () => { it("every supported platform×arch resolves ffmpeg to an asset or an explicit brew/manual path", () => { // The automated download matrix — darwin is deliberately null (brew). for (const [platform, arch] of [ ["win32", "x64"], ["win32", "arm64"], ["linux", "x64"], ["linux", "arm64"], ] as const) { const a = ffmpegAsset(platform, arch); expect(a, `${platform}/${arch}`).not.toBeNull(); expect(a?.sha256).toMatch(/^[0-9a-f]{64}$/); expect(a?.url).toMatch(/^https:\/\/github\.com\//); expect(a?.sizeMB).toBeGreaterThan(0); } expect(ffmpegAsset("darwin", "arm64")).toBeNull(); }); it("whisper prebuilts cover win32 and linux; darwin is brew's job", () => { for (const [platform, arch] of [ ["win32", "x64"], ["linux", "x64"], ["linux", "arm64"], ] as const) { const a = whisperAsset(platform, arch); expect(a, `${platform}/${arch}`).not.toBeNull(); expect(a?.sha256).toMatch(/^[0-9a-f]{64}$/); } expect(whisperAsset("darwin", "arm64")).toBeNull(); }); it("windows binaries carry .exe; posix ones don't", () => { expect(ffmpegAsset("win32", "x64")?.bins).toEqual(["ffmpeg.exe", "ffprobe.exe"]); expect(ffmpegAsset("linux", "x64")?.bins).toEqual(["ffmpeg", "ffprobe"]); expect(whisperAsset("win32", "x64")?.bins).toEqual(["whisper-cli.exe"]); }); it("the model table pins size and upstream SHA-1 for every documented model", () => { for (const name of ["tiny.en", "base.en", "small.en", "medium.en"]) { const m = MODELS[name]; expect(m, name).toBeDefined(); expect(m?.sha1).toMatch(/^[0-9a-f]{40}$/); expect(m?.sizeMB).toBeGreaterThan(0); } }); }); describe("setup planner", () => { it("a fully healthy machine plans zero work", async () => { const steps = await planSetup(CFG, healthy(), OPTS); expect(steps.every((s) => s.status === "satisfied")).toBe(true); expect(formatPlan(steps)).not.toContain("download"); }); it("missing ffmpeg on linux-x64 → download the pinned static build", async () => { const steps = await planSetup( CFG, healthy({ binRuns: async (bin) => bin !== "ffmpeg" && bin !== "ffprobe" }), OPTS, ); const ffmpeg = byKind(steps, "ffmpeg"); expect(ffmpeg.status).toBe("download"); expect(ffmpeg.asset?.url).toContain("linux64-gpl"); }); it("missing whisper on darwin → brew when brew exists, manual with the recipe when not", async () => { const withBrew = await planSetup( CFG, healthy({ platform: "darwin", binRuns: async (bin) => bin !== "whisper-cli" }), OPTS, ); expect(byKind(withBrew, "whisper").status).toBe("brew"); expect(byKind(withBrew, "whisper").hint).toBe("whisper-cpp"); const noBrew = await planSetup( CFG, healthy({ platform: "darwin", binRuns: async (bin) => bin !== "whisper-cli" && bin !== "brew", }), OPTS, ); const step = byKind(noBrew, "whisper"); expect(step.status).toBe("manual"); expect(step.hint).toContain("brew.sh"); }); it("an unsupported platform degrades to manual, never to silence", async () => { const steps = await planSetup( CFG, healthy({ platform: "freebsd", binRuns: async () => false, exists: () => false, env: {} }), OPTS, ); for (const kind of ["ffmpeg", "whisper"]) { const s = byKind(steps, kind); expect(s.status).toBe("manual"); expect(s.hint).toBeTruthy(); } }); it("--force re-provisions managed paths but never a user's own absolute path", async () => { const forced = { ...OPTS, force: true }; // Bare name (PATH) — setup may take over. const bare = await planSetup(CFG, healthy(), forced); expect(byKind(bare, "ffmpeg").status).toBe("download"); // Managed path — setup owns it. const managed = await planSetup( { ...CFG, ffmpegPath: "/home/u/.ossclip/bin/ffmpeg-x/bin/ffmpeg" }, healthy(), forced, ); expect(byKind(managed, "ffmpeg").status).toBe("download"); // User's own path — hands off, even under --force. const custom = await planSetup( { ...CFG, ffmpegPath: "/opt/myffmpeg/ffmpeg" }, healthy(), forced, ); expect(byKind(custom, "ffmpeg").status).toBe("satisfied"); }); it("a present model is satisfied; a missing known model plans a sized download", async () => { const steps = await planSetup(CFG, healthy({ exists: () => false }), OPTS); const model = byKind(steps, "model"); expect(model.status).toBe("download"); expect(model.sizeMB).toBe(466); expect(model.detail).toContain("ggml-small.en.bin"); }); // Remote transcription (2026-09-01 weak-CPU field report): downloading a // 466 MB model to run an engine too slow to use is exactly the cliff // remote removes — but it is reported with a reason, never skipped silently. it("remote configured → the whisper and model steps are satisfied, saying why", async () => { const steps = await planSetup( { ...CFG, whisperUrl: "https://api.groq.com/openai/v1" }, healthy({ binRuns: async (b) => b !== "whisper-cli", exists: () => false }), OPTS, ); for (const kind of ["whisper", "model"] as const) { const step = byKind(steps, kind); expect(step.status).toBe("satisfied"); expect(step.detail).toContain("remote transcription configured (https://api.groq.com/openai/v1)"); expect(step.detail).toContain("local whisper not needed"); } // Nothing is downloaded for a machine that transcribes remotely. expect(formatPlan(steps)).not.toContain("ggml-small.en.bin"); }); it("a local whisper and model already present are still reported as found", async () => { // Ground rule one: setup never uninstalls, and a user with both keeps // `--whisper-backend local` working — so the plan names the real paths. const steps = await planSetup( { ...CFG, whisperUrl: "https://api.groq.com/openai/v1" }, healthy(), OPTS, ); expect(byKind(steps, "whisper")).toMatchObject({ status: "satisfied", detail: "whisper-cli" }); expect(byKind(steps, "model")).toMatchObject({ status: "satisfied", detail: "/home/u/.ossclip/models/ggml-small.en.bin", }); }); it("provider: --skip-llm and a detected provider are both satisfied; neither prompts", async () => { // No agy and no claude (§132: the CLIs are now probed before the keys) — // "missing" must mean ALL four detection branches came up empty. const noCli = async (b: string) => b !== "claude" && b !== "agy"; const skipped = await planSetup(CFG, healthy({ env: {}, binRuns: noCli }), { ...OPTS, skipLlm: true, }); expect(byKind(skipped, "provider").status).toBe("satisfied"); const missing = await planSetup(CFG, healthy({ env: {}, binRuns: noCli }), OPTS); expect(byKind(missing, "provider").status).toBe("prompt"); }); // Lockstep guard for §132: the planner duplicates doctor's detection order, // and the agy CLI must beat a key here exactly as it does there. it("provider: an installed agy CLI satisfies the step even with keys set", async () => { const steps = await planSetup( CFG, healthy({ env: { GEMINI_API_KEY: "g" }, binRuns: async () => true }), OPTS, ); expect(byKind(steps, "provider").detail).toContain("antigravity"); }); it("the plan discloses the total download size up front", async () => { const steps = await planSetup( CFG, healthy({ binRuns: async (b) => b === "brew" || b === "claude", exists: () => false }), OPTS, ); const text = formatPlan(steps); // 119 (ffmpeg linux64) + 9 (whisper) + 466 (small.en) expect(text).toContain("total download ~594 MB"); }); }); describe("promptForProvider (the LLM step of setup)", () => { const io = (answers: string[]) => { const said: string[] = []; return { said, ask: async () => answers.shift() ?? "", say: (line: string) => void said.push(line), }; }; // §132: Antigravity is a keyless subscription CLI exactly like Claude Code // — choosing it must save NOTHING. A key invented here would be worse than // no answer: there is no key, and produce finds agy on PATH by itself. it("choice 4 (Antigravity) says nothing-to-save and writes no .env", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-provider-")); const t = io(["4"]); await promptForProvider(t, dir); expect(t.said.join("\n")).toContain("agy CLI on PATH"); expect(() => readFileSync(join(dir, ".env"))).toThrow(); rmSync(dir, { recursive: true, force: true }); }); it("the menu offers Antigravity as choice 4", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-provider-")); const t = io([""]); // just Enter — skip await promptForProvider(t, dir); expect(t.said.some((l) => l.includes("4)") && l.includes("Antigravity"))).toBe(true); rmSync(dir, { recursive: true, force: true }); }); }); describe("download (resume + integrity)", () => { const body = (text: string) => new Response(new TextEncoder().encode(text), { status: 200 }); it("fresh download lands, is hash-checked, and .part disappears", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-dl-")); const dest = join(dir, "file.bin"); // sha256 of "hello" const sha = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; await download("https://x/file", dest, { sha256: sha, fetchImpl: async () => body("hello") }); expect(readFileSync(dest, "utf8")).toBe("hello"); rmSync(dir, { recursive: true, force: true }); }); it("an existing .part resumes with a Range header and appends on 206", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-dl-")); const dest = join(dir, "file.bin"); writeFileSync(`${dest}.part`, "hel"); let range: string | null = null; await download("https://x/file", dest, { fetchImpl: async (_url, init) => { range = new Headers(init?.headers).get("range"); return new Response(new TextEncoder().encode("lo"), { status: 206 }); }, }); expect(range).toBe("bytes=3-"); expect(readFileSync(dest, "utf8")).toBe("hello"); rmSync(dir, { recursive: true, force: true }); }); it("a checksum mismatch removes the partial and throws — never a corrupt install", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-dl-")); const dest = join(dir, "file.bin"); await expect( download("https://x/file", dest, { sha256: "0".repeat(64), fetchImpl: async () => body("evil"), }), ).rejects.toThrow(/checksum mismatch/); expect(() => readFileSync(dest)).toThrow(); expect(() => readFileSync(`${dest}.part`)).toThrow(); rmSync(dir, { recursive: true, force: true }); }); /** * The HTTP-error branch had no test at all, which is part of why #6 was a * dead end rather than a detour: a rotated-away pin and a flaky server read * identically to the user (§145). */ it("a 404 is a STALE PIN, typed separately from any other HTTP failure", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-dl-")); const dest = join(dir, "file.bin"); const err = await download("https://x/gone", dest, { fetchImpl: async () => new Response(null, { status: 404 }), }).catch((e: unknown) => e); expect(err).toBeInstanceOf(PinnedAssetGoneError); // The message must say whose fault it is, or the user goes hunting their // own network for a bug that is in this repo's manifest. expect((err as Error).message).toMatch(/not a problem on your machine/); expect((err as Error).message).toContain("https://x/gone"); rmSync(dir, { recursive: true, force: true }); }); it("other HTTP failures keep the generic message — they really might be transient", async () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-dl-")); const dest = join(dir, "file.bin"); const err = await download("https://x/file", dest, { fetchImpl: async () => new Response(null, { status: 503 }), }).catch((e: unknown) => e); expect(err).not.toBeInstanceOf(PinnedAssetGoneError); expect((err as Error).message).toMatch(/download failed: HTTP 503/); rmSync(dir, { recursive: true, force: true }); }); }); /** * The manual-install matrix (§145). When a pin rots, this is the only thing * standing between the user and a dead end, so the whole cross-platform * decision is asserted here rather than discovered by a Windows user (§136). */ describe("manualInstall — the detour when a pinned download is gone", () => { it("ffmpeg: the platform's real package command", () => { expect(manualInstall("ffmpeg", "darwin")).toBe("brew install ffmpeg"); expect(manualInstall("ffmpeg", "linux")).toBe("sudo apt install ffmpeg"); expect(manualInstall("ffmpeg", "win32")).toMatch(/^winget install ffmpeg/); expect(manualInstall("ffmpeg", "freebsd")).toMatch(/ffmpeg\.org/); }); it("win32 ffmpeg names the env vars — winget's shims land off PATH for open shells", () => { // Exactly the workaround #6's reporter had to find on their own. expect(manualInstall("ffmpeg", "win32")).toMatch(/OSSCLIP_FFMPEG/); }); it("whisper: brew on darwin, build-from-source everywhere else — apt has no package", () => { expect(manualInstall("whisper", "darwin")).toBe("brew install whisper-cpp"); expect(manualInstall("whisper", "linux")).toBe(WHISPER_BUILD_HINT); expect(manualInstall("whisper", "win32")).toBe(WHISPER_BUILD_HINT); }); it("model and provider are not package-manager installs", () => { expect(manualInstall("model", "linux")).toMatch(/ossclip doctor/); expect(manualInstall("provider", "linux")).toMatch(/API key/); }); }); describe("saveConfigPatch", () => { it("merges over hand-edited config, preserving keys setup doesn't touch", () => { const dir = mkdtempSync(join(tmpdir(), "ossclip-cfg-")); writeFileSync( join(dir, "config.json"), JSON.stringify({ speaker: "Ahsan", pricing: { x: { in: 1, out: 2 } }, model: "small.en" }), ); saveConfigPatch({ ffmpegPath: "/managed/ffmpeg" }, dir); const after = JSON.parse(readFileSync(join(dir, "config.json"), "utf8")); expect(after.speaker).toBe("Ahsan"); expect(after.pricing).toEqual({ x: { in: 1, out: 2 } }); expect(after.model).toBe("small.en"); expect(after.ffmpegPath).toBe("/managed/ffmpeg"); rmSync(dir, { recursive: true, force: true }); }); it("creates the file (and dir) when absent", () => { const dir = join(mkdtempSync(join(tmpdir(), "ossclip-cfg-")), "deeper"); const path = saveConfigPatch({ whisperPath: "/w" }, dir); expect(JSON.parse(readFileSync(path, "utf8"))).toEqual({ whisperPath: "/w" }); rmSync(dir, { recursive: true, force: true }); }); }); describe("tarCandidates (§117: GNU tar can't read a zip)", () => { it("windows tries the system bsdtar by ABSOLUTE path before bare tar", () => { // Any box with Git for Windows — every GitHub runner — puts MSYS GNU tar // ahead of the system bsdtar on PATH, and GNU tar exits 128 on a zip. const c = tarCandidates("win32", { SystemRoot: "C:\\Windows" }); expect(c[0]).toBe("C:\\Windows\\System32\\tar.exe"); expect(c[1]).toBe("tar"); }); it("windows honours a relocated SystemRoot, and falls back when it is unset", () => { expect(tarCandidates("win32", { SystemRoot: "D:\\Win" })[0]).toBe("D:\\Win\\System32\\tar.exe"); expect(tarCandidates("win32", {})[0]).toBe("C:\\Windows\\System32\\tar.exe"); }); it("posix just uses tar", () => { expect(tarCandidates("linux", {})).toEqual(["tar"]); expect(tarCandidates("darwin", {})).toEqual(["tar"]); }); }); describe("openCommand (the `open` spawn crashed everywhere but macOS)", () => { it("picks the platform's opener", () => { expect(openCommand("http://u", "darwin")).toEqual({ bin: "open", args: ["http://u"] }); expect(openCommand("http://u", "linux")).toEqual({ bin: "xdg-open", args: ["http://u"] }); // The empty string fills `start`'s title slot so the URL isn't eaten. expect(openCommand("http://u", "win32")).toEqual({ bin: "cmd", args: ["/c", "start", "", "http://u"], }); }); // The thumbnail confirm reuses the same opener for FILE paths (viewer, not // browser) — every platform's opener treats them identically, so the rows // pin that a path rides through verbatim, exactly like a URL. it("opens file paths through the same per-platform opener", () => { expect(openCommand("/tmp/final.thumbnail.png", "darwin")).toEqual({ bin: "open", args: ["/tmp/final.thumbnail.png"], }); expect(openCommand("/tmp/final.thumbnail.png", "linux")).toEqual({ bin: "xdg-open", args: ["/tmp/final.thumbnail.png"], }); // A path with spaces is why the empty title arg is load-bearing: spawn // quotes the spaced arg, and without the empty string `start` would read // the quoted path as its window title and open nothing. expect(openCommand("C:\\out\\my talk.thumbnail.png", "win32")).toEqual({ bin: "cmd", args: ["/c", "start", "", "C:\\out\\my talk.thumbnail.png"], }); }); }); // The whole cross-platform decision asserted here, the picker matrix's // convention (§136): a Linux or Windows user must never be the one who // discovers the command was wrong — 0.1.4's `ossclip edit` crash is the // cautionary tale. Syntax facts pinned below were verified 2026-08-18, not // guessed. describe("revealCommand (select the render output, don't launch it)", () => { it("darwin: `open -R` reveals in Finder instead of playing the video", () => { expect(revealCommand("/out/final.mp4", "darwin")).toEqual({ bin: "open", args: ["-R", "/out/final.mp4"], }); }); it("win32: `/select,` and the path are ONE comma-joined, unquoted argument", () => { // explorer.exe does its own command-line parsing: split into two // arguments (or with the path quoted) it ignores the switch and opens // the default folder instead of selecting the file. expect(revealCommand("C:\\out\\final.mp4", "win32")).toEqual({ bin: "explorer", args: ["/select,C:\\out\\final.mp4"], }); }); it("win32: a spaced path STAYS one argument — no quoting added around it", () => { expect(revealCommand("C:\\out\\my talk.mp4", "win32")).toEqual({ bin: "explorer", args: ["/select,C:\\out\\my talk.mp4"], }); }); it("linux: opens the CONTAINING directory — no portable select verb exists across file managers", () => { expect(revealCommand("/home/u/out/final.mp4", "linux")).toEqual({ bin: "xdg-open", args: ["/home/u/out"], }); }); });