Files
6c14797cd6 Culture: what a line means, native recordings by link, and one page for what is usual here (#460)
* feat(situations): a line may name a convention, held to a source

What a line does beyond its words, as an id on the line whose sentence is in
the seven dictionaries: shown after a heard line is answered, above a line the
learner builds, and on the scene's list of lines with its sources linked.

Two conventions on four lines: a polite question from whoever hands out the
work is the assignment; an order is put as a wish. Each names sources opened
for it. The contract suite refuses an unknown id, a convention on a line of
the other direction, one without a source and one no line carries.

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

* wip(review): a native speaker says a line and sends it

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

* feat(review): a native speaker says a line and sends it; approved voices play on the list of lines

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

* fix(review): say that a recording was taken back; audit reads the declared upload size

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

* test(audit): the recording walk measures the uploaded audio in the page

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

* test(course): line_recordings is not a course table

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

* feat(culture): one page for what is usual here, in the menu after the situations

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

* test(audit): the layout walk covers /culture, a scene's lines and the review page

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

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: forge merge bridge <forge@git.orangecat.ch>
2026-12-03 18:39:07 +01:00

129 lines
6.4 KiB
JavaScript

#!/usr/bin/env node
/**
* A native speaker says a line and sends it, in a real browser with a real
* (fake-device) microphone: `pnpm run audit:recording`.
*
* WHY THIS EXISTS. AGENTS.md: a microphone fact needs a real `getUserMedia`,
* a real `MediaRecorder` and a real upload, and reading the source finds none
* of them. The unit tests around `recordings.ts` pass whether or not a single
* byte of audio ever leaves the page. This walks the reader's own path on
* `/situations/<scene>/review` at a phone's width:
*
* accept a line → open the recorder → record → stop → the take is there to
* listen to → send → the upload is a 200 carrying audio → the line offers
* to take it back → take it back → the delete is a 200.
*
* It needs the app running with a database (the route answers 503 without
* one). Nothing here approves a recording: that is a person's ear.
*
* pnpm run dev # with DATABASE_URL set
* pnpm run audit:recording
* BASE=http://localhost:3077 SCENE=bakery pnpm run audit:recording
* SHOT=/tmp/review.png pnpm run audit:recording # keep a screenshot
*/
import { chromium } from "playwright";
import { execFileSync } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const BASE = (process.env.BASE ?? "http://localhost:3000").replace(/\/$/, "");
const SCENE = process.env.SCENE ?? "bakery";
const LOCALE = process.env.LOCALE ?? "de";
const SHOT = process.env.SHOT;
/**
* The audio the fake microphone plays. Chrome's own synthetic tone is quiet
* enough that a level meter reads it as a dead microphone (`speaking.mjs`
* found the same); pink noise under a slow tremolo has a level and a shape.
*/
function fakeMicrophone() {
const path = join(mkdtempSync(join(tmpdir(), "heidi-rec-")), "mic.wav");
execFileSync("ffmpeg", ["-loglevel", "error", "-y", "-f", "lavfi", "-i", "anoisesrc=color=pink:duration=6:amplitude=0.5", "-af", "tremolo=f=4:d=0.8", "-ar", "48000", "-ac", "1", path]);
return path;
}
const failures = [];
const check = (ok, what) => {
console.log(`${ok ? " ok " : " FAIL "} ${what}`);
if (!ok) failures.push(what);
};
const browser = await chromium.launch({
args: ["--use-fake-ui-for-media-stream", "--use-fake-device-for-media-stream", `--use-file-for-fake-audio-capture=${fakeMicrophone()}`],
});
try {
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, permissions: ["microphone"] });
const page = await context.newPage();
// Playwright does not hand back the body of a form that carries a file, so
// the page itself notes how large the audio in each upload was.
await page.addInitScript(() => {
const sent = [];
Object.defineProperty(window, "__uploadedAudio", { value: sent });
const original = window.fetch;
window.fetch = (input, init) => {
const audio = init?.body instanceof FormData ? init.body.get("audio") : null;
if (audio instanceof Blob) sent.push({ bytes: audio.size, type: audio.type });
return original(input, init);
};
});
const uploads = [];
const deletes = [];
page.on("response", (response) => {
const request = response.request();
if (!response.url().endsWith("/api/review/recording")) return;
if (request.method() === "POST") uploads.push({ status: response.status() });
if (request.method() === "DELETE") deletes.push({ status: response.status() });
});
await page.goto(`${BASE}/${LOCALE}/situations/${SCENE}/review`, { waitUntil: "domcontentloaded" });
const line = page.locator("[data-review-line]").first();
await line.locator('[data-verdict="ok"]').waitFor({ timeout: 60_000 });
// The buttons are there before the page is interactive in a dev build.
await page.waitForTimeout(1500);
check((await line.locator('[data-record="open"]').count()) === 0, "no recorder is offered on a line nobody has accepted");
await line.locator('[data-verdict="ok"]').click();
await line.locator('[data-record="open"]').click();
await line.locator('[data-record="start"]').click();
await line.locator('[data-record="stop"]').waitFor({ timeout: 15_000 });
check(true, "the microphone opened and recording started");
await page.waitForTimeout(2500);
await line.locator('[data-record="stop"]').click();
await line.locator('[data-record="send"]').waitFor({ timeout: 15_000 });
check((await line.locator("[data-record-playback]").count()) === 1, "the take is there to listen to before anything is sent");
check(uploads.length === 0, "nothing was uploaded before «send»");
// No sideways scroll at a phone's width with the recorder open.
const overflow = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
check(overflow <= 0, `no horizontal overflow at 390px with the recorder open (${overflow}px)`);
if (SHOT) await page.screenshot({ path: SHOT, fullPage: false });
await line.locator('[data-record="send"]').click();
await line.locator('[data-record="take-back"]').waitFor({ timeout: 20_000 });
check(uploads.length === 1 && uploads[0].status === 200, `one upload, answered 200 (${JSON.stringify(uploads)})`);
const audio = (await page.evaluate(() => window.__uploadedAudio))[0];
check((audio?.bytes ?? 0) > 2000 && /^audio\//.test(audio?.type ?? ""), `the upload carried audio (${audio?.bytes ?? 0} bytes, ${audio?.type || "no type"})`);
await page.reload({ waitUntil: "domcontentloaded" });
// Read from the browser's own store, so it is there once the page is interactive, not in the first HTML.
const kept = await page
.locator("[data-review-line]")
.first()
.locator('[data-record="take-back"]')
.waitFor({ timeout: 20_000 })
.then(() => true, () => false);
check(kept, "after a reload the line still offers to take the recording back");
await page.locator("[data-review-line]").first().locator('[data-record="take-back"]').click();
await page.locator('[data-record-status="taken-back"]').waitFor({ timeout: 15_000 });
check(deletes.length === 1 && deletes[0].status === 200, `taking it back is one delete, answered 200 (${JSON.stringify(deletes)})`);
} catch (error) {
check(false, `the walk did not finish: ${error instanceof Error ? error.message.split("\n")[0] : String(error)}`);
} finally {
await browser.close();
}
console.log(failures.length === 0 ? "\nrecording: every step passed" : `\nrecording: ${failures.length} step(s) failed`);
process.exit(failures.length === 0 ? 0 : 1);