> }) => unknown);
+}
+
+const sha256 = (data: string | Buffer) => createHash("sha256").update(data).digest("hex");
+
+function clipTags(lines: Line[], options: Options): string {
+ return lines
+ .map((line) => {
+ const tag = options.untagged?.includes(line.id) ? "" : ` data-narration-line="${line.id}"`;
+ const duration = options.clipDuration?.[line.id] ?? line.duration;
+ return ``;
+ })
+ .join("\n ");
+}
+
+/** The caption array's words: each line's words at its clip, in the captions' own time base. */
+function captionWords(lines: Line[], offset: number) {
+ return lines.flatMap((line) =>
+ line.words.map((word) => ({
+ text: word.text,
+ start: line.start + word.start - offset,
+ end: line.start + word.end - offset,
+ line: line.id,
+ })),
+ );
+}
+
+function manifestLine(line: Line, wav: string) {
+ return {
+ id: line.id,
+ text: line.text,
+ audio: `assets/voice/${line.id}.wav`,
+ voiced: {
+ textHash: narrationTextHash(line.text),
+ voiceKey: "kokoro:af_heart:1:en-us",
+ audioHash: sha256(wav),
+ },
+ };
+}
+
+const SCENE_HOST = ``;
+
+/** Where the caption array lives: the root's own script, or a scene mounted at 2 s. */
+function captionPlacement(options: Options, lines: Line[]) {
+ const inScene = options.captionsInScene === true;
+ const words = options.captions ?? captionWords(lines, inScene ? 2 : 0);
+ const script = ``;
+ return inScene
+ ? { host: SCENE_HOST, rootScript: "", sceneScript: script, file: "compositions/captions.html" }
+ : { host: "", rootScript: script, sceneScript: "", file: "index.html" };
+}
+
+function narratedProject(options: Options = {}): string {
+ const lines = options.lines ?? LINES;
+ const captions = captionPlacement(options, lines);
+ const dir = mkdtempSync(join(tmpdir(), "hf-narration-lint-"));
+ dirs.push(dir);
+ const write = (path: string, content: string | Buffer) => {
+ mkdirSync(dirname(join(dir, path)), { recursive: true });
+ writeFileSync(join(dir, path), content);
+ };
+ write(
+ "index.html",
+ `
+
+ ${clipTags(lines, options)}
+ ${captions.host}
+
+ ${captions.rootScript}
+
+`,
+ );
+ write(
+ "compositions/captions.html",
+ `${captions.sceneScript}
+
`,
+ );
+ const manifest: Record = {
+ version: 1,
+ defaults: { provider: "kokoro", voice: "af_heart", speed: 1, lang: "en-us" },
+ captions: { file: captions.file },
+ lines: lines.map((line) => {
+ const wav = `RIFF${line.id}`;
+ write(`assets/voice/${line.id}.wav`, wav);
+ return manifestLine(line, wav);
+ }),
+ };
+ const edited = options.manifest ? options.manifest(manifest) : manifest;
+ write("narration.json", JSON.stringify(edited, null, 2));
+ if (options.audioMeta !== false) {
+ const meta = {
+ tts_provider: "kokoro",
+ voices: lines.map((line) => ({
+ id: line.id,
+ path: `assets/voice/${line.id}.wav`,
+ duration_s: line.duration,
+ words: line.words,
+ })),
+ };
+ write("audio_meta.json", JSON.stringify(options.audioMeta ? options.audioMeta(meta) : meta));
+ }
+ return dir;
+}
+
+async function narrationFindings(dir: string) {
+ const { results } = await lintProject(dir);
+ return results
+ .flatMap((entry) => entry.result.findings)
+ .filter((finding) => finding.code.startsWith("narration_"))
+ .map((finding) => `${finding.severity} ${finding.code}: ${finding.message}`);
+}
+
+const editLine =
+ (index: number, change: (line: Record) => void) =>
+ (manifest: Record) => {
+ change((manifest.lines as Array>)[index]!);
+ return manifest;
+ };
+
+describe("narration lint", () => {
+ it("finds nothing without narration.json", async () => {
+ const dir = narratedProject();
+ rmSync(join(dir, "narration.json"));
+ expect(await narrationFindings(dir)).toEqual([]);
+ });
+
+ it("finds nothing when clips and captions match the manifest", async () => {
+ expect(await narrationFindings(narratedProject())).toEqual([]);
+ });
+
+ it("reports a manifest that is not one", async () => {
+ const dir = narratedProject({ manifest: () => ({ version: 1, lines: "nope" }) });
+ expect(await narrationFindings(dir)).toEqual([
+ "warning narration_manifest_invalid: narration.json is not a narration manifest: lines must be an array.",
+ ]);
+ });
+
+ it("reports a line whose text, voice or WAV changed since it was voiced", async () => {
+ const dir = narratedProject({
+ manifest: editLine(0, (line) => {
+ line.text = "Hi there.";
+ line.voice = "bf_emma";
+ }),
+ });
+ writeFileSync(join(dir, "assets/voice/l1.wav"), "RIFF-replaced");
+ expect(await narrationFindings(dir)).toContain(
+ "warning narration_audio_stale: Narration line l1 needs re-voicing: its text changed, its voice settings changed, its WAV was replaced.",
+ );
+ });
+
+ it("reports a missing audio file", async () => {
+ const dir = narratedProject();
+ rmSync(join(dir, "assets/voice/l2.wav"));
+ expect(await narrationFindings(dir)).toContain(
+ "warning narration_audio_stale: Narration line l2 needs re-voicing: its audio file assets/voice/l2.wav is missing.",
+ );
+ });
+
+ it("reports a line no clip links, and a clip linked to an unknown line", async () => {
+ const dir = narratedProject({
+ untagged: ["l2"],
+ manifest: editLine(0, (line) => {
+ line.id = "intro";
+ }),
+ });
+ const findings = await narrationFindings(dir);
+ expect(findings).toContain(
+ 'warning narration_line_unlinked: assets/voice/l1.wav is linked to narration line "l1", which narration.json does not list.',
+ );
+ expect(findings).toContain(
+ 'warning narration_line_unlinked: Narration line intro has no