File size: 2,968 Bytes
d9e3e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { Hono } from "hono";

import { elevenLabsStreamUrl } from "../services/tts/providers/elevenlabs.provider";
import { ttsApi } from "./tts.api";

const PCM_CONTENT_TYPE = "audio/pcm;encoding=s16le;rate=24000;channels=1";

function app(): Hono {
  const root = new Hono();
  root.route("/api/tts", ttsApi);
  return root;
}

describe("runtime TTS formats", () => {
  const previousKey = process.env.ELEVENLABS_API_KEY;
  let fetchSpy: ReturnType<typeof spyOn<typeof globalThis, "fetch">>;
  let requestedUrls: string[];

  beforeEach(() => {
    process.env.ELEVENLABS_API_KEY = "test-key";
    requestedUrls = [];
    fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: string | URL | Request) => {
      requestedUrls.push(String(input));
      return new Response(new Uint8Array([1, 0, 2, 0]), { status: 200 });
    }) as typeof fetch);
  });

  afterEach(() => {
    fetchSpy.mockRestore();
    if (previousKey === undefined) delete process.env.ELEVENLABS_API_KEY;
    else process.env.ELEVENLABS_API_KEY = previousKey;
  });

  test("MP3 stays the default for preflight and speech", async () => {
    const head = await app().request("/api/tts/speak?text=Hi", { method: "HEAD" });
    expect(head.headers.get("content-type")).toBe("audio/mpeg");
    expect(requestedUrls).toEqual([]);

    const speech = await app().request("/api/tts/speak?text=Hi");
    expect(speech.headers.get("content-type")).toBe("audio/mpeg");
    expect(requestedUrls).toHaveLength(1);
    expect(requestedUrls[0]).not.toContain("output_format");
  });

  test("pcm_24000 asks ElevenLabs for raw PCM and labels its sample format", async () => {
    const head = await app().request("/api/tts/speak?text=Hi&format=pcm_24000", { method: "HEAD" });
    expect(head.status).toBe(200);
    expect(head.headers.get("content-type")).toBe(PCM_CONTENT_TYPE);
    expect(requestedUrls).toEqual([]);

    const speech = await app().request("/api/tts/speak?text=Hi&format=pcm_24000");
    expect(speech.headers.get("content-type")).toBe(PCM_CONTENT_TYPE);
    expect(requestedUrls[0]).toEndWith("/stream?output_format=pcm_24000");
    expect(new Uint8Array(await speech.arrayBuffer())).toEqual(new Uint8Array([1, 0, 2, 0]));
  });

  test("rejects an unknown format before synthesizing", async () => {
    const response = await app().request("/api/tts/speak?text=Hi&format=opus_48000_32");
    expect(response.status).toBe(400);
    expect(requestedUrls).toEqual([]);
  });

  test("builds the stream URL per format", () => {
    expect(elevenLabsStreamUrl("voice", "mp3")).toBe(
      "https://api.elevenlabs.io/v1/text-to-speech/voice/stream",
    );
    expect(elevenLabsStreamUrl("voice", "pcm_24000")).toBe(
      "https://api.elevenlabs.io/v1/text-to-speech/voice/stream?output_format=pcm_24000",
    );
  });
});