Skip to content

Commit 96551c1

Browse files
committed
fix unit test
1 parent 3ba7a73 commit 96551c1

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/bindings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export interface AccountingService {
1414
}
1515

1616
export interface Accounting {
17-
create: ({ serviceURL }: { serviceURL?: string }) => AccountingService
17+
create: ({ serviceURL }: { serviceURL: string }) => AccountingService
1818
}
1919

test/unit/middlewares/egress-tracker.spec.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,26 @@ const env =
7575
FF_EGRESS_TRACKER_ENABLED: 'true',
7676
})
7777

78-
const recordStub = stub('record').callsFake((cid, bytes, servedAt) => {
78+
const recordFake = sinon.fake((cid, bytes, servedAt) => {
7979
console.log(`[mock] record called with cid: ${cid}, bytes: ${bytes}, servedAt: ${servedAt}`)
8080
return Promise.resolve()
8181
})
8282

8383
/**
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 }) => {
8891
console.log(`[mock] Accounting.create called with serviceURL: ${serviceURL}`)
92+
8993
return {
90-
record: recordStub,
91-
getTokenMetadata: stub('getTokenMetadata').returns(Promise.resolve(null))
94+
record: recordFake,
95+
getTokenMetadata: stub('getTokenMetadata')
9296
}
93-
})
97+
}
9498

9599
const ctx =
96100
/** @satisfies {import('../../../src/handlers/egress-tracker.js').EgressTrackerContext} */
@@ -99,20 +103,15 @@ const ctx =
99103
waitUntil: stub('waitUntil').returns(undefined),
100104
path: '',
101105
searchParams: new URLSearchParams(),
102-
ACCOUNTING_SERVICE: accountingService
106+
ACCOUNTING_SERVICE: AccountingService({ serviceURL: env.ACCOUNTING_SERVICE_URL })
103107
})
104108

105109
describe('withEgressTracker', async () => {
106110

107-
beforeEach(() => {
108-
// @ts-expect-error - stub is typed as AccountingService
109-
accountingService.reset()
110-
})
111-
112111
afterEach(() => {
113112
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()
116115
})
117116

118117
it('should track egress bytes for a successful request', async () => {
@@ -125,20 +124,21 @@ describe('withEgressTracker', async () => {
125124
}
126125
}), { status: 200 })
127126

128-
const mockHandler = sinon.stub().resolves(mockResponse)
127+
const innerHandler = sinon.stub().resolves(mockResponse)
129128

130-
const handler = withEgressHandler(mockHandler)
129+
const handler = withEgressHandler(innerHandler)
131130
const request = await createRequest()
132131
const response = await handler(request, env, ctx)
133132
// Ensure the response body is fully consumed
134133
const responseBody = await response.text()
135134

136135
expect(response.status).to.equal(200)
137136
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)
141140

142141
}).timeout(10_000)
143142

143+
144144
})

0 commit comments

Comments
 (0)