Setting the file. One moment. TTS Test · Media Use · heygen-com/hyperframes · Skills Docs⋯
scripts/11 files
(opens in a new tab)
audio/scripts/lib/tts.test.mjs
JavaScript·157 lines·6 KB
9
synthesizeOne,
10 synthesizeHeygen,
11 synthResult,
12} from "./tts.mjs";
13
14test("parseFfmpegDurationBanner reads ffmpeg's stderr Duration line", () => {
15 const stderr = [
16 "ffmpeg version 6.0",
17 "Input #0, wav, from 'a.wav':",
18 " Duration: 00:00:03.42, bitrate: 705 kb/s",
19 "At least one output file must be specified",
20 ].join("\n");
21 assert.equal(parseFfmpegDurationBanner(stderr), 3.42);
22});
23
24test("parseFfmpegDurationBanner handles an hours component", () => {
25 const stderr = " Duration: 01:02:03.50, start: 0.000000, bitrate: 128 kb/s";
26 assert.equal(parseFfmpegDurationBanner(stderr), 3723.5);
27});
28
29test("parseFfmpegDurationBanner returns NaN when there is no Duration line", () => {
30 assert.ok(Number.isNaN(parseFfmpegDurationBanner("ffmpeg: command not found")));
31 assert.ok(Number.isNaN(parseFfmpegDurationBanner("")));
32 assert.ok(Number.isNaN(parseFfmpegDurationBanner(undefined)));
33});
34
35// Regression for the actual bug: ffprobeDuration used to collapse "ffprobe
36// binary is missing" (ENOENT — the "essentials"-style Windows ffmpeg build
37// with no ffprobe.exe) and "file is genuinely unreadable" into the same NaN,
38// giving audio.mjs no way to tell "measure differently" from "give up".
39//
40// Builds an isolated PATH containing only a fake `ffmpeg` stub (no `ffprobe`
41// at all) so ffprobeDuration's spawnSync("ffprobe", ...) call ENOENTs for
42// real, then verifies it recovers the duration via the ffmpeg fallback
43// instead of returning NaN.
44test("ffprobeDuration falls back to ffmpeg when the ffprobe binary itself is missing", () => {
45 const dir = mkdtempSync(join(tmpdir(), "tts-ffprobe-fallback-"));
46 const fakeFfmpeg = join(dir, "ffmpeg");
47 writeFileSync(
48 fakeFfmpeg,
49 "#!/bin/sh\necho 'Duration: 00:00:02.50, start: 0.000000, bitrate: 128 kb/s' 1>&2\nexit 1\n",
50 );
51 chmodSync(fakeFfmpeg, 0o755);
52 const originalPath = process.env.PATH;
53 try {
54 process.env.PATH = dir; // only the fake ffmpeg resolves; no real ffprobe on this PATH
55 assert.equal(ffprobeDuration("/does/not/matter.wav"), 2.5);
56 } finally {
57 process.env.PATH = originalPath;
58 rmSync(dir, { recursive: true, force: true });
59 }
60});
61
62test("ffprobeDuration returns NaN when neither ffprobe nor ffmpeg resolve", () => {
63 const dir = mkdtempSync(join(tmpdir(), "tts-no-binaries-"));
64 const originalPath = process.env.PATH;
65 try {
66 process.env.PATH = dir; // empty directory — nothing resolves
67 assert.ok(Number.isNaN(ffprobeDuration("/does/not/matter.wav")));
68 } finally {
69 process.env.PATH = originalPath;
70 rmSync(dir, { recursive: true, force: true });
71 }
72});
73
74test("synthesizeOne(elevenlabs) creates the output dir before writing", async () => {
75 const dir = mkdtempSync(join(tmpdir(), "tts-el-mkdir-"));
76 const wavAbs = join(dir, "assets", "voice", "line-0.wav"); // nested, not yet created
77 const savedKey = process.env.ELEVENLABS_API_KEY;
78 try {
79 // Unset the key so the Python side fails fast — the mkdir must run before
80 // the spawn regardless, which is what this guards.
81 delete process.env.ELEVENLABS_API_KEY;
82 await synthesizeOne({
83 provider: "elevenlabs",
84 text: "hi",
85 voiceId: "v",
86 wavAbs,
87 hyperframesDir: dir,
88 });
89 assert.ok(existsSync(dirname(wavAbs)), "output directory should be created");
90 } finally {
91 if (savedKey === undefined) delete process.env.ELEVENLABS_API_KEY;
92 else process.env.ELEVENLABS_API_KEY = savedKey;
93 rmSync(dir, { recursive: true, force: true });
94 }
95});
96
97test("synthesizeHeygen surfaces a thrown HTTP error (e.g. 402) instead of swallowing it", async () => {
98 const res = await synthesizeHeygen(
99 { text: "hi", voiceId: "v1", lang: "en", speed: 1, wavAbs: "/tmp/x.wav" },
100 {
101 heygenAuthHeaders: () => ({}),
102 heygenJSON: async () => {
103 throw new Error("HeyGen POST /voices/speech → HTTP 402\nplan_upgrade_required");
104 },
105 },
106 );
107 assert.equal(res.ok, false);
108 assert.match(res.error, /402/);
109 assert.match(res.error, /plan_upgrade_required/);
110});
111
112test("synthesizeHeygen surfaces a failed audio_url fetch with its status", async () => {
113 const res = await synthesizeHeygen(
114 { text: "hi", voiceId: "v1", lang: "en", speed: 1, wavAbs: "/tmp/x.wav" },
115 {
116 heygenAuthHeaders: () => ({}),
117 heygenJSON: async () => ({ data: { audio_url: "http://audio.example/x" } }),
118 fetch: async () => ({ ok: false, status: 403 }),
119 },
120 );
121 assert.equal(res.ok, false);
122 assert.match(res.error, /HTTP 403/);
123});
124
125test("synthesizeHeygen reports a missing audio_url", async () => {
126 const res = await synthesizeHeygen(
127 { text: "hi", voiceId: "v1", lang: "en", speed: 1, wavAbs: "/tmp/x.wav" },
128 { heygenAuthHeaders: () => ({}), heygenJSON: async () => ({}) },
129 );
130 assert.equal(res.ok, false);
131 assert.match(res.error, /no audio_url/);
132});
133
134test("synthesizeHeygen reports wav transcode failures", async () => {
135 const dir = mkdtempSync(join(tmpdir(), "hf-tts-test-"));
136 try {
137 const res = await synthesizeHeygen(
138 { text: "hi", voiceId: "v1", lang: "en", speed: 1, wavAbs: join(dir, "voice.wav") },
139 {
140 heygenAuthHeaders: () => ({}),
141 heygenJSON: async () => ({ data: { audio_url: "http://audio.example/x" } }),
142 fetch: async () => ({ ok: true, status: 200, arrayBuffer: async () => new ArrayBuffer(0) }),
143 transcodeToWav: () => false,
144 },
145 );
146 assert.equal(res.ok, false);
147 assert.equal(res.error, "wav transcode failed (ffmpeg)");
148 } finally {
149 rmSync(dir, { recursive: true, force: true });
150 }
151});
152
153test("synthResult names a non-zero subprocess exit", () => {
154 const res = synthResult({ status: 2 }, "/tmp/none.wav", "kokoro (npx hyperframes tts)");
155 assert.equal(res.ok, false);
156 assert.match(res.error, /kokoro .* exited with status 2/);
157});