|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { synthesizeOne } from "./tts.mjs"; |
| 7 | + |
| 8 | +// Regression: synthesizeHeygen used to collapse every failure (a real HTTP |
| 9 | +// error from HeyGen, a missing audio_url, a failed transcode) into a bare |
| 10 | +// { ok: false }, so callers could never tell a 402 plan-upgrade response |
| 11 | +// from a transient 500 or a local ffmpeg problem. heygenJSON already throws |
| 12 | +// a message carrying the HTTP status and response body — the fix is just to |
| 13 | +// stop swallowing it. Exercised via the real synthesizeOne() -> heygenJSON() |
| 14 | +// -> global fetch() path (heygenJSON has no seam to mock other than fetch |
| 15 | +// itself, which is a real global here, not a per-module import). |
| 16 | +test("synthesizeOne surfaces the real HeyGen HTTP error instead of a bare ok:false", async () => { |
| 17 | + const dir = mkdtempSync(join(tmpdir(), "heygen-tts-error-")); |
| 18 | + const originalFetch = globalThis.fetch; |
| 19 | + const originalApiKey = process.env.HEYGEN_API_KEY; |
| 20 | + try { |
| 21 | + process.env.HEYGEN_API_KEY = "fake-key-for-test"; |
| 22 | + globalThis.fetch = async () => ({ |
| 23 | + ok: false, |
| 24 | + status: 402, |
| 25 | + text: async () => JSON.stringify({ error: "plan_upgrade_required" }), |
| 26 | + }); |
| 27 | + |
| 28 | + const result = await synthesizeOne({ |
| 29 | + provider: "heygen", |
| 30 | + text: "hello", |
| 31 | + voiceId: "some-voice", |
| 32 | + wavAbs: join(dir, "out.wav"), |
| 33 | + hyperframesDir: dir, |
| 34 | + }); |
| 35 | + |
| 36 | + assert.equal(result.ok, false); |
| 37 | + assert.match(result.error, /HTTP 402/); |
| 38 | + assert.match(result.error, /plan_upgrade_required/); |
| 39 | + } finally { |
| 40 | + globalThis.fetch = originalFetch; |
| 41 | + if (originalApiKey === undefined) delete process.env.HEYGEN_API_KEY; |
| 42 | + else process.env.HEYGEN_API_KEY = originalApiKey; |
| 43 | + rmSync(dir, { recursive: true, force: true }); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +test("synthesizeOne reports a missing audio_url distinctly from an HTTP error", async () => { |
| 48 | + const dir = mkdtempSync(join(tmpdir(), "heygen-tts-no-url-")); |
| 49 | + const originalFetch = globalThis.fetch; |
| 50 | + const originalApiKey = process.env.HEYGEN_API_KEY; |
| 51 | + try { |
| 52 | + process.env.HEYGEN_API_KEY = "fake-key-for-test"; |
| 53 | + globalThis.fetch = async () => ({ |
| 54 | + ok: true, |
| 55 | + status: 200, |
| 56 | + json: async () => ({ data: {} }), |
| 57 | + }); |
| 58 | + |
| 59 | + const result = await synthesizeOne({ |
| 60 | + provider: "heygen", |
| 61 | + text: "hello", |
| 62 | + voiceId: "some-voice", |
| 63 | + wavAbs: join(dir, "out.wav"), |
| 64 | + hyperframesDir: dir, |
| 65 | + }); |
| 66 | + |
| 67 | + assert.equal(result.ok, false); |
| 68 | + assert.match(result.error, /no audio_url/); |
| 69 | + } finally { |
| 70 | + globalThis.fetch = originalFetch; |
| 71 | + if (originalApiKey === undefined) delete process.env.HEYGEN_API_KEY; |
| 72 | + else process.env.HEYGEN_API_KEY = originalApiKey; |
| 73 | + rmSync(dir, { recursive: true, force: true }); |
| 74 | + } |
| 75 | +}); |
0 commit comments