"use strict";
var import_vitest = require("vitest");
var import_language = require("../language.cjs");
var import_log = require("../log.cjs");
var import_types = require("../types.cjs");
var import_tts = require("./tts.cjs");
(0, import_vitest.beforeAll)(() => {
  (0, import_log.initializeLogger)({ level: "silent", pretty: false });
});
function makeTts(overrides = {}) {
  const defaults = {
    model: "cartesia/sonic",
    apiKey: "test-key",
    apiSecret: "test-secret",
    baseURL: "https://example.livekit.cloud"
  };
  return new import_tts.TTS({ ...defaults, ...overrides });
}
(0, import_vitest.describe)("parseTTSModelString", () => {
  (0, import_vitest.it)("simple model without voice", () => {
    const [model, voice] = (0, import_tts.parseTTSModelString)("cartesia");
    (0, import_vitest.expect)(model).toBe("cartesia");
    (0, import_vitest.expect)(voice).toBeUndefined();
  });
  (0, import_vitest.it)("model with voice suffix", () => {
    const [model, voice] = (0, import_tts.parseTTSModelString)("cartesia:my-voice-id");
    (0, import_vitest.expect)(model).toBe("cartesia");
    (0, import_vitest.expect)(voice).toBe("my-voice-id");
  });
  (0, import_vitest.it)("provider/model format without voice", () => {
    const [model, voice] = (0, import_tts.parseTTSModelString)("cartesia/sonic");
    (0, import_vitest.expect)(model).toBe("cartesia/sonic");
    (0, import_vitest.expect)(voice).toBeUndefined();
  });
  (0, import_vitest.it)("provider/model format with voice", () => {
    const [model, voice] = (0, import_tts.parseTTSModelString)("cartesia/sonic:my-voice-id");
    (0, import_vitest.expect)(model).toBe("cartesia/sonic");
    (0, import_vitest.expect)(voice).toBe("my-voice-id");
  });
  import_vitest.it.each([
    ["elevenlabs/eleven_flash_v2:voice123", "elevenlabs/eleven_flash_v2", "voice123"],
    ["rime:speaker-a", "rime", "speaker-a"],
    ["rime/mist:narrator", "rime/mist", "narrator"],
    ["inworld/inworld-tts-1:character", "inworld/inworld-tts-1", "character"],
    ["cartesia/sonic-turbo:deep-voice", "cartesia/sonic-turbo", "deep-voice"]
  ])("various providers and voices: %s", (modelStr, expectedModel, expectedVoice) => {
    const [model, voice] = (0, import_tts.parseTTSModelString)(modelStr);
    (0, import_vitest.expect)(model).toBe(expectedModel);
    (0, import_vitest.expect)(voice).toBe(expectedVoice);
  });
  (0, import_vitest.it)("empty voice after colon", () => {
    const [model, voice] = (0, import_tts.parseTTSModelString)("cartesia/sonic:");
    (0, import_vitest.expect)(model).toBe("cartesia/sonic");
    (0, import_vitest.expect)(voice).toBe("");
  });
});
(0, import_vitest.describe)("normalizeTTSFallback", () => {
  (0, import_vitest.it)("single string model", () => {
    const result = (0, import_tts.normalizeTTSFallback)("cartesia/sonic");
    (0, import_vitest.expect)(result).toEqual([{ model: "cartesia/sonic", voice: "" }]);
  });
  (0, import_vitest.it)("single string model with voice", () => {
    const result = (0, import_tts.normalizeTTSFallback)("cartesia/sonic:my-voice");
    (0, import_vitest.expect)(result).toEqual([{ model: "cartesia/sonic", voice: "my-voice" }]);
  });
  (0, import_vitest.it)("single FallbackModel dict", () => {
    const fallback = { model: "cartesia/sonic", voice: "narrator" };
    const result = (0, import_tts.normalizeTTSFallback)(fallback);
    (0, import_vitest.expect)(result).toEqual([{ model: "cartesia/sonic", voice: "narrator" }]);
  });
  (0, import_vitest.it)("list of string models", () => {
    const result = (0, import_tts.normalizeTTSFallback)(["cartesia/sonic", "elevenlabs/eleven_flash_v2"]);
    (0, import_vitest.expect)(result).toEqual([
      { model: "cartesia/sonic", voice: "" },
      { model: "elevenlabs/eleven_flash_v2", voice: "" }
    ]);
  });
  (0, import_vitest.it)("list of string models with voices", () => {
    const result = (0, import_tts.normalizeTTSFallback)(["cartesia/sonic:voice1", "elevenlabs:voice2"]);
    (0, import_vitest.expect)(result).toEqual([
      { model: "cartesia/sonic", voice: "voice1" },
      { model: "elevenlabs", voice: "voice2" }
    ]);
  });
  (0, import_vitest.it)("list of FallbackModel dicts", () => {
    const fallbacks = [
      { model: "cartesia/sonic", voice: "narrator" },
      { model: "elevenlabs", voice: "" }
    ];
    const result = (0, import_tts.normalizeTTSFallback)(fallbacks);
    (0, import_vitest.expect)(result).toEqual([
      { model: "cartesia/sonic", voice: "narrator" },
      { model: "elevenlabs", voice: "" }
    ]);
  });
  (0, import_vitest.it)("mixed list of strings and dicts", () => {
    const result = (0, import_tts.normalizeTTSFallback)([
      "cartesia/sonic:voice1",
      { model: "elevenlabs/eleven_flash_v2", voice: "custom" },
      "rime/mist"
    ]);
    (0, import_vitest.expect)(result).toEqual([
      { model: "cartesia/sonic", voice: "voice1" },
      { model: "elevenlabs/eleven_flash_v2", voice: "custom" },
      { model: "rime/mist", voice: "" }
    ]);
  });
  (0, import_vitest.it)("FallbackModel with extraKwargs is preserved", () => {
    const fallback = {
      model: "cartesia/sonic",
      voice: "narrator",
      extraKwargs: { duration: 30, speed: "fast" }
    };
    const result = (0, import_tts.normalizeTTSFallback)(fallback);
    (0, import_vitest.expect)(result).toEqual([
      {
        model: "cartesia/sonic",
        voice: "narrator",
        extraKwargs: { duration: 30, speed: "fast" }
      }
    ]);
  });
  (0, import_vitest.it)("list with extraKwargs preserved", () => {
    const result = (0, import_tts.normalizeTTSFallback)([
      { model: "cartesia/sonic", voice: "v1", extraKwargs: { speed: "slow" } },
      "elevenlabs:voice2",
      { model: "rime/mist", voice: "", extraKwargs: { custom: true } }
    ]);
    (0, import_vitest.expect)(result).toEqual([
      { model: "cartesia/sonic", voice: "v1", extraKwargs: { speed: "slow" } },
      { model: "elevenlabs", voice: "voice2" },
      { model: "rime/mist", voice: "", extraKwargs: { custom: true } }
    ]);
  });
  (0, import_vitest.it)("empty list returns empty list", () => {
    const result = (0, import_tts.normalizeTTSFallback)([]);
    (0, import_vitest.expect)(result).toEqual([]);
  });
  (0, import_vitest.it)("FallbackModel with empty voice", () => {
    const fallback = { model: "cartesia/sonic", voice: "" };
    const result = (0, import_tts.normalizeTTSFallback)(fallback);
    (0, import_vitest.expect)(result).toEqual([{ model: "cartesia/sonic", voice: "" }]);
  });
});
(0, import_vitest.describe)("TTS constructor fallback and connOptions", () => {
  (0, import_vitest.it)("normalizes language in constructor", () => {
    const tts = makeTts({ language: "english" });
    (0, import_vitest.expect)(tts["opts"].language).toBe("en");
  });
  (0, import_vitest.it)("normalizes updated language values", () => {
    const tts = makeTts();
    tts.updateOptions({ language: "en_US" });
    (0, import_vitest.expect)(tts["opts"].language).toBe((0, import_language.normalizeLanguage)("en_US"));
  });
  (0, import_vitest.it)("fallback not given defaults to undefined", () => {
    const tts = makeTts();
    (0, import_vitest.expect)(tts["opts"].fallback).toBeUndefined();
  });
  (0, import_vitest.it)("fallback single string is normalized", () => {
    const tts = makeTts({ fallback: "elevenlabs/eleven_flash_v2" });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([{ model: "elevenlabs/eleven_flash_v2", voice: "" }]);
  });
  (0, import_vitest.it)("fallback single string with voice is normalized", () => {
    const tts = makeTts({ fallback: "cartesia/sonic:my-voice" });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([{ model: "cartesia/sonic", voice: "my-voice" }]);
  });
  (0, import_vitest.it)("fallback list of strings is normalized", () => {
    const tts = makeTts({ fallback: ["cartesia/sonic", "elevenlabs"] });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([
      { model: "cartesia/sonic", voice: "" },
      { model: "elevenlabs", voice: "" }
    ]);
  });
  (0, import_vitest.it)("fallback single FallbackModel is normalized to list", () => {
    const tts = makeTts({ fallback: { model: "cartesia/sonic", voice: "narrator" } });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([{ model: "cartesia/sonic", voice: "narrator" }]);
  });
  (0, import_vitest.it)("fallback with extraKwargs is preserved", () => {
    const tts = makeTts({
      fallback: {
        model: "cartesia/sonic",
        voice: "narrator",
        extraKwargs: { duration: 30, speed: "fast" }
      }
    });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([
      {
        model: "cartesia/sonic",
        voice: "narrator",
        extraKwargs: { duration: 30, speed: "fast" }
      }
    ]);
  });
  (0, import_vitest.it)("fallback mixed list is normalized", () => {
    const tts = makeTts({
      fallback: [
        "cartesia/sonic:voice1",
        { model: "elevenlabs", voice: "custom", extraKwargs: { speed: "slow" } },
        "rime/mist"
      ]
    });
    (0, import_vitest.expect)(tts["opts"].fallback).toEqual([
      { model: "cartesia/sonic", voice: "voice1" },
      { model: "elevenlabs", voice: "custom", extraKwargs: { speed: "slow" } },
      { model: "rime/mist", voice: "" }
    ]);
  });
  (0, import_vitest.it)("connOptions not given uses default", () => {
    const tts = makeTts();
    (0, import_vitest.expect)(tts["opts"].connOptions).toEqual(import_types.DEFAULT_API_CONNECT_OPTIONS);
  });
  (0, import_vitest.it)("connOptions custom timeout", () => {
    const custom = { timeoutMs: 3e4, maxRetry: 3, retryIntervalMs: 2e3 };
    const tts = makeTts({ connOptions: custom });
    (0, import_vitest.expect)(tts["opts"].connOptions).toEqual(custom);
    (0, import_vitest.expect)(tts["opts"].connOptions.timeoutMs).toBe(3e4);
  });
  (0, import_vitest.it)("connOptions custom maxRetry", () => {
    const custom = { timeoutMs: 1e4, maxRetry: 5, retryIntervalMs: 2e3 };
    const tts = makeTts({ connOptions: custom });
    (0, import_vitest.expect)(tts["opts"].connOptions).toEqual(custom);
    (0, import_vitest.expect)(tts["opts"].connOptions.maxRetry).toBe(5);
  });
  (0, import_vitest.it)("connOptions full custom", () => {
    const custom = { timeoutMs: 6e4, maxRetry: 10, retryIntervalMs: 2e3 };
    const tts = makeTts({ connOptions: custom });
    (0, import_vitest.expect)(tts["opts"].connOptions).toEqual(custom);
    (0, import_vitest.expect)(tts["opts"].connOptions.timeoutMs).toBe(6e4);
    (0, import_vitest.expect)(tts["opts"].connOptions.maxRetry).toBe(10);
    (0, import_vitest.expect)(tts["opts"].connOptions.retryIntervalMs).toBe(2e3);
  });
});
(0, import_vitest.describe)("TTS provider modelOptions", () => {
  (0, import_vitest.it)("preserves ElevenLabs inference model options", () => {
    const modelOptions = {
      speed: 1.2,
      stability: 0.5,
      similarity_boost: 0.8,
      enable_logging: false
    };
    const tts = new import_tts.TTS({
      model: "elevenlabs/eleven_flash_v2_5",
      apiKey: "test-key",
      apiSecret: "test-secret",
      baseURL: "https://example.livekit.cloud",
      modelOptions
    });
    (0, import_vitest.expect)(tts["opts"].modelOptions).toEqual(modelOptions);
  });
  (0, import_vitest.it)("accepts expanded Cartesia inference model options", () => {
    const modelOptions = {
      speed: 1.15,
      emotion: "curious",
      add_timestamps: true
    };
    const tts = new import_tts.TTS({
      model: "cartesia/sonic",
      apiKey: "test-key",
      apiSecret: "test-secret",
      baseURL: "https://example.livekit.cloud",
      modelOptions
    });
    (0, import_vitest.expect)(tts["opts"].modelOptions).toEqual(modelOptions);
  });
  (0, import_vitest.it)("accepts Deepgram inference model options", () => {
    const modelOptions = { mip_opt_out: true };
    const tts = new import_tts.TTS({
      model: "deepgram/aura-2",
      apiKey: "test-key",
      apiSecret: "test-secret",
      baseURL: "https://example.livekit.cloud",
      modelOptions
    });
    (0, import_vitest.expect)(tts["opts"].modelOptions).toEqual(modelOptions);
  });
  (0, import_vitest.it)("accepts Rime inference model options", () => {
    const modelOptions = {
      speed_alpha: 0.9,
      pause_between_brackets: true
    };
    const tts = new import_tts.TTS({
      model: "rime/mistv2",
      apiKey: "test-key",
      apiSecret: "test-secret",
      baseURL: "https://example.livekit.cloud",
      modelOptions
    });
    (0, import_vitest.expect)(tts["opts"].modelOptions).toEqual(modelOptions);
  });
  (0, import_vitest.it)("accepts Inworld inference model options", () => {
    const modelOptions = {
      timestamp_type: "WORD",
      apply_text_normalization: "ON"
    };
    const tts = new import_tts.TTS({
      model: "inworld/inworld-tts-1",
      apiKey: "test-key",
      apiSecret: "test-secret",
      baseURL: "https://example.livekit.cloud",
      modelOptions
    });
    (0, import_vitest.expect)(tts["opts"].modelOptions).toEqual(modelOptions);
  });
});
(0, import_vitest.describe)("hasAlignedTranscript", () => {
  (0, import_vitest.it)("returns false for unknown provider", () => {
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("rime/mistv2", { add_timestamps: true })).toBe(false);
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("deepgram/aura-2", { sync_alignment: true })).toBe(false);
  });
  (0, import_vitest.it)("returns false for an empty options payload", () => {
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("cartesia/sonic", {})).toBe(false);
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("elevenlabs/eleven_flash_v2", void 0)).toBe(false);
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)(void 0, { add_timestamps: true })).toBe(false);
  });
  (0, import_vitest.it)("detects Cartesia add_timestamps opt-in", () => {
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("cartesia/sonic", { add_timestamps: true })).toBe(true);
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("cartesia/sonic-3", { add_timestamps: false })).toBe(false);
  });
  (0, import_vitest.it)("detects ElevenLabs sync_alignment opt-in", () => {
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("elevenlabs/eleven_flash_v2", { sync_alignment: true })).toBe(true);
    (0, import_vitest.expect)(
      (0, import_tts.hasAlignedTranscript)("elevenlabs/eleven_multilingual_v2", { sync_alignment: false })
    ).toBe(false);
  });
  (0, import_vitest.it)("detects Inworld WORD/CHARACTER timestamp types", () => {
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("inworld/inworld-tts-1", { timestamp_type: "WORD" })).toBe(true);
    (0, import_vitest.expect)((0, import_tts.hasAlignedTranscript)("inworld/inworld-tts-1", { timestamp_type: "CHARACTER" })).toBe(
      true
    );
    (0, import_vitest.expect)(
      (0, import_tts.hasAlignedTranscript)("inworld/inworld-tts-1", {
        timestamp_type: "TIMESTAMP_TYPE_UNSPECIFIED"
      })
    ).toBe(false);
  });
});
(0, import_vitest.describe)("TTS alignedTranscript capability", () => {
  (0, import_vitest.it)("defaults to alignedTranscript=false when no opt-in is provided", () => {
    const tts = makeTts();
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(false);
  });
  (0, import_vitest.it)("reports alignedTranscript=true when Cartesia add_timestamps is set", () => {
    const tts = makeTts({
      model: "cartesia/sonic",
      modelOptions: { add_timestamps: true }
    });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(true);
  });
  (0, import_vitest.it)("reports alignedTranscript=true when ElevenLabs sync_alignment is set", () => {
    const tts = makeTts({
      model: "elevenlabs/eleven_flash_v2",
      modelOptions: { sync_alignment: true }
    });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(true);
  });
  (0, import_vitest.it)("reports alignedTranscript=true when Inworld timestamp_type is WORD", () => {
    const tts = makeTts({
      model: "inworld/inworld-tts-1",
      modelOptions: { timestamp_type: "WORD" }
    });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(true);
  });
  (0, import_vitest.it)("recomputes alignedTranscript when updateOptions changes modelOptions", () => {
    const tts = makeTts({ model: "cartesia/sonic" });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(false);
    tts.updateOptions({ modelOptions: { add_timestamps: true } });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(true);
    tts.updateOptions({ modelOptions: { add_timestamps: false } });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(false);
  });
  (0, import_vitest.it)("recomputes alignedTranscript when updateOptions changes the model", () => {
    const tts = makeTts({
      model: "cartesia/sonic",
      modelOptions: { sync_alignment: true }
    });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(false);
    tts.updateOptions({ model: "elevenlabs/eleven_flash_v2" });
    (0, import_vitest.expect)(tts.capabilities.alignedTranscript).toBe(true);
  });
  (0, import_vitest.it)("invalidates the connection pool when session-affecting options change", () => {
    const tts = makeTts({ model: "cartesia/sonic" });
    const invalidateSpy = import_vitest.vi.spyOn(tts.pool, "invalidate");
    tts.updateOptions({ modelOptions: { add_timestamps: true } });
    (0, import_vitest.expect)(invalidateSpy).toHaveBeenCalledTimes(1);
    tts.updateOptions({ model: "elevenlabs/eleven_flash_v2" });
    (0, import_vitest.expect)(invalidateSpy).toHaveBeenCalledTimes(2);
    tts.updateOptions({ voice: "narrator" });
    (0, import_vitest.expect)(invalidateSpy).toHaveBeenCalledTimes(3);
    tts.updateOptions({ language: "en" });
    (0, import_vitest.expect)(invalidateSpy).toHaveBeenCalledTimes(4);
    tts.updateOptions({});
    (0, import_vitest.expect)(invalidateSpy).toHaveBeenCalledTimes(4);
  });
});
//# sourceMappingURL=tts.test.cjs.map