|
| 1 | +const A2AService = require('../../src/services/a2aService'); |
| 2 | +const MandateService = require('../../src/services/mandate'); |
| 3 | + |
| 4 | +describe('A2AService', () => { |
| 5 | + let a2aService; |
| 6 | + let mockWalletService; |
| 7 | + let mockDb; |
| 8 | + let mandateService; |
| 9 | + let validMandate; |
| 10 | + |
| 11 | + beforeAll(async () => { |
| 12 | + mandateService = new MandateService({ signingKey: 'test-secret' }); |
| 13 | + validMandate = await mandateService.issueIntentMandate({ |
| 14 | + userDid: 'did:key:user', |
| 15 | + agentDid: 'did:key:agent1', |
| 16 | + maxBudget: 1000 |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockWalletService = { |
| 22 | + transfer: jest.fn().mockResolvedValue({ success: true, transferId: 'tx123' }) |
| 23 | + }; |
| 24 | + mockDb = { |
| 25 | + findAgentById: jest.fn() |
| 26 | + }; |
| 27 | + a2aService = new A2AService(mockWalletService, mockDb, { |
| 28 | + mandateConfig: { signingKey: 'test-secret' }, |
| 29 | + strictMandateMode: true |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('executeTransfer', () => { |
| 34 | + it('should throw error if mandate is missing in strict mode', async () => { |
| 35 | + await expect(a2aService.executeTransfer({ |
| 36 | + fromAgentId: 'agent1', |
| 37 | + toAgentId: 'agent2', |
| 38 | + amount: 100 |
| 39 | + })).rejects.toThrow('Zero Trust Validation Failed: Mandate required for A2A transfer in strict mode'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should throw error if mandate is invalid', async () => { |
| 43 | + await expect(a2aService.executeTransfer({ |
| 44 | + fromAgentId: 'agent1', |
| 45 | + toAgentId: 'agent2', |
| 46 | + amount: 100, |
| 47 | + mandate: 'invalid-token' |
| 48 | + })).rejects.toThrow(/Zero Trust Validation Failed: Mandate verification failed/); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should execute transfer when mandate is valid and agents exist', async () => { |
| 52 | + const fromAgent = { |
| 53 | + id: 'agent1', |
| 54 | + name: 'Agent 1', |
| 55 | + status: 'active', |
| 56 | + walletId: 'wallet1', |
| 57 | + config: { limits: { perTransaction: 500 } } |
| 58 | + }; |
| 59 | + const toAgent = { |
| 60 | + id: 'agent2', |
| 61 | + name: 'Agent 2', |
| 62 | + status: 'active', |
| 63 | + walletId: 'wallet2' |
| 64 | + }; |
| 65 | + |
| 66 | + mockDb.findAgentById.mockImplementation((id) => { |
| 67 | + if (id === 'agent1') return fromAgent; |
| 68 | + if (id === 'agent2') return toAgent; |
| 69 | + return null; |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await a2aService.executeTransfer({ |
| 73 | + fromAgentId: 'agent1', |
| 74 | + toAgentId: 'agent2', |
| 75 | + amount: 100, |
| 76 | + mandate: validMandate |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result.success).toBe(true); |
| 80 | + expect(mockWalletService.transfer).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should throw error if agent is not found', async () => { |
| 84 | + mockDb.findAgentById.mockResolvedValue(null); |
| 85 | + |
| 86 | + await expect(a2aService.executeTransfer({ |
| 87 | + fromAgentId: 'agent1', |
| 88 | + toAgentId: 'agent2', |
| 89 | + amount: 100, |
| 90 | + mandate: validMandate |
| 91 | + })).rejects.toThrow('Zero Trust Validation Failed: Sender agent agent1 not found or inactive'); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments