Skip to content

Level 1: a unit test

Use a unit test when code around a model call (parsing, retries, fallbacks, caching, error handling) must handle known replies without a network call.

import { expect, test } from 'vitest';
import { triageTicket } from '../src/triage.ts';
import { fakeOpenAI } from './fake-openai.ts';
test('reads a strict reply', async () => {
const client = fakeOpenAI(() => ({
content: '{"category":"billing","priority":2,"summary":"Double charge"}',
}));
const result = await triageTicket('charged twice', { client, model: 'm' });
expect(result.triage.category).toBe('billing');
});
  • Give the app’s real SDK client a fake fetch. Test the request, returned value and thrown error; do not assert on live model text.
  • Block the network so an accidental call fails at once. Keep a strict parser in production and test its bad replies.

examples/support-triage/test has the fake client and network guard.