~bigbes/lethe

ref: a4e6ca5c5d5520261f0646c934559cb7914eaca6 lethe/web/src/lib/keyboard.test.ts -rw-r--r-- 7.7 KiB
a4e6ca5c — Eugene Blikh auth: fix OIDCVerifier injection wiring + OIDCDevStub root attachment a month ago
                                                                                
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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { createKeyboardController } from './keyboard'
import type { RouteName } from './keyboard'

// ── Helpers ──────────────────────────────────────────────────────────────────

function makeOpts() {
  const opts = {
    go:            vi.fn(),
    openPalette:   vi.fn(),
    closePalette:  vi.fn(),
    isPaletteOpen: vi.fn().mockReturnValue(false) as () => boolean,
    cursor: {
      move:     vi.fn(),
      activate: vi.fn(),
    },
  }
  return opts
}

type Opts = ReturnType<typeof makeOpts>

function makeCtrl(opts: Opts) {
  // Cast to KeyboardOpts — vi.fn() satisfies the shape at runtime.
  return createKeyboardController(opts as Parameters<typeof createKeyboardController>[0])
}

function makeKey(
  key: string,
  init: Partial<KeyboardEventInit> & { target?: EventTarget } = {},
): KeyboardEvent {
  const { target: tgt, ...rest } = init
  const e = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true, ...rest })
  if (tgt) {
    Object.defineProperty(e, 'target', { value: tgt, configurable: true })
  }
  return e
}

// ── Tests ────────────────────────────────────────────────────────────────────

describe('g + letter navigation', () => {
  beforeEach(() => {
    vi.useFakeTimers()
  })
  afterEach(() => {
    vi.useRealTimers()
  })

  it('g then h within 800 ms → go("home") once; gPending reset', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    vi.advanceTimersByTime(400) // still within 800 ms window
    ctrl.onKeyDown(makeKey('h'))

    expect(opts.go).toHaveBeenCalledOnce()
    expect(opts.go).toHaveBeenCalledWith('home' satisfies RouteName)

    // gPending should be cleared — a second 'h' without 'g' should NOT call go
    opts.go.mockClear()
    ctrl.onKeyDown(makeKey('h'))
    expect(opts.go).not.toHaveBeenCalled()
  })

  it('g then idle 1 s, then h → no call', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    vi.advanceTimersByTime(1000) // timeout fires, gPending cleared
    ctrl.onKeyDown(makeKey('h'))

    expect(opts.go).not.toHaveBeenCalled()
  })

  it('g then p → go("projects")', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    ctrl.onKeyDown(makeKey('p'))

    expect(opts.go).toHaveBeenCalledWith('projects' satisfies RouteName)
  })

  it('g then s → go("stats")', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    ctrl.onKeyDown(makeKey('s'))

    expect(opts.go).toHaveBeenCalledWith('stats' satisfies RouteName)
  })

  it('g then i → go("health")', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    ctrl.onKeyDown(makeKey('i'))

    expect(opts.go).toHaveBeenCalledWith('health' satisfies RouteName)
  })

  it('g then h while palette is open → no go() (palette guards prevent navigation mid-query)', () => {
    const opts = makeOpts()
    opts.isPaletteOpen = vi.fn().mockReturnValue(true)
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    ctrl.onKeyDown(makeKey('h'))

    expect(opts.go).not.toHaveBeenCalled()
  })

  it('g while target is <input> → no gPending set, follow-up h does nothing', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const input = document.createElement('input')
    ctrl.onKeyDown(makeKey('g', { target: input }))
    ctrl.onKeyDown(makeKey('h', { target: input }))

    expect(opts.go).not.toHaveBeenCalled()
  })

  it('teardown clears pending timer', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('g'))
    ctrl.teardown() // should not throw; timer cleared
    vi.advanceTimersByTime(1000)

    ctrl.onKeyDown(makeKey('h'))
    expect(opts.go).not.toHaveBeenCalled()
  })
})

describe('j / k cursor movement', () => {
  it('j while body is target → cursor.move(1)', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('j', { target: document.body }))

    expect(opts.cursor.move).toHaveBeenCalledWith(1)
  })

  it('k while body is target → cursor.move(-1)', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('k', { target: document.body }))

    expect(opts.cursor.move).toHaveBeenCalledWith(-1)
  })

  it('j while target is <input> → no cursor.move', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const input = document.createElement('input')
    ctrl.onKeyDown(makeKey('j', { target: input }))

    expect(opts.cursor.move).not.toHaveBeenCalled()
  })

  it('j while target is <textarea> → no cursor.move', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const ta = document.createElement('textarea')
    ctrl.onKeyDown(makeKey('j', { target: ta }))

    expect(opts.cursor.move).not.toHaveBeenCalled()
  })

  it('j while target is [contenteditable] → no cursor.move', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const div = document.createElement('div')
    div.setAttribute('contenteditable', 'true')
    ctrl.onKeyDown(makeKey('j', { target: div }))

    expect(opts.cursor.move).not.toHaveBeenCalled()
  })

  it('j while palette is open → no cursor.move', () => {
    const opts = makeOpts()
    opts.isPaletteOpen = vi.fn().mockReturnValue(true)
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('j', { target: document.body }))

    expect(opts.cursor.move).not.toHaveBeenCalled()
  })
})

describe('⌘K / Ctrl+K', () => {
  it('⌘K → openPalette called, preventDefault', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const e = makeKey('k', { metaKey: true })
    const spy = vi.spyOn(e, 'preventDefault')
    ctrl.onKeyDown(e)

    expect(opts.openPalette).toHaveBeenCalledOnce()
    expect(spy).toHaveBeenCalledOnce()
  })

  it('Ctrl+K → openPalette called, preventDefault', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    const e = makeKey('k', { ctrlKey: true })
    const spy = vi.spyOn(e, 'preventDefault')
    ctrl.onKeyDown(e)

    expect(opts.openPalette).toHaveBeenCalledOnce()
    expect(spy).toHaveBeenCalledOnce()
  })
})

describe('Escape', () => {
  it('Esc while isPaletteOpen()===true → closePalette called', () => {
    const opts = makeOpts()
    opts.isPaletteOpen = vi.fn().mockReturnValue(true)
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('Escape'))

    expect(opts.closePalette).toHaveBeenCalledOnce()
  })

  it('Esc while palette closed → closePalette NOT called', () => {
    const opts = makeOpts()
    // isPaletteOpen already returns false by default
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('Escape'))

    expect(opts.closePalette).not.toHaveBeenCalled()
  })
})

describe('Enter', () => {
  it('Enter while palette closed + target is body → cursor.activate()', () => {
    const opts = makeOpts()
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('Enter', { target: document.body }))

    expect(opts.cursor.activate).toHaveBeenCalledOnce()
  })

  it('Enter while palette open → cursor.activate NOT called', () => {
    const opts = makeOpts()
    opts.isPaletteOpen = vi.fn().mockReturnValue(true)
    const ctrl = makeCtrl(opts)

    ctrl.onKeyDown(makeKey('Enter', { target: document.body }))

    expect(opts.cursor.activate).not.toHaveBeenCalled()
  })
})