import { expect, test, type Page } from "@playwright/test"; import { expectModalOpen } from "./helpers"; import { installMissingSourceRoutes, makeWav, slugOf } from "./fixtures/missing-source"; // THE VISITOR'S OWN COPY (lane missing-source, slice 2). A local file picked on // a video page plays in the page from an object URL, the transcript follows it // and a cue click seeks it, through an offset the visitor can nudge or set from // "This line is playing now". The file never leaves the browser: from the pick // on, the page makes NO request but blob: ones, and localStorage gains nothing // but the offset. const OFFSET_KEY = `archilyzer:local-offset:${slugOf("synthmem001")}`; const FILE = { name: "my-synth-copy.wav", mimeType: "audio/wav", buffer: makeWav() }; const video = (page: Page) => page.locator('video[aria-label="Your copy"]'); const fileTime = (page: Page) => video(page).evaluate((v: HTMLVideoElement) => v.currentTime); const localStorageKeys = (page: Page) => page.evaluate(() => Object.keys(localStorage).sort()); // Playback itself is the browser's; what is tested is where the page puts the // file's clock. So play() starts nothing and every currentTime read is exactly // where the page put it. async function stillPlayback(page: Page) { await page.addInitScript(() => { HTMLMediaElement.prototype.play = function () { return Promise.resolve(); }; }); } async function pick(page: Page) { await expect(page.getByRole("button", { name: "Play your own copy" })).toBeVisible(); await page.getByTestId("local-file-input").setInputFiles(FILE); await expect(video(page)).toHaveAttribute("src", /^blob:/); await expect.poll(() => video(page).evaluate((v: HTMLVideoElement) => v.readyState)).toBeGreaterThanOrEqual(1); } async function cue(page: Page, text: string) { await page.locator("li", { hasText: text }).getByRole("button").click(); } test.describe("export — play your own copy", () => { test("on a source no longer public: plays, follows, seeks, takes an offset, sends nothing", async ({ page }) => { await installMissingSourceRoutes(page); await stillPlayback(page); await page.goto(`/?v=${slugOf("synthmem001")}`); await expectModalOpen(page); const panel = page.getByTestId("source-missing"); await expect(panel.getByRole("button", { name: "Play your own copy" })).toBeVisible(); await expect(panel).toContainText("Your file stays in this browser: nothing is uploaded."); const keysBefore = await localStorageKeys(page); // From here on, the only requests allowed are blob: ones. const after: string[] = []; page.on("request", (r) => { if (!r.url().startsWith("blob:")) after.push(`${r.method()} ${r.url()}`); }); await pick(page); await expect(page.getByTestId("local-file-bar")).toContainText("Your copy: my-synth-copy.wav"); await expect(page.getByTestId("local-file-offset")).toHaveText("Offset 0.00 s"); // Click-to-seek, on the cue clock. await cue(page, "second synth line"); await expect.poll(() => fileTime(page)).toBeCloseTo(2, 1); // +1 s: the transcript's 0:03 is now 0:04 into the file. await page.getByRole("button", { name: "Offset +1 second" }).click(); await expect(page.getByTestId("local-file-offset")).toHaveText("Offset +1.00 s"); await cue(page, "third synth line"); await expect.poll(() => fileTime(page)).toBeCloseTo(4, 1); // The transcript follows the file: at 4.0 s with +1 s, the 0:03 line is on. await expect(page.locator("li", { hasText: "third synth line" })).toHaveClass(/bg-blue-950/); // "This line is playing now": the file at 5.5 s is the 0:02 line → +3.50 s. await video(page).evaluate( (v: HTMLVideoElement) => new Promise((res) => { v.addEventListener("seeked", () => res(), { once: true }); v.currentTime = 5.5; }), ); await page.getByRole("button", { name: "This line is playing now" }).click(); await expect(page.getByText("Click the line you hear now (Esc cancels).")).toBeVisible(); await cue(page, "second synth line"); await expect(page.getByTestId("local-file-offset")).toHaveText("Offset +3.50 s"); expect(await fileTime(page)).toBeCloseTo(5.5, 1); // Only the offset was stored, as a number. const keysAfter = await localStorageKeys(page); expect(keysAfter.filter((k) => !keysBefore.includes(k))).toEqual([OFFSET_KEY]); expect(await page.evaluate((k) => localStorage.getItem(k), OFFSET_KEY)).toBe("3.5"); const stored = await page.evaluate(() => JSON.stringify({ ...localStorage })); expect(stored).not.toContain("my-synth-copy"); // Nothing went over the network once the file was picked. expect(after).toEqual([]); // Stop: the URL is revoked and the panel is back. const src = await video(page).getAttribute("src"); await page.getByRole("button", { name: "Stop using my copy" }).click(); await expect(video(page)).toHaveCount(0); await expect(page.getByTestId("source-missing")).toBeVisible(); // Focus goes back to the control that picked the file. await expect(panel.getByRole("button", { name: "Play your own copy" })).toBeFocused(); const revoked = await page.evaluate((u) => fetch(u as string).then(() => false, () => true), src); expect(revoked).toBe(true); }); test("the offset comes back for the same video, and Esc cancels the sync", async ({ page }) => { await installMissingSourceRoutes(page); await stillPlayback(page); await page.addInitScript((k) => localStorage.setItem(k, "-0.5"), OFFSET_KEY); await page.goto(`/?v=${slugOf("synthmem001")}`); await expectModalOpen(page); await pick(page); await expect(page.getByTestId("local-file-offset")).toHaveText("Offset −0.50 s"); await page.getByRole("button", { name: "This line is playing now" }).click(); await page.keyboard.press("Escape"); await expect(page.getByText("Click the line you hear now (Esc cancels).")).toHaveCount(0); // Esc cancelled the sync; it did not collapse the modal. await expectModalOpen(page); }); test("on a public video: behind a plain control, and the embed gives way", async ({ page }) => { await installMissingSourceRoutes(page); await stillPlayback(page); await page.goto(`/?v=${slugOf("synthpub001")}`); await expectModalOpen(page); const offer = page.getByTestId("local-file-offer"); await expect(offer.getByRole("button", { name: "Play your own copy" })).toBeVisible(); await expect(offer).toContainText("Your file stays in this browser: nothing is uploaded."); const after: string[] = []; page.on("request", (r) => { if (!r.url().startsWith("blob:")) after.push(`${r.method()} ${r.url()}`); }); await pick(page); await expect(page.locator("iframe")).toHaveCount(0); await cue(page, "fourth synth line"); await expect.poll(() => fileTime(page)).toBeCloseTo(5, 1); expect(after).toEqual([]); await page.getByRole("button", { name: "Stop using my copy" }).click(); await expect(offer.getByRole("button", { name: "Play your own copy" })).toBeFocused(); }); });