Repository navigation
Expand file tree
/
Copy pathuse-paste-handler.test.ts
More file actions
192 lines (158 loc) · 5.91 KB
/
Copy pathuse-paste-handler.test.ts
File metadata and controls
192 lines (158 loc) · 5.91 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
import { describe, expect, test } from 'bun:test'
import type { KeyEvent } from '@opentui/core'
import { createPasteFallbackController } from './use-paste-handler'
import { isPasteFallbackKey } from '@magnitudedev/client-common'
function createKey(overrides: Partial<KeyEvent> = {}): KeyEvent {
return {
name: '',
sequence: '',
ctrl: false,
meta: false,
option: false,
shift: false,
...overrides,
} as KeyEvent
}
function defer<T>() {
let resolve!: (value: T) => void
const promise = new Promise<T>((r) => {
resolve = r
})
return { promise, resolve }
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
describe('isPasteFallbackKey', () => {
test('matches Ctrl+V and Cmd+V fallback shortcuts', () => {
expect(isPasteFallbackKey(createKey({ name: 'v', ctrl: true }))).toBe(true)
expect(isPasteFallbackKey(createKey({ name: 'V', ctrl: true }))).toBe(true)
expect(isPasteFallbackKey(createKey({ name: 'v', meta: true }))).toBe(true)
})
test('rejects ctrl+meta+v and option-modified variants', () => {
expect(isPasteFallbackKey(createKey({ name: 'v', ctrl: true, meta: true }))).toBe(false)
expect(isPasteFallbackKey(createKey({ name: 'v', ctrl: true, option: true }))).toBe(false)
expect(isPasteFallbackKey(createKey({ name: 'v', meta: true, option: true }))).toBe(false)
})
test('rejects non-v keys', () => {
expect(isPasteFallbackKey(createKey({ name: 'c', ctrl: true }))).toBe(false)
expect(isPasteFallbackKey(createKey({ name: 'c', meta: true }))).toBe(false)
})
})
describe('createPasteFallbackController', () => {
test('fallback-empty then late native inserts once from native', async () => {
const calls: Array<string | undefined> = []
const controller = createPasteFallbackController((text) => {
calls.push(text)
return Boolean(text)
}, 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
await sleep(20)
controller.handlePasteEvent({ text: 'native-late' })
await Promise.resolve()
expect(calls).toEqual([undefined, 'native-late'])
controller.dispose()
})
test('fallback-success then late native is dropped', async () => {
const calls: Array<string | undefined> = []
const controller = createPasteFallbackController((text) => {
calls.push(text)
return true
}, 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
await sleep(20)
controller.handlePasteEvent({ text: 'native-late' })
await Promise.resolve()
expect(calls).toEqual([undefined])
controller.dispose()
})
test('native while fallback in-flight is dropped when fallback later succeeds', async () => {
const calls: Array<string | undefined> = []
const fallback = defer<boolean>()
const controller = createPasteFallbackController((text) => {
calls.push(text)
if (text === undefined) return fallback.promise
return true
}, 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
await sleep(20)
controller.handlePasteEvent({ text: 'native-during-inflight' })
await Promise.resolve()
fallback.resolve(true)
await Promise.resolve()
await Promise.resolve()
expect(calls).toEqual([undefined])
controller.dispose()
})
test('native while fallback in-flight is replayed when fallback later resolves empty', async () => {
const calls: Array<string | undefined> = []
const fallback = defer<boolean>()
const controller = createPasteFallbackController((text) => {
calls.push(text)
if (text === undefined) return fallback.promise
return Boolean(text)
}, 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
await sleep(20)
controller.handlePasteEvent({ text: 'native-during-inflight-empty' })
await Promise.resolve()
fallback.resolve(false)
await Promise.resolve()
await Promise.resolve()
expect(calls).toEqual([undefined, 'native-during-inflight-empty'])
controller.dispose()
})
test('near-deadline native paste still yields single insertion', async () => {
const calls: Array<string | undefined> = []
const controller = createPasteFallbackController((text) => {
calls.push(text)
return Boolean(text)
}, 20)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
await sleep(18)
controller.handlePasteEvent({ text: 'native-near-deadline' })
await sleep(30)
expect(calls).toEqual(['native-near-deadline'])
controller.dispose()
})
test('native paste without key path still inserts', async () => {
const calls: Array<string | undefined> = []
const controller = createPasteFallbackController((text) => {
calls.push(text)
return true
}, 10)
controller.handlePasteEvent({ text: 'native-only' })
await Promise.resolve()
expect(calls).toEqual(['native-only'])
controller.dispose()
})
test('dispose cancels pending fallback timer', async () => {
const calls: Array<string | undefined> = []
const controller = createPasteFallbackController((text) => {
calls.push(text)
return true
}, 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
controller.dispose()
await sleep(25)
expect(calls).toEqual([])
})
test('callback updates do not reset pending attempt state', async () => {
const calls: Array<string | undefined> = []
let callback = (text?: string) => {
calls.push(text)
return Boolean(text)
}
const controller = createPasteFallbackController((text) => callback(text), 10)
controller.handlePasteKey(createKey({ name: 'v', ctrl: true }))
callback = (text?: string) => {
calls.push(text)
return Boolean(text)
}
await sleep(20)
controller.handlePasteEvent({ text: 'native-late' })
await Promise.resolve()
expect(calls).toEqual([undefined, 'native-late'])
controller.dispose()
})
})