On this page

Test with mock()

Test voice code in memory with scripted transcripts and fake audio, with no network and no API keys.

mock() from voiceai-sdk/mock is a provider that runs inside your process. It has the same session API as a real provider, makes no network calls, and needs no key. Use it to test the code around speech: your handlers, your branches on ok, and your cleanup.

Make the provider a parameter

Tests can only swap in mock() if your code lets them. Take the provider as an argument, with the real one as the default.

TypeScript
// agent.ts
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

type Provider = Parameters<typeof createSession>[0];

export function openAgentSession(provider: Provider = { stt: deepgram(), tts: elevenlabs() }) {
  return createSession(provider, { vad: { silenceMs: 800 } });
}

export async function answer(text: string): Promise<string> {
  if (text.toLowerCase().includes("hours")) return "We're open nine to five, Monday to Friday.";
  return "I can help with opening hours. What would you like to know?";
}

answer is a converse handler. In production it would call a model; keep that call behind a parameter too, so tests don't reach the network.

Script what the session hears

UseTo
mock()Get a session with no network and no key
mock.audio(text)Make fake audio bytes that transcribe to text
mock({ transcripts: [...] })Fix what transcribe and listen return, in order, whatever audio you pass
session.speak on a mock sessionGet silent PCM of a plausible length, so duration checks work

mock.audio suits tests that start from audio. transcripts suits tests where your code reads audio from somewhere else, such as a transport, and only the words matter.

TypeScript
import { createSession } from "voiceai-sdk";
import { mock } from "voiceai-sdk/mock";

await using session = await createSession(mock({ transcripts: ["what are your hours", "thanks, bye"] }));

const first = await session.transcribe(new Uint8Array(3200));
const second = await session.transcribe(new Uint8Array(3200));

console.log(first.text, second.text); // "what are your hours" "thanks, bye"

Test a converse handler

Call the handler the way converse would, with the text the session heard, then speak the reply. Pick your test runner:

import { expect, test } from "bun:test";
import { mock } from "voiceai-sdk/mock";
import { answer, openAgentSession } from "./agent";

test("answers a question about opening hours", async () => {
  await using session = await openAgentSession(mock());

  const heard = await session.transcribe(mock.audio("what are your hours"));
  const reply = await answer(heard.text);
  const spoken = await session.speak(reply);

  expect(heard.ok).toBe(true);
  expect(reply).toContain("nine to five");
  expect(spoken.ok).toBe(true);
});

If your handler reads ctx, pass it an object with signal, history and session, using the mock session and new AbortController().signal. Aborting that controller in a test checks how the handler behaves when the caller interrupts.

Assert on silence

Audio with no speech is a result, not an error. transcribe resolves with ok: false and a reason, so test that your code checks ok instead of wrapping the call in try.

TypeScript
import { expect, test } from "bun:test";
import { mock } from "voiceai-sdk/mock";
import { openAgentSession } from "./agent";

test("hears nothing in silence", async () => {
  await using session = await openAgentSession(mock());

  const heard = await session.transcribe(mock.audio(""));

  expect(heard.ok).toBe(false);
  expect(heard.reason).toBe("no_speech");
});

Then test the branch your app takes on ok: false, such as asking the caller to repeat, with the same audio.

Run it in CI

Mock reads no keys and opens no connections, so these tests run on any CI runner without secrets.

bun test
  • Leave provider keys out of the CI environment. A test that uses a real provider by mistake then fails instead of calling the provider.
  • await using closes each mock session when its test ends, so sessions don't leak between tests.
  • Mock only understands audio made with mock.audio. To check real recordings, run them through a real provider outside your unit tests.

Next steps

Esc

Loading…

↑↓ navigate openCtrl J preview