oclaw/openclaw/extensions/microsoft/tts.test.ts
oliver dbbe3add6a 重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。

Made-with: Cursor
2026-04-26 08:34:33 +08:00

74 lines
1.9 KiB
TypeScript

import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
let edgeTTS: typeof import("./tts.js").edgeTTS;
let mockTtsPromise = vi.fn<(text: string, filePath: string) => Promise<void>>();
vi.mock("node-edge-tts", () => ({
EdgeTTS: class {
ttsPromise(text: string, filePath: string) {
return mockTtsPromise(text, filePath);
}
},
}));
const baseEdgeConfig = {
voice: "en-US-MichelleNeural",
lang: "en-US",
outputFormat: "audio-24khz-48kbitrate-mono-mp3",
saveSubtitles: false,
};
describe("edgeTTS empty audio validation", () => {
let tempDir: string | undefined;
beforeAll(async () => {
({ edgeTTS } = await import("./tts.js"));
});
afterEach(() => {
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
tempDir = undefined;
}
});
it("throws when the output file is 0 bytes", async () => {
tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
const outputPath = path.join(tempDir, "voice.mp3");
mockTtsPromise = vi.fn(async (_text: string, filePath: string) => {
writeFileSync(filePath, "");
});
await expect(
edgeTTS({
text: "Hello",
outputPath,
config: baseEdgeConfig,
timeoutMs: 10000,
}),
).rejects.toThrow("Edge TTS produced empty audio file");
});
it("succeeds when the output file has content", async () => {
tempDir = mkdtempSync(path.join(tmpdir(), "tts-test-"));
const outputPath = path.join(tempDir, "voice.mp3");
mockTtsPromise = vi.fn(async (_text: string, filePath: string) => {
writeFileSync(filePath, Buffer.from([0xff, 0xfb, 0x90, 0x00]));
});
await expect(
edgeTTS({
text: "Hello",
outputPath,
config: baseEdgeConfig,
timeoutMs: 10000,
}),
).resolves.toBeUndefined();
});
});