@ssml-builder-js/azure-tts-client 2.14.0 → 2.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import {
4
+ AzureTtsError,
5
+ SynthesisCancelledError,
6
+ SynthesisTimeoutError,
7
+ UnsupportedMergeFormatError,
8
+ mergeSynthesisResults,
9
+ synthesizeSsmlChunksSafe,
10
+ synthesizeSsmlSafe,
11
+ } from "../src/index.ts";
12
+
13
+ const audio = (values: number[]): ArrayBuffer => Uint8Array.from(values).buffer;
14
+ const validSsml = (text: string) =>
15
+ `<speak version="1.0" xml:lang="en-US"><voice name="en-US-JennyNeural">${text}</voice></speak>`;
16
+
17
+ test("merge APIs require a format at runtime and expose MIME metadata", async () => {
18
+ assert.throws(
19
+ () => mergeSynthesisResults([{ audioData: audio([1]), durationMs: 1 }], undefined as never),
20
+ (error: unknown) => error instanceof UnsupportedMergeFormatError && error.kind === "unsupported-format-error",
21
+ );
22
+ const merged = mergeSynthesisResults([{ audioData: audio([1]), durationMs: 1 }], {
23
+ format: "webm-24khz-16bit-mono-opus",
24
+ customMerger: (buffers) => buffers[0] ?? new ArrayBuffer(0),
25
+ });
26
+ assert.equal((await merged).mimeType, "audio/webm");
27
+ });
28
+
29
+ test("safe synthesis returns each discriminated error kind", async () => {
30
+ const validation = await synthesizeSsmlSafe(
31
+ { synthesizeSsml: async () => ({ audioData: new ArrayBuffer(0), durationMs: 0 }) },
32
+ "<speak>",
33
+ );
34
+ assert.equal(validation.ok, false);
35
+ if (!validation.ok) assert.equal(validation.error.kind, "validation-error");
36
+
37
+ const azure = await synthesizeSsmlSafe(
38
+ {
39
+ synthesizeSsml: async () => {
40
+ throw new AzureTtsError(429, "Too Many Requests", "{}", "req");
41
+ },
42
+ },
43
+ validSsml("hello"),
44
+ );
45
+ assert.equal(azure.ok, false);
46
+ if (!azure.ok) {
47
+ assert.equal(azure.error.kind, "azure-api-error");
48
+ assert.equal(azure.error.status, 429);
49
+ }
50
+
51
+ for (const error of [new SynthesisTimeoutError("timed out"), new SynthesisCancelledError()]) {
52
+ const result = await synthesizeSsmlChunksSafe(
53
+ {
54
+ synthesizeSsml: async () => {
55
+ throw error;
56
+ },
57
+ },
58
+ [validSsml("hello")],
59
+ { outputFormat: "audio-16khz-128kbitrate-mono-mp3" },
60
+ );
61
+ assert.equal(result.ok, false);
62
+ if (!result.ok) assert.equal(result.error.kind, error.kind);
63
+ }
64
+ });
65
+
66
+ test("chunk safe synthesis propagates output, cancellation, timeout, and source path settings", async () => {
67
+ const controller = new AbortController();
68
+ const calls: Array<{ options?: Record<string, unknown> }> = [];
69
+ let validatorSignal: AbortSignal | undefined;
70
+ let validatorPath: readonly string[] | undefined;
71
+ const result = await synthesizeSsmlChunksSafe(
72
+ {
73
+ synthesizeSsml: async (_ssml, options) => {
74
+ calls.push({ options: options as Record<string, unknown> });
75
+ return { audioData: new ArrayBuffer(0), durationMs: 0 };
76
+ },
77
+ },
78
+ [
79
+ {
80
+ ssml: `${validSsml("hello").replace("</voice>", '<audio src="https://example.test/a.mp3"/></voice>')}`,
81
+ originalTextRange: { start: 10, end: 15 },
82
+ },
83
+ ],
84
+ {
85
+ allowExternalAudio: true,
86
+ outputFormat: "audio-16khz-128kbitrate-mono-mp3",
87
+ signal: controller.signal,
88
+ timeoutMs: 1234,
89
+ sourceNodePath: ["speak", "voice[0]"],
90
+ urlValidator: async (_url, context, signal) => {
91
+ validatorPath = context.sourceNodePath;
92
+ validatorSignal = signal;
93
+ return true;
94
+ },
95
+ },
96
+ );
97
+ assert.equal(result.ok, true);
98
+ assert.equal(calls[0]?.options?.outputFormat, "audio-16khz-128kbitrate-mono-mp3");
99
+ assert.equal(calls[0]?.options?.signal, controller.signal);
100
+ assert.equal(calls[0]?.options?.timeoutMs, 1234);
101
+ assert.deepEqual(calls[0]?.options?.sourceNodePath, ["speak", "voice[0]"]);
102
+ assert.equal(validatorSignal, controller.signal);
103
+ assert.deepEqual(validatorPath, ["speak", "voice[0]"]);
104
+ });