@@ -11,6 +11,7 @@ import {
1111 test ,
1212} from 'matchstick-as/assembly/index' ;
1313import { OrdersMatched } from '../../../generated/Core/IexecInterfaceToken' ;
14+ import { App , Workerpool } from '../../../generated/schema' ;
1415import { handleOrdersMatched } from '../../../src/Modules' ;
1516import { toRLC } from '../../../src/utils' ;
1617import { EventParamBuilder } from '../utils/EventParamBuilder' ;
@@ -52,6 +53,29 @@ describe('IexecPoco', () => {
5253 } ) ;
5354
5455 test ( 'Should handle OrdersMatched' , ( ) => {
56+ // Create app and workerpool entities first (they should exist from registry)
57+ let app = new App ( appAddress . toHex ( ) ) ;
58+ app . owner = appOwner . toHex ( ) ;
59+ app . name = 'TestApp' ;
60+ app . type = 'DOCKER' ;
61+ app . multiaddr = mockBytes32 ( 'multiaddr' ) ;
62+ app . checksum = mockBytes32 ( 'checksum' ) ;
63+ app . mrenclave = mockBytes32 ( 'mrenclave' ) ;
64+ app . timestamp = BigInt . zero ( ) ;
65+ app . usageCount = BigInt . zero ( ) ;
66+ app . lastUsageTimestamp = BigInt . zero ( ) ;
67+ app . save ( ) ;
68+
69+ let workerpool = new Workerpool ( workerpoolAddress . toHex ( ) ) ;
70+ workerpool . owner = workerpoolOwner . toHex ( ) ;
71+ workerpool . description = 'TestWorkerpool' ;
72+ workerpool . workerStakeRatio = BigInt . fromI32 ( 50 ) ;
73+ workerpool . schedulerRewardRatio = BigInt . fromI32 ( 10 ) ;
74+ workerpool . timestamp = BigInt . zero ( ) ;
75+ workerpool . usageCount = BigInt . zero ( ) ;
76+ workerpool . lastUsageTimestamp = BigInt . zero ( ) ;
77+ workerpool . save ( ) ;
78+
5579 mockViewDeal ( pocoAddress , dealId ) . returns ( [ buildDefaultDeal ( ) ] ) ;
5680 // Create the mock event
5781 let mockEvent = newTypedMockEventWithParams < OrdersMatched > (
@@ -115,6 +139,173 @@ describe('IexecPoco', () => {
115139 // Assert that a transaction was logged (if applicable)
116140 const transactionId = mockEvent . transaction . hash . toHex ( ) ;
117141 assert . fieldEquals ( 'Transaction' , transactionId , 'id' , transactionId ) ;
142+
143+ // Assert that app usage statistics were updated
144+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'usageCount' , botSize . toString ( ) ) ;
145+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'lastUsageTimestamp' , timestamp . toString ( ) ) ;
146+
147+ // Assert that workerpool usage statistics were updated
148+ assert . fieldEquals (
149+ 'Workerpool' ,
150+ workerpoolAddress . toHex ( ) ,
151+ 'usageCount' ,
152+ botSize . toString ( ) ,
153+ ) ;
154+ assert . fieldEquals (
155+ 'Workerpool' ,
156+ workerpoolAddress . toHex ( ) ,
157+ 'lastUsageTimestamp' ,
158+ timestamp . toString ( ) ,
159+ ) ;
160+ } ) ;
161+
162+ test ( 'Should accumulate usage counts for multiple deals' , ( ) => {
163+ const dealId1 = mockBytes32 ( 'dealId1' ) ;
164+ const dealId2 = mockBytes32 ( 'dealId2' ) ;
165+ const timestamp1 = BigInt . fromI32 ( 123456789 ) ;
166+ const timestamp2 = BigInt . fromI32 ( 123456999 ) ;
167+ const botSize1 = BigInt . fromI32 ( 5 ) ;
168+ const botSize2 = BigInt . fromI32 ( 3 ) ;
169+
170+ // Create app and workerpool entities first (they should exist from registry)
171+ let app = new App ( appAddress . toHex ( ) ) ;
172+ app . owner = appOwner . toHex ( ) ;
173+ app . name = 'TestApp' ;
174+ app . type = 'DOCKER' ;
175+ app . multiaddr = mockBytes32 ( 'multiaddr' ) ;
176+ app . checksum = mockBytes32 ( 'checksum' ) ;
177+ app . mrenclave = mockBytes32 ( 'mrenclave' ) ;
178+ app . timestamp = BigInt . zero ( ) ;
179+ app . usageCount = BigInt . zero ( ) ;
180+ app . lastUsageTimestamp = BigInt . zero ( ) ;
181+ app . save ( ) ;
182+
183+ let workerpool = new Workerpool ( workerpoolAddress . toHex ( ) ) ;
184+ workerpool . owner = workerpoolOwner . toHex ( ) ;
185+ workerpool . description = 'TestWorkerpool' ;
186+ workerpool . workerStakeRatio = BigInt . fromI32 ( 50 ) ;
187+ workerpool . schedulerRewardRatio = BigInt . fromI32 ( 10 ) ;
188+ workerpool . timestamp = BigInt . zero ( ) ;
189+ workerpool . usageCount = BigInt . zero ( ) ;
190+ workerpool . lastUsageTimestamp = BigInt . zero ( ) ;
191+ workerpool . save ( ) ;
192+
193+ // First deal
194+ mockViewDeal ( pocoAddress , dealId1 ) . returns ( [
195+ buildDeal (
196+ appAddress ,
197+ appOwner ,
198+ appPrice ,
199+ datasetAddress ,
200+ datasetOwner ,
201+ datasetPrice ,
202+ workerpoolAddress ,
203+ workerpoolOwner ,
204+ workerpoolPrice ,
205+ trust ,
206+ category ,
207+ tag ,
208+ requester ,
209+ beneficiary ,
210+ callback ,
211+ params ,
212+ startTime ,
213+ botFirst ,
214+ botSize1 ,
215+ workerStake ,
216+ schedulerRewardRatio ,
217+ sponsor ,
218+ ) ,
219+ ] ) ;
220+
221+ let mockEvent1 = newTypedMockEventWithParams < OrdersMatched > (
222+ EventParamBuilder . init ( )
223+ . bytes ( 'dealid' , dealId1 )
224+ . bytes ( 'appHash' , appHash )
225+ . bytes ( 'datasetHash' , datasetHash )
226+ . bytes ( 'workerpoolHash' , workerpoolHash )
227+ . bytes ( 'requestHash' , requestHash )
228+ . build ( ) ,
229+ ) ;
230+ mockEvent1 . block . timestamp = timestamp1 ;
231+ mockEvent1 . address = pocoAddress ;
232+
233+ handleOrdersMatched ( mockEvent1 ) ;
234+
235+ // Verify first deal usage
236+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'usageCount' , botSize1 . toString ( ) ) ;
237+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'lastUsageTimestamp' , timestamp1 . toString ( ) ) ;
238+ assert . fieldEquals (
239+ 'Workerpool' ,
240+ workerpoolAddress . toHex ( ) ,
241+ 'usageCount' ,
242+ botSize1 . toString ( ) ,
243+ ) ;
244+ assert . fieldEquals (
245+ 'Workerpool' ,
246+ workerpoolAddress . toHex ( ) ,
247+ 'lastUsageTimestamp' ,
248+ timestamp1 . toString ( ) ,
249+ ) ;
250+
251+ // Second deal
252+ mockViewDeal ( pocoAddress , dealId2 ) . returns ( [
253+ buildDeal (
254+ appAddress ,
255+ appOwner ,
256+ appPrice ,
257+ datasetAddress ,
258+ datasetOwner ,
259+ datasetPrice ,
260+ workerpoolAddress ,
261+ workerpoolOwner ,
262+ workerpoolPrice ,
263+ trust ,
264+ category ,
265+ tag ,
266+ requester ,
267+ beneficiary ,
268+ callback ,
269+ params ,
270+ startTime ,
271+ botFirst ,
272+ botSize2 ,
273+ workerStake ,
274+ schedulerRewardRatio ,
275+ sponsor ,
276+ ) ,
277+ ] ) ;
278+
279+ let mockEvent2 = newTypedMockEventWithParams < OrdersMatched > (
280+ EventParamBuilder . init ( )
281+ . bytes ( 'dealid' , dealId2 )
282+ . bytes ( 'appHash' , mockBytes32 ( 'appHash2' ) )
283+ . bytes ( 'datasetHash' , mockBytes32 ( 'datasetHash2' ) )
284+ . bytes ( 'workerpoolHash' , mockBytes32 ( 'workerpoolHash2' ) )
285+ . bytes ( 'requestHash' , mockBytes32 ( 'requestHash2' ) )
286+ . build ( ) ,
287+ ) ;
288+ mockEvent2 . block . timestamp = timestamp2 ;
289+ mockEvent2 . address = pocoAddress ;
290+
291+ handleOrdersMatched ( mockEvent2 ) ;
292+
293+ // Verify accumulated usage
294+ const totalUsage = botSize1 . plus ( botSize2 ) ;
295+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'usageCount' , totalUsage . toString ( ) ) ;
296+ assert . fieldEquals ( 'App' , appAddress . toHex ( ) , 'lastUsageTimestamp' , timestamp2 . toString ( ) ) ;
297+ assert . fieldEquals (
298+ 'Workerpool' ,
299+ workerpoolAddress . toHex ( ) ,
300+ 'usageCount' ,
301+ totalUsage . toString ( ) ,
302+ ) ;
303+ assert . fieldEquals (
304+ 'Workerpool' ,
305+ workerpoolAddress . toHex ( ) ,
306+ 'lastUsageTimestamp' ,
307+ timestamp2 . toString ( ) ,
308+ ) ;
118309 } ) ;
119310
120311 test ( 'Should handle OrdersMatched with bulk_cid when no dataset' , ( ) => {
0 commit comments