Skip to content

Commit bd97f52

Browse files
committed
switch initAnalytics to pass in logger
1 parent 2b9e8e3 commit bd97f52

File tree

8 files changed

+52
-136
lines changed

8 files changed

+52
-136
lines changed

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import * as bigquery from '@codebuff/bigquery'
22
import * as analytics from '@codebuff/common/analytics'
33
import { TEST_USER_ID } from '@codebuff/common/old-constants'
4-
import {
5-
clearMockedModules,
6-
mockModule,
7-
} from '@codebuff/common/testing/mock-modules'
84
import { getToolCallString } from '@codebuff/common/tools/utils'
95
import {
106
AgentTemplateTypes,
117
getInitialSessionState,
128
} from '@codebuff/common/types/session-state'
139
import {
14-
afterAll,
1510
afterEach,
16-
beforeAll,
1711
beforeEach,
1812
describe,
1913
expect,
@@ -34,6 +28,7 @@ import * as websocketAction from '../websockets/websocket-action'
3428

3529
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
3630
import type { ProjectFileContext } from '@codebuff/common/util/file'
31+
import type { Logger } from '@codebuff/types/logger'
3732
import type { WebSocket } from 'ws'
3833

3934
const mockAgentStream = (streamOutput: string) => {
@@ -45,6 +40,12 @@ const mockAgentStream = (streamOutput: string) => {
4540

4641
describe('mainPrompt', () => {
4742
let mockLocalAgentTemplates: Record<string, any>
43+
const logger: Logger = {
44+
debug: () => {},
45+
error: () => {},
46+
info: () => {},
47+
warn: () => {},
48+
}
4849

4950
beforeEach(() => {
5051
// Setup common mock agent templates
@@ -84,23 +85,10 @@ describe('mainPrompt', () => {
8485
}
8586
})
8687

87-
beforeAll(() => {
88-
// Mock logger
89-
mockModule('@codebuff/backend/util/logger', () => ({
90-
logger: {
91-
debug: () => {},
92-
error: () => {},
93-
info: () => {},
94-
warn: () => {},
95-
},
96-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
97-
}))
98-
})
99-
10088
beforeEach(() => {
10189
// Mock analytics and tracing
10290
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
103-
analytics.initAnalytics() // Initialize the mock
91+
analytics.initAnalytics({ logger }) // Initialize the mock
10492
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
10593
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
10694
Promise.resolve(true),
@@ -191,10 +179,6 @@ describe('mainPrompt', () => {
191179
mock.restore()
192180
})
193181

194-
afterAll(() => {
195-
clearMockedModules()
196-
})
197-
198182
class MockWebSocket {
199183
send(msg: string) {}
200184
close() {}

backend/src/__tests__/malformed-tool-call.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import * as bigquery from '@codebuff/bigquery'
22
import * as analytics from '@codebuff/common/analytics'
33
import { TEST_USER_ID } from '@codebuff/common/old-constants'
4-
import {
5-
clearMockedModules,
6-
mockModule,
7-
} from '@codebuff/common/testing/mock-modules'
84
import { getToolCallString } from '@codebuff/common/tools/utils'
95
import { getInitialSessionState } from '@codebuff/common/types/session-state'
106
import * as stringUtils from '@codebuff/common/util/string'
117
import {
12-
afterAll,
138
afterEach,
14-
beforeAll,
159
beforeEach,
1610
describe,
1711
expect,
@@ -30,24 +24,18 @@ import type {
3024
Message,
3125
ToolMessage,
3226
} from '@codebuff/common/types/messages/codebuff-message'
27+
import type { Logger } from '@codebuff/types/logger'
3328
import type { WebSocket } from 'ws'
3429

3530
describe('malformed tool call error handling', () => {
3631
let testAgent: AgentTemplate
3732
let mockWs: MockWebSocket
38-
39-
beforeAll(() => {
40-
// Mock logger
41-
mockModule('@codebuff/backend/util/logger', () => ({
42-
logger: {
43-
debug: () => {},
44-
error: () => {},
45-
info: () => {},
46-
warn: () => {},
47-
},
48-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
49-
}))
50-
})
33+
const logger: Logger = {
34+
debug: () => {},
35+
error: () => {},
36+
info: () => {},
37+
warn: () => {},
38+
}
5139

5240
beforeEach(() => {
5341
mockWs = new MockWebSocket()
@@ -71,7 +59,7 @@ describe('malformed tool call error handling', () => {
7159

7260
// Mock analytics and tracing
7361
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
74-
analytics.initAnalytics()
62+
analytics.initAnalytics({ logger })
7563
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
7664
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
7765
Promise.resolve(true),
@@ -102,10 +90,6 @@ describe('malformed tool call error handling', () => {
10290
mock.restore()
10391
})
10492

105-
afterAll(() => {
106-
clearMockedModules()
107-
})
108-
10993
function createMockStream(chunks: string[]) {
11094
async function* generator() {
11195
for (const chunk of chunks) {

backend/src/__tests__/read-docs-tool.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import * as bigquery from '@codebuff/bigquery'
22
import * as analytics from '@codebuff/common/analytics'
33
import { TEST_USER_ID } from '@codebuff/common/old-constants'
4-
import {
5-
clearMockedModules,
6-
mockModule,
7-
} from '@codebuff/common/testing/mock-modules'
84
import { getToolCallString } from '@codebuff/common/tools/utils'
95
import { getInitialSessionState } from '@codebuff/common/types/session-state'
106
import {
11-
afterAll,
127
afterEach,
13-
beforeAll,
148
beforeEach,
159
describe,
1610
expect,
@@ -30,6 +24,7 @@ import { runAgentStep } from '../run-agent-step'
3024
import { assembleLocalAgentTemplates } from '../templates/agent-registry'
3125
import * as websocketAction from '../websockets/websocket-action'
3226

27+
import type { Logger } from '@codebuff/types/logger'
3328
import type { WebSocket } from 'ws'
3429

3530
function mockAgentStream(content: string | string[]) {
@@ -47,19 +42,12 @@ function mockAgentStream(content: string | string[]) {
4742
describe('read_docs tool with researcher agent', () => {
4843
// Track all mocked functions to verify they're being used
4944
const mockedFunctions: Array<{ name: string; spy: any }> = []
50-
51-
beforeAll(() => {
52-
// Mock logger
53-
mockModule('@codebuff/backend/util/logger', () => ({
54-
logger: {
55-
debug: () => {},
56-
error: () => {},
57-
info: () => {},
58-
warn: () => {},
59-
},
60-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
61-
}))
62-
})
45+
const logger: Logger = {
46+
debug: () => {},
47+
error: () => {},
48+
info: () => {},
49+
warn: () => {},
50+
}
6351

6452
beforeEach(() => {
6553
// Clear tracked mocks
@@ -74,7 +62,7 @@ describe('read_docs tool with researcher agent', () => {
7462
name: 'analytics.initAnalytics',
7563
spy: analyticsInitSpy,
7664
})
77-
analytics.initAnalytics()
65+
analytics.initAnalytics({ logger })
7866

7967
const trackEventSpy = spyOn(analytics, 'trackEvent').mockImplementation(
8068
() => {},
@@ -197,10 +185,6 @@ describe('read_docs tool with researcher agent', () => {
197185
mock.restore()
198186
})
199187

200-
afterAll(() => {
201-
clearMockedModules()
202-
})
203-
204188
// MockWebSocket and mockFileContext imported from test-utils
205189
const mockFileContextWithAgents = {
206190
...mockFileContext,

backend/src/__tests__/run-agent-step-tools.test.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ import * as bigquery from '@codebuff/bigquery'
22
import * as analytics from '@codebuff/common/analytics'
33
import db from '@codebuff/common/db'
44
import { TEST_USER_ID } from '@codebuff/common/old-constants'
5-
import {
6-
clearMockedModules,
7-
mockModule,
8-
} from '@codebuff/common/testing/mock-modules'
95
import { getToolCallString } from '@codebuff/common/tools/utils'
106
import { getInitialSessionState } from '@codebuff/common/types/session-state'
117
import {
128
afterAll,
139
afterEach,
14-
beforeAll,
1510
beforeEach,
1611
describe,
1712
expect,
@@ -30,23 +25,17 @@ import * as websocketAction from '../websockets/websocket-action'
3025

3126
import type { AgentTemplate } from '../templates/types'
3227
import type { ProjectFileContext } from '@codebuff/common/util/file'
28+
import type { Logger } from '@codebuff/types/logger'
3329
import type { WebSocket } from 'ws'
3430

3531
describe('runAgentStep - set_output tool', () => {
3632
let testAgent: AgentTemplate
37-
38-
beforeAll(() => {
39-
// Mock logger
40-
mockModule('@codebuff/backend/util/logger', () => ({
41-
logger: {
42-
debug: () => {},
43-
error: () => {},
44-
info: () => {},
45-
warn: () => {},
46-
},
47-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
48-
}))
49-
})
33+
const logger: Logger = {
34+
debug: () => {},
35+
error: () => {},
36+
info: () => {},
37+
warn: () => {},
38+
}
5039

5140
beforeEach(async () => {
5241
// Create a test agent that supports set_output
@@ -80,7 +69,7 @@ describe('runAgentStep - set_output tool', () => {
8069

8170
// Mock analytics and tracing
8271
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
83-
analytics.initAnalytics()
72+
analytics.initAnalytics({ logger })
8473
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
8574
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
8675
Promise.resolve(true),
@@ -132,7 +121,6 @@ describe('runAgentStep - set_output tool', () => {
132121
})
133122

134123
afterAll(() => {
135-
clearMockedModules()
136124
clearAgentGeneratorCache()
137125
})
138126

backend/src/__tests__/run-programmatic-step.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import * as analytics from '@codebuff/common/analytics'
22
import { TEST_USER_ID } from '@codebuff/common/old-constants'
3-
import {
4-
clearMockedModules,
5-
mockModule,
6-
} from '@codebuff/common/testing/mock-modules'
73
import { getInitialSessionState } from '@codebuff/common/types/session-state'
84
import {
9-
afterAll,
105
afterEach,
116
beforeAll,
127
beforeEach,
@@ -34,6 +29,7 @@ import type {
3429
ToolResultPart,
3530
} from '@codebuff/common/types/messages/content-part'
3631
import type { AgentState } from '@codebuff/common/types/session-state'
32+
import type { Logger } from '@codebuff/types/logger'
3733
import type { WebSocket } from 'ws'
3834

3935
describe('runProgrammaticStep', () => {
@@ -44,24 +40,22 @@ describe('runProgrammaticStep', () => {
4440
let getRequestContextSpy: any
4541
let addAgentStepSpy: any
4642
let sendActionSpy: any
43+
let logger: Logger
4744

4845
beforeAll(() => {
4946
// Mock logger
50-
mockModule('@codebuff/backend/util/logger', () => ({
51-
logger: {
52-
debug: () => {},
53-
error: () => {},
54-
info: () => {},
55-
warn: () => {},
56-
},
57-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
58-
}))
47+
logger = {
48+
debug: () => {},
49+
error: () => {},
50+
info: () => {},
51+
warn: () => {},
52+
}
5953
})
6054

6155
beforeEach(() => {
6256
// Mock analytics
6357
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
64-
analytics.initAnalytics()
58+
analytics.initAnalytics({ logger })
6559
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
6660

6761
// Mock executeToolCall
@@ -155,10 +149,6 @@ describe('runProgrammaticStep', () => {
155149
clearAgentGeneratorCache()
156150
})
157151

158-
afterAll(() => {
159-
clearMockedModules()
160-
})
161-
162152
describe('generator lifecycle', () => {
163153
it('should create new generator when none exists', async () => {
164154
const mockGenerator = (function* () {

0 commit comments

Comments
 (0)