@@ -30,41 +30,38 @@ const env =
3030 /** @satisfies {import('../../../src/middleware/withEgressTracker.types.js').Environment } */
3131 ( {
3232 DEBUG : 'true' ,
33- ACCOUNTING_SERVICE_URL : 'http://example.com' ,
3433 FF_EGRESS_TRACKER_ENABLED : 'true'
3534 } )
3635
3736const accountingRecordMethodStub = sinon . stub ( )
3837 . returns (
39- /** @type {import('../../../src/bindings .js').AccountingService['record'] } */
40- async ( cid , bytes , servedAt ) => {
41- console . log ( `[mock] record called with cid : ${ cid } , bytes: ${ bytes } , servedAt: ${ servedAt } ` )
38+ /** @type {import('../../../src/middleware/withAccountingService.types .js').AccountingService['record'] } */
39+ async ( space , resource , bytes , servedAt ) => {
40+ console . log ( `[mock] record called with space : ${ space } , resource: ${ resource } , bytes: ${ bytes } , servedAt: ${ servedAt } ` )
4241 } )
4342
4443/**
4544 * Mock implementation of the AccountingService.
4645 *
47- * @param {Object } options
48- * @param {string } options.serviceURL - The URL of the accounting service.
49- * @returns {import('../../../src/bindings.js').AccountingService }
46+ * @returns {import('../../../src/middleware/withAccountingService.types.js').AccountingService }
5047 */
51- const AccountingService = ( { serviceURL } ) => {
52- console . log ( ` [mock] Accounting.create called with serviceURL: ${ serviceURL } ` )
48+ const AccountingService = ( ) => {
49+ console . log ( ' [mock] Accounting.create called' )
5350
5451 return {
55- record : accountingRecordMethodStub ,
56- getTokenMetadata : sinon . stub ( ) . resolves ( undefined )
52+ record : accountingRecordMethodStub
5753 }
5854}
5955
6056const ctx =
61- /** @satisfies {import('../../../src/middleware/withEgressTracker.js').EgressTrackerContext } */
57+ /** @satisfies {import('../../../src/middleware/withEgressTracker.types. js').Context } */
6258 ( {
59+ space : 'did:key:z6MkknBAHEGCWvBzAi4amdH5FXEXrdKoWF1UJuvc8Psm2Mda' ,
6360 dataCid : CID . parse ( 'bafybeibv7vzycdcnydl5n5lbws6lul2omkm6a6b5wmqt77sicrwnhesy7y' ) ,
6461 waitUntil : sinon . stub ( ) . returns ( undefined ) ,
6562 path : '' ,
6663 searchParams : new URLSearchParams ( ) ,
67- ACCOUNTING_SERVICE : AccountingService ( { serviceURL : env . ACCOUNTING_SERVICE_URL } )
64+ accountingService : AccountingService ( )
6865 } )
6966
7067describe ( 'withEgressTracker' , async ( ) => {
@@ -119,8 +116,9 @@ describe('withEgressTracker', async () => {
119116 expect ( response . status ) . to . equal ( 200 )
120117 expect ( responseBody ) . to . equal ( 'Hello, world!' )
121118 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
122- expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the cid' ) . to . equal ( ctx . dataCid )
123- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
119+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
120+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
121+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( totalBytes )
124122 } ) . timeout ( 10_000 )
125123
126124 it ( 'should record egress for a large file' , async ( ) => {
@@ -142,8 +140,9 @@ describe('withEgressTracker', async () => {
142140
143141 expect ( response . status ) . to . equal ( 200 )
144142 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
145- expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the cid' ) . to . equal ( ctx . dataCid )
146- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
143+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
144+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
145+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( totalBytes )
147146 } )
148147
149148 it ( 'should correctly track egress for responses with chunked transfer encoding' , async ( ) => {
@@ -169,7 +168,9 @@ describe('withEgressTracker', async () => {
169168 expect ( response . status ) . to . equal ( 200 )
170169 expect ( responseBody ) . to . equal ( 'Hello, world!' )
171170 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
172- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
171+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
172+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
173+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( totalBytes )
173174 } )
174175
175176 it ( 'should record egress bytes for a CAR file request' , async ( ) => {
@@ -216,7 +217,9 @@ describe('withEgressTracker', async () => {
216217
217218 // expect(blocks[0].bytes).to.deep.equal(carBytes) - FIXME (fforbeck): how to get the correct byte count?
218219 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
219- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( expectedTotalBytes )
220+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
221+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
222+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( expectedTotalBytes )
220223 } )
221224
222225 it ( 'should correctly track egress for delayed responses' , async ( ) => {
@@ -242,7 +245,9 @@ describe('withEgressTracker', async () => {
242245 expect ( response . status ) . to . equal ( 200 )
243246 expect ( responseBody ) . to . equal ( 'Delayed response content' )
244247 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
245- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
248+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
249+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
250+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( totalBytes )
246251 } ) . timeout ( 5000 )
247252 } )
248253
@@ -330,8 +335,13 @@ describe('withEgressTracker', async () => {
330335 expect ( responseBody2 ) . to . equal ( 'Goodbye, world!' )
331336
332337 expect ( accountingRecordMethodStub . calledTwice , 'record should be called twice' ) . to . be . true
333- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes for first request' ) . to . equal ( totalBytes1 )
334- expect ( accountingRecordMethodStub . args [ 1 ] [ 1 ] , 'second argument should be the total bytes for second request' ) . to . equal ( totalBytes2 )
338+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
339+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid for first request' ) . to . equal ( ctx . dataCid )
340+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes for first request' ) . to . equal ( totalBytes1 )
341+
342+ expect ( accountingRecordMethodStub . args [ 1 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
343+ expect ( accountingRecordMethodStub . args [ 1 ] [ 1 ] , 'second argument should be the cid for second request' ) . to . equal ( ctx . dataCid )
344+ expect ( accountingRecordMethodStub . args [ 1 ] [ 2 ] , 'third argument should be the total bytes for second request' ) . to . equal ( totalBytes2 )
335345 } ) . timeout ( 10_000 )
336346 } )
337347
@@ -356,7 +366,9 @@ describe('withEgressTracker', async () => {
356366 expect ( response . status ) . to . equal ( 200 )
357367 expect ( responseBody ) . to . deep . equal ( { message : 'Hello, JSON!' } )
358368 expect ( accountingRecordMethodStub . calledOnce , 'record should be called once' ) . to . be . true
359- expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
369+ expect ( accountingRecordMethodStub . args [ 0 ] [ 0 ] , 'first argument should be the space' ) . to . equal ( ctx . space )
370+ expect ( accountingRecordMethodStub . args [ 0 ] [ 1 ] , 'second argument should be the cid' ) . to . equal ( ctx . dataCid )
371+ expect ( accountingRecordMethodStub . args [ 0 ] [ 2 ] , 'third argument should be the total bytes' ) . to . equal ( totalBytes )
360372 } ) . timeout ( 10_000 )
361373 } )
362374
@@ -449,7 +461,7 @@ describe('withEgressTracker', async () => {
449461 const request = await createRequest ( )
450462
451463 // Simulate an error in the accounting service record method
452- ctx . ACCOUNTING_SERVICE . record = sinon . stub ( ) . rejects ( new Error ( 'Accounting service error' ) )
464+ ctx . accountingService . record = sinon . stub ( ) . rejects ( new Error ( 'Accounting service error' ) )
453465
454466 const response = await handler ( request , env , ctx )
455467 const responseBody = await response . text ( )
0 commit comments