Skip to content

Testing voice agents without a microphone

Test a voice agent's replies, silence handling, errors and cleanup in memory with mock(), then check real recordings with local(), with no microphone and no API keys in CI.

You can test most of a voice agent without a microphone, because most of it isn't audio. The handler that turns a transcript into a reply, the branches for silence and errors, and the code that closes sessions all run against mock(), an in-process provider with scripted transcripts and fake audio. Keep a small set of real recordings for the checks that need real speech recognition.

The tests are written against the planned 1.0 API of VoiceAI SDK, a developer preview, and were not run against live providers. The mock() tests need no network and no key. The recording checks near the end need whisper-cli and piper for local().

Test layers at a glance

What you testUseNeeds
What the handler repliesmock({ transcripts }) and converseNothing
Silence and unreadable audiomock.audio(""), which resolves with ok: falseNothing
Provider errorsmock({ fail: "rate_limited" })Nothing
Interrupted turnsYour handler with your own AbortControllerNothing
Real speech recognitionlocal() on recorded fixtureswhisper-cli and piper
A specific cloud providerThe CLI, in a separate scheduled jobThat provider's key

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:

// 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 openSession(provider: Provider = { stt: deepgram(), tts: elevenlabs({ voice: process.env.VOICE_ID }) }) {
  return createSession(provider, { vad: { silenceMs: 800 } });
}

export async function answer(text: string): Promise<string> {
  if (/refund/i.test(text)) return "I can help with a refund. What's the order number?";
  return "I can help with orders and refunds. What do you need?";
}

answer stands in for a model call. In a real agent, inject the model the same way as the provider, so tests never reach the network.

Run a whole conversation in memory

mock({ transcripts }) scripts what the session hears. Each string becomes one final transcript, in order, and converse resolves when the script runs out, so the test ends on its own:

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

test("asks for the order number on a refund request", async () => {
  await using session = await openSession(mock({ transcripts: ["hi there", "I'd like a refund"] }));
  const replies: string[] = [];

  await session.converse(async (text) => {
    const reply = await answer(text);
    replies.push(reply);
    return reply;
  });

  expect(replies).toHaveLength(2);
  expect(replies[1]).toContain("order number");
});

This runs the same converse loop as production, with no transport and no audio device.

Test silence and provider errors

Audio with no speech is a result, not an error, so assert on ok and reason. Provider failures are errors, and mock({ fail }) makes the next call throw the VoiceError code you name:

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

test("silence resolves with ok: false", async () => {
  await using session = await openSession(mock());
  const heard = await session.transcribe(mock.audio(""));

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

test("a rate limit throws a VoiceError", async () => {
  const err = await (async () => {
    await using session = await openSession(mock({ fail: "rate_limited" }));
    await session.transcribe(mock.audio("hello"));
  })().catch((e) => e);

  expect(err).toBeInstanceOf(VoiceError);
  expect(err.code).toBe("rate_limited");
});

The second test wraps the session in a function, so it passes whichever call throws. Your app's branch for each case, such as asking the caller to repeat or retrying after a rate limit, can be tested with the same setup.

Test the interrupted path

A handler that reads ctx can be called directly. Pass it an object with signal, history and session, using a mock session and the signal of an AbortController you own. Abort the controller partway through and check that the handler stops its model call and its lookups instead of finishing the reply. mock() simulates timings, so this tells you whether your handler reacts to the signal, not how quickly a real provider stops speaking.

Check real recordings with local()

Mock doesn't recognize real speech: audio not made with mock.audio comes back as no_speech. For a few end-to-end checks, run recorded fixtures through local(), which runs Whisper on the CI machine with no key. Assert on the words that matter rather than the whole text, since another model can differ in filler words and punctuation:

// recordings.test.ts
import { readFile } from "node:fs/promises";
import { expect, test } from "bun:test";
import { local } from "voiceai-sdk/local";
import { openSession } from "./agent";

test("hears a refund request in a real recording", async () => {
  await using session = await openSession(local({ model: "base.en" }));
  const heard = await session.transcribe(await readFile("fixtures/refund-request.wav"));

  expect(heard.ok).toBe(true);
  expect(heard.text.toLowerCase()).toContain("refund");
});

Run it in CI

# Unit tests: no keys, no binaries
bun test agent.test.ts

# Recording checks: whisper-cli and piper on PATH
npx voiceai-sdk doctor && bun test recordings.test.ts
  • Leave provider keys out of the unit-test environment. A test that reaches a real provider by mistake then fails instead of calling it.
  • await using closes each session when its test ends, so sessions don't leak between tests.
  • Cache the Whisper model directory between runs, and keep recording checks in their own job so unit tests stay quick.

Common questions

Can I test barge-in with mock()? Partly. You can test how your handler reacts when ctx.signal aborts, and mock() emits speech.start, speech.end and transcript in the same order as real providers. Its timings are simulated, so use a real provider to see how quickly speech stops.

Should unit tests call a real provider? No. They would need a key and a network connection, and could fail for reasons unrelated to your code. Run provider checks in a separate job with the CLI, such as npx voiceai-sdk transcribe fixtures/hello.wav --provider deepgram --json.

How do I test what the agent says out loud? Assert on the text your handler returns. On a mock session, speak returns silent PCM about as long as the text would take to say, so you can check ok and durationMs, but not the words.

Does this work with node:test? Yes. The tests use only the session API. Swap bun:test for node:test and node:assert, and run them with npx tsx --test.

Next steps

  • Test with mock(): the guide, with bun:test and node:test versions.
  • Mock: every option and behavior of the mock provider.
  • Local (Whisper): set up whisper.cpp and Piper for recording checks.

More guides on the VoiceAI SDK blog, or start with the quickstart.