import AgentFeedback from './commands/agent-feedback'; import { readConfig } from './lib/config'; jest.mock('./lib/config', () => ({ ...jest.requireActual('./lib/config'), readConfig: jest.fn() })); const initialExitCode = process.exitCode; beforeEach(() => { jest .mocked(readConfig) .mockReturnValue({ apiKey: 'lim_test', apiEndpoint: 'https://example.test', consoleEndpoint: '' }); }); afterEach(() => { process.exitCode = initialExitCode; jest.restoreAllMocks(); }); const report = [ 'lim ios create failed twice with HTTP 500.', '--task', 'Create an iOS simulator', '--kind', 'broken', '--impact', 'blocked', '--tool', 'lim ios create', ]; test('an agent report reaches its organization and prints the report ID', async () => { const fetch = jest .spyOn(globalThis, 'fetch') .mockResolvedValueOnce(Response.json({ organization: { id: 'org_test' } })) .mockResolvedValueOnce(Response.json({ id: 'agentfeedback_test' })); const output = jest.spyOn(process.stdout, 'write').mockReturnValue(true); const log = jest.spyOn(console, 'log').mockImplementation(() => {}); await AgentFeedback.run([...report, '--json']); expect(String(fetch.mock.calls[1]?.[0])).toBe( 'https://example.test/v1/organizations/org_test/agent_feedback', ); expect(JSON.parse(String(fetch.mock.calls[1]?.[1]?.body))).toEqual({ task: 'Create an iOS simulator', kind: 'broken', impact: 'blocked', description: 'lim ios create failed twice with HTTP 500.', tool: 'lim ios create', client: 'lim', }); expect([...output.mock.calls, ...log.mock.calls].flat().join('')).toContain('agentfeedback_test'); }); test('a refused report tells the agent to carry on instead of retrying', async () => { jest .spyOn(globalThis, 'fetch') .mockResolvedValueOnce(Response.json({ organization: { id: 'org_test' } })) .mockResolvedValueOnce( Response.json({ message: 'description is a placeholder; describe what was tried' }, { status: 400 }), ); await expect(AgentFeedback.run(report)).rejects.toThrow( /Not recorded: .*description is a placeholder.*Continue the task; do not retry\./s, ); });