Tests: mock fetch for TTS gating

This commit is contained in:
Sebastian 2026-01-24 19:49:49 -05:00 committed by Peter Steinberger
parent d25fc7aa2b
commit 50519189d2

View File

@ -437,16 +437,25 @@ describe("tts", () => {
describe("maybeApplyTtsToPayload", () => { describe("maybeApplyTtsToPayload", () => {
const baseCfg = { const baseCfg = {
agents: { defaults: { model: { primary: "openai/gpt-4o-mini" } } }, agents: { defaults: { model: { primary: "openai/gpt-4o-mini" } } },
messages: { tts: { enabled: true, onlyWhenInboundAudio: true } }, messages: {
tts: {
enabled: true,
onlyWhenInboundAudio: true,
provider: "openai",
openai: { apiKey: "test-key", model: "gpt-4o-mini-tts", voice: "alloy" },
},
},
}; };
it("skips auto-TTS when inbound audio gating is on and the message is not audio", async () => { it("skips auto-TTS when inbound audio gating is on and the message is not audio", async () => {
const prevPrefs = process.env.CLAWDBOT_TTS_PREFS; const prevPrefs = process.env.CLAWDBOT_TTS_PREFS;
process.env.CLAWDBOT_TTS_PREFS = `/tmp/tts-test-${Date.now()}.json`; process.env.CLAWDBOT_TTS_PREFS = `/tmp/tts-test-${Date.now()}.json`;
const spy = vi.spyOn(tts, "textToSpeech").mockResolvedValue({ const originalFetch = globalThis.fetch;
success: false, const fetchMock = vi.fn(async () => ({
error: "nope", ok: true,
}); arrayBuffer: async () => new ArrayBuffer(1),
}));
globalThis.fetch = fetchMock as unknown as typeof fetch;
const payload = { text: "Hello world" }; const payload = { text: "Hello world" };
const result = await maybeApplyTtsToPayload({ const result = await maybeApplyTtsToPayload({
@ -457,30 +466,33 @@ describe("tts", () => {
}); });
expect(result).toBe(payload); expect(result).toBe(payload);
expect(spy).not.toHaveBeenCalled(); expect(fetchMock).not.toHaveBeenCalled();
spy.mockRestore(); globalThis.fetch = originalFetch;
process.env.CLAWDBOT_TTS_PREFS = prevPrefs; process.env.CLAWDBOT_TTS_PREFS = prevPrefs;
}); });
it("attempts auto-TTS when inbound audio gating is on and the message is audio", async () => { it("attempts auto-TTS when inbound audio gating is on and the message is audio", async () => {
const prevPrefs = process.env.CLAWDBOT_TTS_PREFS; const prevPrefs = process.env.CLAWDBOT_TTS_PREFS;
process.env.CLAWDBOT_TTS_PREFS = `/tmp/tts-test-${Date.now()}.json`; process.env.CLAWDBOT_TTS_PREFS = `/tmp/tts-test-${Date.now()}.json`;
const spy = vi.spyOn(tts, "textToSpeech").mockResolvedValue({ const originalFetch = globalThis.fetch;
success: false, const fetchMock = vi.fn(async () => ({
error: "nope", ok: true,
}); arrayBuffer: async () => new ArrayBuffer(1),
}));
globalThis.fetch = fetchMock as unknown as typeof fetch;
await maybeApplyTtsToPayload({ const result = await maybeApplyTtsToPayload({
payload: { text: "Hello world" }, payload: { text: "Hello world" },
cfg: baseCfg, cfg: baseCfg,
kind: "final", kind: "final",
inboundAudio: true, inboundAudio: true,
}); });
expect(spy).toHaveBeenCalledTimes(1); expect(result.mediaUrl).toBeDefined();
expect(fetchMock).toHaveBeenCalledTimes(1);
spy.mockRestore(); globalThis.fetch = originalFetch;
process.env.CLAWDBOT_TTS_PREFS = prevPrefs; process.env.CLAWDBOT_TTS_PREFS = prevPrefs;
}); });
}); });