|
| 1 | +/** Tests for the utility agent — provider ordering, fallback chain, and per-harness one-shot command mapping. */ |
| 2 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 3 | +import { runUtilityPrompt, utilityOrder, seedUtilityProbe, resetUtilityProbeCache } from './utility-agent.js' |
| 4 | +import { getHarness } from './harness-registry.js' |
| 5 | + |
| 6 | +function seedAll(overrides: Partial<Record<'claude' | 'opencode' | 'codex', boolean>> = {}) { |
| 7 | + for (const id of ['claude', 'opencode', 'codex'] as const) { |
| 8 | + const usable = overrides[id] ?? true |
| 9 | + seedUtilityProbe(id, { available: usable, version: usable ? 'v' : '', authenticated: usable }) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + resetUtilityProbeCache() |
| 15 | +}) |
| 16 | + |
| 17 | +describe('utilityOrder', () => { |
| 18 | + it('prefers the requested provider, then registry order', () => { |
| 19 | + seedAll() |
| 20 | + expect(utilityOrder('codex').map((h) => h.id)).toEqual(['codex', 'claude', 'opencode']) |
| 21 | + expect(utilityOrder().map((h) => h.id)).toEqual(['claude', 'opencode', 'codex']) |
| 22 | + }) |
| 23 | + |
| 24 | + it('drops harnesses that are missing or unauthenticated', () => { |
| 25 | + seedAll({ claude: false }) |
| 26 | + expect(utilityOrder('claude').map((h) => h.id)).toEqual(['opencode', 'codex']) |
| 27 | + }) |
| 28 | +}) |
| 29 | + |
| 30 | +describe('runUtilityPrompt', () => { |
| 31 | + it('uses the preferred harness when it succeeds', async () => { |
| 32 | + seedAll() |
| 33 | + const result = await runUtilityPrompt( |
| 34 | + { prompt: 'name this', prefer: 'opencode', timeoutMs: 1000 }, |
| 35 | + async (cmd) => `${cmd.binary} said hi`, |
| 36 | + ) |
| 37 | + expect(result.provider).toBe('opencode') |
| 38 | + }) |
| 39 | + |
| 40 | + it('falls back down the chain and reports every failure when all fail', async () => { |
| 41 | + seedAll() |
| 42 | + const tried: string[] = [] |
| 43 | + await expect( |
| 44 | + runUtilityPrompt({ prompt: 'p', timeoutMs: 1000 }, async (cmd) => { |
| 45 | + tried.push(cmd.binary) |
| 46 | + throw new Error('nope') |
| 47 | + }), |
| 48 | + ).rejects.toThrow(/claude.*opencode.*codex/s) |
| 49 | + expect(tried).toHaveLength(3) |
| 50 | + }) |
| 51 | + |
| 52 | + it('a mid-chain success stops the fallback', async () => { |
| 53 | + seedAll({ claude: false }) |
| 54 | + const result = await runUtilityPrompt({ prompt: 'p', timeoutMs: 1000 }, async (cmd) => |
| 55 | + cmd.args[0] === 'run' ? 'from opencode' : Promise.reject(new Error('x')), |
| 56 | + ) |
| 57 | + expect(result).toEqual({ text: 'from opencode', provider: 'opencode' }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('throws immediately when no harness is usable', async () => { |
| 61 | + seedAll({ claude: false, opencode: false, codex: false }) |
| 62 | + await expect(runUtilityPrompt({ prompt: 'p', timeoutMs: 1000 })).rejects.toThrow(/No usable coding agent/) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('oneShotCommand mapping', () => { |
| 67 | + const opts = { prompt: 'the prompt', systemPrompt: 'be terse', fast: true } |
| 68 | + |
| 69 | + it('claude: -p one-shot with system prompt, fast model, prompt on stdin', () => { |
| 70 | + const cmd = getHarness('claude').oneShotCommand(opts) |
| 71 | + expect(cmd.args).toEqual(['-p', '--max-turns', '1', '--tools', '', '--system-prompt', 'be terse', '--model', 'haiku']) |
| 72 | + expect(cmd.stdin).toBe('the prompt') |
| 73 | + }) |
| 74 | + |
| 75 | + it('codex: exec with the system prompt leading the piped text', () => { |
| 76 | + const cmd = getHarness('codex').oneShotCommand(opts) |
| 77 | + expect(cmd.args).toEqual(['exec']) |
| 78 | + expect(cmd.stdin).toBe('be terse\n\nthe prompt') |
| 79 | + }) |
| 80 | + |
| 81 | + it('opencode: run --pure with the combined message as the argument', () => { |
| 82 | + const cmd = getHarness('opencode').oneShotCommand(opts) |
| 83 | + expect(cmd.args).toEqual(['run', '--pure', 'be terse\n\nthe prompt']) |
| 84 | + expect(cmd.stdin).toBe('') |
| 85 | + }) |
| 86 | +}) |
0 commit comments