import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { once } from "node:events"; import { afterEach, describe, expect, it } from "vitest"; import { VoiceWorkerProcess } from "../src/core/workerProcess.js"; // A stand-in `voice_worker` package run by a real Python, so the spawn line is the one the app uses. const PYTHON = process.env.JARVIS_TEST_PYTHON ?? (process.platform === "win32" ? "python" : "python3"); const FAKE_MAIN = ` import json, os, sys, time out = os.environ["FAKE_OUT"] runs = len(os.listdir(out)) with open(os.path.join(out, f"run{runs}.json"), "w") as f: json.dump({"argv": sys.argv[1:], "token": os.environ.get("JARVIS_VOICE_TOKEN")}, f) if os.environ.get("FAKE_CRASH_FIRST") == "1" and runs == 0: sys.exit(3) print("loading models...", flush=True) print("READY 127.0.0.1:" + sys.argv[sys.argv.index("--port") + 1], flush=True) time.sleep(60) `; function fakeWorkerDir() { const root = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "jarvis-fake-worker-")); mkdirSync(join(root, "voice_worker")); writeFileSync(join(root, "voice_worker", "__init__.py"), ""); writeFileSync(join(root, "voice_worker", "__main__.py"), FAKE_MAIN); const out = join(root, "out"); mkdirSync(out); return { root, out, run: (n: number) => JSON.parse(readFileSync(join(out, `run${n}.json`), "utf8")) }; } const procs: VoiceWorkerProcess[] = []; afterEach(async () => { for (const p of procs.splice(0)) await p.stop(1000); }); describe("VoiceWorkerProcess", () => { it("runs python -m voice_worker on loopback with the token only in the environment", async () => { const d = fakeWorkerDir(); const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: ["--melo-python", "/x/python"], env: { FAKE_OUT: d.out } }); procs.push(p); const ready = once(p, "ready"); await p.start(); await ready; const run = d.run(0); const port = new URL(p.url).port; expect(run.argv).toEqual(["--host", "127.0.0.1", "--port", port, "--melo-python", "/x/python"]); expect(run.token).toBe(p.token); expect(p.token.length).toBeGreaterThanOrEqual(40); expect(run.argv.join(" ")).not.toContain(p.token); expect(new URL(p.url).hostname).toBe("127.0.0.1"); }); it("restarts a worker that dies, on the same port with the same token", async () => { const d = fakeWorkerDir(); const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: [], restartDelayMs: 50, env: { FAKE_OUT: d.out, FAKE_CRASH_FIRST: "1" } }); procs.push(p); const exited = once(p, "exit"); const ready = once(p, "ready"); await p.start(); expect((await exited)[0]).toMatchObject({ code: 3 }); await ready; expect(p.restarts).toBe(1); expect(d.run(1).argv).toEqual(d.run(0).argv); expect(d.run(1).token).toBe(d.run(0).token); }); it("stop ends the worker for good", async () => { const d = fakeWorkerDir(); const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: [], restartDelayMs: 20, env: { FAKE_OUT: d.out } }); const ready = once(p, "ready"); await p.start(); await ready; let exits = 0; p.on("exit", () => exits++); await p.stop(2000); await new Promise((r) => setTimeout(r, 200)); expect(exits).toBe(1); expect(p.restarts).toBe(0); }); });