22 clearMockedModules ,
33 mockModule ,
44} from '@codebuff/common/testing/mock-modules'
5- import { afterAll , afterEach , beforeAll , describe , expect , it } from 'bun:test'
5+ import { afterAll , beforeAll , describe , expect , it } from 'bun:test'
66
77import {
88 calculateOrganizationUsageAndBalance ,
@@ -49,70 +49,56 @@ const logger: Logger = {
4949 warn : ( ) => { } ,
5050}
5151
52- const createDbMock = ( options ?: {
53- grants ?: typeof mockGrants | any [ ]
54- insert ?: ( ) => { values : ( ) => Promise < unknown > }
55- update ?: ( ) => { set : ( ) => { where : ( ) => Promise < unknown > } }
56- } ) => {
57- const {
58- grants = mockGrants ,
59- insert,
60- update,
61- } = options ?? { }
62-
63- return {
64- select : ( ) => ( {
65- from : ( ) => ( {
66- where : ( ) => ( {
67- orderBy : ( ) => grants ,
52+ describe ( 'Organization Billing' , ( ) => {
53+ beforeAll ( ( ) => {
54+ mockModule ( '@codebuff/common/db' , ( ) => ( {
55+ default : {
56+ select : ( ) => ( {
57+ from : ( ) => ( {
58+ where : ( ) => ( {
59+ orderBy : ( ) => mockGrants ,
60+ } ) ,
61+ } ) ,
6862 } ) ,
69- } ) ,
70- } ) ,
71- insert :
72- insert ??
73- ( ( ) => ( {
74- values : ( ) => Promise . resolve ( ) ,
75- } ) ) ,
76- update :
77- update ??
78- ( ( ) => ( {
79- set : ( ) => ( {
80- where : ( ) => Promise . resolve ( ) ,
63+ insert : ( ) => ( {
64+ values : ( ) => Promise . resolve ( ) ,
8165 } ) ,
82- } ) ) ,
83- }
84- }
85-
86- describe ( 'Organization Billing' , ( ) => {
87- beforeAll ( async ( ) => {
88- await mockModule ( '@codebuff/common/db' , ( ) => ( {
89- default : createDbMock ( ) ,
66+ update : ( ) => ( {
67+ set : ( ) => ( {
68+ where : ( ) => Promise . resolve ( ) ,
69+ } ) ,
70+ } ) ,
71+ } ,
9072 } ) )
9173
92- await mockModule ( '@codebuff/common/db/transaction' , ( ) => ( {
93- withSerializableTransaction : async ( {
94- callback,
95- } : {
96- callback : ( tx : any ) => Promise < unknown > | unknown
97- } ) => await callback ( createDbMock ( ) ) ,
74+ mockModule ( '@codebuff/common/db/transaction' , ( ) => ( {
75+ withSerializableTransaction : ( fn : any ) =>
76+ fn ( {
77+ select : ( ) => ( {
78+ from : ( ) => ( {
79+ where : ( ) => ( {
80+ orderBy : ( ) => mockGrants ,
81+ } ) ,
82+ } ) ,
83+ } ) ,
84+ update : ( ) => ( {
85+ set : ( ) => ( {
86+ where : ( ) => Promise . resolve ( ) ,
87+ } ) ,
88+ } ) ,
89+ } ) ,
9890 } ) )
9991 } )
10092
10193 afterAll ( ( ) => {
10294 clearMockedModules ( )
10395 } )
10496
105- afterEach ( async ( ) => {
106- await mockModule ( '@codebuff/common/db' , ( ) => ( {
107- default : createDbMock ( ) ,
108- } ) )
109- } )
110-
111- describe ( 'calculateOrganizationUsageAndBalance' , ( ) => {
112- it ( 'should calculate balance correctly with positive and negative balances' , async ( ) => {
113- const organizationId = 'org-123'
114- const quotaResetDate = new Date ( '2024-01-01' )
115- const now = new Date ( '2024-06-01' )
97+ describe ( 'calculateOrganizationUsageAndBalance' , ( ) => {
98+ it ( 'should calculate balance correctly with positive and negative balances' , async ( ) => {
99+ const organizationId = 'org-123'
100+ const quotaResetDate = new Date ( '2024-01-01' )
101+ const now = new Date ( '2024-06-01' )
116102
117103 const result = await calculateOrganizationUsageAndBalance ( {
118104 organizationId,
@@ -132,11 +118,19 @@ describe('calculateOrganizationUsageAndBalance', () => {
132118 expect ( result . usageThisCycle ) . toBe ( 800 )
133119 } )
134120
135- it ( 'should handle organization with no grants' , async ( ) => {
136- // Mock empty grants
137- await mockModule ( '@codebuff/common/db' , ( ) => ( {
138- default : createDbMock ( { grants : [ ] } ) ,
139- } ) )
121+ it ( 'should handle organization with no grants' , async ( ) => {
122+ // Mock empty grants
123+ mockModule ( '@codebuff/common/db' , ( ) => ( {
124+ default : {
125+ select : ( ) => ( {
126+ from : ( ) => ( {
127+ where : ( ) => ( {
128+ orderBy : ( ) => [ ] ,
129+ } ) ,
130+ } ) ,
131+ } ) ,
132+ } ,
133+ } ) )
140134
141135 const organizationId = 'org-empty'
142136 const quotaResetDate = new Date ( '2024-01-01' )
@@ -207,7 +201,7 @@ describe('calculateOrganizationUsageAndBalance', () => {
207201 it ( 'should reject malformed URLs' , ( ) => {
208202 const result = validateAndNormalizeRepositoryUrl ( 'not-a-url' )
209203 expect ( result . isValid ) . toBe ( false )
210- expect ( result . error ) . toBe ( 'Repository domain not allowed ' )
204+ expect ( result . error ) . toBe ( 'Invalid URL format ' )
211205 } )
212206
213207 it ( 'should accept allowed domains' , ( ) => {
@@ -261,19 +255,19 @@ describe('calculateOrganizationUsageAndBalance', () => {
261255 } )
262256
263257 it ( 'should handle duplicate operation IDs gracefully' , async ( ) => {
264- // Mock database constraint error
265- await mockModule ( '@codebuff/common/db' , ( ) => ( {
266- default : createDbMock ( {
267- insert : ( ) => ( {
268- values : ( ) => {
269- const error = new Error ( 'Duplicate key' )
270- ; ( error as any ) . code = '23505'
271- ; ( error as any ) . constraint = 'credit_ledger_pkey'
272- throw error
273- } ,
274- } ) ,
275- } ) ,
276- } ) )
258+ // Mock database constraint error
259+ mockModule ( '@codebuff/common/db' , ( ) => ( {
260+ default : {
261+ insert : ( ) => ( {
262+ values : ( ) => {
263+ const error = new Error ( 'Duplicate key' )
264+ ; ( error as any ) . code = '23505'
265+ ; ( error as any ) . constraint = 'credit_ledger_pkey'
266+ throw error
267+ } ,
268+ } ) ,
269+ } ,
270+ } ) )
277271
278272 const organizationId = 'org-123'
279273 const userId = 'user-123'
0 commit comments