53 lines
2.2 KiB
JavaScript
53 lines
2.2 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { ACTIONS, comboOf, eventCombo, actionFor, format, valid } from '../src/lib/shortcuts.js';
|
|
import { cleanPrefs } from '../src/lib/prefs.js';
|
|
import { createSimulation } from '../src/lib/graph.js';
|
|
|
|
const ev = (key, mods = {}, code = '') => ({ key, code, ctrlKey: !!mods.ctrl, metaKey: !!mods.meta, altKey: !!mods.alt, shiftKey: !!mods.shift });
|
|
|
|
test('every action has a unique valid default', () => {
|
|
const seen = new Set();
|
|
for (const a of ACTIONS) {
|
|
assert.ok(valid(a.combo), a.id);
|
|
assert.ok(!seen.has(a.combo), 'duplicate ' + a.combo);
|
|
seen.add(a.combo);
|
|
}
|
|
});
|
|
|
|
test('key events become combos; Ctrl and ⌘ are the same Mod', () => {
|
|
assert.equal(eventCombo(ev('k', { ctrl: true })), 'Mod+K');
|
|
assert.equal(eventCombo(ev('K', { meta: true, shift: true })), 'Mod+Shift+K');
|
|
assert.equal(eventCombo(ev('|', { ctrl: true, shift: true }, 'Backslash')), 'Mod+Shift+\\');
|
|
assert.equal(eventCombo(ev('k')), '');
|
|
assert.equal(eventCombo(ev('Control', { ctrl: true })), '');
|
|
});
|
|
|
|
test('a changed shortcut replaces the default, and only that', () => {
|
|
const custom = { tasks: 'Mod+Shift+Y' };
|
|
assert.equal(actionFor(ev('y', { ctrl: true, shift: true }), custom), 'tasks');
|
|
assert.equal(actionFor(ev('t', { ctrl: true, shift: true }), custom), null);
|
|
assert.equal(actionFor(ev('k', { ctrl: true }), custom), 'palette');
|
|
assert.equal(comboOf('tasks', custom), 'Mod+Shift+Y');
|
|
});
|
|
|
|
test('combos are shown for the platform', () => {
|
|
assert.equal(format('Mod+Shift+D', false), 'Ctrl ⇧ D');
|
|
assert.equal(format('Mod+Shift+D', true), '⌘ ⇧ D');
|
|
});
|
|
|
|
test('saved shortcuts are cleaned', () => {
|
|
assert.deepEqual(cleanPrefs({ shortcuts: { tasks: 'Mod+Shift+Y', bad: 'K', worse: 5 } }).shortcuts, { tasks: 'Mod+Shift+Y' });
|
|
assert.deepEqual(cleanPrefs({ shortcuts: [] }).shortcuts, {});
|
|
});
|
|
|
|
test('tickFor stops at its time budget, but always ticks once', () => {
|
|
const nodes = Array.from({ length: 4000 }, (_, i) => ({ id: 'n' + i }));
|
|
const sim = createSimulation().setData(nodes, []);
|
|
const a0 = sim.alpha;
|
|
const t = performance.now();
|
|
sim.tickFor(10000, 30);
|
|
assert.ok(performance.now() - t < 500);
|
|
assert.ok(sim.alpha < a0);
|
|
});
|