Skip to content

Commit ffd9e5e

Browse files
committed
refactor: convert startUserInput and cancelUserInput to use object parameters
1 parent b89c30a commit ffd9e5e

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ describe('live-user-inputs', () => {
2323

2424
describe('startUserInput', () => {
2525
it('should start a new user input', () => {
26-
startUserInput('user-1', 'input-123')
26+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
2727

2828
const liveInputs = getLiveUserInputIds('user-1')
2929
expect(liveInputs).toEqual(['input-123'])
3030
})
3131

3232
it('should handle multiple user inputs for same user', () => {
33-
startUserInput('user-1', 'input-123')
34-
startUserInput('user-1', 'input-456')
33+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
34+
startUserInput({ userId: 'user-1', userInputId: 'input-456' })
3535

3636
const liveInputs = getLiveUserInputIds('user-1')
3737
expect(liveInputs).toEqual(['input-123', 'input-456'])
3838
})
3939

4040
it('should handle user inputs for different users', () => {
41-
startUserInput('user-1', 'input-123')
42-
startUserInput('user-2', 'input-456')
41+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
42+
startUserInput({ userId: 'user-2', userInputId: 'input-456' })
4343

4444
const user1Inputs = getLiveUserInputIds('user-1')
4545
const user2Inputs = getLiveUserInputIds('user-2')
@@ -51,30 +51,30 @@ describe('live-user-inputs', () => {
5151

5252
describe('cancelUserInput', () => {
5353
it('should cancel a specific user input', () => {
54-
startUserInput('user-1', 'input-123')
55-
startUserInput('user-1', 'input-456')
54+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
55+
startUserInput({ userId: 'user-1', userInputId: 'input-456' })
5656

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

5959
const liveInputs = getLiveUserInputIds('user-1')
6060
expect(liveInputs).toEqual(['input-456'])
6161
})
6262

6363
it('should remove user from tracking when all inputs cancelled', () => {
64-
startUserInput('user-1', 'input-123')
64+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
6565

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

6868
const liveInputs = getLiveUserInputIds('user-1')
6969
expect(liveInputs).toBeUndefined()
7070
})
7171

7272
it('should handle cancelling non-existent input gracefully', () => {
73-
startUserInput('user-1', 'input-123')
73+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
7474

7575
// Should not throw
7676
expect(() => {
77-
cancelUserInput('user-1', 'input-nonexistent')
77+
cancelUserInput({ userId: 'user-1', userInputId: 'input-nonexistent' })
7878
}).not.toThrow()
7979

8080
const liveInputs = getLiveUserInputIds('user-1')
@@ -84,7 +84,7 @@ describe('live-user-inputs', () => {
8484
it('should handle cancelling for non-existent user gracefully', () => {
8585
// Should not throw
8686
expect(() => {
87-
cancelUserInput('user-nonexistent', 'input-123')
87+
cancelUserInput({ userId: 'user-nonexistent', userInputId: 'input-123' })
8888
}).not.toThrow()
8989
})
9090
})
@@ -93,8 +93,8 @@ describe('live-user-inputs', () => {
9393
it('should end user input when async agents disabled', () => {
9494
// Note: Testing the actual behavior requires integration with the constants module
9595
// For unit testing, we'll test the function directly
96-
startUserInput('user-1', 'input-123')
97-
cancelUserInput('user-1', 'input-123') // This simulates the behavior when async agents disabled
96+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
97+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' }) // This simulates the behavior when async agents disabled
9898

9999
const liveInputs = getLiveUserInputIds('user-1')
100100
expect(liveInputs).toBeUndefined()
@@ -103,7 +103,7 @@ describe('live-user-inputs', () => {
103103
it('should keep user input when async agents enabled', () => {
104104
// Note: Testing the actual behavior requires integration with the constants module
105105
// For unit testing, we'll test that endUserInput doesn't remove the input
106-
startUserInput('user-1', 'input-123')
106+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
107107
// Don't call cancelUserInput to simulate async agents enabled behavior
108108

109109
const liveInputs = getLiveUserInputIds('user-1')
@@ -113,15 +113,15 @@ describe('live-user-inputs', () => {
113113

114114
describe('checkLiveUserInput', () => {
115115
it('should return true for valid live user input', () => {
116-
startUserInput('user-1', 'input-123')
116+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
117117
setSessionConnected('session-1', true)
118118

119119
const isLive = checkLiveUserInput('user-1', 'input-123', 'session-1')
120120
expect(isLive).toBe(true)
121121
})
122122

123123
it('should return true for user input with matching prefix', () => {
124-
startUserInput('user-1', 'input-123')
124+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
125125
setSessionConnected('session-1', true)
126126

127127
const isLive = checkLiveUserInput(
@@ -151,15 +151,15 @@ describe('live-user-inputs', () => {
151151
})
152152

153153
it('should return false for disconnected session', () => {
154-
startUserInput('user-1', 'input-123')
154+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
155155
setSessionConnected('session-1', false)
156156

157157
const isLive = checkLiveUserInput('user-1', 'input-123', 'session-1')
158158
expect(isLive).toBe(false)
159159
})
160160

161161
it('should return false for non-matching user input', () => {
162-
startUserInput('user-1', 'input-123')
162+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
163163
setSessionConnected('session-1', true)
164164

165165
const isLive = checkLiveUserInput('user-1', 'input-456', 'session-1')
@@ -177,15 +177,15 @@ describe('live-user-inputs', () => {
177177
describe('setSessionConnected', () => {
178178
it('should set session as connected', () => {
179179
setSessionConnected('session-1', true)
180-
startUserInput('user-1', 'input-123')
180+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
181181

182182
const isLive = checkLiveUserInput('user-1', 'input-123', 'session-1')
183183
expect(isLive).toBe(true)
184184
})
185185

186186
it('should set session as disconnected', () => {
187187
setSessionConnected('session-1', true)
188-
startUserInput('user-1', 'input-123')
188+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
189189

190190
// First verify it's connected
191191
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(true)
@@ -199,7 +199,7 @@ describe('live-user-inputs', () => {
199199
setSessionConnected('session-1', true)
200200
setSessionConnected('session-2', false)
201201

202-
startUserInput('user-1', 'input-123')
202+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
203203

204204
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(true)
205205
expect(checkLiveUserInput('user-1', 'input-123', 'session-2')).toBe(false)
@@ -218,8 +218,8 @@ describe('live-user-inputs', () => {
218218
})
219219

220220
it('should return array of input IDs for user with inputs', () => {
221-
startUserInput('user-1', 'input-123')
222-
startUserInput('user-1', 'input-456')
221+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
222+
startUserInput({ userId: 'user-1', userInputId: 'input-456' })
223223

224224
const liveInputs = getLiveUserInputIds('user-1')
225225
expect(liveInputs).toEqual(['input-123', 'input-456'])
@@ -230,14 +230,14 @@ describe('live-user-inputs', () => {
230230
it('should handle complete user input lifecycle', () => {
231231
// Start session and user input
232232
setSessionConnected('session-1', true)
233-
startUserInput('user-1', 'input-123')
233+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
234234

235235
// Verify input is live
236236
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(true)
237237
expect(getLiveUserInputIds('user-1')).toEqual(['input-123'])
238238

239239
// End user input
240-
cancelUserInput('user-1', 'input-123')
240+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
241241

242242
// Verify input is no longer live
243243
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(false)
@@ -247,7 +247,7 @@ describe('live-user-inputs', () => {
247247
it('should handle session disconnect during active input', () => {
248248
// Start session and user input
249249
setSessionConnected('session-1', true)
250-
startUserInput('user-1', 'input-123')
250+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
251251

252252
// Verify input is live
253253
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(true)
@@ -265,15 +265,15 @@ describe('live-user-inputs', () => {
265265
it('should handle multiple concurrent inputs for same user', () => {
266266
setSessionConnected('session-1', true)
267267

268-
startUserInput('user-1', 'input-123')
269-
startUserInput('user-1', 'input-456')
268+
startUserInput({ userId: 'user-1', userInputId: 'input-123' })
269+
startUserInput({ userId: 'user-1', userInputId: 'input-456' })
270270

271271
expect(checkLiveUserInput('user-1', 'input-123', 'session-1')).toBe(true)
272272
expect(checkLiveUserInput('user-1', 'input-456', 'session-1')).toBe(true)
273273
expect(getLiveUserInputIds('user-1')).toEqual(['input-123', 'input-456'])
274274

275275
// Cancel one input
276-
cancelUserInput('user-1', 'input-123')
276+
cancelUserInput({ userId: 'user-1', userInputId: 'input-123' })
277277

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

backend/src/live-user-inputs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ const live: Record<string, string[]> = {}
1111
/** Map from sessionId to WebSocket connection status */
1212
const sessionConnections: Record<string, true | undefined> = {}
1313

14-
export function startUserInput(userId: string, userInputId: string): void {
14+
export function startUserInput(params: {
15+
userId: string
16+
userInputId: string
17+
}): void {
18+
const { userId, userInputId } = params
1519
if (!live[userId]) {
1620
live[userId] = []
1721
}
1822
live[userId].push(userInputId)
1923
}
2024

21-
export function cancelUserInput(userId: string, userInputId: string): void {
25+
export function cancelUserInput(params: {
26+
userId: string
27+
userInputId: string
28+
}): void {
29+
const { userId, userInputId } = params
2230
if (live[userId] && live[userId].includes(userInputId)) {
2331
live[userId] = live[userId].filter((id) => id !== userInputId)
2432
if (live[userId].length === 0) {

backend/src/websockets/websocket-action.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const onPrompt = async (
157157
})
158158
}
159159

160-
startUserInput(userId, promptId)
160+
startUserInput({ userId, userInputId: promptId })
161161

162162
try {
163163
const result = await callMainPrompt(ws, action, {
@@ -179,7 +179,7 @@ const onPrompt = async (
179179
message: response,
180180
})
181181
} finally {
182-
cancelUserInput(userId, promptId)
182+
cancelUserInput({ userId, userInputId: promptId })
183183
const usageResponse = await genUsageResponse(
184184
fingerprintId,
185185
userId,
@@ -319,7 +319,7 @@ const onCancelUserInput = async ({
319319
logger.error({ authToken }, 'User id not found for authToken')
320320
return
321321
}
322-
cancelUserInput(userId, promptId)
322+
cancelUserInput({ userId, userInputId: promptId })
323323
}
324324

325325
/**

0 commit comments

Comments
 (0)