|
| 1 | +import { |
| 2 | + AGENT_COMMAND_PROTOCOL, |
| 3 | + AGENT_COMMAND_PROTOCOL_VERSION, |
| 4 | + agentCommandTypes, |
| 5 | + createAgentCommandFactory, |
| 6 | + getAgentActionKey, |
| 7 | + getAgentCommandActionKey, |
| 8 | + getAgentCommandKey, |
| 9 | + isAgentCommand, |
| 10 | + isAgentCommandEnvelope, |
| 11 | +} from '..'; |
| 12 | + |
| 13 | +describe('agent commands', () => { |
| 14 | + const factory = createAgentCommandFactory({ |
| 15 | + sessionId: 'session-1', |
| 16 | + runId: 'run-1', |
| 17 | + now: () => 100, |
| 18 | + createCommandId: (type) => `command:${type}`, |
| 19 | + createIdempotencyKey: (commandId, type) => `${type}:${commandId}:key`, |
| 20 | + }); |
| 21 | + |
| 22 | + it('creates commands with defaults and explicit envelope options', () => { |
| 23 | + const retry = factory.create('tool.retry', { toolCallId: 'tool-1' }); |
| 24 | + expect(retry).toEqual({ |
| 25 | + commandProtocol: AGENT_COMMAND_PROTOCOL, |
| 26 | + commandProtocolVersion: AGENT_COMMAND_PROTOCOL_VERSION, |
| 27 | + type: 'tool.retry', |
| 28 | + commandId: 'command:tool.retry', |
| 29 | + idempotencyKey: 'tool.retry:command:tool.retry:key', |
| 30 | + sessionId: 'session-1', |
| 31 | + runId: 'run-1', |
| 32 | + timestamp: 100, |
| 33 | + payload: { toolCallId: 'tool-1' }, |
| 34 | + }); |
| 35 | + |
| 36 | + const cancel = factory.create( |
| 37 | + 'run.cancel', |
| 38 | + { reason: 'stop' }, |
| 39 | + { |
| 40 | + commandId: 'custom-command', |
| 41 | + idempotencyKey: 'custom-key', |
| 42 | + timestamp: 200, |
| 43 | + meta: { source: 'test' }, |
| 44 | + }, |
| 45 | + ); |
| 46 | + expect(cancel).toMatchObject({ |
| 47 | + commandId: 'custom-command', |
| 48 | + idempotencyKey: 'custom-key', |
| 49 | + timestamp: 200, |
| 50 | + meta: { source: 'test' }, |
| 51 | + }); |
| 52 | + |
| 53 | + const defaultFactory = createAgentCommandFactory({ sessionId: 's', runId: 'r' }); |
| 54 | + expect(defaultFactory.create('run.cancel', {}).commandId).toContain('r:command:'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('validates every command payload and rejects malformed envelopes', () => { |
| 58 | + const approval = factory.create('approval.resolve', { |
| 59 | + approvalId: 'approval-1', |
| 60 | + decision: 'approved', |
| 61 | + expectedVersion: 1, |
| 62 | + }); |
| 63 | + const modified = factory.create('approval.resolve', { |
| 64 | + approvalId: 'approval-1', |
| 65 | + decision: 'modified', |
| 66 | + expectedVersion: 'v2', |
| 67 | + }); |
| 68 | + const retry = factory.create('tool.retry', { toolCallId: 'tool-1' }); |
| 69 | + const cancel = factory.create('run.cancel', { reason: 'done' }); |
| 70 | + |
| 71 | + expect(agentCommandTypes).toEqual(['approval.resolve', 'tool.retry', 'run.cancel']); |
| 72 | + expect([approval, modified, retry, cancel].every(isAgentCommand)).toBe(true); |
| 73 | + expect(isAgentCommandEnvelope({ ...approval, meta: { trace: true } })).toBe(true); |
| 74 | + |
| 75 | + const invalidEnvelopes = [ |
| 76 | + null, |
| 77 | + [], |
| 78 | + { ...approval, commandProtocol: 'other' }, |
| 79 | + { ...approval, commandProtocolVersion: '9' }, |
| 80 | + { ...approval, type: 'unknown' }, |
| 81 | + { ...approval, commandId: '' }, |
| 82 | + { ...approval, idempotencyKey: '' }, |
| 83 | + { ...approval, sessionId: '' }, |
| 84 | + { ...approval, runId: '' }, |
| 85 | + { ...approval, timestamp: Number.NaN }, |
| 86 | + { ...approval, payload: [] }, |
| 87 | + { ...approval, meta: [] }, |
| 88 | + ]; |
| 89 | + invalidEnvelopes.forEach((command) => { |
| 90 | + expect(isAgentCommandEnvelope(command)).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + expect(isAgentCommand({ ...approval, payload: { ...approval.payload, approvalId: '' } })).toBe( |
| 94 | + false, |
| 95 | + ); |
| 96 | + expect( |
| 97 | + isAgentCommand({ ...approval, payload: { ...approval.payload, decision: 'expired' } }), |
| 98 | + ).toBe(false); |
| 99 | + expect( |
| 100 | + isAgentCommand({ ...approval, payload: { ...approval.payload, expectedVersion: Infinity } }), |
| 101 | + ).toBe(false); |
| 102 | + expect(isAgentCommand({ ...retry, payload: { toolCallId: '' } })).toBe(false); |
| 103 | + expect(isAgentCommand({ ...cancel, payload: { reason: 1 } })).toBe(false); |
| 104 | + expect(isAgentCommand(factory.create('run.cancel', {}))).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it('creates collision-safe command and action keys', () => { |
| 108 | + const approval = factory.create('approval.resolve', { |
| 109 | + approvalId: 'approval-1', |
| 110 | + decision: 'rejected', |
| 111 | + }); |
| 112 | + const retry = factory.create('tool.retry', { toolCallId: 'tool-1' }); |
| 113 | + const cancel = factory.create('run.cancel', {}); |
| 114 | + |
| 115 | + expect(getAgentCommandKey(approval)).toBe(approval.commandId); |
| 116 | + expect(getAgentActionKey({ runId: 'run-1', type: 'run.cancel' })).toBe('5:run-1:run.cancel:'); |
| 117 | + expect(getAgentCommandActionKey(approval)).toBe('5:run-1:approval.resolve:approval-1'); |
| 118 | + expect(getAgentCommandActionKey(retry)).toBe('5:run-1:tool.retry:tool-1'); |
| 119 | + expect(getAgentCommandActionKey(cancel)).toBe('5:run-1:run.cancel:'); |
| 120 | + }); |
| 121 | +}); |
0 commit comments