Skip to content

Commit d13f937

Browse files
committed
refactor(cli): simplify use-send-message with extracted helpers and fix type errors
- Extract createEventHandlerState helper for SDK event handler state - Extract createRunConfig helper for SDK run configuration - Reduce prop drilling by pulling setters directly from useChatStore - Rename EventHandlerContext to EventHandlerState to avoid React confusion - Memoize streamRefs with useRef for referential stability - Fix mock function types in bash-mode.test.ts and referral-mode.test.ts - Update sdk-event-handlers.test.ts for renamed type
1 parent 1013a8a commit d13f937

File tree

8 files changed

+329
-225
lines changed

8 files changed

+329
-225
lines changed

cli/src/__tests__/bash-mode.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect, mock } from 'bun:test'
22

33
import type { InputMode } from '../utils/input-modes'
4+
import type { InputValue } from '../state/chat-store'
45

56
/**
67
* Tests for bash mode functionality in the CLI.
@@ -21,8 +22,8 @@ import type { InputMode } from '../utils/input-modes'
2122
describe('bash-mode', () => {
2223
describe('entering bash mode', () => {
2324
test('typing exactly "!" enters bash mode and clears input', () => {
24-
const setInputMode = mock(() => {})
25-
const setInputValue = mock(() => {})
25+
const setInputMode = mock((_mode: InputMode) => {})
26+
const setInputValue = mock((_value: Partial<InputValue>) => {})
2627

2728
// Simulate user typing '!'
2829
const inputValue = {
@@ -50,8 +51,8 @@ describe('bash-mode', () => {
5051
})
5152

5253
test('typing "!ls" does NOT enter bash mode (not exactly "!")', () => {
53-
const setInputMode = mock(() => {})
54-
const setInputValue = mock(() => {})
54+
const setInputMode = mock((_mode: InputMode) => {})
55+
const setInputValue = mock((_value: Partial<InputValue>) => {})
5556

5657
// Simulate user typing '!ls'
5758
const inputValue = {
@@ -78,8 +79,8 @@ describe('bash-mode', () => {
7879
})
7980

8081
test('typing "!" when already in bash mode does nothing special', () => {
81-
const setInputMode = mock(() => {})
82-
const setInputValue = mock(() => {})
82+
const setInputMode = mock((_mode: InputMode) => {})
83+
const setInputValue = mock((_value: Partial<InputValue>) => {})
8384

8485
const inputValue = {
8586
text: '!',
@@ -108,7 +109,7 @@ describe('bash-mode', () => {
108109

109110
describe('exiting bash mode', () => {
110111
test('backspace at cursor position 0 exits bash mode', () => {
111-
const setInputMode = mock(() => {})
112+
const setInputMode = mock((_mode: InputMode) => {})
112113

113114
// Simulate backspace key press in bash mode at cursor position 0
114115
const inputMode: InputMode = 'bash'
@@ -128,7 +129,7 @@ describe('bash-mode', () => {
128129
})
129130

130131
test('backspace at cursor position 0 with non-empty input DOES exit bash mode', () => {
131-
const setInputMode = mock(() => {})
132+
const setInputMode = mock((_mode: InputMode) => {})
132133

133134
const inputMode: InputMode = 'bash'
134135
const cursorPosition = 0
@@ -147,7 +148,7 @@ describe('bash-mode', () => {
147148
})
148149

149150
test('backspace at cursor position > 0 does NOT exit bash mode', () => {
150-
const setInputMode = mock(() => {})
151+
const setInputMode = mock((_mode: InputMode) => {})
151152

152153
const inputMode: InputMode = 'bash'
153154
const cursorPosition: number = 2
@@ -166,7 +167,7 @@ describe('bash-mode', () => {
166167
})
167168

168169
test('other keys at cursor position 0 do NOT exit bash mode', () => {
169-
const setInputMode = mock(() => {})
170+
const setInputMode = mock((_mode: InputMode) => {})
170171

171172
const inputMode: InputMode = 'bash'
172173
const cursorPosition = 0
@@ -185,7 +186,7 @@ describe('bash-mode', () => {
185186
})
186187

187188
test('backspace when NOT in bash mode does nothing to bash mode', () => {
188-
const setInputMode = mock(() => {})
189+
const setInputMode = mock((_mode: InputMode) => {})
189190

190191
const inputMode = 'default' as InputMode
191192
const cursorPosition = 0
@@ -247,7 +248,7 @@ describe('bash-mode', () => {
247248
})
248249

249250
test('submission saves command WITH "!" to history', () => {
250-
const saveToHistory = mock(() => {})
251+
const saveToHistory = mock((_command: string) => {})
251252
const trimmedInput = 'git status'
252253
const commandWithBang = '!' + trimmedInput
253254

@@ -258,7 +259,7 @@ describe('bash-mode', () => {
258259
})
259260

260261
test('submission exits bash mode after running command', () => {
261-
const setInputMode = mock(() => {})
262+
const setInputMode = mock((_mode: InputMode) => {})
262263

263264
// After submission, bash mode should be exited
264265
setInputMode('default')
@@ -267,7 +268,7 @@ describe('bash-mode', () => {
267268
})
268269

269270
test('terminal command receives value WITHOUT "!" prefix', () => {
270-
const runTerminalCommand = mock(() =>
271+
const runTerminalCommand = mock((_params: Record<string, unknown>) =>
271272
Promise.resolve([{ value: { stdout: 'output' } }]),
272273
)
273274
const trimmedInput = 'echo hello'

cli/src/__tests__/referral-mode.test.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { getInputModeConfig } from '../utils/input-modes'
44

55
import type { InputMode } from '../utils/input-modes'
66

7+
// Helper type for mock functions
8+
type MockSetInputMode = (mode: InputMode) => void
9+
710
/**
811
* Tests for referral mode functionality in the CLI.
912
*
@@ -21,7 +24,7 @@ import type { InputMode } from '../utils/input-modes'
2124
describe('referral-mode', () => {
2225
describe('entering referral mode', () => {
2326
test('typing "/referral" enters referral mode', () => {
24-
const setInputMode = mock(() => {})
27+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
2528
const command = '/referral'
2629

2730
// Simulate command processing
@@ -33,7 +36,7 @@ describe('referral-mode', () => {
3336
})
3437

3538
test('typing "/redeem" also enters referral mode', () => {
36-
const setInputMode = mock(() => {})
39+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
3740
const command = '/redeem' as string
3841

3942
if (command === '/referral' || command === '/redeem') {
@@ -44,8 +47,8 @@ describe('referral-mode', () => {
4447
})
4548

4649
test('/referral with a code argument redeems immediately without entering mode', () => {
47-
const setInputMode = mock(() => {})
48-
const handleReferralCode = mock(async () => {})
50+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
51+
const handleReferralCode = mock(async (_code: string) => {})
4952
const command = '/referral abc123'
5053

5154
// Simulate handler logic
@@ -65,7 +68,7 @@ describe('referral-mode', () => {
6568

6669
describe('exiting referral mode', () => {
6770
test('backspace at cursor position 0 exits referral mode', () => {
68-
const setInputMode = mock(() => {})
71+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
6972

7073
const inputMode = 'referral' as InputMode
7174
const cursorPosition = 0
@@ -84,7 +87,7 @@ describe('referral-mode', () => {
8487
})
8588

8689
test('backspace at cursor position 0 with non-empty input DOES exit referral mode', () => {
87-
const setInputMode = mock(() => {})
90+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
8891

8992
const inputMode = 'referral' as InputMode
9093
const cursorPosition = 0
@@ -103,7 +106,7 @@ describe('referral-mode', () => {
103106
})
104107

105108
test('backspace at cursor position > 0 does NOT exit referral mode', () => {
106-
const setInputMode = mock(() => {})
109+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
107110

108111
const inputMode = 'referral' as InputMode
109112
const cursorPosition = 5 as number
@@ -122,7 +125,7 @@ describe('referral-mode', () => {
122125
})
123126

124127
test('other keys at cursor position 0 do NOT exit referral mode', () => {
125-
const setInputMode = mock(() => {})
128+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
126129

127130
const inputMode = 'referral' as InputMode
128131
const cursorPosition = 0
@@ -316,7 +319,7 @@ describe('referral-mode', () => {
316319
})
317320

318321
test('submission exits referral mode after processing', () => {
319-
const setInputMode = mock(() => {})
322+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
320323

321324
// After submission, referral mode should be exited
322325
setInputMode('default')
@@ -325,8 +328,8 @@ describe('referral-mode', () => {
325328
})
326329

327330
test('invalid code shows error and exits referral mode', () => {
328-
const setInputMode = mock(() => {})
329-
const showError = mock(() => {})
331+
const setInputMode = mock<MockSetInputMode>((_mode) => {})
332+
const showError = mock((_msg: string) => {})
330333
const trimmedInput = 'ab' // Too short
331334
const pattern = /^[a-zA-Z0-9-]{3,50}$/
332335

@@ -459,7 +462,7 @@ describe('referral-mode', () => {
459462

460463
describe('integration with command router', () => {
461464
test('referral mode input is routed to handleReferralCode', () => {
462-
const handleReferralCode = mock(async () => {})
465+
const handleReferralCode = mock(async (_code: string) => {})
463466
const inputMode = 'referral' as InputMode
464467
const trimmedInput = 'abc123'
465468

@@ -474,7 +477,7 @@ describe('referral-mode', () => {
474477
})
475478

476479
test('normal mode input is NOT routed to referral handler', () => {
477-
const handleReferralCode = mock(async () => {})
480+
const handleReferralCode = mock(async (_code: string) => {})
478481
const inputMode = 'default' as InputMode
479482
const trimmedInput = 'abc123'
480483

@@ -502,8 +505,8 @@ describe('referral-mode', () => {
502505

503506
describe('error handling', () => {
504507
test('network error during redemption shows error message', async () => {
505-
const showError = mock(() => {})
506-
const handleReferralCode = mock(async () => {
508+
const showError = mock((_msg: string) => {})
509+
const handleReferralCode = mock(async (_code: string) => {
507510
throw new Error('Network error')
508511
})
509512

@@ -521,8 +524,8 @@ describe('referral-mode', () => {
521524
})
522525

523526
test('validation error prevents redemption attempt', () => {
524-
const handleReferralCode = mock(async () => {})
525-
const showError = mock(() => {})
527+
const handleReferralCode = mock(async (_code: string) => {})
528+
const showError = mock((_msg: string) => {})
526529
const trimmedInput = '!@#' // Invalid characters
527530
const pattern = /^[a-zA-Z0-9-]{3,50}$/
528531

cli/src/chat.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ export const Chat = ({
149149
agentMode,
150150
setAgentMode,
151151
toggleAgentMode,
152-
setHasReceivedPlanResponse,
153-
lastMessageMode,
154-
setLastMessageMode,
155-
addSessionCredits,
156-
setRunState,
157152
isAnnouncementVisible,
158153
setIsAnnouncementVisible,
159154
isRetrying,
@@ -182,12 +177,6 @@ export const Chat = ({
182177
agentMode: store.agentMode,
183178
setAgentMode: store.setAgentMode,
184179
toggleAgentMode: store.toggleAgentMode,
185-
hasReceivedPlanResponse: store.hasReceivedPlanResponse,
186-
setHasReceivedPlanResponse: store.setHasReceivedPlanResponse,
187-
lastMessageMode: store.lastMessageMode,
188-
setLastMessageMode: store.setLastMessageMode,
189-
addSessionCredits: store.addSessionCredits,
190-
setRunState: store.setRunState,
191180
isAnnouncementVisible: store.isAnnouncementVisible,
192181
setIsAnnouncementVisible: store.setIsAnnouncementVisible,
193182
isRetrying: store.isRetrying,
@@ -596,15 +585,9 @@ export const Chat = ({
596585
// Future: Could be used for analytics or debugging
597586

598587
const { sendMessage, clearMessages } = useSendMessage({
599-
setMessages,
600-
setFocusedAgentId,
601-
setInputFocused,
602588
inputRef,
603-
setStreamingAgents,
604589
activeSubagentsRef,
605590
isChainInProgressRef,
606-
setActiveSubagents,
607-
setIsChainInProgress,
608591
setStreamStatus,
609592
setCanProcessQueue,
610593
abortControllerRef,
@@ -613,11 +596,6 @@ export const Chat = ({
613596
mainAgentTimer,
614597
scrollToLatest,
615598
onTimerEvent: () => {}, // No-op for now
616-
setHasReceivedPlanResponse,
617-
lastMessageMode,
618-
setLastMessageMode,
619-
addSessionCredits,
620-
setRunState,
621599
isQueuePausedRef,
622600
resumeQueue,
623601
continueChat,

0 commit comments

Comments
 (0)