Skip to content

Commit bec12a1

Browse files
committed
refactor(billing): replace test-oriented db conn types
1 parent f7ebc77 commit bec12a1

File tree

9 files changed

+535
-431
lines changed

9 files changed

+535
-431
lines changed

common/src/testing/fixtures/billing.ts

Lines changed: 125 additions & 175 deletions
Large diffs are not rendered by default.

common/src/testing/fixtures/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Re-exports all test fixtures for cleaner imports:
55
* @example
66
* ```ts
7-
* import { testLogger, createMockFetch, createGrantCreditsDbMock } from '@codebuff/common/testing/fixtures'
7+
* import { testLogger, createMockFetch, createGrantCreditsStoreMock } from '@codebuff/common/testing/fixtures'
88
* ```
99
*/
1010

@@ -22,21 +22,20 @@ export {
2222

2323
// Billing database mock fixtures
2424
export {
25-
createGrantCreditsDbMock,
26-
createOrgBillingDbMock,
27-
createOrgBillingTransactionMock,
28-
createCreditDelegationDbMock,
25+
createGrantCreditsStoreMock,
26+
createOrgBillingStoreMock,
27+
createCreditDelegationStoreMock,
2928
type GrantCreditsMockOptions,
30-
type GrantCreditsDbConn,
31-
type GrantCreditsTx,
29+
type GrantCreditsStore,
30+
type GrantCreditsTxStore,
3231
type OrgBillingGrant,
3332
type OrgBillingMockOptions,
34-
type OrgBillingDbConn,
35-
type OrgBillingWithTransactionFn,
33+
type OrgBillingStore,
34+
type OrgBillingTxStore,
3635
type UserOrganization,
3736
type OrgRepo,
3837
type CreditDelegationMockOptions,
39-
type CreditDelegationDbConn,
38+
type CreditDelegationStore,
4039
} from './billing'
4140

4241
// Database mock fixtures

packages/billing/src/__tests__/credit-delegation.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, describe, expect, it, mock } from 'bun:test'
22

33
import {
4-
createCreditDelegationDbMock,
4+
createCreditDelegationStoreMock,
55
testLogger,
66
} from '@codebuff/common/testing/fixtures'
77

