Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 6f532a11016ad3898fee59fc1d3f7e11843355ff
parent 51616ccb8f15149294cde6a2e1e1d58eb4af189d
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Thu, 19 Nov 2026 17:38:46 -0500

export: e2e for review 1 of clipping your own copy

A flash and a beep at one known moment (an A/V shift fails it) in the MP4
case and two new ones; the disk path run to the end through a stubbed
showSaveFilePicker whose writable keeps the file in the page; marks
outside the copy (a start before it begins, an end past it) with the
sentences, range and name; a copy whose first timestamp is 3 s, where cue
0:09 shows the flash the clip then holds at 2.0 s; Cancel and toggling
"Clip my copy" off mid-save, said by the one live region, with the focus
landing on "Clip my copy"; the download URL kept on close and let go on
Stop. The lazy chunk is found by the engine's marker string, not a licence
header a minifier strips.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mexport/e2e/local-clip.spec.ts | 277+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 271 insertions(+), 6 deletions(-)

diff --git a/export/e2e/local-clip.spec.ts b/export/e2e/local-clip.spec.ts @@ -1,6 +1,6 @@ import { expect, test, type Page, type Request } from "@playwright/test"; -import { execFileSync } from "node:child_process"; -import { readFileSync } from "node:fs"; +import { execFileSync, spawnSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; import { expectModalOpen } from "./helpers"; import { installMissingSourceRoutes, makeWav, slugOf } from "./fixtures/missing-source"; @@ -12,6 +12,10 @@ import { installMissingSourceRoutes, makeWav, slugOf } from "./fixtures/missing- // a re-encode), the length from the keyframe to the out mark, and audio and // video starting within one audio frame of each other. // +// A flash and a beep at one known moment catch an A/V shift; a stubbed +// showSaveFilePicker (an in-memory writable in the page) runs the disk path +// to the end. +// // The file never leaves the browser: from the pick on, the page asks for // nothing but blob: URLs and, once a clip is asked for, the clipper's own // script chunk (a same-origin GET with no body) — and that chunk is not part @@ -27,6 +31,9 @@ function has(cmd: string): boolean { } } const FFMPEG = has("ffmpeg") && has("ffprobe"); +// The engine's marker string (common/lib/localClipEngine.ts, CLIP_ENGINE_MARK): +// how its lazy chunk is told apart, since a minifier strips licence headers. +const ENGINE_MARK = "archilyzer-local-clip-engine"; const SLUG = slugOf("synthmem001"); const TITLE_BASE = "Synth-members-stream"; @@ -67,6 +74,46 @@ function makeMedia(path: string, kind: keyof typeof CODECS, opts: { seconds?: nu ]); } +// Black video with ONE white frame at 6.00 s (content time), silence with ONE +// 1 kHz beep from 6.00 s. `extra` goes to the output (e.g. a later start). +function makeFlashBeep(path: string, kind: keyof typeof CODECS, extra: string[] = []): void { + execFileSync("ffmpeg", [ + "-v", "error", "-y", + "-f", "lavfi", "-i", "color=c=black:s=160x90:r=25:d=12,drawbox=x=0:y=0:w=iw:h=ih:color=white:t=fill:enable='between(t,5.99,6.01)'", + "-f", "lavfi", "-i", "aevalsrc='0.8*sin(2*PI*1000*t)*between(t,6,6.2)':s=48000:d=12", + ...CODECS[kind].args, ...extra, "-shortest", path, + ]); +} + +// Where, on the clip's own clock, the first bright frame is and the beep starts. +function flashAndBeep(path: string): { flash: number; beep: number } { + const v = execFileSync("ffmpeg", ["-v", "error", "-i", path, "-an", "-vf", "signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=-", "-f", "null", "-"], { + encoding: "utf8", + }); + let flash = NaN; + let pts = NaN; + for (const line of v.split("\n")) { + const t = /pts_time:([\d.]+)/.exec(line); + if (t) pts = Number(t[1]); + const y = /YAVG=([\d.]+)/.exec(line); + if (y && Number(y[1]) > 128 && Number.isNaN(flash)) flash = pts; + } + const err = spawnSync("ffmpeg", ["-v", "info", "-nostats", "-i", path, "-vn", "-af", "silencedetect=n=-30dB:d=0.05", "-f", "null", "-"], { + encoding: "utf8", + }).stderr; + const end = /silence_end: ([\d.]+)/.exec(err); + return { flash, beep: end ? Number(end[1]) : NaN }; +} + +// The flash and the beep land together, `at` seconds into the clip. +function expectInStep(path: string, at: number) { + const { flash, beep } = flashAndBeep(path); + // One video frame is 40 ms; the beep's onset is within an audio frame. + expect(Math.abs(flash - at), `flash at ${flash}`).toBeLessThanOrEqual(0.045); + expect(Math.abs(beep - at), `beep at ${beep}`).toBeLessThanOrEqual(0.03); + expect(Math.abs(flash - beep), `flash ${flash}, beep ${beep}`).toBeLessThanOrEqual(0.045); +} + type Probe = { streams: { codec_type: string; codec_name: string; start_time: string }[]; format: { duration: string } }; function ffprobe(path: string): Probe { return JSON.parse( @@ -86,6 +133,73 @@ function firstVideoFrameIsKey(path: string): boolean { const video = (page: Page) => page.locator('video[aria-label="Your copy"]'); const clipGroup = (page: Page) => page.getByRole("group", { name: "Clip of your copy" }); +// The ONE live region, mounted while a copy plays. +const clipStatus = (page: Page) => page.getByTestId("local-clip-status"); +const clipToggle = (page: Page) => page.getByRole("button", { name: "Clip my copy" }); + +// showSaveFilePicker, stubbed with a writable that keeps the file in the page. +async function stubSavePicker(page: Page) { + await page.addInitScript(() => { + type Chunk = { type: "write"; data: Uint8Array; position: number }; + const saved: { name: string | null; calls: string[]; bytes: Uint8Array | null } = { name: null, calls: [], bytes: null }; + (window as unknown as { __saved: typeof saved }).__saved = saved; + (window as unknown as { showSaveFilePicker: unknown }).showSaveFilePicker = async (opts: { suggestedName: string }) => { + saved.name = opts.suggestedName; + return { + createWritable: async () => { + let file = new Uint8Array(0); + return new WritableStream<Chunk>({ + write(chunk) { + saved.calls.push("write"); + const end = chunk.position + chunk.data.byteLength; + if (end > file.byteLength) { + const grown = new Uint8Array(end); + grown.set(file); + file = grown; + } + file.set(chunk.data, chunk.position); + }, + close() { + saved.calls.push("close"); + saved.bytes = file; + }, + abort() { + saved.calls.push("abort"); + }, + }); + }, + }; + }; + }); +} + +async function savedFile(page: Page): Promise<{ name: string | null; calls: string[]; bytes: Buffer | null }> { + const r = await page.evaluate(() => { + const s = (window as unknown as { __saved: { name: string | null; calls: string[]; bytes: Uint8Array | null } }).__saved; + let b64: string | null = null; + if (s.bytes) { + let bin = ""; + for (let i = 0; i < s.bytes.length; i += 0x8000) bin += String.fromCharCode(...s.bytes.subarray(i, i + 0x8000)); + b64 = btoa(bin); + } + return { name: s.name, calls: s.calls, b64 }; + }); + return { name: r.name, calls: r.calls, bytes: r.b64 === null ? null : Buffer.from(r.b64, "base64") }; +} + +// Every object URL the page revokes. +async function spyRevokes(page: Page) { + await page.addInitScript(() => { + const revoked: string[] = []; + (window as unknown as { __revoked: string[] }).__revoked = revoked; + const orig = URL.revokeObjectURL.bind(URL); + URL.revokeObjectURL = (u: string) => { + revoked.push(u); + orig(u); + }; + }); +} +const revoked = (page: Page) => page.evaluate(() => (window as unknown as { __revoked: string[] }).__revoked); // Playback is the browser's; what is tested is the copy. play() starts // nothing, so every currentTime is exactly where the page put it. @@ -165,6 +279,7 @@ test.describe("export — clip your own copy in the browser", () => { else clipScripts.push(body.then((b) => ({ url: resp.url(), body: b }))); }); + await spyRevokes(page); await open(page); phase = "picked"; await pick(page, FILE); @@ -178,7 +293,10 @@ test.describe("export — clip your own copy in the browser", () => { await expect(group).toBeVisible(); // BEFORE saving: the range, the real start, the name. await expect(group.getByTestId("local-clip-start")).toHaveText("Starts 1.0 s earlier, at the nearest keyframe (0:00:04.0)."); - await expect(group).toContainText("From 0:00:05.0 to 0:00:09.0."); + await expect(group.getByTestId("local-clip-range")).toHaveText("From 0:00:05.0 to 0:00:09.0."); + await expect(group.getByTestId("local-clip-end")).toHaveCount(0); + // The live region says the same, once. + await expect(clipStatus(page)).toContainText("Starts 1.0 s earlier, at the nearest keyframe (0:00:04.0)."); await expect(group.getByTestId("local-clip-name")).toHaveText(`${TITLE_BASE}_00h00m04s-00h00m09s.webm`); await expect(group).toContainText("Clipping uses Mediabunny 1.45.3 (MPL-2.0)"); @@ -189,6 +307,13 @@ test.describe("export — clip your own copy in the browser", () => { const out = info.outputPath("clip.webm"); await d.saveAs(out); await expect(group.getByTestId("local-clip-done")).toContainText(`Your browser is saving ${TITLE_BASE}_00h00m04s-00h00m09s.webm`); + await expect(clipStatus(page)).toContainText(`Your browser is saving ${TITLE_BASE}_00h00m04s-00h00m09s.webm`); + // Closing the panel leaves the download's URL alive; clearing the copy lets it go. + const downloadUrl = d.url(); + expect(downloadUrl).toMatch(/^blob:/); + await group.getByRole("button", { name: "Close the clip" }).click(); + await expect(clipGroup(page)).toHaveCount(0); + expect(await revoked(page)).not.toContain(downloadUrl); // The clip: copied, from a key frame, 9.0 − 4.0 s long, audio and video in step. const probe = ffprobe(out); @@ -213,12 +338,15 @@ test.describe("export — clip your own copy in the browser", () => { expect(r.url).not.toContain("my-synth-copy"); } // The library arrived with the clip, not with the first load. - const engineChunks = (await Promise.all(clipScripts)).filter((s) => s.body.includes("Vanilagy and contributors")); + const engineChunks = (await Promise.all(clipScripts)).filter((s) => s.body.includes(ENGINE_MARK)); expect(engineChunks.length).toBeGreaterThan(0); const firstUrls = new Set(firstLoad.map((r) => r.url)); for (const c of engineChunks) expect(firstUrls.has(c.url), c.url).toBe(false); - for (const body of await Promise.all(firstLoadScripts)) expect(body.includes("Vanilagy and contributors")).toBe(false); + for (const body of await Promise.all(firstLoadScripts)) expect(body.includes(ENGINE_MARK)).toBe(false); await noServiceWorker(page); + + await page.getByRole("button", { name: "Stop using my copy" }).click(); + await expect.poll(() => revoked(page)).toContain(downloadUrl); }); test("an offset moves the marks onto the file's clock; on a keyframe the clip starts at the mark", async ({ page }, info) => { @@ -244,7 +372,7 @@ test.describe("export — clip your own copy in the browser", () => { test("MP4 (H.264 with B-frames, AAC): a stream copy in step, whether or not this browser can play it", async ({ page }, info) => { const src = info.outputPath("synth-copy.mp4"); - makeMedia(src, "mp4"); + makeFlashBeep(src, "mp4"); await open(page); await pick(page, { name: "my-synth-copy.mp4", mimeType: CODECS.mp4.mime, buffer: readFileSync(src) }); await markClip(page, "clip line five", "clip line nine"); @@ -265,9 +393,13 @@ test.describe("export — clip your own copy in the browser", () => { // One AAC frame is 1024 samples at 48 kHz, about 21.3 ms; start_time // already carries the container's edit-list offsets. expect(Math.abs(Number(v.start_time) - Number(a.start_time))).toBeLessThanOrEqual(0.0215); + // The flash and the beep at 6.0 s land together, 2.0 s after the 4.0 s keyframe. + expectInStep(out, 2); }); test("cancel stops the copy and offers nothing as finished", async ({ page }, info) => { + // Two slow saves, each cancelled. + test.setTimeout(90_000); const src = info.outputPath("synth-big.webm"); makeMedia(src, "webm", { seconds: 20, heavy: true }); // The file reads slowly here (100 ms per 64 KiB), so the copy is still @@ -304,11 +436,143 @@ test.describe("export — clip your own copy in the browser", () => { await expect(cancel).toBeFocused(); await cancel.click(); await expect(group.getByTestId("local-clip-cancelled")).toHaveText("Cancelled; nothing was saved."); + await expect(clipStatus(page)).toHaveText("Cancelled; nothing was saved."); + // Cancel is gone, and the focus with it: it lands on "Clip my copy". + await expect(clipToggle(page)).toBeFocused(); + + // Again, and this time "Clip my copy" is toggled off mid-save: the live + // region, still there, says so. + await group.getByRole("button", { name: "Close the clip" }).click(); + await clipToggle(page).click(); + await expect(clipGroup(page).getByTestId("local-clip-start")).toBeVisible({ timeout: 30_000 }); + await clipGroup(page).getByRole("button", { name: "Save clip" }).click(); + await expect(clipGroup(page).getByRole("progressbar", { name: "Saving clip" })).toBeVisible(); + await clipToggle(page).click(); + await expect(clipGroup(page)).toHaveCount(0); + await expect(clipStatus(page)).toHaveText("Cancelled; nothing was saved."); await page.waitForTimeout(1500); expect(downloads).toBe(0); await noServiceWorker(page); }); + test("to a file you choose: the disk path writes the whole clip, in step, and closes the file", async ({ page }, info) => { + const src = info.outputPath("synth-copy.webm"); + makeFlashBeep(src, "webm"); + await stubSavePicker(page); + await open(page); + await pick(page, { name: "my-synth-copy.webm", mimeType: CODECS.webm.mime, buffer: readFileSync(src) }); + await markClip(page, "clip line five", "clip line nine"); + await clipToggle(page).click(); + const group = clipGroup(page); + const name = `${TITLE_BASE}_00h00m04s-00h00m09s.webm`; + await expect(group.getByTestId("local-clip-name")).toHaveText(name); + let downloads = 0; + page.on("download", () => downloads++); + await group.getByRole("button", { name: "Save the clip to a file you choose" }).click(); + await expect(group.getByTestId("local-clip-done")).toHaveText(/^Saved Synth-members-stream_00h00m04s-00h00m09s\.webm \(\d+(\.\d)? (KB|MB)\)\.$/); + await expect(clipStatus(page)).toContainText(`Saved ${name}`); + // Cancel went with the save, and the focus went to "Clip my copy". + await expect(clipToggle(page)).toBeFocused(); + const saved = await savedFile(page); + expect(saved.name).toBe(name); + expect(saved.calls.at(-1)).toBe("close"); + expect(saved.calls).not.toContain("abort"); + expect(downloads).toBe(0); + const out = info.outputPath("disk-clip.webm"); + writeFileSync(out, saved.bytes!); + const probe = ffprobe(out); + expect(probe.streams.map((s) => s.codec_name).sort()).toEqual(["opus", "vp9"]); + expect(firstVideoFrameIsKey(out)).toBe(true); + expect(Math.abs(Number(probe.format.duration) - 5)).toBeLessThan(0.12); + expectInStep(out, 2); + }); + + test("a mark outside the copy: the page says where the clip really starts and ends, and the name agrees", async ({ page }, info) => { + const src = info.outputPath("synth-copy.webm"); + makeMedia(src, "webm"); + await open(page); + await pick(page, { name: "my-synth-copy.webm", mimeType: CODECS.webm.mime, buffer: readFileSync(src) }); + // Marks at cue 0:00 and 0:09, then the offset to −2 s: the in mark is + // before the copy begins (its 0.0 s is cue 0:02). + await markClip(page, "clip line zero", "clip line nine"); + await page.getByRole("button", { name: "Offset −1 second" }).click(); + await page.getByRole("button", { name: "Offset −1 second" }).click(); + await clipToggle(page).click(); + const group = clipGroup(page); + await expect(group.getByTestId("local-clip-start")).toHaveText( + "Your copy begins at 0:00:02.0, after your Clip in mark; the clip starts there.", + ); + await expect(group.getByTestId("local-clip-range")).toHaveText("From 0:00:02.0 to 0:00:09.0, with your offset −2.00 s applied."); + await expect(group.getByTestId("local-clip-end")).toHaveCount(0); + await expect(group.getByTestId("local-clip-name")).toHaveText(`${TITLE_BASE}_00h00m02s-00h00m09s.webm`); + let download = page.waitForEvent("download"); + await group.getByRole("button", { name: "Save clip" }).click(); + let out = info.outputPath("clamped-start.webm"); + await (await download).saveAs(out); + // File 0.0 → 7.0. + expect(Math.abs(Number(ffprobe(out).format.duration) - 7)).toBeLessThan(0.12); + await group.getByRole("button", { name: "Close the clip" }).click(); + + // Marks at cue 0:05 and 0:11, then the offset to +2 s: the out mark is + // past the copy's end (its 12.0 s is cue 0:10). + await markClip(page, "clip line five", "clip line eleven"); + for (let i = 0; i < 4; i++) await page.getByRole("button", { name: "Offset +1 second" }).click(); + await clipToggle(page).click(); + await expect(group.getByTestId("local-clip-end")).toHaveText( + "Your copy ends at 0:00:10.0, before your Clip out mark; the clip ends there.", + ); + await expect(group.getByTestId("local-clip-start")).toHaveText("Starts 1.0 s earlier, at the nearest keyframe (0:00:04.0)."); + await expect(group.getByTestId("local-clip-range")).toHaveText("From 0:00:05.0 to 0:00:10.0, with your offset +2.00 s applied."); + await expect(group.getByTestId("local-clip-name")).toHaveText(`${TITLE_BASE}_00h00m04s-00h00m10s.webm`); + download = page.waitForEvent("download"); + await group.getByRole("button", { name: "Save clip" }).click(); + out = info.outputPath("clamped-end.webm"); + await (await download).saveAs(out); + // File 6.0 → 12.0. + expect(Math.abs(Number(ffprobe(out).format.duration) - 6)).toBeLessThan(0.12); + }); + + test("a copy whose first timestamp is 3 s: the page's times are the <video> clock's", async ({ page }, info) => { + const src = info.outputPath("late-start.webm"); + // The flash and the beep are 6.0 s into the content, so 9.0 s on the file's clock. + makeFlashBeep(src, "webm", ["-output_ts_offset", "3"]); + await open(page); + await pick(page, { name: "my-late-copy.webm", mimeType: CODECS.webm.mime, buffer: readFileSync(src) }); + // Cue 0:09 shows the flash: the <video> clock is the container's. + await cue(page, "clip line nine"); + await expect.poll(() => video(page).evaluate((v: HTMLVideoElement) => v.currentTime)).toBeCloseTo(9, 2); + await expect + .poll( + () => + video(page).evaluate((v: HTMLVideoElement) => { + if (v.seeking || v.readyState < 2) return -1; + const c = document.createElement("canvas"); + c.width = v.videoWidth; + c.height = v.videoHeight; + const g = c.getContext("2d")!; + g.drawImage(v, 0, 0); + const d = g.getImageData(0, 0, c.width, c.height).data; + let sum = 0; + for (let i = 0; i < d.length; i += 4) sum += d[i]; + return sum / (d.length / 4); + }), + { timeout: 10_000 }, + ) + .toBeGreaterThan(128); + // Keyframes are every 2 s from 3.0: cue 0:07 is one. + await markClip(page, "clip line seven", "clip line eleven"); + await clipToggle(page).click(); + const group = clipGroup(page); + await expect(group.getByTestId("local-clip-start")).toHaveText("Starts at your Clip in mark."); + await expect(group.getByTestId("local-clip-name")).toHaveText(`${TITLE_BASE}_00h00m07s-00h00m11s.webm`); + const download = page.waitForEvent("download"); + await group.getByRole("button", { name: "Save clip" }).click(); + const out = info.outputPath("late-start-clip.webm"); + await (await download).saveAs(out); + // The flash and beep the page showed at cue 0:09 are 2.0 s into the clip. + expectInStep(out, 2); + }); + test("a file it cannot copy gets a plain sentence", async ({ page }) => { await open(page); await pick(page, { name: "my-synth-copy.wav", mimeType: "audio/wav", buffer: makeWav(12) }); @@ -319,6 +583,7 @@ test.describe("export — clip your own copy in the browser", () => { await expect(clipGroup(page).getByTestId("local-clip-refusal")).toHaveText( "This file can't be clipped here: clipping takes MP4, MOV, M4A, WebM or MKV.", ); + await expect(clipStatus(page)).toHaveText("This file can't be clipped here: clipping takes MP4, MOV, M4A, WebM or MKV."); // Close gives the focus back to the control. await clipGroup(page).getByRole("button", { name: "Close the clip" }).click(); await expect(clipGroup(page)).toHaveCount(0);