@@ -7,8 +7,16 @@ import {
77import { afterAll , beforeAll , describe , expect , it } from 'bun:test'
88
99import type { GrantType } from '@codebuff/common/db/schema'
10+ import type { Logger } from '@codebuff/types/logger'
1011
1112describe ( 'Usage Calculation System' , ( ) => {
13+ const logger : Logger = {
14+ debug : ( ) => { } ,
15+ error : ( ) => { } ,
16+ info : ( ) => { } ,
17+ warn : ( ) => { } ,
18+ }
19+
1220 beforeAll ( ( ) => {
1321 // Mock the database module before importing the function
1422 mockModule ( '@codebuff/common/db' , ( ) => ( {
@@ -22,17 +30,6 @@ describe('Usage Calculation System', () => {
2230 } ) ,
2331 } ,
2432 } ) )
25-
26- // Mock logger
27- mockModule ( '@codebuff/common/util/logger' , ( ) => ( {
28- logger : {
29- debug : ( ) => { } ,
30- error : ( ) => { } ,
31- info : ( ) => { } ,
32- warn : ( ) => { } ,
33- } ,
34- withLoggerContext : async ( context : any , fn : ( ) => Promise < any > ) => fn ( ) ,
35- } ) )
3633 } )
3734
3835 afterAll ( ( ) => {
@@ -76,11 +73,12 @@ describe('Usage Calculation System', () => {
7673 } ,
7774 } ) )
7875
79- const { usageThisCycle } = await calculateUsageAndBalance (
80- 'test-user' ,
81- new Date ( '2024-01-01' ) ,
82- new Date ( '2024-01-15' ) , // Pass current time when grants are active
83- )
76+ const { usageThisCycle } = await calculateUsageAndBalance ( {
77+ userId : 'test-user' ,
78+ quotaResetDate : new Date ( '2024-01-01' ) ,
79+ now : new Date ( '2024-01-15' ) , // Pass current time when grants are active
80+ logger,
81+ } )
8482
8583 expect ( usageThisCycle ) . toBe ( 400 ) // 200 + 200 = 400 total usage
8684 } )
@@ -112,11 +110,12 @@ describe('Usage Calculation System', () => {
112110 } ,
113111 } ) )
114112
115- const { balance, usageThisCycle } = await calculateUsageAndBalance (
116- 'test-user' ,
117- new Date ( '2024-01-01' ) ,
118- new Date ( '2024-01-16' ) , // Current time after expiry
119- )
113+ const { balance, usageThisCycle } = await calculateUsageAndBalance ( {
114+ userId : 'test-user' ,
115+ quotaResetDate : new Date ( '2024-01-01' ) ,
116+ now : new Date ( '2024-01-16' ) , // Current time after expiry
117+ logger,
118+ } )
120119
121120 expect ( balance . totalRemaining ) . toBe ( 0 ) // Expired grant doesn't count
122121 expect ( balance . totalDebt ) . toBe ( 0 )
@@ -158,11 +157,12 @@ describe('Usage Calculation System', () => {
158157 } ,
159158 } ) )
160159
161- const { balance } = await calculateUsageAndBalance (
162- 'test-user' ,
163- new Date ( '2024-01-01' ) ,
164- new Date ( '2024-01-15' ) , // Pass current time when grants are active
165- )
160+ const { balance } = await calculateUsageAndBalance ( {
161+ userId : 'test-user' ,
162+ quotaResetDate : new Date ( '2024-01-01' ) ,
163+ now : new Date ( '2024-01-15' ) , // Pass current time when grants are active
164+ logger,
165+ } )
166166
167167 expect ( balance . totalRemaining ) . toBe ( 0 )
168168 expect ( balance . totalDebt ) . toBe ( 100 )
@@ -213,11 +213,12 @@ describe('Usage Calculation System', () => {
213213 } ,
214214 } ) )
215215
216- const { balance, usageThisCycle } = await calculateUsageAndBalance (
217- 'test-user' ,
218- new Date ( '2024-01-01' ) ,
219- new Date ( '2024-01-15' ) , // Pass current time when grants are active
220- )
216+ const { balance, usageThisCycle } = await calculateUsageAndBalance ( {
217+ userId : 'test-user' ,
218+ quotaResetDate : new Date ( '2024-01-01' ) ,
219+ now : new Date ( '2024-01-15' ) , // Pass current time when grants are active
220+ logger,
221+ } )
221222
222223 // Settlement: 100 positive balance - 50 debt = 50 remaining
223224 expect ( balance . totalRemaining ) . toBe ( 50 )
0 commit comments