@@ -19,7 +19,7 @@ describe('Credit Delegation', () => {
1919

2020
describe('findOrganizationForRepository', () => {
2121
it('should find organization for matching repository', async () => {
22-
const mockDb = createCreditDelegationDbMock({
22+
const mockStore = createCreditDelegationStoreMock({
2323
userOrganizations: [
2424
{
2525
orgId: 'org-123',
@@ -43,7 +43,7 @@ describe('Credit Delegation', () => {
4343
userId,
4444
repositoryUrl,
4545
logger,
46-
conn: mockDb,
46+
store: mockStore,
4747
})
4848

4949
expect(result.found).toBe(true)
@@ -52,7 +52,7 @@ describe('Credit Delegation', () => {
5252
})
5353

5454
it('should return not found for non-matching repository', async () => {
55-
const mockDb = createCreditDelegationDbMock({
55+
const mockStore = createCreditDelegationStoreMock({
5656
userOrganizations: [
5757
{
5858
orgId: 'org-123',
@@ -76,14 +76,14 @@ describe('Credit Delegation', () => {
7676
userId,
7777
repositoryUrl,
7878
logger,
79-
conn: mockDb,
79+
store: mockStore,
8080
})
8181

8282
expect(result.found).toBe(false)
8383
})
8484

8585
it('should return not found when user has no organizations', async () => {
86-
const mockDb = createCreditDelegationDbMock({
86+
const mockStore = createCreditDelegationStoreMock({
8787
userOrganizations: [],
8888
orgRepos: [],
8989
})
@@ -95,7 +95,7 @@ describe('Credit Delegation', () => {
9595
userId,
9696
repositoryUrl,
9797
logger,
98-
conn: mockDb,
98+
store: mockStore,
9999
})
100100

101101
expect(result.found).toBe(false)
@@ -104,7 +104,7 @@ describe('Credit Delegation', () => {
104104

105105
describe('consumeCreditsWithDelegation', () => {
106106
it('should fail when no repository URL provided', async () => {
107-
const mockDb = createCreditDelegationDbMock()
107+
const mockStore = createCreditDelegationStoreMock()
108108

109109
const userId = 'user-123'
110110
const repositoryUrl = null
@@ -115,15 +115,15 @@ describe('Credit Delegation', () => {
115115
repositoryUrl,
116116
creditsToConsume,
117117
logger,
118-
conn: mockDb,
118+
store: mockStore,
119119
})
120120

121121
expect(result.success).toBe(false)
122122
expect(result.error).toBe('No repository URL provided')
123123
})
124124

125125
it('should fail when no organization found for repository', async () => {
126-
const mockDb = createCreditDelegationDbMock({
126+
const mockStore = createCreditDelegationStoreMock({
127127
userOrganizations: [],
128128
orgRepos: [],
129129
})
@@ -137,7 +137,7 @@ describe('Credit Delegation', () => {
137137
repositoryUrl,
138138
creditsToConsume,
139139
logger,
140-
conn: mockDb,
140+
store: mockStore,
141141
})
142142

143143
expect(result.success).toBe(false)

packages/billing/src/__tests__/grant-credits.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, describe, expect, it, mock } from 'bun:test'
22

33
import {
4-
createGrantCreditsDbMock,
4+
createGrantCreditsStoreMock,
55
testLogger,
66
} from '@codebuff/common/testing/fixtures'
77

@@ -20,7 +20,7 @@ describe('grant-credits', () => {
2020
describe('triggerMonthlyResetAndGrant', () => {
2121
describe('autoTopupEnabled return value', () => {
2222
it('should return autoTopupEnabled: true when user has auto_topup_enabled: true', async () => {
23-
const mockDb = createGrantCreditsDbMock({
23+
const mockStore = createGrantCreditsStoreMock({
2424
user: {
2525
next_quota_reset: futureDate,
2626
auto_topup_enabled: true,
@@ -30,15 +30,15 @@ describe('grant-credits', () => {
3030
const result = await triggerMonthlyResetAndGrant({
3131
userId: 'user-123',
3232
logger,
33-
conn: mockDb,
33+
store: mockStore,
3434
})
3535

3636
expect(result.autoTopupEnabled).toBe(true)
3737
expect(result.quotaResetDate).toEqual(futureDate)
3838
})
3939

4040
it('should return autoTopupEnabled: false when user has auto_topup_enabled: false', async () => {
41-
const mockDb = createGrantCreditsDbMock({
41+
const mockStore = createGrantCreditsStoreMock({
4242
user: {
4343
next_quota_reset: futureDate,
4444
auto_topup_enabled: false,
@@ -48,14 +48,14 @@ describe('grant-credits', () => {
4848
const result = await triggerMonthlyResetAndGrant({
4949
userId: 'user-123',
5050
logger,
51-
conn: mockDb,
51+
store: mockStore,
5252
})
5353

5454
expect(result.autoTopupEnabled).toBe(false)
5555
})
5656

5757
it('should default autoTopupEnabled to false when user has auto_topup_enabled: null', async () => {
58-
const mockDb = createGrantCreditsDbMock({
58+
const mockStore = createGrantCreditsStoreMock({
5959
user: {
6060
next_quota_reset: futureDate,
6161
auto_topup_enabled: null,
@@ -65,30 +65,30 @@ describe('grant-credits', () => {
6565
const result = await triggerMonthlyResetAndGrant({
6666
userId: 'user-123',
6767
logger,
68-
conn: mockDb,
68+
store: mockStore,
6969
})
7070

7171
expect(result.autoTopupEnabled).toBe(false)
7272
})
7373

7474
it('should throw error when user is not found', async () => {
75-
const mockDb = createGrantCreditsDbMock({
75+
const mockStore = createGrantCreditsStoreMock({
7676
user: null,
7777
})
7878

7979
await expect(
8080
triggerMonthlyResetAndGrant({
8181
userId: 'nonexistent-user',
8282
logger,
83-
conn: mockDb,
83+
store: mockStore,
8484
}),
8585
).rejects.toThrow('User nonexistent-user not found')
8686
})
8787
})
8888

8989
describe('quota reset behavior', () => {
9090
it('should return existing reset date when it is in the future', async () => {
91-
const mockDb = createGrantCreditsDbMock({
91+
const mockStore = createGrantCreditsStoreMock({
9292
user: {
9393
next_quota_reset: futureDate,
9494
auto_topup_enabled: false,
@@ -98,7 +98,7 @@ describe('grant-credits', () => {
9898
const result = await triggerMonthlyResetAndGrant({
9999
userId: 'user-123',
100100
logger,
101-
conn: mockDb,
101+
store: mockStore,
102102
})
103103

104104
expect(result.quotaResetDate).toEqual(futureDate)

packages/billing/src/__tests__/org-billing.test.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { afterEach, describe, expect, it, mock } from 'bun:test'
22

33
import {
4-
createOrgBillingDbMock,
5-
createOrgBillingTransactionMock,
4+
createOrgBillingStoreMock,
65
testLogger,
76
type OrgBillingGrant,
87
} from '@codebuff/common/testing/fixtures'
@@ -20,7 +19,7 @@ const mockGrants: OrgBillingGrant[] = [
2019
{
2120
operation_id: 'org-grant-1',
2221
user_id: '',
23-
organization_id: 'org-123',
22+
org_id: 'org-123',
2423
principal: 1000,
2524
balance: 800,
2625
type: 'organization',
@@ -32,7 +31,7 @@ const mockGrants: OrgBillingGrant[] = [
3231
{
3332
operation_id: 'org-grant-2',
3433
user_id: '',
35-
organization_id: 'org-123',
34+
org_id: 'org-123',
3635
principal: 500,
3736
balance: -100, // Debt
3837
type: 'organization',
@@ -62,7 +61,7 @@ describe('Organization Billing', () => {
6261

6362
describe('calculateOrganizationUsageAndBalance', () => {
6463
it('should calculate balance correctly with positive and negative balances', async () => {
65-
const mockDb = createOrgBillingDbMock({ grants: mockGrants })
64+
const mockStore = createOrgBillingStoreMock({ grants: mockGrants })
6665
const organizationId = 'org-123'
6766
const quotaResetDate = new Date('2024-01-01')
6867
const now = new Date('2024-06-01')
@@ -72,7 +71,7 @@ describe('Organization Billing', () => {
7271
quotaResetDate,
7372
now,
7473
logger,
75-
conn: mockDb,
74+
store: mockStore,
7675
})
7776

7877
// Total positive balance: 800
@@ -88,7 +87,7 @@ describe('Organization Billing', () => {
8887

8988
it('should handle organization with no grants', async () => {
9089
// Mock empty grants
91-
const mockDb = createOrgBillingDbMock({ grants: [] })
90+
const mockStore = createOrgBillingStoreMock({ grants: [] })
9291

9392
const organizationId = 'org-empty'
9493
const quotaResetDate = new Date('2024-01-01')
@@ -99,7 +98,7 @@ describe('Organization Billing', () => {
9998
quotaResetDate,
10099
now,
101100
logger,
102-
conn: mockDb,
101+
store: mockStore,
103102
})
104103

105104
expect(result.balance.totalRemaining).toBe(0)
@@ -178,8 +177,7 @@ describe('Organization Billing', () => {
178177

179178
describe('consumeOrganizationCredits', () => {
180179
it('should consume credits from organization grants', async () => {
181-
const mockDb = createOrgBillingDbMock({ grants: mockGrants })
182-
const mockWithTransaction = createOrgBillingTransactionMock(mockDb)
180+
const mockStore = createOrgBillingStoreMock({ grants: mockGrants })
183181

184182
const organizationId = 'org-123'
185183
const creditsToConsume = 100
@@ -188,7 +186,7 @@ describe('Organization Billing', () => {
188186
organizationId,
189187
creditsToConsume,
190188
logger,
191-
withTransaction: mockWithTransaction,
189+
store: mockStore,
192190
})
193191

194192
expect(result.consumed).toBe(100)
@@ -198,7 +196,7 @@ describe('Organization Billing', () => {
198196

199197
describe('grantOrganizationCredits', () => {
200198
it('should create organization credit grant', async () => {
201-
const mockDb = createOrgBillingDbMock({ grants: mockGrants })
199+
const mockStore = createOrgBillingStoreMock({ grants: mockGrants })
202200

203201
const organizationId = 'org-123'
204202
const userId = 'user-123'
@@ -215,23 +213,18 @@ describe('Organization Billing', () => {
215213
operationId,
216214
description,
217215
logger,
218-
conn: mockDb,
216+
store: mockStore,
219217
}),
220218
).resolves.toBeUndefined()
221219
})
222220

223221
it('should handle duplicate operation IDs gracefully', async () => {
224222
// Mock database constraint error
225-
const mockDb = createOrgBillingDbMock({
223+
const mockStore = createOrgBillingStoreMock({
226224
grants: mockGrants,
227-
insert: () => ({
228-
values: () => {
229-
throw new PgUniqueViolationError(
230-
'Duplicate key',
231-
'credit_ledger_pkey',
232-
)
233-
},
234-
}),
225+
insertCreditLedgerEntry: async () => {
226+
throw new PgUniqueViolationError('Duplicate key', 'credit_ledger_pkey')
227+
},
235228
})
236229

237230
const organizationId = 'org-123'
@@ -249,7 +242,7 @@ describe('Organization Billing', () => {
249242
operationId,
250243
description,
251244
logger,
252-
conn: mockDb,
245+
store: mockStore,
253246
}),
254247
).resolves.toBeUndefined()
255248
})

0 commit comments

Comments
 (0)