Skip to content

Commit 832b4fe

Browse files
committed
initial DI
1 parent 158fe9e commit 832b4fe

31 files changed

+640
-676
lines changed

backend/src/__tests__/cost-aggregation.integration.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TEST_USER_ID } from '@codebuff/common/old-constants'
2+
import { testAgentRuntimeImpl } from '@codebuff/common/testing/impl/agent-runtime'
23
import { getInitialSessionState } from '@codebuff/common/types/session-state'
34
import {
45
spyOn,
@@ -18,7 +19,6 @@ import * as websocketAction from '../websockets/websocket-action'
1819

1920
import type { AgentTemplate } from '../templates/types'
2021
import type { ProjectFileContext } from '@codebuff/common/util/file'
21-
import type { Logger } from '@codebuff/types/logger'
2222
import type { WebSocket } from 'ws'
2323

2424
const mockFileContext: ProjectFileContext = {
@@ -99,12 +99,6 @@ class MockWebSocket {
9999
describe('Cost Aggregation Integration Tests', () => {
100100
let mockLocalAgentTemplates: Record<string, any>
101101
let mockWebSocket: MockWebSocket
102-
const logger: Logger = {
103-
debug: () => {},
104-
error: () => {},
105-
info: () => {},
106-
warn: () => {},
107-
}
108102

109103
beforeEach(async () => {
110104
mockWebSocket = new MockWebSocket()
@@ -256,13 +250,13 @@ describe('Cost Aggregation Integration Tests', () => {
256250
}
257251

258252
const result = await mainPrompt({
253+
...testAgentRuntimeImpl,
259254
ws: mockWebSocket as unknown as WebSocket,
260255
action,
261256
userId: TEST_USER_ID,
262257
clientSessionId: 'test-session',
263258
onResponseChunk: () => {},
264259
localAgentTemplates: mockLocalAgentTemplates,
265-
logger,
266260
})
267261

268262
// Verify the total cost includes both main agent and subagent costs
@@ -291,12 +285,12 @@ describe('Cost Aggregation Integration Tests', () => {
291285

292286
// Call through websocket action handler to test full integration
293287
await websocketAction.callMainPrompt({
288+
...testAgentRuntimeImpl,
294289
ws: mockWebSocket as unknown as WebSocket,
295290
action,
296291
userId: TEST_USER_ID,
297292
promptId: 'test-prompt',
298293
clientSessionId: 'test-session',
299-
logger,
300294
})
301295

302296
// Verify final cost is included in prompt response
@@ -361,13 +355,13 @@ describe('Cost Aggregation Integration Tests', () => {
361355
}
362356

363357
const result = await mainPrompt({
358+
...testAgentRuntimeImpl,
364359
ws: mockWebSocket as unknown as WebSocket,
365360
action,
366361
userId: TEST_USER_ID,
367362
clientSessionId: 'test-session',
368363
onResponseChunk: () => {},
369364
localAgentTemplates: mockLocalAgentTemplates,
370-
logger,
371365
})
372366

373367
// Should aggregate costs from all levels: main + sub1 + sub2
@@ -419,13 +413,13 @@ describe('Cost Aggregation Integration Tests', () => {
419413
let result
420414
try {
421415
result = await mainPrompt({
416+
...testAgentRuntimeImpl,
422417
ws: mockWebSocket as unknown as WebSocket,
423418
action,
424419
userId: TEST_USER_ID,
425420
clientSessionId: 'test-session',
426421
onResponseChunk: () => {},
427422
localAgentTemplates: mockLocalAgentTemplates,
428-
logger,
429423
})
430424
} catch (error) {
431425
// Expected to fail, but costs may still be tracked
@@ -468,13 +462,13 @@ describe('Cost Aggregation Integration Tests', () => {
468462
}
469463

470464
await mainPrompt({
465+
...testAgentRuntimeImpl,
471466
ws: mockWebSocket as unknown as WebSocket,
472467
action,
473468
userId: TEST_USER_ID,
474469
clientSessionId: 'test-session',
475470
onResponseChunk: () => {},
476471
localAgentTemplates: mockLocalAgentTemplates,
477-
logger,
478472
})
479473

480474
// Verify no duplicate message IDs (no double-counting)
@@ -508,12 +502,12 @@ describe('Cost Aggregation Integration Tests', () => {
508502

509503
// Call through websocket action to test server-side reset
510504
await websocketAction.callMainPrompt({
505+
...testAgentRuntimeImpl,
511506
ws: mockWebSocket as unknown as WebSocket,
512507
action,
513508
userId: TEST_USER_ID,
514509
promptId: 'test-prompt',
515510
clientSessionId: 'test-session',
516-
logger,
517511
})
518512

519513
// Server should have reset the malicious value and calculated correct cost

backend/src/__tests__/cost-aggregation.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { testAgentRuntimeImpl } from '@codebuff/common/testing/impl/agent-runtime'
12
import {
23
getInitialAgentState,
34
getInitialSessionState,
@@ -18,16 +19,8 @@ import { handleSpawnAgents } from '../tools/handlers/tool/spawn-agents'
1819

1920
import type { AgentState } from '@codebuff/common/types/session-state'
2021
import type { ProjectFileContext } from '@codebuff/common/util/file'
21-
import type { Logger } from '@codebuff/types/logger'
2222
import type { WebSocket } from 'ws'
2323

24-
const logger: Logger = {
25-
debug: () => {},
26-
error: () => {},
27-
info: () => {},
28-
warn: () => {},
29-
}
30-
3124
const mockFileContext: ProjectFileContext = {
3225
projectRoot: '/test',
3326
cwd: '/test',
@@ -187,6 +180,7 @@ describe('Cost Aggregation System', () => {
187180
}
188181

189182
const result = handleSpawnAgents({
183+
...testAgentRuntimeImpl,
190184
previousToolCallFinished: Promise.resolve(),
191185
toolCall: mockToolCall,
192186
fileContext: mockFileContext,
@@ -195,7 +189,6 @@ describe('Cost Aggregation System', () => {
195189
writeToClient: () => {},
196190
getLatestState: () => ({ messages: [] }),
197191
state: mockValidatedState,
198-
logger,
199192
})
200193

201194
await result.result
@@ -267,6 +260,7 @@ describe('Cost Aggregation System', () => {
267260
}
268261

269262
const result = handleSpawnAgents({
263+
...testAgentRuntimeImpl,
270264
previousToolCallFinished: Promise.resolve(),
271265
toolCall: mockToolCall,
272266
fileContext: mockFileContext,
@@ -275,7 +269,6 @@ describe('Cost Aggregation System', () => {
275269
writeToClient: () => {},
276270
getLatestState: () => ({ messages: [] }),
277271
state: mockValidatedState,
278-
logger,
279272
})
280273

281274
await result.result
@@ -424,6 +417,7 @@ describe('Cost Aggregation System', () => {
424417
}
425418

426419
const result = handleSpawnAgents({
420+
...testAgentRuntimeImpl,
427421
previousToolCallFinished: Promise.resolve(),
428422
toolCall: mockToolCall,
429423
fileContext: mockFileContext,
@@ -432,7 +426,6 @@ describe('Cost Aggregation System', () => {
432426
writeToClient: () => {},
433427
getLatestState: () => ({ messages: [] }),
434428
state: mockValidatedState,
435-
logger,
436429
})
437430

438431
await result.result

0 commit comments

Comments
 (0)