cat / goq
clone

turns a linux box into a private steam streaming appliance: plug in, tap your key, play from another computer

fix/architecture-review-findings
goq / portal / keyboard-map.test.mjs
keyboard-map.test.mjs 3.3K
1import assert from 'node:assert/strict';2import test from 'node:test';34import {5 keyboardInputForEvent,6 mapKey,7} from './keyboard-map.mjs';89test('forwards the bounded physical code contract including extended keys', () => {10 for (const code of [11 'KeyA',12 'Digit0',13 'Minus',14 'BracketLeft',15 'IntlBackslash',16 'IntlRo',17 'IntlYen',18 'ShiftLeft',19 'ShiftRight',20 'ControlLeft',21 'ControlRight',22 'AltLeft',23 'AltRight',24 'MetaLeft',25 'MetaRight',26 'ArrowUp',27 'Insert',28 'PrintScreen',29 ...Array.from({ length: 12 }, (_, index) => `F${index + 1}`),30 ]) {31 assert.equal(mapKey({ code, key: 'layout-dependent' }), code, code);32 }33});3435test('physical codes preserve client key positions across keyboard layouts', () => {36 assert.equal(mapKey({ code: 'KeyE', key: 'é' }), 'KeyE');37 assert.equal(mapKey({ code: 'Digit2', key: 'é' }), 'Digit2');38 assert.equal(mapKey({ code: 'KeyQ', key: 'a' }), 'KeyQ');39 assert.equal(mapKey({ code: 'AltRight', key: 'AltGraph' }), 'AltRight');40});4142test('legacy missing-code events retain the compatible named and ASCII map', () => {43 for (const [key, expected] of [44 ['ArrowUp', 'Up'],45 [' ', 'Space'],46 ['Control', 'Control'],47 ['PageDown', 'PageDown'],48 ['Insert', 'Insert'],49 ['PrintScreen', 'PrintScreen'],50 ['F1', 'F1'],51 ['F12', 'F12'],52 ['!', '!'],53 ['a', 'a'],54 ]) {55 assert.equal(mapKey({ code: '', key }), expected, key);56 }57});5859test('rejects unsupported physical and logical keys without guessing', () => {60 for (const event of [61 { code: 'F13', key: 'F13' },62 { code: 'CapsLock', key: 'CapsLock' },63 { code: 'Numpad1', key: '1' },64 { code: 'AudioVolumeUp', key: 'AudioVolumeUp' },65 { code: '', key: 'é' },66 { code: '', key: '😀' },67 { code: '', key: '' },68 null,69 ]) {70 assert.equal(mapKey(event), null);71 }72});7374test('production keyboard capability forwards a physical code without Text', () => {75 assert.deepEqual(76 keyboardInputForEvent(77 { code: 'KeyE', key: 'é' },78 { keyboard: true, text: false },79 ),80 { type: 'key', key: 'KeyE' },81 );82});8384test('hypothetical operational Text capability remains exclusive with keyboard', () => {85 const both = { keyboard: true, text: true };86 assert.deepEqual(87 keyboardInputForEvent({ code: 'F5', key: 'F5' }, both),88 { type: 'key', key: 'F5' },89 );90 assert.deepEqual(91 keyboardInputForEvent({ code: 'Unknown', key: 'é' }, both),92 { type: 'text', text: 'é' },93 );94});9596test('hypothetical Text-only capability accepts one scalar and rejects unsafe input', () => {97 const textOnly = { keyboard: false, text: true };98 assert.deepEqual(99 keyboardInputForEvent({ code: '', key: 'é' }, textOnly),100 { type: 'text', text: 'é' },101 );102 assert.deepEqual(103 keyboardInputForEvent({ code: '', key: '😀' }, textOnly),104 { type: 'text', text: '😀' },105 );106 for (const event of [107 { code: '', key: 'a', ctrlKey: true },108 { code: '', key: 'a', altKey: true },109 { code: '', key: 'a', metaKey: true },110 { code: '', key: 'a', isComposing: true },111 { code: '', key: 'Enter' },112 { code: '', key: '\u007f' },113 { code: '', key: '\ud800' },114 ]) {115 assert.equal(keyboardInputForEvent(event, textOnly), null);116 }117 assert.equal(118 keyboardInputForEvent({ code: 'KeyA', key: 'a' }, {}),119 null,120 );121});