import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { encodeWav16 } from "./voiceMatch.mjs"; import { SPEAKER_CHECK_FILE, readSpeakerCheck, runSpeakerCheck, speakerVerdict, writeSpeakerCheck } from "./speakerCheck.mjs"; import { VOICE_A, VOICE_B, syntheticVoice } from "../../testing/audio/syntheticVoice.mjs"; import { hasFfmpeg } from "../../testing/audio/corruptAudio.mjs"; // The speaker check (UI notes 2026-10-30, #15): which of several named voices // a clip is. Two synthetic voices the test kit's fake embedder tells apart; // no python, no model. const HERE = path.dirname(fileURLToPath(import.meta.url)); const FAKE = path.join(HERE, "..", "..", "testing", "audio", "fakeSpeakerEmbed.mjs"); const FAKE_ENGINE = { python: process.execPath, script: FAKE, model: "fake.onnx", threads: 1 }; const tmp = () => mkdtempSync(path.join(os.tmpdir(), "speaker-check-")); const wav = (dir: string, name: string, x: Float32Array) => { const f = path.join(dir, name); writeFileSync(f, encodeWav16(x)); return f; }; test("speakerVerdict: matches when the best is over the bar and ahead by the margin; none under the floor; else unsure", () => { assert.deepEqual(speakerVerdict([{ name: "Ember", score: 0.93 }, { name: "Kirsche", score: 0.83 }]), { label: "matches", best: "Ember", margin: 0.1 }); assert.deepEqual(speakerVerdict([{ name: "Ember", score: 0.62 }, { name: "Kirsche", score: 0.6 }]), { label: "unsure", best: "Ember", margin: 0.02 }); assert.deepEqual(speakerVerdict([{ name: "Ember", score: 0.2 }]), { label: "none", best: "Ember", margin: null }); assert.deepEqual(speakerVerdict([]), { label: "unsure", best: null, margin: null }); }); test("runSpeakerCheck: each clip against every named voice, best first; a voice's own clip is scored against the rest of it", { skip: !hasFfmpeg() && "needs ffmpeg" }, async () => { const dir = tmp(); try { const a = [0.4, 1.1, 2.2].map((ph, i) => wav(dir, `a${i}.wav`, syntheticVoice({ ...VOICE_A, seconds: 2.5, phase: ph }))); const b = [0.7, 1.9].map((ph, i) => wav(dir, `b${i}.wav`, syntheticVoice({ ...VOICE_B, seconds: 2.5, phase: ph }))); const probe = wav(dir, "probe.wav", syntheticVoice({ ...VOICE_A, seconds: 2.5, phase: 3.3 })); const clip = (key: string, file: string) => ({ key, id: key, file, start: 0, dur: 2.5 }); const doc = await runSpeakerCheck({ clips: [clip("a0", a[0]), clip("a1", a[1]), clip("p", probe)], check: ["p", "a0"], speakers: [ { name: "Host", clipKeys: ["a0", "a1"], items: [{ key: "r2", file: a[2], start: 0, dur: 2.5 }] }, { name: "Guest", items: b.map((f, i) => ({ key: `g${i}`, file: f, start: 0, dur: 2.5 })) }, ], engine: FAKE_ENGINE, scratch: path.join(dir, "scratch"), now: () => new Date("2026-11-02T10:00:00Z"), }); const p = doc.clips.p as { scores: { name: string; score: number }[]; label: string; best: string }; assert.deepEqual(p.scores.map((s) => s.name), ["Host", "Guest"]); assert.equal(p.label, "matches"); assert.equal(p.best, "Host"); assert.ok(p.scores[0].score > p.scores[1].score + 0.05, JSON.stringify(p.scores)); // a0 is in Host's set: scored against a1 and r2, and says so. const a0 = doc.clips.a0 as { scores: { name: string; inReference?: boolean }[]; label: string }; assert.equal(a0.scores[0].name, "Host"); assert.equal(a0.scores[0].inReference, true); assert.equal("a1" in doc.clips, false, "a reference clip not asked for is not a row"); assert.deepEqual(doc.speakers.map((s: { name: string; pieces: number; kept: number }) => [s.name, s.pieces, s.kept]), [["Host", 3, 3], ["Guest", 2, 2]]); // The file merges a later check of one clip into the rows there. await writeSpeakerCheck(dir, doc); const again = { ...doc, clips: { p: { ...p, label: "unsure" } } }; const { doc: merged } = await writeSpeakerCheck(dir, again); assert.deepEqual(Object.keys(merged.clips).sort(), ["a0", "p"]); assert.equal((await readSpeakerCheck(dir))?.clips.p && (merged.clips.p as { label: string }).label, "unsure"); // New reference sets start the file over. const { doc: fresh } = await writeSpeakerCheck(dir, { ...again, speakers: [{ ...doc.speakers[0], name: "Renamed" }] }); assert.deepEqual(Object.keys(fresh.clips), ["p"]); assert.ok(SPEAKER_CHECK_FILE.endsWith(".json")); } finally { rmSync(dir, { recursive: true, force: true }); } });