1// Exercise the production key handler without a browser or dependencies.
2import assert from 'node:assert/strict';
3import { readFileSync } from 'node:fs';
4import { test } from 'node:test';
5import { runInNewContext } from 'node:vm';
6const source = readFileSync(new URL('../static/js/ermite.js', import.meta.url), 'utf8');
7function keyboard({ owner = true, off = false, typing = false, dialog = false } = {}) {
8 let handler;
9 const fired = [];
10 const timers = new Map();
11 let timerID = 0;
12 runInNewContext(source.slice(source.indexOf(' const single ='), source.indexOf(' // 2. A [data-fire]')), {
13 addEventListener: (_, fn) => { handler = fn; },
14 window: { ermiteKeys: { blocks: () => off } },
15 ermite: { fire: name => fired.push(name) },
16 $: selector => (owner && selector === '[data-inbox]') || (dialog && selector === 'dialog[open]'),
17 closeMenus: () => false,
18 clearTimeout: id => timers.delete(id),
19 setTimeout: fn => { timers.set(++timerID, fn); return timerID; },
20 });
21 return { fired, expireTimers() {
22 const callbacks = [...timers.values()];
23 timers.clear();
24 for (const callback of callbacks) callback();
25 }, press(key, shiftKey = false) {
26 handler({ key, shiftKey, target: { closest: selector => typing && selector.startsWith('input'), blur() {} }, preventDefault() {} });
27 } };
28}
29test('sections require g and have no old t alias', () => {
30 const k = keyboard();
31 for (const key of ['s', 't', 'c', 'r', 'm', 'i']) k.press(key);
32 assert.deepEqual(k.fired, []);
33 for (const key of ['s', 'f', 'c', 'b', 'm', 'i', 'r', 'a', 'h', 'g', 't']) { k.press('g'); k.press(key); }
34 assert.deepEqual(k.fired, ['s', 'f', 'c', 'b', 'm', 'i', 'r', 'a', 'gh', 'top']);
35});
36test('uppercase actions work with either reported casing and interrupt prefixes', () => {
37 for (const [key, action] of Object.entries({ G: 'bottom', M: 'merge', R: 'read', E: 'expandAll', I: 'inbox' })) {
38 for (const reported of [key, key.toLowerCase()]) {
39 const k = keyboard();
40 k.press('g'); k.press(reported, true); k.press(reported, true);
41 assert.deepEqual(k.fired, [action, action]);
42 }
43 }
44});
45test('uppercase actions respect preference, fields and dialogs', () => {
46 for (const options of [{ off: true }, { typing: true }, { dialog: true }]) {
47 const k = keyboard(options); k.press('g', true);
48 assert.deepEqual(k.fired, []);
49 }
50});
51
52test('Shift+I opens inbox while issues need the g i sequence', () => {
53 const k = keyboard();
54 k.press('I', true); k.press('i');
55 assert.deepEqual(k.fired, ['inbox']);
56 k.press('g'); k.press('i');
57 assert.deepEqual(k.fired, ['inbox', 'i']);
58});
59test('inbox shortcut respects preference, fields, dialogs, and owner target', () => {
60 for (const options of [{ off: true }, { typing: true }, { dialog: true }, { owner: false }]) {
61 const k = keyboard(options); k.press('I', true);
62 assert.deepEqual(k.fired, [], JSON.stringify(options));
63 }
64});
65test('shifted punctuation still opens help and the command palette', () => {
66 const k = keyboard();
67 k.press('?', true); k.press(':', true);
68 assert.deepEqual(k.fired, ['help', 'cmd']);
69});
70test('g prefix expires without scheduling real timers', () => {
71 const k = keyboard();
72 k.press('g'); k.expireTimers(); k.press('s');
73 assert.deepEqual(k.fired, []);
74 k.press('g'); k.press('s');
75 assert.deepEqual(k.fired, ['s']);
76});