import { test, expect } from "@playwright/test"; import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import { join } from "node:path"; // By relative path, as report-draft.spec.ts imports common: the kit's fakes // and the synthetic poker stream are plain .mjs. import { FAKE_KEY, startFakeAnthropic, toolTurn, type FakeAnthropic } from "../../common/testing/llm/fakeAnthropic.mjs"; import { makeSyntheticPokerStream, pokerTruthAt, SPOKEN_CUES } from "../../common/testing/frames/syntheticPoker.mjs"; import { resetData, testPaths, writeSettings } from "./helpers"; // The frame scan (release 30, lane P) on the video page: a saved synthetic // poker stream (ffmpeg-made), the Scan button, the job against the fake // Anthropic (started here; settings point at it with a mode-600 key file), // and the "Frame scan" panel it leaves: the phase strip, the events linked to // the moment on the video's platform, the flag where a spoken number and a // frame disagree. const SLUG = "test-transcribe"; const ID = "vidA"; let api: FakeAnthropic; let tmp: string; const timesOf = (body: any): number[] => [...JSON.stringify(body.messages[0].content).matchAll(/frame \d+ at (\d+):(\d+)/g)].map((m) => Number(m[1]) * 60 + Number(m[2])); test.beforeAll(async () => { api = await startFakeAnthropic(); tmp = await mkdtemp(join(os.tmpdir(), "e2e-frame-scan-")); }); test.afterAll(async () => { await api?.close(); await rm(tmp, { recursive: true, force: true }); }); test("Scan frames on a saved stream: the job, then the panel's phases, events and flags", async ({ page }) => { test.setTimeout(120_000); await resetData("one-transcribe-channel"); const keyFile = join(tmp, "anthropic.env"); await writeFile(keyFile, `ANTHROPIC_API_KEY=${FAKE_KEY}\n`); await chmod(keyFile, 0o600); await writeSettings({ minFreeDiskGB: 0, generation: { apiKeyFile: keyFile, anthropicBaseUrl: api.url, caps: { daily: 20, max: 5 } } }); const videoDir = join(testPaths().transcriptsDir, "channels", SLUG, "data", ID); await mkdir(videoDir, { recursive: true }); await writeFile(join(videoDir, "metadata.info.json"), JSON.stringify({ id: ID, title: "Synthetic poker stream", webpage_url: "https://rumble.com/v7synth-synthetic-poker.html", duration: 210 })); await writeFile(join(videoDir, "transcript.cues.json"), JSON.stringify({ cues: SPOKEN_CUES })); const store = join(tmp, "saved", SLUG, ID); await mkdir(store, { recursive: true }); const { file } = await makeSyntheticPokerStream(store, { name: "source-media.mp4" }); await writeFile(join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "2026-10-15T00:00:00Z", dir: store, file: "source-media.mp4", bytes: 1 })); void file; for (let i = 0; i < 10; i++) api.push((body: any) => toolTurn("answer", { frames: timesOf(body).map((t, k) => ({ frame: k + 1, ...pokerTruthAt(t) })) })); await page.goto(`/channels/${SLUG}/videos/${ID}`); const panel = page.getByTestId("frame-scan-panel"); await expect(panel).toContainText("Not scanned."); await panel.getByRole("button", { name: "Scan frames", exact: true }).click(); await expect(page.getByLabel(`Scan frames of ${ID} output`)).toContainText("@@frame-scan", { timeout: 60_000 }); await page.reload(); await expect(panel.getByLabel("frame scan summary")).toContainText("21 frames every 10 s"); await expect(panel.getByLabel("frame scan summary")).toContainText("claude-haiku-4-5"); await expect(panel.locator('[data-phase="table"]').first()).toBeVisible(); await expect(panel.locator('[data-phase="break"]')).toHaveCount(1); const events = panel.getByLabel("frame scan events"); await expect(events).toContainText("double-up"); await expect(events).toContainText("bust"); await expect(events.getByRole("link", { name: "1:30", exact: true })).toHaveAttribute("href", "https://rumble.com/v7synth-synthetic-poker.html?start=90"); await expect(panel.getByLabel("tournament Synthetic Cup")).toContainText("Stack (chips)"); await expect(panel.getByLabel("frame scan flags")).toContainText("said 4, the frame at 2:00 reads 9"); await expect(panel.getByRole("button", { name: "Scan again", exact: true })).toBeVisible(); });