Skip to content

Commit d45a2a5

Browse files
Refactor live-user-inputs.ts to pass logger as parameter
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 50c56a9 commit d45a2a5

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

backend/src/__tests__/live-user-inputs.test.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import {
1010
resetLiveUserInputsState,
1111
} from '../live-user-inputs'
1212

13+
import type { Logger } from '@codebuff/types/logger'
14+
15+
const logger: Logger = {
16+
debug: () => {},
17+
info: () => {},
18+
warn: () => {},
19+
error: () => {},
20+
}
21+
1322
describe('live-user-inputs', () => {
1423
beforeEach(() => {
1524
// Clear any existing state before each test
@@ -54,7 +63,7 @@ describe('live-user-inputs', () => {
5463
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
5564
startUserInput({ userId: 'user-1', userInputId: 'input-456' })
5665

57-
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
66+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123', logger })
5867

5968
const liveInputs = getLiveUserInputIds('user-1')
6069
expect(liveInputs).toEqual(['input-456'])
@@ -63,7 +72,7 @@ describe('live-user-inputs', () => {
6372
it('should remove user from tracking when all inputs cancelled', () => {
6473
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
6574

66-
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
75+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123', logger })
6776

6877
const liveInputs = getLiveUserInputIds('user-1')
6978
expect(liveInputs).toBeUndefined()
@@ -74,7 +83,11 @@ describe('live-user-inputs', () => {
7483

7584
// Should not throw
7685
expect(() => {
77-
cancelUserInput({ userId: 'user-1', userInputId: 'input-nonexistent' })
86+
cancelUserInput({
87+
userId: 'user-1',
88+
userInputId: 'input-nonexistent',
89+
logger,
90+
})
7891
}).not.toThrow()
7992

8093
const liveInputs = getLiveUserInputIds('user-1')
@@ -84,7 +97,11 @@ describe('live-user-inputs', () => {
8497
it('should handle cancelling for non-existent user gracefully', () => {
8598
// Should not throw
8699
expect(() => {
87-
cancelUserInput({ userId: 'user-nonexistent', userInputId: 'input-123' })
100+
cancelUserInput({
101+
userId: 'user-nonexistent',
102+
userInputId: 'input-123',
103+
logger,
104+
})
88105
}).not.toThrow()
89106
})
90107
})
@@ -94,7 +111,7 @@ describe('live-user-inputs', () => {
94111
// Note: Testing the actual behavior requires integration with the constants module
95112
// For unit testing, we'll test the function directly
96113
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
97-
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' }) // This simulates the behavior when async agents disabled
114+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123', logger }) // This simulates the behavior when async agents disabled
98115

99116
const liveInputs = getLiveUserInputIds('user-1')
100117
expect(liveInputs).toBeUndefined()
@@ -237,7 +254,7 @@ describe('live-user-inputs', () => {
237254
expect(getLiveUserInputIds('user-1')).toEqual(['input-123'])
238255

239256
// End user input
240-
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
257+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123', logger })
241258

242259
// Verify input is no longer live
243260
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(false)
@@ -273,7 +290,7 @@ describe('live-user-inputs', () => {
273290
expect(getLiveUserInputIds('user-1')).toEqual(['input-123', 'input-456'])
274291

275292
// Cancel one input
276-
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
293+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123', logger })
277294

278295
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(false)
279296
expect(checkLiveUserInput('user-1', 'input-456', 'session-1')).toBe(true)

backend/src/live-user-inputs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger } from './util/logger'
1+
import type { Logger } from '@codebuff/types/logger'
22

33
let liveUserInputCheckEnabled = true
44
export const disableLiveUserInputCheck = () => {
@@ -25,8 +25,9 @@ export function startUserInput(params: {
2525
export function cancelUserInput(params: {
2626
userId: string
2727
userInputId: string
28+
logger: Logger
2829
}): void {
29-
const { userId, userInputId } = params
30+
const { userId, userInputId, logger } = params
3031
if (live[userId] && live[userId].includes(userInputId)) {
3132
live[userId] = live[userId].filter((id) => id !== userInputId)
3233
if (live[userId].length === 0) {

backend/src/websockets/websocket-action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const onPrompt = async (
188188
message: response,
189189
})
190190
} finally {
191-
cancelUserInput({ userId, userInputId: promptId })
191+
cancelUserInput({ userId, userInputId: promptId, logger })
192192
const usageResponse = await genUsageResponse({
193193
fingerprintId,
194194
userId,
@@ -325,7 +325,7 @@ const onCancelUserInput = async ({
325325
logger.error({ authToken }, 'User id not found for authToken')
326326
return
327327
}
328-
cancelUserInput({ userId, userInputId: promptId })
328+
cancelUserInput({ userId, userInputId: promptId, logger })
329329
}
330330

331331
/**

0 commit comments

Comments
 (0)