import { test, expect, type Page } from "@playwright/test"; import { readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { portFor } from "yt-dlp-transcript-common/lib/ports.mjs"; // --------------------------------------------------------------------------- // The source backlog on a report video's page (UI notes item 16, // lib/report/sourceBacklog.*): // // source-backlog-fixture a deck cut citing testchan (the editor stub's // backlog: 1,454/2,798 transcribed, 2,751 held), // backlog-done (nothing left) and not-archived (the // stub's 404). WRITES source-watch.json, removed after. // // The line per channel from the editor's snapshot counts; Prioritize through // the editor's channel-priority (the focus body, never settings); Re-sweep // when complete armed, then FIRED by a read once every channel is done, and // the Inbox's sources-complete card (Re-sweep). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const DIR = path.join(FIXTURE, "reports", "source-backlog-fixture"); const PROJECT = "reports/source-backlog-fixture"; const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); const STUB = `http://127.0.0.1:${portFor("EDITOR_STUB_PORT")}`; const SHOTS = process.env.U4_SHOTS ?? null; const stubLog = () => { try { return JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route: string; focus?: { kind: string; slugs?: string[] } }[]; } catch { return []; } }; const counts = (page: Page, body: Record) => page.request.post(`${STUB}/__stub/channel-counts`, { data: body }); test.beforeEach(async ({ page }) => { test.setTimeout(90_000); rmSync(path.join(DIR, "source-watch.json"), { force: true }); await counts(page, {}); }); test.afterAll(async ({ request }) => { rmSync(path.join(DIR, "source-watch.json"), { force: true }); await request.post(`${STUB}/__stub/channel-counts`, { data: {} }); }); async function open(page: Page) { await page.goto(urls.projectHref(PROJECT, { tab: "overview" })); await expect(page.getByTestId("source-backlog")).toBeVisible({ timeout: 20_000 }); } test("each cited channel: transcribed/listed and held from the editor; complete; one the editor does not archive", async ({ page }) => { await open(page); const box = page.getByTestId("source-backlog"); await expect(box.locator("[data-backlog-line='testchan']")).toHaveText("1,454/2,798 transcribed ยท 2,751 held"); await expect(box.locator("[data-backlog-channel='testchan']")).toHaveAttribute("data-backlog-done", "0"); await expect(box.locator("[data-backlog-channel='backlog-done']")).toContainText("complete"); await expect(box.locator("[data-backlog-channel='not-archived']")).toContainText("the editor does not archive it"); await expect(box.locator("[data-backlog-prioritize='testchan']")).toBeVisible(); await expect(box.locator("[data-backlog-prioritize='backlog-done']")).toHaveCount(0); if (SHOTS) { await page.setViewportSize({ width: 1366, height: 900 }); await page.screenshot({ path: path.join(SHOTS, "16-source-backlog.png") }); } }); test("Prioritize asks the editor's channel-priority for its focus; the line says in focus", async ({ page }) => { await open(page); const before = stubLog().length; await page.locator("[data-backlog-prioritize='testchan']").click(); await expect(page.locator("[data-backlog-said]")).toContainText("testchan is in the editor's focus"); const asked = stubLog().slice(before).filter((r) => r.route === "channel-priority"); expect(asked).toHaveLength(1); expect(asked[0].focus).toEqual({ kind: "channels", slugs: ["testchan"] }); await expect(page.locator("[data-backlog-channel='testchan']")).toContainText("in focus"); await expect(page.locator("[data-backlog-prioritize='testchan']")).toHaveCount(0); }); test("Re-sweep when complete: armed; fired once every channel is done; the Inbox's card says Re-sweep", async ({ page }) => { await open(page); await page.locator("[data-backlog-watch-arm]").click(); await expect(page.locator("[data-backlog-watch='armed']")).toContainText("re-sweep when complete"); const armed = JSON.parse(readFileSync(path.join(DIR, "source-watch.json"), "utf8")); // A channel the editor does not archive can never be complete: the watch is for the ones it answers. expect(armed.watch.slugs.sort()).toEqual(["backlog-done", "testchan"]); // A reload before the backlog is done fires nothing. await page.reload(); await expect(page.locator("[data-backlog-watch='armed']")).toBeVisible({ timeout: 20_000 }); await counts(page, { slug: "testchan", totals: { videos: 2798, transcribed: 2798, downloaded: 2798 }, undownloaded: 0, untranscribed: 0 }); await page.reload(); await expect(page.locator("[data-backlog-watch='fired']")).toContainText("complete since", { timeout: 20_000 }); const fired = JSON.parse(readFileSync(path.join(DIR, "source-watch.json"), "utf8")); expect(fired.watch.firedAt).toBeTruthy(); await page.goto(urls.homeHref()); const card = page.locator(`[data-lit-line][data-piece='p:${PROJECT}'][data-card='sources-complete']`); await expect(card).toBeVisible({ timeout: 20_000 }); await expect(card).toContainText("Re-sweep"); if (SHOTS) await card.screenshot({ path: path.join(SHOTS, "16-inbox-resweep.png") }); // Cleared, the card goes. await open(page); await page.locator("[data-backlog-unwatch]").click(); await expect(page.locator("[data-backlog-watch]")).toHaveCount(0); await page.goto(urls.homeHref()); await expect(card).toHaveCount(0); });