release / linux-x64 (push) Failing after 59s
ci / linux-x64 (push) Failing after 1m9s
ci / linux-arm64-cross (push) Failing after 4m15s
release / linux-arm64 (push) Successful in 4m36s
release / appimage-startup (push) Skipped
release / publish (push) Skipped
61 lines
3.8 KiB
JavaScript
61 lines
3.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createRequire } from 'node:module';
|
|
import { KeyTracker, KeyChordRecorder, matchKeydown, keyupReleases } from '../app/renderer/js/keybind-match.js';
|
|
const require = createRequire(import.meta.url);
|
|
const schema = require('../app/main/keybind-schema.js');
|
|
|
|
const key = (code, mods = {}) => ({ code, ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, repeat: false, ...mods });
|
|
const bind = (id, code, mods = {}) => ({ id, code, ctrl: false, alt: false, shift: false, meta: false, ...mods });
|
|
|
|
|
|
test('exact match: modifiers must be equal and no other key held', () => {
|
|
const tracker = new KeyTracker();
|
|
const bindings = [bind('f9', 'F9'), bind('cf9', 'F9', { ctrl: true })];
|
|
tracker.down('KeyW'); tracker.down('F9');
|
|
assert.deepEqual(matchKeydown(bindings, key('F9'), { exact: true, pressed: tracker.pressed }), [], 'W is also held');
|
|
tracker.up('KeyW');
|
|
assert.deepEqual(matchKeydown(bindings, key('F9'), { exact: true, pressed: tracker.pressed }).map((b) => b.id), ['f9']);
|
|
assert.deepEqual(matchKeydown(bindings, key('F9', { ctrlKey: true, shiftKey: true }), { exact: true, pressed: tracker.pressed }), []);
|
|
});
|
|
|
|
test('non-exact match fires despite other keys, and only the most specific binding', () => {
|
|
const pressed = new Set(['KeyW', 'F9']);
|
|
const bindings = [bind('f9', 'F9'), bind('cf9', 'F9', { ctrl: true })];
|
|
assert.deepEqual(matchKeydown(bindings, key('F9', { shiftKey: true }), { exact: false, pressed }).map((b) => b.id), ['f9']);
|
|
assert.deepEqual(matchKeydown(bindings, key('F9', { ctrlKey: true, shiftKey: true }), { exact: false, pressed }).map((b) => b.id), ['cf9']);
|
|
});
|
|
|
|
test('held bindings release on their key or any of their modifiers going up', () => {
|
|
const b = bind('x', 'F9', { ctrl: true });
|
|
assert.equal(keyupReleases(b, key('KeyW', { ctrlKey: true })), false);
|
|
assert.equal(keyupReleases(b, key('ControlLeft')), true);
|
|
assert.equal(keyupReleases(b, key('F9', { ctrlKey: true })), true);
|
|
});
|
|
|
|
test('keyboard recorder completes on a full down/up chord and cancels on Escape', () => {
|
|
const r = new KeyChordRecorder();
|
|
assert.equal(r.keydown(key('ControlLeft', { ctrlKey: true })).kind, 'update');
|
|
assert.equal(r.keydown(key('KeyK', { ctrlKey: true })).chord.code, 'KeyK');
|
|
assert.equal(r.keyup(key('KeyK', { ctrlKey: true })).kind, 'none', 'Ctrl still held');
|
|
assert.deepEqual(r.keyup(key('ControlLeft')), { kind: 'complete', chord: { code: 'KeyK', ctrl: true, alt: false, shift: false, meta: false } });
|
|
assert.equal(new KeyChordRecorder().keydown(key('Escape')).kind, 'cancel');
|
|
const modifiersOnly = new KeyChordRecorder();
|
|
modifiersOnly.keydown(key('ShiftLeft', { shiftKey: true }));
|
|
assert.equal(modifiersOnly.keyup(key('ShiftLeft')).kind, 'update', 'a modifier alone is not a binding');
|
|
});
|
|
|
|
test('variant expansion never takes another binding\'s exact chord and respects the helper limit', () => {
|
|
const a = { id: 'a', code: 'F9', ctrl: false, alt: false, shift: false, meta: false };
|
|
const b = { id: 'b', code: 'F9', ctrl: true, alt: false, shift: false, meta: false };
|
|
const { registrations, variantOf } = schema.expandRegistrations([a, b], { exact: false });
|
|
const chords = registrations.map(schema.chordKey);
|
|
assert.equal(new Set(chords).size, chords.length);
|
|
assert.equal(variantOf.get(registrations.find((r) => schema.chordKey(r) === 'C+F9').id), 'b');
|
|
assert.equal(variantOf.get(registrations.find((r) => schema.chordKey(r) === 'CS+F9').id), 'b', 'Ctrl+Shift+F9 is closer to Ctrl+F9');
|
|
const many = Array.from({ length: 10 }, (_, i) => ({ ...a, id: `k${i}`, code: `F${i + 1}` }));
|
|
const limited = schema.expandRegistrations(many, { exact: false });
|
|
assert.equal(limited.registrations.length, 64);
|
|
assert.ok(limited.variants.wanted > limited.variants.registered);
|
|
});
|