// RECORD -> FILTER -> PREVIEW (recordPreview.mjs; postRecord.mjs keepRecording's spans; narrate-via-editor.mjs // opWaitOf): the chain into a scratch dir with no take made, each step kept and not run again while what it was made // from stands (another pitch asks for the convert alone; a mark moved re-cuts and converts, never cleans again), the // heavy slot's wait read as a state, Keep making the takes from the preview's files, and the CLI twin // (`posts narrate --recording --preview`, `--keep-preview`, `--discard-preview`). No editor: the voice ops are // stubs; every project is a temp dir. // Run: node --test umtool/lib/report/recordPreview.test.mjs import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { encodeWav16 } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { VOICE_A, VOICE_B, syntheticVoice, withSilence } from "yt-dlp-transcript-common/testing/audio/syntheticVoice.mjs"; import { opWaitOf } from "umtool-report-to-video/narrate-via-editor"; import { validatePostNarration } from "umtool-report-to-video/post-narration"; import { castCommand } from "./castCli.mjs"; import { measureTake, trimSpans } from "./postNarrationCli.mjs"; import { postsCommand } from "./postsCli.mjs"; import { decodePart, explicitSpan, keepRecording, looksLikeText } from "./postRecord.mjs"; import { PREVIEW_PEAKS, discardPreview, keepPreview, preparePreview, previewDirOf, previewFile, previewView, readPreview, runPreview } from "./recordPreview.mjs"; import { finishSession, savePart } from "./recordSession.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-record-preview-")); after(() => rm(ROOT, { recursive: true, force: true })); const wavOf = async (file, voice, seconds = 1.5, silence = 0.3) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...voice, seconds }), { before: silence, after: silence }))); }; const run = (command, key) => async (args, extra = {}) => { const lines = []; const code = await command(args, { [key]: ROOT, log: (s) => lines.push(s), out: (s) => lines.push(s), err: (s) => lines.push(s), ...extra }); return { code, lines, text: () => lines.join("\n"), json: () => JSON.parse(lines.join("\n")) }; }; const cast = run(castCommand, "reportsRoot"); const posts = run(postsCommand, "root"); const DIR = path.join(ROOT, "rec"); { const post = (id, handle, author, o = {}) => ({ type: "post", id: `p-${id}`, post: `hostx/${id}`, platform: "x", author, handle, date: `2026-10-30T1${id}:00:00.000Z`, text: `words of ${id} from ${handle}.`, ...o }); await mkdir(DIR, { recursive: true }); await writeFile(path.join(DIR, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "rec", title: "Record", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" }, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [ post("1", "savvy", "Madam Savvy", { narration: { script: "Savvy's line, read aloud." } }), post("2", "savvy", "Madam Savvy", { narration: { script: "A first paragraph.\n\nA second one." } }), post("3", "nobody", "No Body", { narration: { script: "Nobody's line." } }), ], }, null, 2)); } const project = { id: "rec", dir: DIR }; const manifest = async () => JSON.parse(await readFile(path.join(DIR, "video.manifest.json"), "utf8")); const item = async (id) => (await manifest()).timeline.find((e) => e.id === id); // The editor's two voice ops: each writes a voice to `out` (and to each part's), and says what it ran. const editor = () => { const ops = []; const ask = async (route, body, o = {}) => { const op = route.split("/").at(-1); ops.push({ op, ...body }); // The heavy slot's gate, then the engine: what the editor's job says while it runs. o.onState?.({ jobId: `op-${ops.length}`, status: "queued", queue: { position: 2 }, tail: [] }); const gate = ["heavy: waiting for memory — 4100 MB available, the floor is 6000 MB", " (HEAVY_MIN_FREE_MB= to change it; 0, or HEAVY=0, to skip it)"]; o.onState?.({ jobId: `op-${ops.length}`, status: "running", tail: gate }); o.onState?.({ jobId: `op-${ops.length}`, status: "running", tail: [...gate, "heavy: 6900 MB available after 40s — admitted"] }); const { seconds } = await measureTake(body.file); await wavOf(body.out, op === "voice-convert" ? VOICE_A : VOICE_B, seconds, 0); for (const p of body.parts ?? []) await wavOf(p.out, op === "voice-convert" ? VOICE_A : VOICE_B, (await measureTake(p.file)).seconds, 0); return { jobId: `op-${ops.length}`, result: { engine: body.engine ?? (op === "voice-enhance" ? "deepfilternet" : "openvoice"), voice: body.voice, ...(body.pitch ? { pitch: body.pitch, semitones: body.semitones ?? 0 } : {}) } }; }; return { ops, ask, names: () => ops.map((o) => o.op) }; }; test("the heavy slot's wait is a state, not an error: queued, waiting for memory, the slot held, admitted", () => { assert.deepEqual(opWaitOf({ status: "queued", queue: { position: 3 } }), { state: "queued", position: 3, line: null }); assert.equal(opWaitOf({ status: "queued" }).position, null); // The gate's own lines (scripts/queue-lock.mjs), as the editor's job log carries them. const first = [ "Running Seed-VC (v1, 22 kHz speech) behind the heavy slot (estimate 6500 MB)…", "heavy: waiting for memory — 6706 MB available, 8000 MB promised to 2 running jobs, the floor is 6000 MB", " running: lane-remote-archive (lane/remote-archive, pid 917913, 1500 MB) for 34m41s: playwright test", " (HEAVY_MIN_FREE_MB= to change it; 0, or HEAVY=0, to skip it)", ]; const mem = opWaitOf({ status: "running", tail: first }); assert.deepEqual([mem.state, /6706 MB available/.test(mem.line)], ["memory", true], "the continuation lines after it are still the wait"); assert.equal(opWaitOf({ status: "running", tail: [...first, "heavy: still waiting (memory, 30s; 6372 MB available)"] }).state, "memory"); assert.equal(opWaitOf({ status: "running", tail: ["heavy: still waiting for memory (4200 MB available, 1m 10s)"] }).state, "memory"); assert.equal(opWaitOf({ status: "running", tail: ["heavy: this machine has 3900 MB in all, under the 6000 MB floor — one heavy job at a time; waiting for: x"] }).state, "memory"); assert.equal(opWaitOf({ status: "running", tail: ["heavy: still waiting (build, 1m00s; 9000 MB available)"] }).state, "slot"); assert.equal(opWaitOf({ status: "running", tail: ["heavy: waiting for a job that runs alone: a render"] }).state, "slot"); assert.equal(opWaitOf({ status: "running", tail: ["queue-lock: held for a render (umtool take p --id a, since 13:31) — waiting", " (render mode: `umtool render-mode off` ends it; RENDER_PRIORITY=0 to run anyway)"] }).state, "slot"); assert.equal(opWaitOf({ status: "running", tail: ["queue-lock: still waiting for the render (2m; umtool take p --id a)"] }).state, "slot"); // Admitted: the gate says so, or the engine has spoken since. assert.equal(opWaitOf({ status: "running", tail: [...first, "heavy: 7100 MB available after 1m10s — admitted"] }).state, "running"); assert.equal(opWaitOf({ status: "running", tail: [...first, "heavy: admitted beside 1 running job (1500 MB promised)"] }).state, "running"); assert.equal(opWaitOf({ status: "running", tail: [...first, "heavy: capped at 8192 MB (swap 2048 MB) in a scope of its own"] }).state, "running"); assert.equal(opWaitOf({ status: "running", tail: ["queue-lock: the render let go after 3m — going on"] }).state, "running"); assert.equal(opWaitOf({ status: "running", tail: [...first, "loading the model"] }).state, "running"); assert.equal(opWaitOf({ status: "running", tail: ["Running Seed-VC behind the heavy slot (estimate 5000 MB)…"] }).state, "running"); assert.equal(opWaitOf({ status: "running" }).state, "running"); }); test("cut marks: a span a person set is kept inside the part, a bad one is not one; --trim-span says them", () => { assert.deepEqual(explicitSpan({ from: 0.4, to: 2.1 }, 3), { from: 0.4, to: 2.1 }); assert.deepEqual(explicitSpan({ from: -1, to: 9 }, 3), { from: 0, to: 3 }, "clamped"); assert.equal(explicitSpan({ from: 1, to: 1.1 }, 3), null, "under 0.2 s"); assert.equal(explicitSpan(null, 3), null); assert.equal(explicitSpan({ from: "a", to: 2 }, 3), null); assert.equal(trimSpans([], 2), null); assert.deepEqual(trimSpans(["0.4-3.1"], 1), [{ from: 0.4, to: 3.1 }]); assert.deepEqual(trimSpans(["2=0.2-4"], 2), [null, { from: 0.2, to: 4 }]); for (const bad of [["3=0-1"], ["1-1.1"], ["x"], ["1=0-1", "0-2"]]) assert.throws(() => trimSpans(bad, 2), /--trim-span/); }); test("keepRecording with explicit spans: the part is cut at the marks, whatever the trim says", async () => { const rec = path.join(ROOT, "spans.wav"); await wavOf(rec, VOICE_B, 2, 0.6); // 3.2 s: 0.6 s of silence either side const auto = await keepRecording(project, await item("p-3"), [{ file: rec }], { manifest: await manifest() }); assert.ok(auto.take.seconds < 2.6, `auto: trimmed (${auto.take.seconds})`); const marked = await keepRecording(project, await item("p-3"), [{ file: rec }], { manifest: await manifest(), spans: [{ from: 1, to: 2 }] }); assert.ok(Math.abs(marked.take.seconds - 1) < 0.05, `the marks' second (${marked.take.seconds})`); const off = await keepRecording(project, await item("p-3"), [{ file: rec }], { manifest: await manifest(), trim: { on: false }, spans: [{ from: 0.5, to: 3.0 }] }); assert.ok(Math.abs(off.take.seconds - 2.5) < 0.05, "a mark over trim off"); const whole = await keepRecording(project, await item("p-3"), [{ file: rec }], { manifest: await manifest(), trim: { on: false }, spans: [null] }); assert.ok(whole.take.seconds > 3.1, "no mark, trim off: as recorded"); }); test("the chain into the scratch dir: no take; a step is not run again while what it was made from stands; Keep makes the takes from its files", async () => { await cast(["project", "rec", "add", "madam-savvy", "--name", "Madam Savvy", "--handles", "savvy", "--voice", "orpheus:mia", "--convert-engine", "seed-vc", "--convert-pitch", "contour", "--convert-semitones", "2", "--direction", "higher, brisk, amused"]); const rec = path.join(ROOT, "savvy.wav"); await wavOf(rec, VOICE_B, 2, 0.6); const ed = editor(); const stages = []; const emit = (ev, f) => ev === "stage" && stages.push(`${f.stage}:${f.state}`); const before = JSON.stringify((await item("p-1")).narration); let r = await runPreview(project, await item("p-1"), [{ file: rec, text: "Savvy's line, read aloud." }], { ask: ed.ask, emit }); assert.deepEqual(ed.names(), ["voice-enhance", "voice-convert"]); assert.equal(JSON.stringify((await item("p-1")).narration), before, "no take made"); assert.deepEqual(r.character, { id: "madam-savvy", name: "Madam Savvy", layer: "project" }); assert.equal(r.direction, "higher, brisk, amused"); assert.deepEqual(r.convert, { engine: "seed-vc", voice: "orpheus:mia", pitch: "contour", semitones: 2 }); assert.deepEqual([ed.ops[1].engine, ed.ops[1].voice, ed.ops[1].pitch, ed.ops[1].semitones], ["seed-vc", "orpheus:mia", "contour", 2], "the preset's convert, asked whole"); const dir = previewDirOf(DIR, await item("p-1")); assert.equal(dir, path.join(DIR, "narration", ".record-preview", "1.operator")); assert.equal(r.dir, dir); for (const f of [r.raw, r.clean, r.filtered]) assert.ok(existsSync(f), f); assert.equal(r.listen, r.filtered); // Cleaned WHOLE (the part as recorded, 3.2 s), then cut: the convert was asked of the cut, cleaned read. assert.ok(ed.ops[0].file.endsWith("p01.raw.wav")); assert.ok(Math.abs((await measureTake(ed.ops[0].file)).seconds - 3.2) < 0.05); assert.ok(ed.ops[1].file.endsWith("clean.wav")); assert.ok((await measureTake(r.raw)).seconds < 2.6, "its edges trimmed"); // The wait at the heavy slot was a state on the way, never a failure. assert.deepEqual(stages, ["decode:running", "clean:running", "clean:queued", "clean:memory", "clean:running", "clean:done", "trim:running", "trim:done", "convert:running", "convert:queued", "convert:memory", "convert:running", "convert:done", "done:done"]); // What the page is told: the waveform, the auto trim's marks, both levels. const v = previewView(await readPreview(DIR, await item("p-1"))); assert.equal(v.parts.length, 1); assert.equal(v.parts[0].peaks.length, PREVIEW_PEAKS); assert.ok(v.parts[0].auto.from > 0.3 && v.parts[0].auto.to < 2.9, JSON.stringify(v.parts[0].auto)); assert.deepEqual(v.parts[0].span, v.parts[0].auto); assert.ok(Number.isFinite(v.raw.lufs) && Number.isFinite(v.filtered.lufs)); assert.deepEqual([v.filtered.pitch, v.filtered.semitones, v.cleaned], ["contour", 2, "done"]); assert.equal("silences" in v.parts[0], false); assert.equal(await previewFile(DIR, await item("p-1"), "filtered"), r.filtered); assert.ok((await previewFile(DIR, await item("p-1"), "part", { n: 1 })).endsWith("p01.raw.wav")); // The same again: nothing is asked. r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask }); assert.equal(ed.ops.length, 2); assert.deepEqual(r.cached, { clean: true, convert: true }); // Another pitch: the convert ALONE, on the cleaned sound already there. r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask, pitch: "source", semitones: -1 }); assert.deepEqual(ed.names(), ["voice-enhance", "voice-convert", "voice-convert"]); assert.deepEqual([ed.ops[2].pitch, ed.ops[2].semitones, ed.ops[2].voice], ["source", -1, "orpheus:mia"]); assert.deepEqual(r.cached, { clean: true, convert: false }); // A mark moved: re-cut and converted, never cleaned again. r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask, spans: [{ from: 1, to: 2.2 }] }); assert.deepEqual(ed.names().slice(3), ["voice-convert"]); assert.deepEqual(r.spans, [{ from: 1, to: 2.2 }]); assert.ok(Math.abs((await measureTake(r.raw)).seconds - 1.2) < 0.05); // --out: the filtered read copied where asked. const out = path.join(ROOT, "out", "savvy-filtered.wav"); r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask, spans: [{ from: 1, to: 2.2 }], out }); assert.equal(r.out, out); assert.ok(existsSync(out)); assert.equal(ed.ops.length, 4, "a copy asks nothing"); // KEEP: three takes from the preview's files, the last picked, nothing asked of the editor; the scratch dir gone. const k = await keepPreview(project, await item("p-1")); assert.equal(ed.ops.length, 4); assert.deepEqual(k.takes.map((t) => t.step), ["recorded", "cleaned", "converted to orpheus:mia (pitch: contour +2 st)"]); assert.deepEqual(k.takes.map((t) => t.file), ["narration/posts/1/take-1.recorded-operator.wav", "narration/posts/1/take-2.enhance-default.wav", "narration/posts/1/take-3.convert-seed-vc.wav"]); const n = (await item("p-1")).narration; assert.equal(n.pick, 2); assert.notEqual(n.approved, true, "awaiting approval"); assert.deepEqual(validatePostNarration(await item("p-1")), []); assert.deepEqual(n.takes[0].source, { kind: "recorded", voice: "operator" }); assert.match(n.takes[0].note, /recorded for Madam Savvy/); assert.deepEqual(n.takes[1].source, { kind: "take", from: "take-1.recorded-operator" }); assert.deepEqual([n.takes[1].steps[0].op, n.takes[1].steps[0].engine, n.takes[1].steps[0].job], ["enhance", "deepfilternet", "op-1"]); assert.deepEqual(n.takes[2].source, { kind: "take", from: "take-2.enhance-default" }); assert.deepEqual([n.takes[2].voice, n.takes[2].steps[0].engine, n.takes[2].steps[0].voice, n.takes[2].steps[0].pitch, n.takes[2].steps[0].semitones], ["mia", "seed-vc", "orpheus:mia", "contour", 2]); for (const t of n.takes) assert.ok(existsSync(path.join(DIR, t.file)), t.file); assert.equal(k.disclosure, "Narration: voice converted to a synthetic voice (seed-vc · orpheus:mia)", "the disclosure line is what a recording made a read says"); assert.equal(existsSync(dir), false, "the scratch dir is removed on Keep"); await assert.rejects(keepPreview(project, await item("p-1")), /no preview to keep/); }); test("the preset is honoured: enhance off, trim off; an editor with no enhance engine is said and gone without; no character converts nothing", async () => { const rec = path.join(ROOT, "quiet.wav"); await wavOf(rec, VOICE_B, 2, 0.6); await cast(["project", "rec", "set", "madam-savvy", "--enhance", "off", "--trim", "off"]); let ed = editor(); let r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask }); assert.deepEqual(ed.names(), ["voice-convert"]); assert.match(r.notes[0], /not cleaned: madam-savvy's recorded preset says so/); assert.equal(r.clean, null); assert.ok((await measureTake(r.raw)).seconds > 3.1, "trim.on false: its edges as recorded"); assert.ok(ed.ops[0].file.endsWith("raw.wav"), "converted from the raw read"); let v = previewView(await readPreview(DIR, await item("p-1"))); assert.deepEqual(v.parts[0].auto, { from: 0, to: v.parts[0].seconds }); assert.deepEqual(v.trim, { on: false, padMs: null }); // The command over the preset, and a pad: the room kept either side. r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: ed.ask, trim: { on: true, padMs: 400 }, clean: true }); assert.deepEqual(ed.names(), ["voice-convert", "voice-enhance", "voice-convert"]); const padded = (await measureTake(r.raw)).seconds; assert.ok(padded > 2.6 && padded < 3.1, `the pad kept (${padded})`); await discardPreview(DIR, await item("p-1")); assert.equal(existsSync(previewDirOf(DIR, await item("p-1"))), false); await cast(["project", "rec", "set", "madam-savvy", "--enhance", "on", "--trim", "on"]); // No enhance engine: said, and the raw read converted. ed = editor(); const noEnhance = async (route, body, o) => { if (route.endsWith("voice-enhance")) throw Object.assign(new Error("the editor has no engine for /api/ops/voice-enhance yet (not installed: DeepFilterNet 3 is not installed)"), { name: "OpNotInstalled" }); return ed.ask(route, body, o); }; r = await runPreview(project, await item("p-1"), [{ file: rec }], { ask: noEnhance }); assert.match(r.notes[0], /not cleaned: .*not installed.*archilyzer voice setup --engine deepfilternet/); assert.deepEqual(ed.names(), ["voice-convert"]); assert.ok(r.filtered && !r.clean); const k = await keepPreview(project, await item("p-1")); assert.deepEqual(k.takes.map((t) => t.step), ["recorded", "converted to orpheus:mia (pitch: contour +2 st)"]); // p-3's author has no character (and there is no cast): cleaned, not converted -- the cleaned read is what to listen to. ed = editor(); r = await runPreview(project, await item("p-3"), [{ file: rec }], { ask: ed.ask }); assert.deepEqual(ed.names(), ["voice-enhance"]); assert.match(r.notes[0], /not converted: a convert needs a stock voice to convert to, and this post has no character with one/); assert.equal(r.character, null); assert.equal(r.listen, r.clean); // A voice named for this read converts it all the same. r = await runPreview(project, await item("p-3"), [{ file: rec }], { ask: ed.ask, voice: "kokoro:am_puck" }); assert.deepEqual([ed.names().at(-1), ed.ops.at(-1).voice], ["voice-convert", "kokoro:am_puck"]); await assert.rejects(runPreview(project, await item("p-3"), [{ file: rec }], { ask: ed.ask, as: "ghost" }), /--as ghost: no such character/); await discardPreview(DIR, await item("p-3")); }); test("the record page's session: a parted read previewed part by part, kept from the terminal (the session over), or kept alone at its marks", async () => { const e = await item("p-2"); const a = path.join(ROOT, "part-a.wav"); const b = path.join(ROOT, "part-b.wav"); await wavOf(a, VOICE_B, 1.5, 0.4); await wavOf(b, VOICE_B, 1.0, 0.4); await savePart(DIR, e, { n: 1, count: 2, text: "A first paragraph.", para: 0, bytes: await readFile(a), type: "audio/wav" }); await savePart(DIR, e, { n: 2, count: 2, text: "A second one.", para: 1, bytes: await readFile(b), type: "audio/wav" }); const { sessionParts } = await import("./recordSession.mjs"); const parts = await sessionParts(DIR, e); const pre = await preparePreview(project, e, parts); assert.deepEqual(pre.parts.map((p) => [p.n, p.text]), [[1, "A first paragraph."], [2, "A second one."]]); const ed = editor(); const r = await runPreview(project, e, parts, { ask: ed.ask, spans: [null, { from: 0.3, to: 1.5 }] }); assert.deepEqual(ed.names(), ["voice-enhance", "voice-convert"]); assert.equal(ed.ops[0].parts.length, 1, "the second part cleaned in the same job"); assert.equal(ed.ops[1].parts.length, 2, "each cut part converted beside the whole"); assert.deepEqual(r.spans[1], { from: 0.3, to: 1.5 }); // The CLI twin over the session: cached, so nothing is asked; --keep-preview makes parted takes. let c = await posts(["narrate", "rec", "p-2", "--preview", "--trim-span", "2=0.3-1.5", "--json"], { ask: ed.ask }); assert.equal(c.code, 0, c.text()); assert.equal(ed.ops.length, 2); assert.deepEqual(c.json().cached, { clean: true, convert: true }); c = await posts(["narrate", "rec", "p-2", "--keep-preview", "--json"]); assert.equal(c.code, 0, c.text()); const n = (await item("p-2")).narration; assert.deepEqual(n.takes.map((t) => t.parts.length), [2, 2, 2]); assert.deepEqual(n.takes[2].parts.map((p) => p.file), ["narration/posts/2/take-3.convert-seed-vc.parts/p01.wav", "narration/posts/2/take-3.convert-seed-vc.parts/p02.wav"]); for (const t of n.takes) for (const p of t.parts) assert.ok(existsSync(path.join(DIR, p.file)), p.file); assert.ok(Math.abs(n.takes[0].parts[1].seconds - 1.2) < 0.02, "the second part at its marks"); assert.deepEqual(validatePostNarration(await item("p-2")), []); assert.equal(existsSync(path.join(DIR, "narration", "posts", "2", ".recording-operator")), false, "a session's preview kept: the session is over"); // finishSession with marks (Keep with no Filter): the recorded take alone, cut at them. await savePart(DIR, e, { n: 1, count: 2, text: "A first paragraph.", para: 0, bytes: await readFile(a), type: "audio/wav" }); await savePart(DIR, e, { n: 2, count: 2, text: "A second one.", para: 1, bytes: await readFile(b), type: "audio/wav" }); const kept = await finishSession(project, await item("p-2"), { manifest: await manifest(), spans: [{ from: 0.5, to: 1.5 }, null] }); assert.ok(Math.abs(kept.take.parts[0].seconds - 1) < 0.02); assert.equal(existsSync(path.join(DIR, "narration", "posts", "2", ".recording-operator")), false); }); test("the CLI twin: --recording --preview prints the paths and makes no take; --events is one object a line; --discard-preview", async () => { const rec = path.join(ROOT, "cli.wav"); await wavOf(rec, VOICE_B, 2, 0.6); const ed = editor(); const takes = (await item("p-1")).narration.takes.length; let c = await posts(["narrate", "rec", "p-1", "--recording", rec, "--preview", "--pitch", "source"], { ask: ed.ask }); assert.equal(c.code, 0, c.text()); assert.match(c.text(), /p-1: previewed as Madam Savvy -- NO TAKE MADE/); assert.match(c.text(), /raw\s+.*narration\/\.record-preview\/1\.operator\/raw\.wav/); assert.match(c.text(), /filtered\s+.*filtered\.wav \(seed-vc -> orpheus:mia, pitch: source \+2 st\)/); assert.match(c.text(), /converting: waiting for memory \(the heavy slot admits it when there is room -- not an error\)/); assert.match(c.text(), /keep it: umtool posts narrate rec p-1 --keep-preview/); assert.equal((await item("p-1")).narration.takes.length, takes, "no take"); c = await posts(["narrate", "rec", "p-1", "--recording", rec, "--preview", "--as", "madam-savvy", "--pitch", "target", "--events"], { ask: ed.ask }); const evs = c.lines.map((l) => JSON.parse(l)); assert.deepEqual(evs.at(-1).ev, "result"); assert.ok(evs.at(-1).filtered.endsWith("filtered.wav")); assert.ok(evs.some((x) => x.ev === "stage" && x.stage === "convert" && x.state === "memory")); assert.equal(evs.at(-1).character.layer, "flag"); assert.equal(ed.ops.at(-1).semitones, undefined, "the target's own pitch takes no semitones"); c = await posts(["narrate", "rec", "p-1", "--recording", path.join(ROOT, "nope.wav"), "--preview"]); assert.equal(c.code, 2); c = await posts(["narrate", "rec", "p-3", "--preview"]); assert.equal(c.code, 2); assert.match(c.text(), /--preview takes --recording , or the record page's session of p-3: nothing recorded yet/); c = await posts(["narrate", "rec", "p-1", "--discard-preview"]); assert.equal(c.code, 0); assert.equal(existsSync(previewDirOf(DIR, await item("p-1"))), false); c = await posts(["narrate", "rec", "p-1", "--keep-preview"]); assert.notEqual(c.code, 0); assert.match(c.text(), /no preview to keep/); }); test("a preview is kept only for the recording it was made from: a part recorded again since the Filter is refused, nothing kept", async () => { const e = await item("p-2"); const a = path.join(ROOT, "again-a.wav"); const b = path.join(ROOT, "again-b.wav"); await wavOf(a, VOICE_B, 1.5, 0.4); await wavOf(b, VOICE_B, 1.0, 0.4); await savePart(DIR, e, { n: 1, count: 2, text: "A first paragraph.", para: 0, bytes: await readFile(a), type: "audio/wav" }); await savePart(DIR, e, { n: 2, count: 2, text: "A second one.", para: 1, bytes: await readFile(b), type: "audio/wav" }); const { sessionParts } = await import("./recordSession.mjs"); const ed = editor(); await runPreview(project, e, await sessionParts(DIR, e), { ask: ed.ask, session: true }); const takes = (await item("p-2")).narration.takes.length; // Part 2 recorded again (Space, with no Again): the session is another recording now. await new Promise((res) => setTimeout(res, 20)); await savePart(DIR, e, { n: 2, count: 2, text: "A second one.", para: 1, bytes: await readFile(a), type: "audio/wav" }); await assert.rejects(keepPreview(project, await item("p-2"), { parts: await sessionParts(DIR, e) }), (err) => err.status === 409 && /preview is of an earlier recording/.test(err.message)); assert.equal((await item("p-2")).narration.takes.length, takes, "nothing kept"); assert.ok(existsSync(path.join(DIR, "narration", "posts", "2", ".recording-operator", "session.json")), "the new part is still there"); // The terminal's --keep-preview asks the same. const c = await posts(["narrate", "rec", "p-2", "--keep-preview"]); assert.notEqual(c.code, 0); assert.match(c.text(), /preview is of an earlier recording/); // The same recording: kept. await runPreview(project, await item("p-2"), await sessionParts(DIR, e), { ask: ed.ask, session: true }); const k = await keepPreview(project, await item("p-2"), { parts: await sessionParts(DIR, e) }); assert.equal(k.session, true); const { clearSession } = await import("./recordSession.mjs"); await clearSession(DIR, e); }); test("an upload that is text (a playlist, a concat script) is refused before ffmpeg opens it; ffmpeg may open local files only", async () => { const secret = path.join(ROOT, "secret.wav"); await wavOf(secret, VOICE_A, 1, 0); const lists = { "list.m3u8": `#EXTM3U\n#EXT-X-TARGETDURATION:10\n#EXTINF:10,\n${secret}\n#EXT-X-ENDLIST\n`, "list.wav": `#EXTM3U\n#EXTINF:10,\nhttp://127.0.0.1:9/x.ts\n`, "script.ffconcat": `ffconcat version 1.0\nfile '${secret}'\n`, }; for (const [name, text] of Object.entries(lists)) { const f = path.join(ROOT, name); await writeFile(f, text); assert.equal(await looksLikeText(f), true, name); const out = path.join(ROOT, `${name}.out.wav`); await assert.rejects(decodePart(f, out), (err) => err.status === 400 && /not a sound file: it is text/.test(err.message), name); assert.equal(existsSync(out), false, `${name}: nothing decoded`); await assert.rejects(keepRecording(project, await item("p-3"), [{ file: f }], { manifest: await manifest() }), /not a sound file/); await assert.rejects(runPreview(project, await item("p-3"), [{ file: f }], { ask: editor().ask }), /not a sound file/); } assert.equal(await looksLikeText(secret), false, "a wav is binary"); const ok = await decodePart(secret, path.join(ROOT, "secret.out.wav")); assert.ok(ok.seconds > 0.9); });