|
| 1 | +import type { KeyDown, KeyEvent } from '@bomb.sh/tty'; |
| 2 | +import type { Node } from './core.ts'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | +import { DispatchApi } from './dispatch.ts'; |
| 5 | +import { FocusApi } from './focus.ts'; |
| 6 | +import { createHost, type Host } from './host.ts'; |
| 7 | +import { KeyboardApi } from './keyboard.ts'; |
| 8 | + |
| 9 | +describe('keyboard dispatch', () => { |
| 10 | + test('routes keydown, keyup, and keyrepeat to the currently focused node', () => { |
| 11 | + const host = setupKeyboardHost(); |
| 12 | + const first = createFocusable(host); |
| 13 | + const second = createFocusable(host); |
| 14 | + const firstEvents: RecordedKeyEvent[] = []; |
| 15 | + const secondEvents: RecordedKeyEvent[] = []; |
| 16 | + recordKeyboard(first, firstEvents); |
| 17 | + recordKeyboard(second, secondEvents); |
| 18 | + setFocus(second); |
| 19 | + |
| 20 | + const events: KeyEvent[] = [ |
| 21 | + { type: 'keydown', key: 'a', code: 'a', text: 'a' }, |
| 22 | + { type: 'keyup', key: 'a', code: 'a' }, |
| 23 | + { type: 'keyrepeat', key: 'a', code: 'a', text: 'a' }, |
| 24 | + ]; |
| 25 | + for (const event of events) { |
| 26 | + expect(DispatchApi.at(host.root).dispatch(event).ok).toBe(true); |
| 27 | + } |
| 28 | + |
| 29 | + expect(firstEvents).toEqual([]); |
| 30 | + expect(secondEvents.map(({ node }) => node)).toEqual([second, second, second]); |
| 31 | + for (const [index, event] of events.entries()) { |
| 32 | + expect(secondEvents[index]?.event).toBe(event); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + test('returns ordinary dispatch success without a handled payload', () => { |
| 37 | + const host = setupKeyboardHost(); |
| 38 | + const target = createFocusable(host); |
| 39 | + recordKeyboard(target, []); |
| 40 | + |
| 41 | + expect(DispatchApi.at(host.root).dispatch(keydown('a', 'a'))).toEqual({ ok: true }); |
| 42 | + }); |
| 43 | + |
| 44 | + test('delegates non-key and malformed key-like events unchanged', () => { |
| 45 | + const host = setupKeyboardHost(); |
| 46 | + const delegated: unknown[] = []; |
| 47 | + DispatchApi.decorate(host.root, () => ({ |
| 48 | + dispatch(event) { |
| 49 | + delegated.push(event); |
| 50 | + return super.dispatch(event); |
| 51 | + }, |
| 52 | + })); |
| 53 | + |
| 54 | + DispatchApi.at(host.root).dispatch(keydown('a', 'a')); |
| 55 | + const others: unknown[] = [ |
| 56 | + { type: 'resize', width: 80, height: 24 }, |
| 57 | + { type: 'keydown', code: 'a' }, |
| 58 | + { type: 'keydown', key: 'a' }, |
| 59 | + { type: 'keypress', key: 'a', code: 'a' }, |
| 60 | + ]; |
| 61 | + for (const event of others) { |
| 62 | + DispatchApi.at(host.root).dispatch(event); |
| 63 | + } |
| 64 | + |
| 65 | + expect(delegated).toEqual(others); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +interface RecordedKeyEvent { |
| 70 | + node: Node; |
| 71 | + event: KeyEvent; |
| 72 | +} |
| 73 | + |
| 74 | +function setupKeyboardHost(): Host { |
| 75 | + const host = createHost(); |
| 76 | + DispatchApi.init(host.root); |
| 77 | + FocusApi.init(host.root); |
| 78 | + KeyboardApi.init(host.root); |
| 79 | + return host; |
| 80 | +} |
| 81 | + |
| 82 | +function createFocusable(host: Host): Node { |
| 83 | + const element = host.createElement('box'); |
| 84 | + host.insertBefore(host.element, element); |
| 85 | + setFocusable(element.node!); |
| 86 | + return element.node!; |
| 87 | +} |
| 88 | + |
| 89 | +function setFocus(node: Node): void { |
| 90 | + FocusApi.at(node).setFocus(node); |
| 91 | +} |
| 92 | + |
| 93 | +function setFocusable(node: Node): void { |
| 94 | + FocusApi.at(node).setFocusable(); |
| 95 | +} |
| 96 | + |
| 97 | +function keydown(code: KeyDown['code'], text?: string): KeyDown { |
| 98 | + return { |
| 99 | + type: 'keydown', |
| 100 | + key: text ?? code, |
| 101 | + code, |
| 102 | + ...(text === undefined ? {} : { text }), |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +function recordKeyboard(node: Node, events: RecordedKeyEvent[]): void { |
| 107 | + KeyboardApi.decorate(node, () => ({ |
| 108 | + keydown(event) { |
| 109 | + events.push({ node: this.node, event }); |
| 110 | + }, |
| 111 | + keyup(event) { |
| 112 | + events.push({ node: this.node, event }); |
| 113 | + }, |
| 114 | + keyrepeat(event) { |
| 115 | + events.push({ node: this.node, event }); |
| 116 | + }, |
| 117 | + })); |
| 118 | +} |
0 commit comments