@@ -75,22 +75,26 @@ const env =
75
75
FF_EGRESS_TRACKER_ENABLED : 'true' ,
76
76
} )
77
77
78
- const recordStub = stub ( 'record' ) . callsFake ( ( cid , bytes , servedAt ) => {
78
+ const recordFake = sinon . fake ( ( cid , bytes , servedAt ) => {
79
79
console . log ( `[mock] record called with cid: ${ cid } , bytes: ${ bytes } , servedAt: ${ servedAt } ` )
80
80
return Promise . resolve ( )
81
81
} )
82
82
83
83
/**
84
- * @type {import('../../../src/bindings.js').AccountingService }
85
- */
86
- // @ts -expect-error - stub is typed as unknown
87
- const accountingService = sinon . stub ( Accounting , 'create' ) . callsFake ( ( { serviceURL } ) => {
84
+ * Mock implementation of the AccountingService.
85
+ *
86
+ * @param {Object } options
87
+ * @param {string } options.serviceURL - The URL of the accounting service.
88
+ * @returns {import('../../../src/bindings.js').AccountingService }
89
+ */
90
+ const AccountingService = ( { serviceURL } ) => {
88
91
console . log ( `[mock] Accounting.create called with serviceURL: ${ serviceURL } ` )
92
+
89
93
return {
90
- record : recordStub ,
91
- getTokenMetadata : stub ( 'getTokenMetadata' ) . returns ( Promise . resolve ( null ) )
94
+ record : recordFake ,
95
+ getTokenMetadata : stub ( 'getTokenMetadata' )
92
96
}
93
- } )
97
+ }
94
98
95
99
const ctx =
96
100
/** @satisfies {import('../../../src/handlers/egress-tracker.js').EgressTrackerContext } */
@@ -99,20 +103,15 @@ const ctx =
99
103
waitUntil : stub ( 'waitUntil' ) . returns ( undefined ) ,
100
104
path : '' ,
101
105
searchParams : new URLSearchParams ( ) ,
102
- ACCOUNTING_SERVICE : accountingService
106
+ ACCOUNTING_SERVICE : AccountingService ( { serviceURL : env . ACCOUNTING_SERVICE_URL } )
103
107
} )
104
108
105
109
describe ( 'withEgressTracker' , async ( ) => {
106
110
107
- beforeEach ( ( ) => {
108
- // @ts -expect-error - stub is typed as AccountingService
109
- accountingService . reset ( )
110
- } )
111
-
112
111
afterEach ( ( ) => {
113
112
sandbox . reset ( )
114
- // @ts -expect-error - stub is typed as AccountingService
115
- accountingService . restore ( )
113
+ // @ts -expect-error - recordFake is a SinonSpy
114
+ recordFake . reset ( )
116
115
} )
117
116
118
117
it ( 'should track egress bytes for a successful request' , async ( ) => {
@@ -125,20 +124,21 @@ describe('withEgressTracker', async () => {
125
124
}
126
125
} ) , { status : 200 } )
127
126
128
- const mockHandler = sinon . stub ( ) . resolves ( mockResponse )
127
+ const innerHandler = sinon . stub ( ) . resolves ( mockResponse )
129
128
130
- const handler = withEgressHandler ( mockHandler )
129
+ const handler = withEgressHandler ( innerHandler )
131
130
const request = await createRequest ( )
132
131
const response = await handler ( request , env , ctx )
133
132
// Ensure the response body is fully consumed
134
133
const responseBody = await response . text ( )
135
134
136
135
expect ( response . status ) . to . equal ( 200 )
137
136
expect ( responseBody ) . to . equal ( 'Hello, world!' )
138
- expect ( recordSpy . calledOnce , 'record should be called once' ) . to . be . true
139
- expect ( recordSpy . args [ 0 ] [ 0 ] , 'first argument should be the cid' ) . to . equal ( ctx . dataCid . toString ( ) )
140
- expect ( recordSpy . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
137
+ expect ( recordFake . calledOnce , 'record should be called once' ) . to . be . true
138
+ expect ( recordFake . args [ 0 ] [ 0 ] , 'first argument should be the cid' ) . to . equal ( ctx . dataCid )
139
+ expect ( recordFake . args [ 0 ] [ 1 ] , 'second argument should be the total bytes' ) . to . equal ( totalBytes )
141
140
142
141
} ) . timeout ( 10_000 )
143
142
143
+
144
144
} )
0 commit comments