import { spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { Clip } from "./clip.js"; import { Cutouts, useCli } from "./cutouts.js"; import { transcribe } from "./work.js"; import { loadProjectDotEnv } from "../utils/dotEnv.js"; // A project .env key, a different saved API key and no login: every CLI child auto starts must use the saved key, as // auto itself does. const cliEntry = join(dirname(fileURLToPath(import.meta.url)), "..", "cli.ts"); const hasTools = spawnSync("ffmpeg", ["-version"]).status === 0 && spawnSync("bun", ["--version"]).status === 0; const AUTH_ENV = [ "HEYGEN_API_KEY", "HYPERFRAMES_API_KEY", "HEYGEN_ACCESS_TOKEN", "HEYGEN_API_BASE", "HEYGEN_API_URL", "HYPERFRAMES_PROJECT_ENV_ORIGINS", "HYPERFRAMES_PROJECT_ENV_YIELDED", ]; let root = ""; let project = ""; beforeEach(() => { root = realpathSync(mkdtempSync(join(tmpdir(), "hf-auto-child-key-"))); project = join(root, "project"); const config = join(root, "config"); mkdirSync(project); mkdirSync(config); writeFileSync(join(config, "credentials"), JSON.stringify({ api_key: "saved-key" })); writeFileSync(join(project, ".env"), "HEYGEN_API_KEY=project-key\n"); for (const name of AUTH_ENV) vi.stubEnv(name, undefined); vi.stubEnv("HYPERFRAMES_CONFIG_DIR", config); vi.stubEnv("HOME", root); vi.stubEnv("USERPROFILE", root); vi.stubEnv("CI", "1"); vi.stubEnv("HYPERFRAMES_NO_TELEMETRY", "1"); loadProjectDotEnv(project, process.env, () => undefined); }); afterEach(() => { vi.unstubAllEnvs(); rmSync(root, { recursive: true, force: true }); }); // Answers every request with a refusal and keeps the key each one carried. async function keysSent(run: (base: string) => Promise): Promise { const keys: string[] = []; const server: Server = createServer((req, res) => { keys.push(String(req.headers["x-api-key"] ?? req.headers.authorization ?? "")); req.resume(); res.writeHead(401, { "content-type": "application/json" }); res.end(JSON.stringify({ error: { code: "unauthorized", message: "no" } })); }); await new Promise((done) => server.listen(0, "127.0.0.1", done)); try { await run(`http://127.0.0.1:${(server.address() as AddressInfo).port}`).catch(() => undefined); } finally { await new Promise((done) => server.close(done)); } return keys; } describe.runIf(hasTools)("a CLI child auto starts", () => { // Both fail without the fix: the child took the inherited project key for the shell's own and sent it. it("transcribes with the saved key, not the project .env key", async () => { expect(process.env.HEYGEN_API_KEY).toBe("project-key"); const clip = join(project, "clip.wav"); spawnSync("ffmpeg", [ ...["-v", "error", "-f", "lavfi", "-i", "anullsrc=r=16000:cl=mono"], ...["-t", "0.5", clip], ]); useCli("bun", ["run", cliEntry]); const keys = await keysSent(async (base) => { vi.stubEnv("HEYGEN_API_URL", base); await transcribe(project, clip, join(root, "words")); }); expect(keys.length).toBeGreaterThan(0); expect(new Set(keys)).toEqual(new Set(["saved-key"])); }, 90_000); it("cuts out with the saved key, not the project .env key", async () => { const file = join(root, "clip.mp4"); spawnSync("ffmpeg", [ ...["-v", "error", "-f", "lavfi", "-i", "testsrc=size=180x320:rate=25", "-t", "1"], ...["-pix_fmt", "yuv420p", file], ]); const clip: Clip = { src: "clip.mp4", path: file, tag: "", seconds: 1, width: 180, height: 320, sound: false, }; useCli("bun", ["run", cliEntry]); const work = join(root, "work"); mkdirSync(work); const cutouts = new Cutouts(clip, work, () => undefined); const keys = await keysSent(async (base) => { vi.stubEnv("HEYGEN_API_URL", base); vi.stubEnv("HEYGEN_ALLOW_HTTP", "1"); await cutouts .of({ id: "p1", start: 0, end: 0.5, effect: { name: "the test" } }) .finally(() => cutouts.stop()); }); expect(keys.length).toBeGreaterThan(0); expect(new Set(keys)).toEqual(new Set(["saved-key"])); }, 120_000); });