mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-13 08:15:14 +02:00
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
parseCodexExecEventLine,
|
|
summarizeCodexExecEvents,
|
|
} from '../../../src/context/llm/codex-exec-events.js';
|
|
|
|
describe('Codex exec event parsing', () => {
|
|
it('captures final agent text, usage, steps, and natural completion', () => {
|
|
const summary = summarizeCodexExecEvents(
|
|
[
|
|
{ type: 'thread.started', thread: { id: 'thr_1' } },
|
|
{ type: 'turn.started' },
|
|
{ type: 'item.completed', item: { id: 'item_1', type: 'agent_message', text: 'hello from codex' } },
|
|
{ type: 'turn.completed', usage: { input_tokens: 12, output_tokens: 5, total_tokens: 17 } },
|
|
],
|
|
{ startedAt: 100, now: () => 125 },
|
|
);
|
|
|
|
expect(summary).toEqual({
|
|
finalText: 'hello from codex',
|
|
stopReason: 'natural',
|
|
usage: { inputTokens: 12, outputTokens: 5, totalTokens: 17 },
|
|
stepCount: 1,
|
|
stepBoundariesMs: [25],
|
|
toolCallCount: 0,
|
|
toolFailures: [],
|
|
});
|
|
});
|
|
|
|
it('maps turn failures into error stop reason', () => {
|
|
const summary = summarizeCodexExecEvents([
|
|
{ type: 'turn.started' },
|
|
{ type: 'turn.failed', error: { message: 'Codex could not connect to required MCP server' } },
|
|
]);
|
|
|
|
expect(summary.stopReason).toBe('error');
|
|
expect(summary.error?.message).toContain('Codex could not connect to required MCP server');
|
|
});
|
|
|
|
it('maps max-turns terminal reasons into budget stop reason', () => {
|
|
const summary = summarizeCodexExecEvents([
|
|
{ type: 'turn.started' },
|
|
{ type: 'turn.completed', reason: 'max_turns', usage: { input_tokens: 1, output_tokens: 1 } },
|
|
]);
|
|
|
|
expect(summary.stopReason).toBe('budget');
|
|
});
|
|
|
|
it('counts MCP tool calls and failed MCP tool calls', () => {
|
|
const summary = summarizeCodexExecEvents([
|
|
{ type: 'turn.started' },
|
|
{ type: 'item.started', item: { id: 'call_1', type: 'mcp_tool_call', name: 'search' } },
|
|
{ type: 'item.completed', item: { id: 'call_1', type: 'mcp_tool_call', name: 'search', error: 'denied' } },
|
|
{ type: 'turn.completed' },
|
|
]);
|
|
|
|
expect(summary.toolCallCount).toBe(1);
|
|
expect(summary.toolFailures).toEqual(['search: denied']);
|
|
});
|
|
|
|
it('throws a clear error for malformed JSONL lines', () => {
|
|
expect(() => parseCodexExecEventLine('{not-json')).toThrow('Codex JSONL event stream was malformed');
|
|
});
|
|
});
|