import { test, expect, type Page } from "@playwright/test"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // A FAST BURST, PLAYED IN FULL ON A SLOW NETWORK (operator, 4 Nov: "The rapid // sections on play the cut don't load fast enough such that most of it is cut // out. Can we maybe preload?"). Fixture: e2e/fixtures/play-preload-fixture.mjs -- // ten narrated posts at `pace: "fast"`, ~1.3-2 s each, the clock on, three with // a picture and one with a video. // // Every post item's request (the card's page, its faces, the takes, the // attachments) is held 300-800 ms before it goes on. Play fetches the items // ahead (playEngine/postPreload.ts) and holds its clock on an item that is not // ready (engine.ts `gate`), so each item is played in full: its card drawn // for at least 90% of its seconds on the cut's clock while the clock runs, and // its voice heard. What it waited is printed (and in the console: "[play] held"). // --------------------------------------------------------------------------- const PROJECT = "reports/play-preload-fixture"; const IDS = ["pp-1", "pp-2", "pp-3", "pp-4", "pp-5", "pp-6", "pp-7", "pp-8", "pp-9", "pp-10"]; test.use({ launchOptions: { args: ["--autoplay-policy=no-user-gesture-required"] } }); test.beforeEach(() => test.setTimeout(180_000)); const root = (page: Page) => page.locator("[data-play-cut]"); /** 300-800 ms, the same for a URL every run. */ const delayOf = (url: string) => { let h = 2166136261; for (let i = 0; i < url.length; i += 1) h = Math.imul(h ^ url.charCodeAt(i), 16777619) >>> 0; return 300 + (h % 501); }; async function throttle(page: Page) { const seen = { n: 0 }; await page.route(/\/api\/report\/(post-item|file)\?/, async (route) => { if (route.request().method() !== "GET") return route.continue(); seen.n += 1; await new Promise((r) => setTimeout(r, delayOf(route.request().url()))); await route.continue().catch(() => {}); }); return seen; } /** Every animation frame from now: the post item on screen, its second, its card drawn, its voice, the clock's state. */ const startSampling = (page: Page) => page.evaluate(() => { const w = window as unknown as { __pp: unknown[]; __ppStop?: boolean }; w.__pp = []; w.__ppStop = false; const loop = () => { const cut = document.querySelector("[data-play-cut]"); const p = document.querySelector("[data-post-play]"); if (cut && p) { const id = p.getAttribute("data-post-play"); const f = p.querySelector('iframe[data-post-card-frame="read"]') as HTMLIFrameElement | null; const body = f?.contentDocument?.body; const a = p.querySelector("audio[data-post-narration-audio]") as HTMLAudioElement | null; w.__pp.push({ id, s: Number(p.getAttribute("data-post-s")), card: !!body && body.getAttribute("data-post-ready") === "1" && body.getAttribute("data-post-item") === id && f!.style.visibility !== "hidden", voice: !!a && !a.paused && a.currentTime > 0.02, state: cut.getAttribute("data-play-state"), }); } if (!w.__ppStop) requestAnimationFrame(loop); }; requestAnimationFrame(loop); }); type Sample = { id: string; s: number; card: boolean; voice: boolean; state: string }; type Report = { id: string; seconds: number; covered: number; voice: boolean }; /** Per item: the share of its seconds (0.1 s at a time, as the page shows it: from-start to to-start) drawn while the clock ran, and its voice. */ async function coverage(page: Page, ids: string[]): Promise<{ items: Report[]; waits: { count: number; ms: number; last: unknown[] } }> { const { samples, items, waits } = await page.evaluate(() => { const w = window as unknown as { __pp: Sample[]; __ppStop?: boolean; __umtoolPlayEngine?: { plan: { items: { id: string; start: number; from: number; to: number }[] }; stats: { waits: { count: number; ms: number; last: unknown[] } } } }; w.__ppStop = true; const e = w.__umtoolPlayEngine!; // (No `waits` before the gate existed: none counted.) return { samples: w.__pp, items: e.plan.items.map((x) => ({ id: x.id, start: x.start, from: x.from, to: x.to })), waits: e.stats.waits ?? { count: 0, ms: 0, last: [] } }; }); const out: Report[] = []; for (const id of ids) { const it = items.find((x) => x.id === id)!; const s0 = it.from - it.start; const n = Math.max(1, Math.ceil((it.to - it.from) * 10 - 1e-6)); const lit = new Set(); let voice = false; for (const x of samples) { if (x.id !== id) continue; voice ||= x.voice; if (!x.card || x.state !== "playing") continue; const k = Math.floor((x.s - s0) * 10 + 1e-6); if (k >= 0 && k < n) lit.add(k); } out.push({ id, seconds: Math.round((it.to - it.from) * 100) / 100, covered: Math.round((lit.size / n) * 100) / 100, voice }); } return { items: out, waits }; } async function playToEnd(page: Page) { await startSampling(page); await page.locator("[data-play-toggle]").click(); await expect(root(page)).toHaveAttribute("data-play-state", "ended", { timeout: 120_000 }); } function judge(label: string, r: Awaited>, requests: number) { console.log(`play-preload ${label}: ${requests} slowed requests; held ${r.waits.count} time(s), ${(r.waits.ms / 1000).toFixed(2)} s in all`); for (const x of r.items) console.log(` ${x.id.padEnd(6)} ${x.seconds.toFixed(2)} s card drawn ${(x.covered * 100).toFixed(0)}% voice ${x.voice ? "heard" : "NOT HEARD"}`); for (const x of r.items) { expect.soft(x.covered, `${x.id}: its card drawn for at least 90% of its ${x.seconds} s`).toBeGreaterThanOrEqual(0.9); expect.soft(x.voice, `${x.id}: its narration started`).toBe(true); } } test("THE BURST, SLOWED: every item of a fast run plays in full -- its card drawn for >= 90% of its seconds, its voice heard", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); const seen = await throttle(page); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "pp-1" })); await expect(root(page)).toHaveAttribute("data-play-item", "pp-1"); await expect(root(page)).toHaveAttribute("data-play-compose", "ready", { timeout: 60_000 }); await expect(page.locator('[data-post-play="pp-1"]')).toHaveCount(1); await playToEnd(page); judge("from the top", await coverage(page, IDS), seen.n); }); test("A JUMP INTO THE BURST: the items after a seek are fetched from there, and play in full", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); const seen = await throttle(page); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "pp-1" })); await expect(root(page)).toHaveAttribute("data-play-item", "pp-1"); await expect(root(page)).toHaveAttribute("data-play-compose", "ready", { timeout: 60_000 }); // N: the next item, five times -- a seek to pp-6. for (let k = 0; k < 5; k += 1) await page.keyboard.press("n"); await expect(root(page)).toHaveAttribute("data-play-item", "pp-6"); await playToEnd(page); judge("after a jump to pp-6", await coverage(page, IDS.slice(5)), seen.n); });