import { expect, test, type Page } from "@playwright/test"; import { Buffer } from "node:buffer"; const ORIGIN = "http://127.0.0.1:4197"; async function localOnly(page: Page) { const external: string[] = []; await page.route("**/*", async (route) => { const url = new URL(route.request().url()); if ( (url.protocol === "http:" || url.protocol === "https:") && url.origin !== ORIGIN ) { external.push(url.href); await route.abort(); } else await route.continue(); }); return external; } test("runs from a nested path without external requests", async ({ page }) => { const errors: string[] = []; page.on("pageerror", (error) => errors.push(error.message)); page.on("console", (message) => { if (message.type() === "error") errors.push(message.text()); }); const external = await localOnly(page); await page.goto("/deep/nested/subtitle/"); await expect( page.getByRole("heading", { name: "Subtitle Tools" }), ).toBeVisible(); await expect(page.getByText("No network requests")).toBeVisible(); expect(external).toEqual([]); expect(errors).toEqual([]); }); test("parses, validates, edits and converts the example", async ({ page }) => { const external = await localOnly(page); await page.goto("/deep/nested/subtitle/"); await expect(page.getByText("SRT", { exact: true })).toBeVisible(); await page.getByRole("button", { name: "Cues & checks" }).click(); await expect(page.getByText("overlap", { exact: true })).toBeVisible(); await page.getByLabel("Text").fill("Edited locally"); await page.getByRole("button", { name: "Source & export" }).click(); await page.getByLabel("Output format").selectOption("vtt"); await expect(page.getByLabel("Export preview")).toHaveValue(/WEBVTT/u); await expect(page.getByLabel("Export preview")).toHaveValue( /Edited locally/u, ); expect(external).toEqual([]); }); test("applies a bounded timing shift", async ({ page }) => { await page.goto("/deep/nested/subtitle/"); await page.getByRole("button", { name: "Timing" }).click(); await page.getByLabel("Shift (ms)").fill("500"); await page.getByRole("button", { name: "Apply timing transform" }).click(); await page.getByRole("button", { name: "Cues & checks" }).click(); await expect(page.getByText(/00:00:01\.500/u).first()).toBeVisible(); }); test("imports flat TTML and performs timeline edits", async ({ page }) => { await page.goto("/deep/nested/subtitle/"); await page .getByLabel("Subtitle source") .fill( `

One
line

`, ); await page.getByRole("button", { name: "Parse source" }).click(); await expect( page.getByLabel("Subtitle summary").getByText("TTML", { exact: true }), ).toBeVisible(); await page.getByRole("button", { name: "Cues & checks" }).click(); await page.getByRole("button", { name: "Duplicate" }).click(); await expect( page .getByLabel("Subtitle summary") .locator(":scope > div") .filter({ hasText: /^Cues2$/u }) .locator("strong"), ).toHaveText("2"); await page.getByRole("button", { name: "Uppercase" }).click(); await page.getByRole("button", { name: "Source & export" }).click(); await page.getByLabel("Output format").selectOption("ttml"); await expect(page.getByLabel("Export preview")).toHaveValue(/ { await page.goto("/deep/nested/subtitle/"); await page.getByRole("button", { name: "Waveform" }).click(); await page.getByLabel("Open media").setInputFiles({ name: "tone.wav", mimeType: "audio/wav", buffer: wavFixture(), }); await expect(page.locator("audio")).toBeVisible(); await expect( page.getByLabel("Audio waveform with subtitle start markers"), ).toBeVisible(); await expect(page.getByRole("alert")).toHaveCount(0); }); test("serves release identity and hardened local-only headers", async ({ request, }) => { const index = await request.get("/deep/nested/subtitle/"); expect(index.ok()).toBe(true); expect(index.headers()["content-security-policy"]).toContain( "connect-src 'self'", ); expect(index.headers()["content-security-policy"]).not.toMatch( /connect-src[^;]*https?:/u, ); expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u); const manifest = await request.get("/deep/nested/subtitle/toolbox-app.json"); await expect(manifest.json()).resolves.toMatchObject({ id: "de.add-ideas.subtitle-tools", version: "0.2.0", entry: "./", privacy: { processing: "local", telemetry: false }, }); }); function wavFixture(): Buffer { const sampleRate = 8_000; const samples = sampleRate / 4; const output = Buffer.alloc(44 + samples * 2); output.write("RIFF", 0); output.writeUInt32LE(output.length - 8, 4); output.write("WAVEfmt ", 8); output.writeUInt32LE(16, 16); output.writeUInt16LE(1, 20); output.writeUInt16LE(1, 22); output.writeUInt32LE(sampleRate, 24); output.writeUInt32LE(sampleRate * 2, 28); output.writeUInt16LE(2, 32); output.writeUInt16LE(16, 34); output.write("data", 36); output.writeUInt32LE(samples * 2, 40); for (let index = 0; index < samples; index += 1) output.writeInt16LE( Math.round(Math.sin((index / sampleRate) * Math.PI * 2 * 440) * 12_000), 44 + index * 2, ); return output; }