36 lines
2.0 KiB
TypeScript
36 lines
2.0 KiB
TypeScript
import { afterEach, expect, it, vi } from "vitest";
|
|
import { createEdgeTtsVoiceCatalog, fetchEdgeTtsVoices } from "./edge-tts-voices";
|
|
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
|
|
it("deduplicates simultaneous catalog loads and refreshes after one hour", async () => {
|
|
let now = 0;
|
|
const load = vi.fn(async () => [{ id: "zh-CN-XiaoxiaoNeural", name: "Xiaoxiao", locale: "zh-CN" as const, gender: "Female" as const }]);
|
|
const catalog = createEdgeTtsVoiceCatalog(load, () => now);
|
|
const [first, second] = await Promise.all([catalog(), catalog()]);
|
|
expect(first).toBe(second);
|
|
expect(await catalog()).toBe(first);
|
|
expect(load).toHaveBeenCalledOnce();
|
|
now = 3_600_001;
|
|
await catalog();
|
|
expect(load).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("requests v2 authentication and only exposes validated mainland Mandarin voices", async () => {
|
|
const upstream = [
|
|
{ ShortName: "zh-CN-XiaoxiaoNeural", FriendlyName: "Xiaoxiao", Locale: "zh-CN", Gender: "Female" },
|
|
{ ShortName: "en-US-AriaNeural", FriendlyName: "Aria", Locale: "en-US", Gender: "Female" },
|
|
{ ShortName: "zh-HK-HiuMaanNeural", FriendlyName: "HiuMaan", Locale: "zh-HK", Gender: "Female" },
|
|
{ ShortName: "zh-TW-HsiaoChenNeural", FriendlyName: "HsiaoChen", Locale: "zh-TW", Gender: "Female" },
|
|
{ ShortName: "zh-CN-liaoning-XiaobeiNeural", FriendlyName: "Xiaobei", Locale: "zh-CN-liaoning", Gender: "Female" }
|
|
];
|
|
const fetchMock = vi.fn(async () => new Response(JSON.stringify(upstream)));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
expect(await fetchEdgeTtsVoices()).toEqual([{ id: upstream[0].ShortName, name: "Xiaoxiao", locale: "zh-CN", gender: "Female" }]);
|
|
const [url] = fetchMock.mock.calls[0] as unknown as [URL];
|
|
expect(url.searchParams.get("Sec-MS-GEC-Version")).toMatch(/^2-/);
|
|
expect(url.searchParams.get("Sec-MS-GEC")).toMatch(/^[0-9A-F]{16}-[0-9A-F]{64}$/);
|
|
fetchMock.mockImplementation(async () => new Response(JSON.stringify([{ ...upstream[0], ShortName: "bad<xml>" }])));
|
|
await expect(fetchEdgeTtsVoices()).rejects.toThrow();
|
|
});
|