























@@ -1,13 +1,20 @@
11import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
22import { tmpdir } from "node:os";
33import path from "node:path";
4-import { afterEach, beforeAll, describe, expect, it } from "vitest";
4+import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
5566let edgeTTS: typeof import("./tts.js").edgeTTS;
778-function createEdgeTTSDeps(ttsPromise: (text: string, filePath: string) => Promise<void>) {
8+function createEdgeTTSDeps(
9+ttsPromise: (text: string, filePath: string) => Promise<void>,
10+onConstruct?: () => void,
11+) {
912return {
1013EdgeTTS: class {
14+constructor() {
15+onConstruct?.();
16+}
17+1118ttsPromise(text: string, filePath: string) {
1219return ttsPromise(text, filePath);
1320}
@@ -36,11 +43,35 @@ describe("edgeTTS empty audio validation", () => {
3643}
3744});
384539-it("throws when the output file is 0 bytes", async () => {
46+it("rejects blank text before constructing Edge TTS", async () => {
4047tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
4148const outputPath = path.join(tempDir, "voice.mp3");
42-49+ const onConstruct = vi.fn();
4350const deps = createEdgeTTSDeps(async (_text: string, filePath: string) => {
51+writeFileSync(filePath, Buffer.from([0xff]));
52+}, onConstruct);
53+54+await expect(
55+edgeTTS(
56+{
57+text: " \n\t ",
58+ outputPath,
59+config: baseEdgeConfig,
60+timeoutMs: 10000,
61+},
62+deps,
63+),
64+).rejects.toThrow("Microsoft TTS text cannot be empty");
65+expect(onConstruct).not.toHaveBeenCalled();
66+});
67+68+it("throws after one retry when the output file stays empty", async () => {
69+tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
70+const outputPath = path.join(tempDir, "voice.mp3");
71+const calls: string[] = [];
72+73+const deps = createEdgeTTSDeps(async (text: string, filePath: string) => {
74+calls.push(text);
4475writeFileSync(filePath, "");
4576});
4677@@ -54,7 +85,8 @@ describe("edgeTTS empty audio validation", () => {
5485},
5586deps,
5687),
57-).rejects.toThrow("Edge TTS produced empty audio file");
88+).rejects.toThrow("Edge TTS produced empty audio file after retry");
89+expect(calls).toEqual(["Hello", "Hello"]);
5890});
59916092it("succeeds when the output file has content", async () => {
@@ -77,4 +109,78 @@ describe("edgeTTS empty audio validation", () => {
77109),
78110).resolves.toBeUndefined();
79111});
112+113+it("retries once when the first output file is empty", async () => {
114+tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
115+const outputPath = path.join(tempDir, "voice.mp3");
116+const calls: string[] = [];
117+118+const deps = createEdgeTTSDeps(async (text: string, filePath: string) => {
119+calls.push(text);
120+writeFileSync(filePath, calls.length === 1 ? "" : Buffer.from([0xff, 0xfb, 0x90, 0x00]));
121+});
122+123+await expect(
124+edgeTTS(
125+{
126+text: "Hello",
127+ outputPath,
128+config: baseEdgeConfig,
129+timeoutMs: 10000,
130+},
131+deps,
132+),
133+).resolves.toBeUndefined();
134+expect(calls).toEqual(["Hello", "Hello"]);
135+});
136+137+it("retries once when Edge TTS resolves without creating an output file", async () => {
138+tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
139+const outputPath = path.join(tempDir, "voice.mp3");
140+const calls: string[] = [];
141+142+const deps = createEdgeTTSDeps(async (text: string, filePath: string) => {
143+calls.push(text);
144+if (calls.length === 2) {
145+writeFileSync(filePath, Buffer.from([0xff, 0xfb, 0x90, 0x00]));
146+}
147+});
148+149+await expect(
150+edgeTTS(
151+{
152+text: "Hello",
153+ outputPath,
154+config: baseEdgeConfig,
155+timeoutMs: 10000,
156+},
157+deps,
158+),
159+).resolves.toBeUndefined();
160+expect(calls).toEqual(["Hello", "Hello"]);
161+});
162+163+it("does not retry provider errors", async () => {
164+tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
165+const outputPath = path.join(tempDir, "voice.mp3");
166+const calls: string[] = [];
167+168+const deps = createEdgeTTSDeps(async (text: string) => {
169+calls.push(text);
170+throw new Error("upstream timeout");
171+});
172+173+await expect(
174+edgeTTS(
175+{
176+text: "Hello",
177+ outputPath,
178+config: baseEdgeConfig,
179+timeoutMs: 10000,
180+},
181+deps,
182+),
183+).rejects.toThrow("upstream timeout");
184+expect(calls).toEqual(["Hello"]);
185+});
80186});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。