import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/channels/*/videos/fetchWindowTwitch.test.ts" // // The Twitch join, option (b) (release 23 F1), through the editor's two // fetch-window doors: a Twitch record's `v` (what the MCP's fetch_clip and // a report's citation send) is answered from its `data//`. Nothing here // reaches the network: every window asked for is cached, or the run is dry. // Synthetic ids. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-window-twitch-")); const SLUG = "example-twitch"; const N = "2700000001"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); // A missing binary: the cached window's height probe answers null, not a throw. process.env.FFPROBE_BIN = path.join(ROOT, "no-ffprobe"); await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 })); await mkdir(path.join(ROOT, "channels", SLUG), { recursive: true }); // No platform key: the url says Twitch, as on a real Twitch channel. await writeFile( path.join(ROOT, "channels", SLUG, "config.json"), JSON.stringify({ name: "Example", handling: "transcribe", url: "https://www.twitch.tv/example" }), ); const DIR = path.join(ROOT, "channels", SLUG, "data", N); await mkdir(path.join(DIR, "clips"), { recursive: true }); await writeFile(path.join(DIR, "clips", "0.00-60.00.mp4"), "mp4"); await writeFile( path.join(DIR, "metadata.info.json"), JSON.stringify({ id: `v${N}`, webpage_url: `https://www.twitch.tv/videos/${N}` }), ); const { fetchWindowAction } = await import("./[id]/videoActions"); const { fetchWindowsAction } = await import("./fetchWindowsAction"); test.after(() => rm(ROOT, { recursive: true, force: true })); test("fetchWindowAction answers a v record from its data// window cache", async () => { const r = await fetchWindowAction({ slug: SLUG, videoId: `v${N}`, from: 5, to: 15, provenance: { requestedBy: "test" }, }); assert.ok(r.ok, JSON.stringify(r)); if (!r.ok) return; assert.equal(r.cached, true); assert.equal("file" in r ? r.file : null, path.join(DIR, "clips", "0.00-60.00.mp4")); }); test("fetchWindowsAction finds a v item cached in data//, and the item keeps its id", async () => { const r = await fetchWindowsAction({ items: [{ slug: SLUG, id: `v${N}`, from: 5, to: 15, clipId: "r#1" }], requestedBy: "test", dryRun: true, }); assert.ok(r.ok, JSON.stringify(r)); if (!r.ok) return; assert.deepEqual(r.cached.map((i) => i.id), [`v${N}`]); assert.deepEqual(r.groups, []); assert.deepEqual(r.unresolved, []); });