Skip to content

Commit 1fe4433

Browse files
committed
fix mocks in unit tests
1 parent afbe2da commit 1fe4433

File tree

7 files changed

+32
-84
lines changed

7 files changed

+32
-84
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ jobs:
8282
needs: [build-and-check]
8383
strategy:
8484
matrix:
85-
package:
86-
[.agents, backend, cli, common, npm-app, packages/agent-runtime, web]
85+
package: [.agents, backend, cli, common, npm-app, packages, web]
8786
include:
8887
- package: .agents
8988
- package: backend
9089
- package: cli
9190
- package: common
9291
- package: npm-app
93-
- package: packages/agent-runtime
92+
- package: packages
9493
- package: web
9594
name: test-${{ matrix.package }}
9695
runs-on: ubuntu-latest
@@ -156,15 +155,22 @@ jobs:
156155
needs: [build-and-check]
157156
strategy:
158157
matrix:
159-
package:
160-
[.agents, backend, cli, common, npm-app, packages/agent-runtime, web]
158+
package: [
159+
.agents,
160+
backend,
161+
cli,
162+
common,
163+
npm-app,
164+
packages
165+
web,
166+
]
161167
include:
162168
- package: .agents
163169
- package: backend
164170
- package: cli
165171
- package: common
166172
- package: npm-app
167-
- package: packages/agent-runtime
173+
- package: packages
168174
- package: web
169175
name: test-integration-${{ matrix.package }}
170176
runs-on: ubuntu-latest

