Skip to content

Commit c6f05b6

Browse files
committed
Merge branch 'main' into set_output
2 parents 1440182 + 24bea3f commit c6f05b6

19 files changed

+763
-526
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import type { CreditBalance } from '@codebuff/billing'
2-
import { checkAndTriggerAutoTopup } from '@codebuff/billing'
31
import * as billing from '@codebuff/billing'
2+
import { checkAndTriggerAutoTopup } from '@codebuff/billing'
43
import {
4+
clearMockedModules,
5+
mockModule,
6+
} from '@codebuff/common/testing/mock-modules'
7+
import {
8+
afterAll,
9+
afterEach,
510
beforeEach,
611
describe,
712
expect,
813
it,
914
mock,
10-
afterEach,
1115
spyOn,
1216
} from 'bun:test'
1317

@@ -60,7 +64,7 @@ describe('Auto Top-up System', () => {
6064
grantCreditsMock = mock(() => Promise.resolve())
6165

6266
// Mock the database
63-
mock.module('@codebuff/common/db', () => ({
67+
mockModule('@codebuff/common/db', () => ({
6468
default: {
6569
query: {
6670
user: {
@@ -84,7 +88,7 @@ describe('Auto Top-up System', () => {
8488
)
8589

8690
// Mock Stripe payment intent creation
87-
mock.module('@codebuff/common/util/stripe', () => ({
91+
mockModule('@codebuff/common/util/stripe', () => ({
8892
stripeServer: {
8993
paymentIntents: {
9094
create: mock(() =>
@@ -98,6 +102,10 @@ describe('Auto Top-up System', () => {
98102
}))
99103
})
100104

105+
afterAll(() => {
106+
clearMockedModules()
107+
})
108+
101109
it('should trigger top-up when balance below threshold', async () => {
102110
await checkAndTriggerAutoTopup('test-user')
103111

backend/src/__tests__/dynamic-agent-loader.test.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
1+
import {
2+
clearMockedModules,
3+
mockModule,
4+
} from '@codebuff/common/testing/mock-modules'
15
import { ProjectFileContext } from '@codebuff/common/util/file'
2-
import { describe, expect, it, mock } from 'bun:test'
6+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
37
import {
48
dynamicAgentService,
59
DynamicAgentService,
610
} from '../templates/dynamic-agent-service'
711

8-
// Mock backend utility module
9-
mock.module('../util/file-resolver', () => ({
10-
resolvePromptField: (field: string | { path: string }, basePath: string) => {
11-
if (typeof field === 'string') {
12-
return field
13-
}
14-
if (field.path?.includes('brainstormer-system.md')) {
15-
return 'You are a creative brainstormer.'
16-
}
17-
if (field.path?.includes('brainstormer-user-input.md')) {
18-
return 'Help brainstorm ideas.'
19-
}
20-
return 'Mock content'
21-
},
22-
resolveFileContent: (filePath: string, basePath: string) => {
23-
if (filePath.includes('brainstormer-system.md')) {
24-
return 'You are a creative brainstormer.'
25-
}
26-
if (filePath.includes('brainstormer-user-input.md')) {
27-
return 'Help brainstorm ideas.'
28-
}
29-
return 'Mock content'
30-
},
31-
}))
32-
3312
describe('Dynamic Agent Loader', () => {
3413
const mockFileContext: ProjectFileContext = {
3514
projectRoot: '/test',
@@ -56,6 +35,40 @@ describe('Dynamic Agent Loader', () => {
5635
},
5736
}
5837

38+
beforeAll(() => {
39+
// Mock backend utility module
40+
mockModule('@codebuff/backend/util/file-resolver', () => ({
41+
resolvePromptField: (
42+
field: string | { path: string },
43+
basePath: string
44+
) => {
45+
if (typeof field === 'string') {
46+
return field
47+
}
48+
if (field.path?.includes('brainstormer-system.md')) {
49+
return 'You are a creative brainstormer.'
50+
}
51+
if (field.path?.includes('brainstormer-user-input.md')) {
52+
return 'Help brainstorm ideas.'
53+
}
54+
return 'Mock content'
55+
},
56+
resolveFileContent: (filePath: string, basePath: string) => {
57+
if (filePath.includes('brainstormer-system.md')) {
58+
return 'You are a creative brainstormer.'
59+
}
60+
if (filePath.includes('brainstormer-user-input.md')) {
61+
return 'Help brainstorm ideas.'
62+
}
63+
return 'Mock content'
64+
},
65+
}))
66+
})
67+
68+
afterAll(() => {
69+
clearMockedModules()
70+
})
71+
5972
it('should load valid dynamic agent template', async () => {
6073
const fileContext: ProjectFileContext = {
6174
...mockFileContext,

backend/src/__tests__/dynamic-agent-schema-validation.test.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1+
import {
2+
clearMockedModules,
3+
mockModule,
4+
} from '@codebuff/common/testing/mock-modules'
15
import {
26
getStubProjectFileContext,
37
ProjectFileContext,
48
} from '@codebuff/common/util/file'
5-
import { beforeEach, describe, expect, it, mock } from 'bun:test'
9+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'bun:test'
610
import { DynamicAgentService } from '../templates/dynamic-agent-service'
711

8-
// Mock logger to avoid console output during tests
9-
mock.module('../util/logger', () => ({
10-
logger: {
11-
debug: () => {},
12-
warn: () => {},
13-
error: () => {},
14-
},
15-
}))
16-
1712
describe('Dynamic Agent Schema Validation', () => {
1813
let service: DynamicAgentService
1914
let mockFileContext: ProjectFileContext
2015

16+
beforeAll(() => {
17+
// Mock logger to avoid console output during tests
18+
mockModule('@codebuff/backend/util/logger', () => ({
19+
logger: {
20+
debug: () => {},
21+
warn: () => {},
22+
error: () => {},
23+
},
24+
}))
25+
})
26+
2127
beforeEach(() => {
2228
service = new DynamicAgentService()
2329
mockFileContext = getStubProjectFileContext()
2430
})
2531

32+
afterAll(() => {
33+
clearMockedModules()
34+
})
35+
2636
describe('Default Schema Behavior', () => {
2737
it('should have no prompt schema when no promptSchema provided', async () => {
2838
const fileContext: ProjectFileContext = {

backend/src/__tests__/fast-rewrite.test.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1-
import { describe, expect, it, mock } from 'bun:test'
21
import { TEST_USER_ID } from '@codebuff/common/constants'
2+
import {
3+
clearMockedModules,
4+
mockModule,
5+
} from '@codebuff/common/testing/mock-modules'
6+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
37
import { createPatch } from 'diff'
48
import path from 'path'
59
import { rewriteWithOpenAI } from '../fast-rewrite'
610

7-
// Mock database interactions
8-
mock.module('pg-pool', () => ({
9-
Pool: class {
10-
connect() {
11-
return {
12-
query: () => ({
13-
rows: [{ id: 'test-user-id' }],
14-
rowCount: 1,
15-
}),
16-
release: () => {},
17-
}
18-
}
19-
},
20-
}))
11+
describe.skip('rewriteWithOpenAI', () => {
12+
beforeAll(() => {
13+
// Mock database interactions
14+
mockModule('pg-pool', () => ({
15+
Pool: class {
16+
connect() {
17+
return {
18+
query: () => ({
19+
rows: [{ id: 'test-user-id' }],
20+
rowCount: 1,
21+
}),
22+
release: () => {},
23+
}
24+
}
25+
},
26+
}))
2127

22-
// Mock message saving
23-
mock.module('backend/llm-apis/message-cost-tracker', () => ({
24-
saveMessage: () => Promise.resolve(),
25-
}))
28+
// Mock message saving
29+
mockModule('@codebuff/backend/llm-apis/message-cost-tracker', () => ({
30+
saveMessage: () => Promise.resolve(),
31+
}))
32+
})
33+
34+
afterAll(() => {
35+
clearMockedModules()
36+
})
2637

27-
describe.skip('rewriteWithOpenAI', () => {
2838
it('should correctly integrate edit snippet changes while preserving formatting', async () => {
2939
const testDataDir = path.join(__dirname, 'test-data', 'dex-go')
3040
const originalContent = await Bun.file(`${testDataDir}/original.go`).text()

backend/src/__tests__/main-prompt.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import * as analytics from '@codebuff/common/analytics'
33
import { TEST_USER_ID } from '@codebuff/common/constants'
44
import { getInitialSessionState } from '@codebuff/common/types/session-state'
55
import {
6+
afterAll,
67
afterEach,
8+
beforeAll,
79
beforeEach,
810
describe,
911
expect,
@@ -25,28 +27,34 @@ import {
2527
getToolCallString,
2628
renderToolResults,
2729
} from '@codebuff/common/constants/tools'
30+
import {
31+
clearMockedModules,
32+
mockModule,
33+
} from '@codebuff/common/testing/mock-modules'
2834
import { ProjectFileContext } from '@codebuff/common/util/file'
2935
import * as getDocumentationForQueryModule from '../get-documentation-for-query'
3036
import * as websocketAction from '../websockets/websocket-action'
3137

32-
// Mock logger
33-
mock.module('../util/logger', () => ({
34-
logger: {
35-
debug: () => {},
36-
error: () => {},
37-
info: () => {},
38-
warn: () => {},
39-
},
40-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
41-
}))
42-
4338
const mockAgentStream = (streamOutput: string) => {
4439
spyOn(aisdk, 'promptAiSdkStream').mockImplementation(async function* () {
4540
yield streamOutput
4641
})
4742
}
4843

4944
describe('mainPrompt', () => {
45+
beforeAll(() => {
46+
// Mock logger
47+
mockModule('@codebuff/backend/util/logger', () => ({
48+
logger: {
49+
debug: () => {},
50+
error: () => {},
51+
info: () => {},
52+
warn: () => {},
53+
},
54+
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
55+
}))
56+
})
57+
5058
beforeEach(() => {
5159
// Mock analytics and tracing
5260
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
@@ -141,6 +149,10 @@ describe('mainPrompt', () => {
141149
mock.restore()
142150
})
143151

152+
afterAll(() => {
153+
clearMockedModules()
154+
})
155+
144156
class MockWebSocket {
145157
send(msg: string) {}
146158
close() {}

0 commit comments

Comments
 (0)