|
1 | 1 | // Azure Speech tests cover tts plugin behavior. |
| 2 | +import { createServer } from "node:http"; |
| 3 | +import type { AddressInfo } from "node:net"; |
2 | 4 | import { installPinnedHostnameTestHooks } from "openclaw/plugin-sdk/test-media-understanding"; |
3 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
4 | 6 | import { |
@@ -226,4 +228,73 @@ describe("azure speech tts", () => { |
226 | 228 | }, |
227 | 229 | ]); |
228 | 230 | }); |
| 231 | + it.each([ |
| 232 | + { name: "JSON error", contentType: "application/json", body: '{"error":"denied"}' }, |
| 233 | + { name: "problem JSON", contentType: "application/problem+json", body: '{"title":"denied"}' }, |
| 234 | + { name: "HTML", contentType: "text/html; charset=utf-8", body: "<html>sign in</html>" }, |
| 235 | + { name: "empty audio", contentType: "audio/mpeg", body: "" }, |
| 236 | + ])("rejects a successful $name response as synthesized audio", async ({ contentType, body }) => { |
| 237 | + const fetchMock = vi |
| 238 | + .fn() |
| 239 | + .mockResolvedValue( |
| 240 | + new Response(body, { status: 200, headers: { "content-type": contentType } }), |
| 241 | + ); |
| 242 | + vi.stubGlobal("fetch", fetchMock); |
| 243 | + |
| 244 | + await expect( |
| 245 | + azureSpeechTTS({ |
| 246 | + text: "hello", |
| 247 | + apiKey: "fixture-value", |
| 248 | + region: "eastus", |
| 249 | + voice: "en-US-JennyNeural", |
| 250 | + lang: "en-US", |
| 251 | + outputFormat: "audio-24khz-48kbitrate-mono-mp3", |
| 252 | + timeoutMs: 5_000, |
| 253 | + }), |
| 254 | + ).rejects.toThrow("Azure Speech TTS API error: malformed audio response"); |
| 255 | + }); |
| 256 | + |
| 257 | + it("closes the upstream socket for a never-ending malformed response over a real connection", async () => { |
| 258 | + let notifySocketClosed: ((closed: boolean) => void) | undefined; |
| 259 | + const socketClosed = new Promise<boolean>((resolve) => { |
| 260 | + notifySocketClosed = resolve; |
| 261 | + }); |
| 262 | + const server = createServer((request, response) => { |
| 263 | + request.socket.once("close", () => notifySocketClosed?.(true)); |
| 264 | + response.writeHead(200, { "content-type": "application/json" }); |
| 265 | + // Headers land, then the body never ends: only an explicit cancel closes this. |
| 266 | + response.write('{"error":"still streaming'); |
| 267 | + }); |
| 268 | + await new Promise<void>((resolve) => { |
| 269 | + server.listen(0, "127.0.0.1", resolve); |
| 270 | + }); |
| 271 | + |
| 272 | + try { |
| 273 | + const { port } = server.address() as AddressInfo; |
| 274 | + await expect( |
| 275 | + azureSpeechTTS({ |
| 276 | + text: "hello", |
| 277 | + apiKey: "fixture-value", |
| 278 | + endpoint: `http://127.0.0.1:${port}`, |
| 279 | + voice: "en-US-JennyNeural", |
| 280 | + lang: "en-US", |
| 281 | + timeoutMs: 5_000, |
| 282 | + }), |
| 283 | + ).rejects.toThrow("Azure Speech TTS API error: malformed audio response"); |
| 284 | + |
| 285 | + await expect( |
| 286 | + Promise.race([ |
| 287 | + socketClosed, |
| 288 | + new Promise<boolean>((resolve) => { |
| 289 | + setTimeout(() => resolve(false), 250); |
| 290 | + }), |
| 291 | + ]), |
| 292 | + ).resolves.toBe(true); |
| 293 | + } finally { |
| 294 | + server.closeAllConnections(); |
| 295 | + await new Promise<void>((resolve, reject) => { |
| 296 | + server.close((error) => (error ? reject(error) : resolve())); |
| 297 | + }); |
| 298 | + } |
| 299 | + }); |
229 | 300 | }); |
0 commit comments