@@ -3,6 +3,7 @@ const AgentService = require('../../src/services/agent');
33describe ( 'AgentService' , ( ) => {
44 let agentService ;
55 let mockDb ;
6+ let mockA2AService ;
67
78 beforeEach ( ( ) => {
89 mockDb = {
@@ -11,7 +12,10 @@ describe('AgentService', () => {
1112 findAllAgents : jest . fn ( ) ,
1213 updateAgent : jest . fn ( ) ,
1314 } ;
14- agentService = new AgentService ( mockDb ) ;
15+ mockA2AService = {
16+ executeTransfer : jest . fn ( )
17+ } ;
18+ agentService = new AgentService ( mockDb , { } , mockA2AService ) ;
1519 } ) ;
1620
1721 describe ( 'registerAgent' , ( ) => {
@@ -85,4 +89,67 @@ describe('AgentService', () => {
8589 expect ( result . success ) . toBe ( true ) ;
8690 } ) ;
8791 } ) ;
92+
93+ describe ( 'performA2ATransfer' , ( ) => {
94+ it ( 'should delegate to a2aService if provided' , async ( ) => {
95+ const transferParams = {
96+ fromAgentId : 'agent1' ,
97+ toAgentId : 'agent2' ,
98+ amount : 100 ,
99+ currency : 'USD'
100+ } ;
101+ const expectedResult = { success : true , transferId : 'tx123' } ;
102+ mockA2AService . executeTransfer . mockResolvedValue ( expectedResult ) ;
103+
104+ const result = await agentService . performA2ATransfer ( transferParams ) ;
105+
106+ expect ( mockA2AService . executeTransfer ) . toHaveBeenCalledWith ( {
107+ fromAgentId : 'agent1' ,
108+ toAgentId : 'agent2' ,
109+ amount : 100 ,
110+ ucpPayload : { currency : 'USD' }
111+ } ) ;
112+ expect ( result ) . toEqual ( expectedResult ) ;
113+ } ) ;
114+
115+ it ( 'should fallback to local logic if a2aService is not provided' , async ( ) => {
116+ const localAgentService = new AgentService ( mockDb ) ;
117+ const fromAgent = {
118+ id : 'agent1' ,
119+ config : { limits : { perTransaction : 500 } , authorizedCounterparties : [ 'agent2' ] }
120+ } ;
121+ const toAgent = { id : 'agent2' } ;
122+
123+ mockDb . findAgentById . mockResolvedValueOnce ( fromAgent ) ;
124+ mockDb . findAgentById . mockResolvedValueOnce ( toAgent ) ;
125+
126+ const result = await localAgentService . performA2ATransfer ( {
127+ fromAgentId : 'agent1' ,
128+ toAgentId : 'agent2' ,
129+ amount : 100 ,
130+ currency : 'USD'
131+ } ) ;
132+
133+ expect ( result . success ) . toBe ( true ) ;
134+ expect ( result . amount ) . toBe ( 100 ) ;
135+ } ) ;
136+
137+ it ( 'should throw error in fallback if amount exceeds limit' , async ( ) => {
138+ const localAgentService = new AgentService ( mockDb ) ;
139+ const fromAgent = {
140+ id : 'agent1' ,
141+ config : { limits : { perTransaction : 50 } }
142+ } ;
143+
144+ mockDb . findAgentById . mockResolvedValueOnce ( fromAgent ) ;
145+ mockDb . findAgentById . mockResolvedValueOnce ( { id : 'agent2' } ) ;
146+
147+ await expect ( localAgentService . performA2ATransfer ( {
148+ fromAgentId : 'agent1' ,
149+ toAgentId : 'agent2' ,
150+ amount : 100 ,
151+ currency : 'USD'
152+ } ) ) . rejects . toThrow ( 'Zero Trust Validation Failed: Amount 100 exceeds agent per-transaction limit of 50' ) ;
153+ } ) ;
154+ } ) ;
88155} ) ;
0 commit comments