-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathuseChatComposerShortcuts.test.ts
More file actions
371 lines (314 loc) · 13.3 KB
/
Copy pathuseChatComposerShortcuts.test.ts
File metadata and controls
371 lines (314 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { ref } from 'vue'
import { useChatComposerShortcuts } from './useChatComposerShortcuts'
import type { ChatSlashCommand } from './useChatSlashCommands'
import type { ChatMessage, ChatPendingItem } from '@/types/chat'
// The composable gates the Alt+Arrow queue chords on caret position via
// `e.target instanceof HTMLTextAreaElement`. The unit env is `node` (no DOM), so
// register a minimal stand-in as the global the composable checks against.
class FakeTextArea {
value = ''
selectionStart = 0
selectionEnd = 0
setSelectionRange(start: number, end: number) {
this.selectionStart = start
this.selectionEnd = end
}
}
beforeEach(() => {
;(globalThis as unknown as { HTMLTextAreaElement: unknown }).HTMLTextAreaElement = FakeTextArea
})
afterEach(() => {
delete (globalThis as unknown as { HTMLTextAreaElement?: unknown }).HTMLTextAreaElement
})
function field(value: string, caret: 'start' | 'end' | 'middle'): FakeTextArea
function field(value: string, caret: number, end?: number): FakeTextArea
function field(value: string, caret: 'start' | 'end' | 'middle' | number, end?: number): FakeTextArea {
const ta = new FakeTextArea()
ta.value = value
const pos = typeof caret === 'number'
? caret
: caret === 'start' ? 0 : caret === 'end' ? value.length : Math.floor(value.length / 2)
ta.selectionStart = pos
ta.selectionEnd = typeof end === 'number' ? end : pos
return ta
}
function harness(over: {
inputText?: string
pendingQueue?: ChatPendingItem[]
canQueueMore?: boolean
safari?: boolean
slashOpen?: boolean
filteredSlashCmds?: ChatSlashCommand[]
cancelMessageEdit?: () => boolean
} = {}) {
const inputText = ref(over.inputText ?? '')
const spies = {
popPendingTail: vi.fn(() => true),
enqueuePendingInput: vi.fn(() => true),
sendCurrentInput: vi.fn(),
autoResizeTextarea: vi.fn(),
handleSlashInput: vi.fn(),
closeSlashMenu: vi.fn(),
completeSlashCmd: vi.fn(),
activateSlashCmd: vi.fn(),
cancelMessageEdit: vi.fn(over.cancelMessageEdit ?? (() => false)),
}
const api = useChatComposerShortcuts({
inputText,
composing: ref(false),
messages: ref<ChatMessage[]>([]),
pendingQueue: ref<ChatPendingItem[]>(over.pendingQueue ?? []),
canQueueMore: ref(over.canQueueMore ?? true),
slashOpen: ref(over.slashOpen ?? false),
slashIdx: ref(0),
filteredSlashCmds: ref(over.filteredSlashCmds ?? []),
isStreaming: ref(false),
isSafariWebKit: () => over.safari ?? false,
...spies,
})
return { api, inputText, spies }
}
function keydown(opts: {
key: string
altKey?: boolean
shiftKey?: boolean
isComposing?: boolean
keyCode?: number
target: unknown
}): KeyboardEvent {
return {
key: opts.key,
altKey: opts.altKey ?? false,
shiftKey: opts.shiftKey ?? false,
isComposing: opts.isComposing ?? false,
keyCode: opts.keyCode ?? 0,
target: opts.target,
preventDefault: vi.fn(),
} as unknown as KeyboardEvent
}
function inputEvent(inputType: string, target: unknown): InputEvent {
return {
inputType,
target,
} as unknown as InputEvent
}
const QUEUE = [{ id: 'q1', text: 'queued' }] as unknown as ChatPendingItem[]
describe('useChatComposerShortcuts', () => {
describe('Slash completion safety', () => {
const coding = {
name: '/coding',
cmd: '/coding',
label: '/coding',
desc: 'Toggle Coding mode',
aliases: [],
execution: { action: 'coding.mode' },
}
it('uses Tab only to complete the active candidate', () => {
const { api, spies } = harness({
inputText: '/co',
slashOpen: true,
filteredSlashCmds: [coding],
})
const e = keydown({ key: 'Tab', target: field('/co', 'end') })
api.onTextareaKeydown(e)
expect(spies.completeSlashCmd).toHaveBeenCalledWith(coding)
expect(spies.activateSlashCmd).not.toHaveBeenCalled()
expect(e.preventDefault).toHaveBeenCalled()
})
it('routes Enter through exact-aware activation instead of executing directly', () => {
const { api, spies } = harness({
inputText: '/co',
slashOpen: true,
filteredSlashCmds: [coding],
})
const e = keydown({ key: 'Enter', target: field('/co', 'end') })
api.onTextareaKeydown(e)
expect(spies.activateSlashCmd).toHaveBeenCalledWith(coding)
expect(spies.completeSlashCmd).not.toHaveBeenCalled()
expect(spies.sendCurrentInput).not.toHaveBeenCalled()
expect(e.preventDefault).toHaveBeenCalled()
})
})
describe('IME composition guard', () => {
it('does not send on Enter while the IME is composing (isComposing)', () => {
const { api, spies } = harness({ inputText: '你好' })
api.onTextareaKeydown(keydown({ key: 'Enter', isComposing: true, target: field('你好', 'end') }))
expect(spies.sendCurrentInput).not.toHaveBeenCalled()
})
it('does not send on Enter during legacy keyCode 229 composition', () => {
const { api, spies } = harness({ inputText: '你好' })
api.onTextareaKeydown(keydown({ key: 'Enter', keyCode: 229, target: field('你好', 'end') }))
expect(spies.sendCurrentInput).not.toHaveBeenCalled()
})
it('sends on a plain Enter when not composing', () => {
const { api, spies } = harness({ inputText: 'hi' })
const e = keydown({ key: 'Enter', target: field('hi', 'end') })
api.onTextareaKeydown(e)
expect(spies.sendCurrentInput).toHaveBeenCalledOnce()
expect(e.preventDefault).toHaveBeenCalled()
})
})
describe('Alt+Arrow queue chords are caret-gated (preserve macOS paragraph nav)', () => {
it('enqueues on Alt+ArrowDown only when the caret is at the end', () => {
const { api, spies } = harness({ inputText: 'line1\nline2', canQueueMore: true })
const atEnd = keydown({ key: 'ArrowDown', altKey: true, target: field('line1\nline2', 'end') })
api.onTextareaKeydown(atEnd)
expect(spies.enqueuePendingInput).toHaveBeenCalledWith('line1\nline2')
expect(atEnd.preventDefault).toHaveBeenCalled()
spies.enqueuePendingInput.mockClear()
const midDraft = keydown({ key: 'ArrowDown', altKey: true, target: field('line1\nline2', 'middle') })
api.onTextareaKeydown(midDraft)
expect(spies.enqueuePendingInput).not.toHaveBeenCalled()
expect(midDraft.preventDefault).not.toHaveBeenCalled() // native Option+ArrowDown paragraph move survives
})
it('pops the queue on Alt+ArrowUp only when the caret is at the start', () => {
const { api, spies } = harness({ pendingQueue: QUEUE })
const atStart = keydown({ key: 'ArrowUp', altKey: true, target: field('', 'start') })
api.onTextareaKeydown(atStart)
expect(spies.popPendingTail).toHaveBeenCalledOnce()
expect(atStart.preventDefault).toHaveBeenCalled()
spies.popPendingTail.mockClear()
const midDraft = keydown({ key: 'ArrowUp', altKey: true, target: field('line1\nline2', 'middle') })
api.onTextareaKeydown(midDraft)
expect(spies.popPendingTail).not.toHaveBeenCalled()
expect(midDraft.preventDefault).not.toHaveBeenCalled() // native Option+ArrowUp paragraph move survives
})
})
describe('Safari textarea undo guard', () => {
it('repairs Safari historyUndo when undoing a deletion clears the whole draft', () => {
const { api, inputText } = harness({ inputText: 'hello world', safari: true })
const ta = field('hello world', 6, 'hello world'.length)
api.onTextareaBeforeInput(inputEvent('deleteContentBackward', ta))
ta.value = 'hello '
ta.setSelectionRange(6, 6)
inputText.value = ta.value
api.onTextareaInput(inputEvent('deleteContentBackward', ta))
api.onTextareaBeforeInput(inputEvent('historyUndo', ta))
ta.value = ''
ta.setSelectionRange(0, 0)
inputText.value = ta.value
api.onTextareaInput(inputEvent('historyUndo', ta))
expect(ta.value).toBe('hello world')
expect(inputText.value).toBe('hello world')
expect(ta.selectionStart).toBe(6)
expect(ta.selectionEnd).toBe('hello world'.length)
})
it('keeps the first value in a repeated-delete group', () => {
const { api, inputText } = harness({ inputText: 'hello world', safari: true })
const ta = field('hello world', 'end')
api.onTextareaBeforeInput(inputEvent('deleteContentBackward', ta))
ta.value = 'hello worl'
ta.setSelectionRange(10, 10)
inputText.value = ta.value
api.onTextareaInput(inputEvent('deleteContentBackward', ta))
api.onTextareaBeforeInput(inputEvent('deleteContentBackward', ta))
ta.value = 'hello wor'
ta.setSelectionRange(9, 9)
inputText.value = ta.value
api.onTextareaInput(inputEvent('deleteContentBackward', ta))
api.onTextareaBeforeInput(inputEvent('historyUndo', ta))
ta.value = ''
ta.setSelectionRange(0, 0)
inputText.value = ta.value
api.onTextareaInput(inputEvent('historyUndo', ta))
expect(ta.value).toBe('hello world')
expect(inputText.value).toBe('hello world')
expect(ta.selectionStart).toBe('hello world'.length)
expect(ta.selectionEnd).toBe('hello world'.length)
})
it('does not resurrect a stale deletion snapshot after an unrelated draft change', () => {
const { api, inputText } = harness({ inputText: 'hello world', safari: true })
const ta = field('hello world', 'end')
api.onTextareaBeforeInput(inputEvent('deleteContentBackward', ta))
ta.value = 'hello worl'
ta.setSelectionRange(10, 10)
inputText.value = ta.value
api.onTextareaInput(inputEvent('deleteContentBackward', ta))
ta.value = 'new draft'
ta.setSelectionRange('new draft'.length, 'new draft'.length)
inputText.value = ta.value
api.onTextareaBeforeInput(inputEvent('historyUndo', ta))
ta.value = ''
ta.setSelectionRange(0, 0)
inputText.value = ta.value
api.onTextareaInput(inputEvent('historyUndo', ta))
expect(ta.value).toBe('')
expect(inputText.value).toBe('')
})
it('leaves non-Safari native undo untouched', () => {
const { api, inputText } = harness({ inputText: 'hello world', safari: false })
const ta = field('hello world', 6, 'hello world'.length)
api.onTextareaBeforeInput(inputEvent('deleteContentBackward', ta))
ta.value = 'hello '
ta.setSelectionRange(6, 6)
inputText.value = ta.value
api.onTextareaInput(inputEvent('deleteContentBackward', ta))
api.onTextareaBeforeInput(inputEvent('historyUndo', ta))
ta.value = ''
ta.setSelectionRange(0, 0)
inputText.value = ta.value
api.onTextareaInput(inputEvent('historyUndo', ta))
expect(ta.value).toBe('')
expect(inputText.value).toBe('')
})
})
})
describe('Escape and message edits', () => {
it('cancels an uncommitted edit instead of clearing the composer', () => {
// #1372: edit mode empties the transcript on the first click, and Escape
// used to clear the draft and leave that empty state on screen. Cancelling
// the edit is the whole action — the composer is restored by the cancel
// itself, so Escape must not go on to blank it.
const { api, inputText, spies } = harness({
inputText: 'B',
cancelMessageEdit: () => true,
})
const e = keydown({ key: 'Escape', target: field('B', 'end') })
api.onTextareaKeydown(e)
expect(spies.cancelMessageEdit).toHaveBeenCalledOnce()
expect(e.preventDefault).toHaveBeenCalledOnce()
expect(inputText.value).toBe('B')
})
it('offers the cancel even when the composer has been emptied by hand', () => {
// The old guard required a non-empty draft, so clearing the box first left
// no way out of the truncated transcript at all.
const { api, spies } = harness({ inputText: '', cancelMessageEdit: () => true })
api.onTextareaKeydown(keydown({ key: 'Escape', target: field('', 'end') }))
expect(spies.cancelMessageEdit).toHaveBeenCalledOnce()
})
it('offers the cancel before the pending-queue guard', () => {
const { api, spies } = harness({
inputText: 'B',
pendingQueue: QUEUE,
cancelMessageEdit: () => true,
})
const e = keydown({ key: 'Escape', target: field('B', 'end') })
api.onTextareaKeydown(e)
expect(spies.cancelMessageEdit).toHaveBeenCalledOnce()
expect(e.preventDefault).toHaveBeenCalledOnce()
})
it('still clears the draft when there is no edit to cancel', () => {
const { api, inputText, spies } = harness({ inputText: 'just a draft' })
const e = keydown({ key: 'Escape', target: field('just a draft', 'end') })
api.onTextareaKeydown(e)
expect(spies.cancelMessageEdit).toHaveBeenCalledOnce()
expect(inputText.value).toBe('')
expect(e.preventDefault).toHaveBeenCalledOnce()
})
it('leaves the slash menu Escape alone', () => {
// Escape closes the menu first; an edit underneath it is not touched until
// the menu is out of the way.
const { api, spies } = harness({
inputText: '/co',
slashOpen: true,
filteredSlashCmds: [
{ name: '/coding', cmd: '/coding', label: '/coding', desc: '' },
] as unknown as ChatSlashCommand[],
cancelMessageEdit: () => true,
})
api.onTextareaKeydown(keydown({ key: 'Escape', target: field('/co', 'end') }))
expect(spies.closeSlashMenu).toHaveBeenCalledOnce()
expect(spies.cancelMessageEdit).not.toHaveBeenCalled()
})
})