import { afterEach, describe, expect, it, vi } from 'vitest'; import { assertDefined, type AgentCliReport, type ModelDescriptor, type ProviderDef } from '@moxxy/sdk'; import { resolveTurnModel } from './turn-model.js'; import { AgentCliWatch } from './agent-cli-watch.js'; import { silentLogger } from './logger.js'; import { Session } from './session.js'; /** * Where each agent connection's CLI stands is one state the session keeps: * checked when the host starts and every hour after, read by every surface * through the session's info. */ const report = (state: AgentCliReport['state'], installed: string | null, checkedAt = 1): AgentCliReport => ({ state, command: 'codex', installed, latest: '0.162.1', minimum: '0.155.1', checkedAt, }); function agent(name: string, check: () => Promise): ProviderDef { return { name, models: [], createClient: () => ({}) as never, agentCli: { check } }; } afterEach(() => { vi.useRealTimers(); }); describe('AgentCliWatch', () => { it('ends the live sessions of one agent before a sign-out, and leaves the rest alone', async () => { const closed: string[] = []; const withSessions = (name: string): ProviderDef => ({ ...agent(name, async () => report('current', '0.162.1')), agentCli: { check: async () => report('current', '0.162.1'), closeSessions: async () => void closed.push(name) }, }); const plain: ProviderDef = { name: 'anthropic', models: [], createClient: () => ({}) as never }; const watch = new AgentCliWatch(() => [withSessions('codex-agent'), withSessions('claude-code'), plain], silentLogger); await watch.closeSessions('codex-agent'); await watch.closeSessions('anthropic'); await watch.closeSessions('nobody'); expect(closed).toEqual(['codex-agent']); }); it('keeps the last report of every connection that has a CLI', async () => { const plain: ProviderDef = { name: 'anthropic', models: [], createClient: () => ({}) as never }; const watch = new AgentCliWatch(() => [agent('codex-agent', async () => report('update-available', '0.155.1')), plain], silentLogger); await watch.check(); expect(watch.report('codex-agent')).toMatchObject({ state: 'update-available', installed: '0.155.1' }); expect(watch.report('anthropic')).toBeUndefined(); }); it('says when a CLI changed, not when it was merely checked again', async () => { let next = report('update-available', '0.155.1', 1); const watch = new AgentCliWatch(() => [agent('codex-agent', async () => next)], silentLogger); const changes = vi.fn(); watch.onChange(changes); await watch.check(); next = report('update-available', '0.155.1', 2); await watch.check(); expect(changes).toHaveBeenCalledTimes(1); next = report('current', '0.162.1', 3); await watch.check(); expect(changes).toHaveBeenCalledTimes(2); }); it('says when the agent was signed in or out', async () => { let next: AgentCliReport = { ...report('current', '0.162.1'), signedIn: false, account: null }; const watch = new AgentCliWatch(() => [agent('codex-agent', async () => next)], silentLogger); const changes = vi.fn(); watch.onChange(changes); await watch.check(); next = { ...next, signedIn: true, account: 'ChatGPT' }; await watch.check(); expect(changes).toHaveBeenCalledTimes(2); }); it('says when Moxxy learns of a known problem with a version, or it is cleared', async () => { let next: AgentCliReport = report('current', '0.166.0'); const watch = new AgentCliWatch(() => [agent('codex-agent', async () => next)], silentLogger); const changes = vi.fn(); watch.onChange(changes); await watch.check(); next = { ...report('current', '0.166.0', 2), knownIssue: { version: '0.166.0', reason: 'Turns hang' } }; await watch.check(); expect(changes).toHaveBeenCalledTimes(2); next = { ...report('current', '0.166.0', 3), knownIssue: { version: '0.166.0', reason: 'Turns hang' } }; await watch.check(); expect(changes).toHaveBeenCalledTimes(2); next = report('current', '0.166.0', 4); await watch.check(); expect(changes).toHaveBeenCalledTimes(3); }); it('keeps checking the others when one check fails', async () => { const watch = new AgentCliWatch( () => [ agent('claude-code', async () => { throw new Error('spawn failed'); }), agent('opencode', async () => report('current', '1.19.0')), ], silentLogger, ); await watch.check(); expect(watch.report('claude-code')).toBeUndefined(); expect(watch.report('opencode')).toMatchObject({ state: 'current' }); }); it('asks the registry again only when told to', async () => { const seen: Array = []; const watch = new AgentCliWatch( () => [ agent('codex-agent', async () => report('current', '0.162.1')), { ...agent('opencode', async () => report('current', '1.19.0')), agentCli: { check: async (opts) => { seen.push(opts?.refresh); return report('current', '1.19.0'); }, }, }, ], silentLogger, ); await watch.check(); await watch.check({ refresh: true }); expect(seen).toEqual([false, true]); }); it('checks when started and every hour after, until stopped', async () => { vi.useFakeTimers(); const check = vi.fn(async () => report('current', '0.162.1')); const watch = new AgentCliWatch(() => [agent('codex-agent', check)], silentLogger); watch.start(); await vi.advanceTimersByTimeAsync(0); expect(check).toHaveBeenCalledTimes(1); await vi.advanceTimersByTimeAsync(60 * 60_000); expect(check).toHaveBeenCalledTimes(2); watch.stop(); await vi.advanceTimersByTimeAsync(2 * 60 * 60_000); expect(check).toHaveBeenCalledTimes(2); }); }); const model = (id: string): ModelDescriptor => ({ id, contextWindow: 200_000, maxOutputTokens: 64_000, supportsTools: true }); const signedIn = (): AgentCliReport => ({ ...report('current', '0.163.0'), signedIn: true }); function withModels(name: string, check: () => Promise, models: () => Promise>): ProviderDef { return { name, models: [model('built-in')], createClient: () => ({}) as never, agentCli: { check, models } }; } describe('AgentCliWatch — the models an agent offers', () => { it('asks a signed-in agent for the models it offers now', async () => { const watch = new AgentCliWatch( () => [withModels('codex-agent', async () => signedIn(), async () => [model('gpt-6.1-sol'), model('gpt-6-astra')])], silentLogger, ); await watch.check(); expect(watch.models('codex-agent')?.map((m) => m.id)).toEqual(['gpt-6.1-sol', 'gpt-6-astra']); }); it('does not ask an agent that is missing or signed out', async () => { const models = vi.fn(async () => [model('x')]); const watch = new AgentCliWatch( () => [ withModels('codex-agent', async () => report('missing', null), models), withModels('claude-code', async () => ({ ...report('current', '2.1.0'), signedIn: false }), models), ], silentLogger, ); await watch.check(); expect(models).not.toHaveBeenCalled(); expect(watch.models('codex-agent')).toBeUndefined(); }); it('keeps the last list when the agent cannot answer, and the report still lands', async () => { let fail = false; const watch = new AgentCliWatch( () => [ withModels('codex-agent', async () => signedIn(), async () => { if (fail) throw new Error('app-server crashed'); return [model('gpt-6.1-sol')]; }), ], silentLogger, ); await watch.check(); fail = true; await watch.check(); expect(watch.models('codex-agent')?.map((m) => m.id)).toEqual(['gpt-6.1-sol']); expect(watch.report('codex-agent')).toMatchObject({ state: 'current' }); }); it('ignores an empty answer rather than offering no models', async () => { const watch = new AgentCliWatch(() => [withModels('codex-agent', async () => signedIn(), async () => [])], silentLogger); await watch.check(); expect(watch.models('codex-agent')).toBeUndefined(); }); it('says when the offered models changed, not when they were listed again', async () => { let ids = ['gpt-6-astra']; const watch = new AgentCliWatch( () => [withModels('codex-agent', async () => signedIn(), async () => ids.map(model))], silentLogger, ); const changes = vi.fn(); watch.onChange(changes); await watch.check(); await watch.check(); expect(changes).toHaveBeenCalledTimes(1); ids = ['gpt-6.1-sol', 'gpt-6-astra']; await watch.check(); expect(changes).toHaveBeenCalledTimes(2); }); }); describe("the session's info", () => { it("offers the models the agent listed in place of the connection's built-in ones", async () => { const session = new Session({ cwd: '/tmp', silent: true }); session.providers.register(withModels('codex-agent', async () => signedIn(), async () => [model('gpt-6.1-sol')])); expect(session.getInfo().providers.find((p) => p.name === 'codex-agent')?.models.map((m) => m.id)).toEqual(['built-in']); await session.agentClis.check(); expect(session.getInfo().providers.find((p) => p.name === 'codex-agent')?.models.map((m) => m.id)).toEqual(['gpt-6.1-sol']); await session.close(); }); it('keeps running the model the conversation chose when the agent stops listing it', async () => { const session = new Session({ cwd: '/tmp', silent: true }); session.providers.register(withModels('codex-agent', async () => signedIn(), async () => [model('gpt-6.1-sol')])); session.lastResolvedProvider = 'codex-agent'; session.lastResolvedModel = 'gpt-6-retired'; await session.agentClis.check(); const listed = session.getInfo().providers.find((p) => p.name === 'codex-agent'); assertDefined(listed, 'codex-agent in the info'); expect(resolveTurnModel(session, listed)).toBe('gpt-6-retired'); await session.close(); }); it("carries each connection's CLI report", async () => { const session = new Session({ cwd: '/tmp', silent: true }); session.providers.register(agent('codex-agent', async () => report('update-required', '0.150.0'))); await session.agentClis.check(); expect(session.getInfo().providers.find((p) => p.name === 'codex-agent')?.agentCli).toMatchObject({ state: 'update-required', installed: '0.150.0', }); await session.close(); }); it('tells its listeners when a CLI changed, with the new info', async () => { const session = new Session({ cwd: '/tmp', silent: true }); let next = report('update-available', '0.155.1'); session.providers.register(agent('codex-agent', async () => next)); const seen: Array = []; const off = session.onInfoChanged((info) => seen.push(info.providers.find((p) => p.name === 'codex-agent')?.agentCli?.state)); await session.agentClis.check(); off(); next = report('current', '0.162.1'); await session.agentClis.check({ refresh: true }); expect(seen).toEqual(['update-available']); await session.close(); }); });