|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { spawnSync } from "node:child_process"; |
| 6 | +import test from "node:test"; |
| 7 | + |
| 8 | +const script = new URL("./audio.mjs", import.meta.url).pathname; |
| 9 | + |
| 10 | +// The header of this adapter declares it "intentionally identical across the |
| 11 | +// reusing skills" — pin that contract so a fix landing in one copy can't |
| 12 | +// silently miss the other (the fully-silent marker bug did exactly that). |
| 13 | +test("audio.mjs is byte-identical to pr-to-video's copy (the stated contract)", () => { |
| 14 | + const here = readFileSync(script, "utf8"); |
| 15 | + const sibling = readFileSync( |
| 16 | + new URL("../../pr-to-video/scripts/audio.mjs", import.meta.url), |
| 17 | + "utf8", |
| 18 | + ); |
| 19 | + assert.equal(here, sibling); |
| 20 | +}); |
| 21 | + |
| 22 | +// ── the canonical fully-silent marker (SKILL.md Step 3.1) ──────────────────── |
| 23 | +// Same contract as product-launch (see its audio.test.mjs): `music: none` in |
| 24 | +// the storyboard's top YAML block + no SCRIPT.md ⇒ generate nothing; with |
| 25 | +// narration ⇒ TTS runs, BGM off. |
| 26 | + |
| 27 | +function runAudioRaw({ storyboard, scriptMd = null, preexistingMeta = null }) { |
| 28 | + const dir = mkdtempSync(join(tmpdir(), "faceless-audio-")); |
| 29 | + const engine = join(dir, "engine.mjs"); |
| 30 | + writeFileSync(join(dir, "STORYBOARD.md"), storyboard); |
| 31 | + if (scriptMd != null) writeFileSync(join(dir, "SCRIPT.md"), scriptMd); |
| 32 | + if (preexistingMeta != null) writeFileSync(join(dir, "audio_meta.json"), preexistingMeta); |
| 33 | + writeFileSync( |
| 34 | + engine, |
| 35 | + `import { readFileSync, writeFileSync } from "node:fs"; |
| 36 | +const argv = process.argv.slice(2); |
| 37 | +const flag = (name) => argv[argv.indexOf(name) + 1]; |
| 38 | +const request = JSON.parse(readFileSync(flag("--request"), "utf8")); |
| 39 | +writeFileSync(new URL("request.json", import.meta.url), JSON.stringify(request)); |
| 40 | +writeFileSync(flag("--out"), JSON.stringify({ voices: [], bgm: null, sfx: [] })); |
| 41 | +`, |
| 42 | + ); |
| 43 | + const result = spawnSync( |
| 44 | + process.execPath, |
| 45 | + [script, "--hyperframes", dir, "--storyboard", join(dir, "STORYBOARD.md")], |
| 46 | + { encoding: "utf8", env: { ...process.env, HF_MEDIA_ENGINE: engine } }, |
| 47 | + ); |
| 48 | + return { dir, result }; |
| 49 | +} |
| 50 | + |
| 51 | +test("music: none + no SCRIPT.md = fully silent: no engine run, no audio_meta.json", () => { |
| 52 | + const { dir, result } = runAudioRaw({ storyboard: "---\nmessage: Test\nmusic: none\n---\n" }); |
| 53 | + |
| 54 | + assert.equal(result.status, 0, result.stderr); |
| 55 | + assert.match(result.stdout, /marked silent/); |
| 56 | + assert.equal(existsSync(join(dir, "request.json")), false); |
| 57 | + assert.equal(existsSync(join(dir, "audio_meta.json")), false); |
| 58 | +}); |
| 59 | + |
| 60 | +test("fully-silent run removes stale audio_meta.json from a previous non-silent run", () => { |
| 61 | + const { dir, result } = runAudioRaw({ |
| 62 | + storyboard: "---\nmessage: Test\nmusic: none\n---\n", |
| 63 | + preexistingMeta: JSON.stringify({ bgm: { path: "old.mp3" }, voices: [], sfx: [] }), |
| 64 | + }); |
| 65 | + |
| 66 | + assert.equal(result.status, 0, result.stderr); |
| 67 | + assert.equal(existsSync(join(dir, "audio_meta.json")), false); |
| 68 | +}); |
| 69 | + |
| 70 | +test("music: none with narration keeps TTS but turns BGM off (not fully silent)", () => { |
| 71 | + const { dir, result } = runAudioRaw({ |
| 72 | + storyboard: "---\nmessage: Test\nmusic: none\n---\n", |
| 73 | + scriptMd: "## Hook (Frame 1)\n\n Spoken line for frame one.\n", |
| 74 | + }); |
| 75 | + |
| 76 | + assert.equal(result.status, 0, result.stderr); |
| 77 | + const request = JSON.parse(readFileSync(join(dir, "request.json"), "utf8")); |
| 78 | + assert.equal(request.bgm.mode, "none"); |
| 79 | + assert.equal(request.lines.length, 1); |
| 80 | +}); |
| 81 | + |
| 82 | +test("a storyboard music mood still retrieves BGM (marker is exact, not fuzzy)", () => { |
| 83 | + const { dir, result } = runAudioRaw({ |
| 84 | + storyboard: "---\nmessage: Test\nmusic: upbeat synthwave with heavy drums\n---\n", |
| 85 | + }); |
| 86 | + |
| 87 | + assert.equal(result.status, 0, result.stderr); |
| 88 | + const request = JSON.parse(readFileSync(join(dir, "request.json"), "utf8")); |
| 89 | + assert.equal(request.bgm.mode, "retrieve"); |
| 90 | + assert.equal(request.bgm.query, "upbeat synthwave with heavy drums"); |
| 91 | +}); |
0 commit comments