Skip to content

Commit 55186e3

Browse files
committed
Fix build/tests
1 parent 802fd36 commit 55186e3

File tree

8 files changed

+62
-99
lines changed

8 files changed

+62
-99
lines changed

common/src/__tests__/analytics-server.test.ts

Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
55
import type { AnalyticsClient } from '../analytics-core'
66

77
import {
8-
initAnalytics,
98
trackEvent,
109
flushAnalytics,
1110
configureAnalytics,
@@ -48,15 +47,12 @@ describe('server-side analytics', () => {
4847
resetServerAnalyticsState(createTestDeps())
4948
})
5049

51-
describe('initAnalytics', () => {
52-
test('should configure analytics from clientEnv', () => {
53-
initAnalytics({
54-
logger: mockLogger,
55-
clientEnv: {
56-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
57-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
58-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
59-
},
50+
describe('configureAnalytics', () => {
51+
test('should configure analytics with config object', () => {
52+
configureAnalytics({
53+
envName: 'prod',
54+
posthogApiKey: 'test-key',
55+
posthogHostUrl: 'https://posthog.test',
6056
})
6157

6258
const config = getAnalyticsConfig()
@@ -67,13 +63,10 @@ describe('server-side analytics', () => {
6763
})
6864

6965
test('should not create client in non-prod environments', () => {
70-
initAnalytics({
71-
logger: mockLogger,
72-
clientEnv: {
73-
NEXT_PUBLIC_CB_ENVIRONMENT: 'dev',
74-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
75-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
76-
},
66+
configureAnalytics({
67+
envName: 'dev',
68+
posthogApiKey: 'test-key',
69+
posthogHostUrl: 'https://posthog.test',
7770
})
7871

7972
// Track an event - should not call capture since not prod
@@ -87,13 +80,10 @@ describe('server-side analytics', () => {
8780
})
8881

8982
test('should create client in prod environment', () => {
90-
initAnalytics({
91-
logger: mockLogger,
92-
clientEnv: {
93-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
94-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
95-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
96-
},
83+
configureAnalytics({
84+
envName: 'prod',
85+
posthogApiKey: 'test-key',
86+
posthogHostUrl: 'https://posthog.test',
9787
})
9888

9989
// Track an event - should call capture since prod
@@ -106,15 +96,8 @@ describe('server-side analytics', () => {
10696
expect(captureMock).toHaveBeenCalledTimes(1)
10797
})
10898

109-
test('should set config to null if env vars are missing', () => {
110-
initAnalytics({
111-
logger: mockLogger,
112-
clientEnv: {
113-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
114-
NEXT_PUBLIC_POSTHOG_API_KEY: '', // Missing
115-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
116-
},
117-
})
99+
test('should set config to null when passed null', () => {
100+
configureAnalytics(null)
118101

119102
const config = getAnalyticsConfig()
120103
expect(config).toBeNull()
@@ -123,13 +106,10 @@ describe('server-side analytics', () => {
123106

124107
describe('trackEvent', () => {
125108
test('should send events with correct parameters in prod', () => {
126-
initAnalytics({
127-
logger: mockLogger,
128-
clientEnv: {
129-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
130-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
131-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
132-
},
109+
configureAnalytics({
110+
envName: 'prod',
111+
posthogApiKey: 'test-key',
112+
posthogHostUrl: 'https://posthog.test',
133113
})
134114

135115
trackEvent({
@@ -146,8 +126,8 @@ describe('server-side analytics', () => {
146126
})
147127
})
148128

149-
test('should warn when client is not initialized', () => {
150-
// Configure as prod but don't initialize properly
129+
test('should lazily initialize client in prod when tracking events', () => {
130+
// Configure as prod
151131
configureAnalytics({
152132
envName: 'prod',
153133
posthogApiKey: 'test-key',
@@ -168,18 +148,15 @@ describe('server-side analytics', () => {
168148
logger: mockLogger,
169149
})
170150

171-
expect(mockLogger.warn).toHaveBeenCalled()
172-
expect(captureMock).not.toHaveBeenCalled()
151+
// With lazy initialization, the client should be created and capture should be called
152+
expect(captureMock).toHaveBeenCalled()
173153
})
174154

175155
test('should silently skip events in non-prod environments', () => {
176-
initAnalytics({
177-
logger: mockLogger,
178-
clientEnv: {
179-
NEXT_PUBLIC_CB_ENVIRONMENT: 'test',
180-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
181-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
182-
},
156+
configureAnalytics({
157+
envName: 'test',
158+
posthogApiKey: 'test-key',
159+
posthogHostUrl: 'https://posthog.test',
183160
})
184161

185162
trackEvent({
@@ -193,13 +170,10 @@ describe('server-side analytics', () => {
193170
})
194171

195172
test('should handle capture errors gracefully', () => {
196-
initAnalytics({
197-
logger: mockLogger,
198-
clientEnv: {
199-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
200-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
201-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
202-
},
173+
configureAnalytics({
174+
envName: 'prod',
175+
posthogApiKey: 'test-key',
176+
posthogHostUrl: 'https://posthog.test',
203177
})
204178

205179
// Make capture throw an error
@@ -221,14 +195,18 @@ describe('server-side analytics', () => {
221195
})
222196

223197
describe('flushAnalytics', () => {
224-
test('should call client flush in prod', async () => {
225-
initAnalytics({
198+
test('should call client flush in prod after tracking an event', async () => {
199+
configureAnalytics({
200+
envName: 'prod',
201+
posthogApiKey: 'test-key',
202+
posthogHostUrl: 'https://posthog.test',
203+
})
204+
205+
// Track an event to initialize the client
206+
trackEvent({
207+
event: AnalyticsEvent.APP_LAUNCHED,
208+
userId: 'user-123',
226209
logger: mockLogger,
227-
clientEnv: {
228-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
229-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
230-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
231-
},
232210
})
233211

234212
await flushAnalytics(mockLogger)
@@ -242,13 +220,17 @@ describe('server-side analytics', () => {
242220
})
243221

244222
test('should log errors but not throw on flush failure', async () => {
245-
initAnalytics({
223+
configureAnalytics({
224+
envName: 'prod',
225+
posthogApiKey: 'test-key',
226+
posthogHostUrl: 'https://posthog.test',
227+
})
228+
229+
// Track an event to initialize the client
230+
trackEvent({
231+
event: AnalyticsEvent.APP_LAUNCHED,
232+
userId: 'user-123',
246233
logger: mockLogger,
247-
clientEnv: {
248-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
249-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
250-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
251-
},
252234
})
253235

254236
flushMock.mockImplementation(() => {
@@ -260,7 +242,7 @@ describe('server-side analytics', () => {
260242
})
261243
})
262244

263-
describe('configureAnalytics', () => {
245+
describe('configureAnalytics manual configuration', () => {
264246
test('should allow manual configuration', () => {
265247
configureAnalytics({
266248
envName: 'prod',
@@ -289,13 +271,10 @@ describe('server-side analytics', () => {
289271

290272
describe('edge cases', () => {
291273
test('should handle events with undefined properties', () => {
292-
initAnalytics({
293-
logger: mockLogger,
294-
clientEnv: {
295-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
296-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
297-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
298-
},
274+
configureAnalytics({
275+
envName: 'prod',
276+
posthogApiKey: 'test-key',
277+
posthogHostUrl: 'https://posthog.test',
299278
})
300279

301280
trackEvent({
@@ -313,13 +292,10 @@ describe('server-side analytics', () => {
313292
})
314293

315294
test('should handle events with empty properties', () => {
316-
initAnalytics({
317-
logger: mockLogger,
318-
clientEnv: {
319-
NEXT_PUBLIC_CB_ENVIRONMENT: 'prod',
320-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-key',
321-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://posthog.test',
322-
},
295+
configureAnalytics({
296+
envName: 'prod',
297+
posthogApiKey: 'test-key',
298+
posthogHostUrl: 'https://posthog.test',
323299
})
324300

325301
trackEvent({

packages/agent-runtime/src/__tests__/loop-agent-steps.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
8484
}
8585

8686
// Mock analytics
87-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
8887
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
8988

9089
// Mock crypto.randomUUID

packages/agent-runtime/src/__tests__/main-prompt.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ describe('mainPrompt', () => {
102102
}
103103

104104
// Mock analytics and tracing
105-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
106-
analytics.initAnalytics(mainPromptBaseParams) // Initialize the mock
107105
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
108106
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
109107
Promise.resolve(true),

packages/agent-runtime/src/__tests__/n-parameter.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ describe('n parameter and GENERATE_N functionality', () => {
4949
}
5050

5151
// Mock analytics
52-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
53-
analytics.initAnalytics({ logger })
5452
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
5553

5654
// Mock crypto.randomUUID

packages/agent-runtime/src/__tests__/read-docs-tool.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
5454
beforeEach(() => {
5555
agentRuntimeImpl = { ...TEST_AGENT_RUNTIME_IMPL, sendAction: () => {} }
5656

57-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
58-
analytics.initAnalytics(agentRuntimeImpl)
5957
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
6058
spyOn(analytics, 'flushAnalytics').mockImplementation(() =>
6159
Promise.resolve(),

packages/agent-runtime/src/__tests__/run-agent-step-tools.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ describe('runAgentStep - set_output tool', () => {
7676
} as any)
7777

7878
// Mock analytics and tracing
79-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
80-
analytics.initAnalytics(agentRuntimeImpl)
8179
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
8280
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
8381
Promise.resolve(true),

packages/agent-runtime/src/__tests__/run-programmatic-step.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ describe('runProgrammaticStep', () => {
6262
}
6363

6464
// Mock analytics
65-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
66-
analytics.initAnalytics({ logger })
6765
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
6866

6967
// Mock executeToolCall

packages/agent-runtime/src/__tests__/web-search-tool.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ describe('web_search tool with researcher agent (via web API facade)', () => {
7878
}
7979

8080
// Mock analytics and tracing
81-
spyOn(analytics, 'initAnalytics').mockImplementation(() => {})
82-
analytics.initAnalytics(runAgentStepBaseParams)
8381
spyOn(analytics, 'trackEvent').mockImplementation(() => {})
8482
spyOn(bigquery, 'insertTrace').mockImplementation(() =>
8583
Promise.resolve(true),

0 commit comments

Comments
 (0)