import { describe, expect, it } from 'vitest'; import { AudioTurnQuery, check, ContractError, ERROR_CODES, ERROR_CODE_LIST, ErrorResponse, firstError, parse, SCHEMA_VERSION, TextTurnRequest, TurnResult, } from '../src/index.ts'; const ID = '11111111-1111-4111-8111-111111111111'; describe('contract v1', () => { const text = { schema_version: SCHEMA_VERSION, request_id: ID, conversation_id: ID, locale: 'en-US', input: { kind: 'text', text: 'hello' }, }; it('accepts a well-formed text turn and rejects extra or malformed fields', () => { expect(check(TextTurnRequest, text)).toBe(true); expect(check(TextTurnRequest, { ...text, hermes_session_id: 'x' })).toBe(false); expect(check(TextTurnRequest, { ...text, profile: 'someone-else' })).toBe(false); expect(check(TextTurnRequest, { ...text, request_id: 'not-a-uuid' })).toBe(false); expect(check(TextTurnRequest, { ...text, request_id: 'ABCDEF12-1111-4111-8111-111111111111' })).toBe(false); expect(check(TextTurnRequest, { ...text, schema_version: 2 })).toBe(false); expect(check(TextTurnRequest, { ...text, locale: 'en_US; DROP' })).toBe(false); expect(check(TextTurnRequest, { ...text, input: { kind: 'text', text: '' } })).toBe(false); expect(check(TextTurnRequest, { ...text, input: { kind: 'text', text: 'x'.repeat(4001) } })).toBe(false); expect(check(TextTurnRequest, { ...text, input: { kind: 'audio', text: 'x' } })).toBe(false); }); it('audio metadata admits exactly one validated format', () => { const q = { schema_version: '1', request_id: ID, conversation_id: ID, locale: 'en-US', input_kind: 'audio', encoding: 'pcm_s16le', sample_rate: '16000', channels: '1', }; expect(check(AudioTurnQuery, q)).toBe(true); for (const bad of [{ sample_rate: '8000' }, { channels: '2' }, { encoding: 'opus' }, { input_kind: 'text' }, { extra: '1' }]) { expect(check(AudioTurnQuery, { ...q, ...bad })).toBe(false); } }); it('a turn result keeps state, terminal flag and outcome distinct', () => { const result = { schema_version: SCHEMA_VERSION, request_id: ID, conversation_id: ID, state: 'done', terminal: true, outcome: 'unknown', cancel_requested: false, input_text: null, answer_text: null, error: { code: 'outcome_unknown', message: 'Unknown.', retryable: false }, replayed: false, created_at: '2026-01-01T00:00:00.000Z', updated_at: '2026-01-01T00:00:00.000Z', timings: { transcribe_ms: null, agent_ms: 12 }, }; expect(check(TurnResult, result)).toBe(true); expect(check(TurnResult, { ...result, outcome: 'approval_required' })).toBe(false); expect(check(TurnResult, { ...result, state: 'ANSWER' })).toBe(false); expect(check(TurnResult, { ...result, error: { code: 'made_up', message: '', retryable: false } })).toBe(false); expect(firstError(TurnResult, { ...result, state: 'ANSWER' })).toContain('/state'); expect(() => parse(TurnResult, {}, 'turn')).toThrow(ContractError); }); it('error codes are stable, and nothing ambiguous is ever marked retryable', () => { expect(ERROR_CODE_LIST).toContain('outcome_unknown'); for (const code of ['outcome_unknown', 'agent_failed', 'agent_interrupted', 'agent_timeout', 'approval_timeout', 'request_id_conflict'] as const) { expect(ERROR_CODES[code].retryable, code).toBe(false); } for (const code of ['stt_empty', 'stt_timeout', 'interrupted_before_agent', 'agent_unreachable'] as const) { expect(ERROR_CODES[code].retryable, code).toBe(true); } expect(check(ErrorResponse, { schema_version: 1, error: { code: 'rate_limited', message: 'x', retryable: true } })).toBe(true); }); });