backend/src/__tests__/auto-topup.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('Auto Top-up System', () => {
4545
)
4646

4747
// Mock the database
48-
mockModule('@codebuff/common/db', () => ({
48+
mockModule('@codebuff/internal/db', () => ({
4949
default: {
5050
query: {
5151
user: {

backend/src/__tests__/usage-calculation.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { calculateUsageAndBalance } from '@codebuff/billing'
22
import { GRANT_PRIORITIES } from '@codebuff/common/constants/grant-priorities'
33
import {
4-
clearMockedModules,
54
mockModule,
5+
clearMockedModules,
66
} from '@codebuff/common/testing/mock-modules'
77
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
88

@@ -19,7 +19,7 @@ describe('Usage Calculation System', () => {
1919

2020
beforeAll(() => {
2121
// Mock the database module before importing the function
22-
mockModule('@codebuff/common/db', () => ({
22+
mockModule('@codebuff/internal/db', () => ({
2323
default: {
2424
select: () => ({
2525
from: () => ({
@@ -61,7 +61,7 @@ describe('Usage Calculation System', () => {
6161
]
6262

6363
// Mock the database module with the test data
64-
mockModule('@codebuff/common/db', () => ({
64+
mockModule('@codebuff/internal/db', () => ({
6565
default: {
6666
select: () => ({
6767
from: () => ({
@@ -98,7 +98,7 @@ describe('Usage Calculation System', () => {
9898
]
9999

100100
// Mock the database module with the test data
101-
mockModule('@codebuff/common/db', () => ({
101+
mockModule('@codebuff/internal/db', () => ({
102102
default: {
103103
select: () => ({
104104
from: () => ({
@@ -145,7 +145,7 @@ describe('Usage Calculation System', () => {
145145
]
146146

147147
// Mock the database module with the test data
148-
mockModule('@codebuff/common/db', () => ({
148+
mockModule('@codebuff/internal/db', () => ({
149149
default: {
150150
select: () => ({
151151
from: () => ({
@@ -201,7 +201,7 @@ describe('Usage Calculation System', () => {
201201
]
202202

203203
// Mock the database module with the test data
204-
mockModule('@codebuff/common/db', () => ({
204+
mockModule('@codebuff/internal/db', () => ({
205205
default: {
206206
select: () => ({
207207
from: () => ({

packages/agent-runtime/src/templates/__tests__/agent-registry.test.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TEST_AGENT_RUNTIME_IMPL } from '@codebuff/common/testing/impl/agent-runtime'
2-
import { mockModule } from '@codebuff/common/testing/mock-modules'
32
import { getStubProjectFileContext } from '@codebuff/common/util/file'
43
import {
54
describe,
@@ -9,7 +8,6 @@ import {
98
afterEach,
109
spyOn,
1110
mock,
12-
beforeAll,
1311
} from 'bun:test'
1412

1513
import {
@@ -69,44 +67,6 @@ const mockStaticTemplates: Record<string, AgentTemplate> = {
6967
describe('Agent Registry', () => {
7068
let mockFileContext: ProjectFileContext
7169

72-
beforeAll(() => {
73-
// Mock the database module
74-
mockModule('@codebuff/common/db', () => ({
75-
default: {
76-
select: () => ({
77-
from: () => ({
78-
where: () => ({
79-
orderBy: () => ({
80-
limit: () => Promise.resolve([]),
81-
}),
82-
then: (fn: (rows: any[]) => any) => fn([]),
83-
}),
84-
}),
85-
}),
86-
},
87-
}))
88-
89-
// Mock the schema module
90-
mockModule('@codebuff/common/db/schema', () => ({
91-
agentConfig: {
92-
id: 'id',
93-
publisher_id: 'publisher_id',
94-
version: 'version',
95-
major: 'major',
96-
minor: 'minor',
97-
patch: 'patch',
98-
data: 'data',
99-
},
100-
}))
101-
102-
// Mock drizzle-orm
103-
mockModule('drizzle-orm', () => ({
104-
and: (...args: any[]) => ({ type: 'and', args }),
105-
desc: (field: any) => ({ type: 'desc', field }),
106-
eq: (field: any, value: any) => ({ type: 'eq', field, value }),
107-
}))
108-
})
109-
11070
beforeEach(async () => {
11171
agentRuntimeImpl = {
11272
...TEST_AGENT_RUNTIME_IMPL,

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('Credit Delegation', () => {
3131
}))
3232

3333
// Mock common dependencies
34-
await mockModule('@codebuff/common/db', () => {
34+
await mockModule('@codebuff/internal/db', () => {
3535
const select = mock((fields: Record<string, unknown>) => {
3636
if ('orgId' in fields && 'orgName' in fields) {
3737
return {
@@ -78,17 +78,6 @@ describe('Credit Delegation', () => {
7878
},
7979
}
8080
})
81-
82-
await mockModule('@codebuff/common/db/schema', () => ({
83-
orgMember: { org_id: 'org_id', user_id: 'user_id' },
84-
org: { id: 'id', name: 'name', slug: 'slug' },
85-
orgRepo: {
86-
org_id: 'org_id',
87-
repo_url: 'repo_url',
88-
repo_name: 'repo_name',
89-
is_active: 'is_active',
90-
},
91-
}))
9281
})
9382

9483
afterAll(() => {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
clearMockedModules,
33
mockModule,
44
} from '@codebuff/common/testing/mock-modules'
5-
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'bun:test'
5+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
66

77
import {
88
calculateOrganizationUsageAndBalance,
@@ -80,12 +80,11 @@ const createDbMock = (options?: {
8080
}
8181

8282
describe('Organization Billing', () => {
83-
beforeAll(async () => {
84-
await mockModule('@codebuff/common/db', () => ({
83+
beforeEach(async () => {
84+
await mockModule('@codebuff/internal/db', () => ({
8585
default: createDbMock(),
8686
}))
87-
88-
await mockModule('@codebuff/common/db/transaction', () => ({
87+
await mockModule('@codebuff/internal/db/transaction', () => ({
8988
withSerializableTransaction: async ({
9089
callback,
9190
}: {
@@ -94,16 +93,10 @@ describe('Organization Billing', () => {
9493
}))
9594
})
9695

97-
afterAll(() => {
96+
afterEach(() => {
9897
clearMockedModules()
9998
})
10099

101-
afterEach(async () => {
102-
await mockModule('@codebuff/common/db', () => ({
103-
default: createDbMock(),
104-
}))
105-
})
106-
107100
describe('calculateOrganizationUsageAndBalance', () => {
108101
it('should calculate balance correctly with positive and negative balances', async () => {
109102
const organizationId = 'org-123'
@@ -130,7 +123,7 @@ describe('Organization Billing', () => {
130123

131124
it('should handle organization with no grants', async () => {
132125
// Mock empty grants
133-
await mockModule('@codebuff/common/db', () => ({
126+
await mockModule('@codebuff/internal/db', () => ({
134127
default: createDbMock({ grants: [] }),
135128
}))
136129

@@ -258,7 +251,7 @@ describe('Organization Billing', () => {
258251

259252
it('should handle duplicate operation IDs gracefully', async () => {
260253
// Mock database constraint error
261-
await mockModule('@codebuff/common/db', () => ({
254+
await mockModule('@codebuff/internal/db', () => ({
262255
default: createDbMock({
263256
insert: () => ({
264257
values: () => {

packages/internal/src/utils/__tests__/version-utils.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('version-utils', () => {
128128
describe('getLatestAgentVersion', () => {
129129
it('should return version 0.0.0 when no agent exists', async () => {
130130
// Mock the database to return empty result
131-
mockModule('@codebuff/common/db', () => ({
131+
mockModule('@codebuff/internal/db', () => ({
132132
default: {
133133
select: () => ({
134134
from: () => ({
@@ -150,7 +150,7 @@ describe('version-utils', () => {
150150

151151
it('should return latest version when agent exists', async () => {
152152
// Mock the database to return a version
153-
mockModule('@codebuff/common/db', () => ({
153+
mockModule('@codebuff/internal/db', () => ({
154154
default: {
155155
select: () => ({
156156
from: () => ({
@@ -173,7 +173,7 @@ describe('version-utils', () => {
173173

174174
it('should handle null values in database response', async () => {
175175
// Mock the database to return null values
176-
mockModule('@codebuff/common/db', () => ({
176+
mockModule('@codebuff/internal/db', () => ({
177177
default: {
178178
select: () => ({
179179
from: () => ({
@@ -268,7 +268,7 @@ describe('version-utils', () => {
268268
describe('versionExists', () => {
269269
it('should return true when version exists', async () => {
270270
// Mock the database to return a result
271-
mockModule('@codebuff/common/db', () => ({
271+
mockModule('@codebuff/internal/db', () => ({
272272
default: {
273273
select: () => ({
274274
from: () => ({
@@ -290,7 +290,7 @@ describe('version-utils', () => {
290290

291291
it('should return false when version does not exist', async () => {
292292
// Mock the database to return empty result
293-
mockModule('@codebuff/common/db', () => ({
293+
mockModule('@codebuff/internal/db', () => ({
294294
default: {
295295
select: () => ({
296296
from: () => ({

0 commit comments

Comments
 (0)