-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathusage-calculation.test.ts
More file actions
249 lines (226 loc) · 6.73 KB
/
usage-calculation.test.ts
File metadata and controls
249 lines (226 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { calculateUsageAndBalance } from '@codebuff/billing'
import { GRANT_PRIORITIES } from '@codebuff/common/constants/grant-priorities'
import {
mockModule,
clearMockedModules,
} from '@codebuff/common/testing/mock-modules'
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
import type { Logger } from '@codebuff/common/types/contracts/logger'
import type { GrantType } from '@codebuff/internal/db/schema'
describe('Usage Calculation System', () => {
const logger: Logger = {
debug: () => {},
error: () => {},
info: () => {},
warn: () => {},
}
beforeAll(async () => {
// Mock the database module before importing the function
await mockModule('@codebuff/internal/db', () => ({
default: {
select: () => ({
from: () => ({
where: () => ({
orderBy: () => Promise.resolve([]),
}),
}),
}),
},
}))
})
afterAll(() => {
clearMockedModules()
})
it('should calculate usage this cycle correctly', async () => {
const mockGrants = [
{
operation_id: 'test-1',
user_id: 'test-user',
type: 'free' as GrantType,
principal: 500, // Used 200 (500 - 300)
balance: 300,
created_at: new Date('2024-01-01'),
expires_at: new Date('2024-02-01'),
priority: GRANT_PRIORITIES.free,
},
{
operation_id: 'test-2',
user_id: 'test-user',
type: 'purchase' as GrantType,
principal: 1000, // Used 200 (1000 - 800)
balance: 800,
created_at: new Date('2024-01-15'),
expires_at: null,
priority: GRANT_PRIORITIES.purchase,
},
]
// Mock the database module with the test data
await mockModule('@codebuff/internal/db', () => ({
default: {
select: () => ({
from: () => ({
where: () => ({
orderBy: () => Promise.resolve(mockGrants),
}),
}),
}),
},
}))
const { usageThisCycle } = await calculateUsageAndBalance({
userId: 'test-user',
quotaResetDate: new Date('2024-01-01'),
now: new Date('2024-01-15'), // Pass current time when grants are active
logger,
})
expect(usageThisCycle).toBe(400) // 200 + 200 = 400 total usage
})
it('should handle expired grants', async () => {
const mockGrants = [
{
operation_id: 'test-1',
user_id: 'test-user',
type: 'free' as GrantType,
principal: 500,
balance: 300,
created_at: new Date('2024-01-01'),
expires_at: new Date('2024-01-15'), // Already expired
priority: GRANT_PRIORITIES.free,
},
]
// Mock the database module with the test data
await mockModule('@codebuff/internal/db', () => ({
default: {
select: () => ({
from: () => ({
where: () => ({
orderBy: () => Promise.resolve(mockGrants),
}),
}),
}),
},
}))
const { balance, usageThisCycle } = await calculateUsageAndBalance({
userId: 'test-user',
quotaResetDate: new Date('2024-01-01'),
now: new Date('2024-01-16'), // Current time after expiry
logger,
})
expect(balance.totalRemaining).toBe(0) // Expired grant doesn't count
expect(balance.totalDebt).toBe(0)
expect(balance.netBalance).toBe(0)
expect(balance.breakdown).toEqual({
free: 0,
purchase: 0,
referral: 0,
admin: 0,
organization: 0,
})
expect(usageThisCycle).toBe(200) // 500 - 300 = 200 used
})
it('should handle grants with debt', async () => {
const mockGrants = [
{
operation_id: 'test-1',
user_id: 'test-user',
type: 'free' as GrantType,
principal: 500,
balance: -100, // In debt
created_at: new Date('2024-01-01'),
expires_at: new Date('2024-02-01'),
priority: GRANT_PRIORITIES.free,
},
]
// Mock the database module with the test data
await mockModule('@codebuff/internal/db', () => ({
default: {
select: () => ({
from: () => ({
where: () => ({
orderBy: () => Promise.resolve(mockGrants),
}),
}),
}),
},
}))
const { balance } = await calculateUsageAndBalance({
userId: 'test-user',
quotaResetDate: new Date('2024-01-01'),
now: new Date('2024-01-15'), // Pass current time when grants are active
logger,
})
expect(balance.totalRemaining).toBe(0)
expect(balance.totalDebt).toBe(100)
expect(balance.netBalance).toBe(-100)
expect(balance.breakdown).toEqual({
free: 0,
purchase: 0,
referral: 0,
admin: 0,
organization: 0,
}) // No positive balances
})
it('should handle in-memory settlement between positive balance and debt', async () => {
const mockGrants = [
{
operation_id: 'test-1',
user_id: 'test-user',
type: 'free' as GrantType,
principal: 200,
balance: 100, // Positive balance
created_at: new Date('2024-01-01'),
expires_at: new Date('2024-02-01'),
priority: GRANT_PRIORITIES.free,
},
{
operation_id: 'test-2',
user_id: 'test-user',
type: 'purchase' as GrantType,
principal: 100,
balance: -50, // Debt
created_at: new Date('2024-01-15'),
expires_at: null,
priority: GRANT_PRIORITIES.purchase,
},
]
// Mock the database module with the test data
await mockModule('@codebuff/internal/db', () => ({
default: {
select: () => ({
from: () => ({
where: () => ({
orderBy: () => Promise.resolve(mockGrants),
}),
}),
}),
},
}))
const { balance, usageThisCycle } = await calculateUsageAndBalance({
userId: 'test-user',
quotaResetDate: new Date('2024-01-01'),
now: new Date('2024-01-15'), // Pass current time when grants are active
logger,
})
// Settlement: 100 positive balance - 50 debt = 50 remaining
expect(balance.totalRemaining).toBe(50)
expect(balance.totalDebt).toBe(0)
expect(balance.netBalance).toBe(50)
// Breakdown shows positive balances before settlement
expect(balance.breakdown).toEqual({
free: 100,
purchase: 0, // No positive balance for purchase grant
referral: 0,
admin: 0,
organization: 0,
})
// Principals show original grant amounts
expect(balance.principals).toEqual({
free: 200,
purchase: 100,
referral: 0,
admin: 0,
organization: 0,
})
// Usage calculation: (200-100) + (100-(-50)) = 100 + 150 = 250
expect(usageThisCycle).toBe(250)
})
})