134 lines
6.4 KiB
JavaScript
134 lines
6.4 KiB
JavaScript
// Pure logic of the font zoom (zoom.js) and its place in the layout record. Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/zoom.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { clamp, stepValue, format, cleanMap, wheelDir, wheelDelta, wheelSteps, WHEEL_STEP, zoom } from './zoom.js';
|
|
import { serialize, migrate, restore } from './layout.js';
|
|
|
|
test('clamp keeps 8..40 px and 50..300 %', () => {
|
|
assert.equal(clamp(3, 'px'), 8);
|
|
assert.equal(clamp(99, 'px'), 40);
|
|
assert.equal(clamp(13.5, 'px'), 13.5);
|
|
assert.equal(clamp(10, 'pct'), 50);
|
|
assert.equal(clamp(1000, 'pct'), 300);
|
|
assert.equal(clamp(NaN, 'px'), null);
|
|
assert.equal(clamp('12', 'px'), null);
|
|
});
|
|
|
|
test('steps: 1 px, 5 % on the grid, stopping at the limits', () => {
|
|
assert.equal(stepValue(13, 1, 'px'), 14);
|
|
assert.equal(stepValue(13.5, -1, 'px'), 12.5);
|
|
assert.equal(stepValue(40, 1, 'px'), 40);
|
|
assert.equal(stepValue(8, -1, 'px'), 8);
|
|
assert.equal(stepValue(100, 1, 'pct'), 105);
|
|
assert.equal(stepValue(102, 1, 'pct'), 105);
|
|
assert.equal(stepValue(50, -1, 'pct'), 50);
|
|
assert.equal(stepValue(300, 1, 'pct'), 300);
|
|
let v = 100; for (let i = 0; i < 100; i++) v = stepValue(v, 1, 'pct'); assert.equal(v, 300);
|
|
});
|
|
|
|
test('format and wheel direction', () => {
|
|
assert.equal(format(14, 'px'), '14 px');
|
|
assert.equal(format(120, 'pct'), '120 %');
|
|
assert.equal(format(13.54, 'px'), '13.5 px');
|
|
assert.equal(wheelDir({ deltaY: -100 }), 1);
|
|
assert.equal(wheelDir({ deltaY: 100 }), -1);
|
|
assert.equal(wheelDir({ deltaY: 0 }), 0);
|
|
});
|
|
|
|
test('cleanMap drops junk and clamps', () => {
|
|
assert.deepEqual(cleanMap({ editor: 99, claude: 120, bad: 'x', 'a b': 100 }, { editor: 'px' }), { editor: 40, claude: 120 });
|
|
assert.deepEqual(cleanMap(null), {});
|
|
assert.deepEqual(cleanMap([1]), {});
|
|
});
|
|
|
|
test('the manager: set / get / reset / snapshot / restore with a fake store', () => {
|
|
const data = {};
|
|
zoom.use({ get: (k, d) => (k in data ? data[k] : d), set: (k, v) => { data[k] = v; } });
|
|
const applied = [];
|
|
zoom.define('editor', { kind: 'px', def: () => 13.5, apply: (v) => applied.push(['editor', v]) });
|
|
zoom.define('claude', { kind: 'pct', apply: (v) => applied.push(['claude', v]) });
|
|
assert.equal(zoom.get('editor'), 13.5);
|
|
assert.equal(zoom.step('editor', 1), 14.5);
|
|
assert.equal(zoom.step('claude', 1), 105);
|
|
assert.deepEqual(zoom.snapshot(), { editor: 14.5, claude: 105 });
|
|
assert.equal(data['zoom:editor'], 14.5);
|
|
zoom.reset('editor');
|
|
assert.equal(zoom.get('editor'), 13.5);
|
|
assert.deepEqual(zoom.snapshot(), { claude: 105 });
|
|
zoom.restore({ editor: 20, claude: 9999, unknown: 5 });
|
|
assert.equal(zoom.get('editor'), 20);
|
|
assert.equal(zoom.get('claude'), 300);
|
|
assert.equal(zoom.set('editor', 1), 8); // out of range values are clamped
|
|
zoom.resetAll();
|
|
assert.deepEqual(zoom.snapshot(), {});
|
|
assert.deepEqual(applied.at(-1), ['claude', 100]);
|
|
});
|
|
|
|
test('layout record: zoom is kept, cleaned, optional (older records) and restored', async () => {
|
|
const r = serialize({ zoom: { editor: 99, terminal: 15, claude: 130, run: 10, bad: 'x' } }, 1);
|
|
assert.deepEqual(r.zoom, { editor: 40, terminal: 15, claude: 130, run: 50 });
|
|
assert.deepEqual(migrate(JSON.parse(JSON.stringify(r))), r);
|
|
// a record written before zoom existed
|
|
const old = JSON.parse(JSON.stringify(serialize({}, 1))); delete old.zoom;
|
|
assert.deepEqual(migrate(old).zoom, {});
|
|
let got = null;
|
|
await restore(r, { toolIds: () => [], zoom: (m) => { got = m; } });
|
|
assert.deepEqual(got, r.zoom);
|
|
got = null;
|
|
await restore(migrate(old), { toolIds: () => [], zoom: (m) => { got = m; } });
|
|
assert.equal(got, null); // nothing saved: the host is not asked
|
|
});
|
|
|
|
test('the wheel: a notch is one step, a touchpad\'s small deltas add up, Shift (deltaX) and line mode are read', () => {
|
|
assert.equal(wheelDelta({ deltaY: 0, deltaX: -100, shiftKey: true }), 100);
|
|
assert.equal(wheelDelta({ deltaY: 3, deltaMode: 1 }), -99);
|
|
assert.equal(wheelDelta({ deltaY: 0 }), 0);
|
|
let acc = { sum: 0, at: 0, fresh: true };
|
|
assert.equal(wheelSteps(acc, 100, 1000), 1); // one mouse notch
|
|
for (let i = 0; i < 5; i++) assert.equal(wheelSteps(acc, 100, 1100 + i * 30), 1); // the next ones: one each, never two
|
|
assert.equal(wheelSteps(acc, 53, 1300), 1); // a smaller notch (WebKit / Firefox lines)
|
|
acc = { sum: 0, at: 0, fresh: true };
|
|
let n = 0; for (let i = 0; i < 30; i++) n += wheelSteps(acc, 4, 2000 + i * 8); // a touchpad: 30 x 4 px = 120 px
|
|
assert.ok(n >= 2 && n <= 3, 'touchpad steps ' + n);
|
|
acc = { sum: 0, at: 0, fresh: true };
|
|
assert.equal(wheelSteps(acc, 2, 3000), 1); // the first event of a turn always does something
|
|
assert.equal(wheelSteps(acc, 2, 3010), 0);
|
|
assert.equal(wheelSteps(acc, -2, 3020), -1); // the other way: at once
|
|
assert.equal(wheelSteps(acc, 1000, 3030), 3); // a fling is capped
|
|
});
|
|
|
|
test('live changes (wheel / keys) are drawn once per frame and saved after', async () => {
|
|
const data = {};
|
|
const frames = [];
|
|
globalThis.requestAnimationFrame = (fn) => { frames.push(fn); return frames.length; };
|
|
globalThis.cancelAnimationFrame = () => {};
|
|
zoom.use({ get: (k, d) => (k in data ? data[k] : d), set: (k, v) => { data[k] = v; } });
|
|
const applied = [];
|
|
zoom.define('run', { kind: 'pct', apply: (v) => applied.push(v) });
|
|
zoom.set('run', null);
|
|
applied.length = 0;
|
|
for (let i = 0; i < 5; i++) zoom.step('run', 1, { live: true });
|
|
assert.equal(zoom.get('run'), 125);
|
|
assert.deepEqual(applied, []); // nothing drawn yet
|
|
frames.splice(0).forEach((f) => f());
|
|
assert.deepEqual(applied, [125]); // one draw for five steps
|
|
assert.equal(data['zoom:run'] ?? null, null); // saved a moment later
|
|
zoom.flushSave();
|
|
assert.equal(data['zoom:run'], 125);
|
|
delete globalThis.requestAnimationFrame;
|
|
delete globalThis.cancelAnimationFrame;
|
|
});
|
|
|
|
test('the whole IDE zoom is its own value, not in the layout record of a project', () => {
|
|
const data = {};
|
|
zoom.use({ get: (k, d) => (k in data ? data[k] : d), set: (k, v) => { data[k] = v; } });
|
|
assert.equal(zoom.get('ui'), 100);
|
|
assert.equal(zoom.step('ui', 1), 110);
|
|
assert.equal(zoom.step('ui', -3), 80);
|
|
assert.equal(data['zoom:ui'], 80);
|
|
assert.equal('ui' in zoom.snapshot(), false);
|
|
zoom.restore({ ui: 200 });
|
|
assert.equal(zoom.get('ui'), 80);
|
|
zoom.resetAll();
|
|
assert.equal(zoom.get('ui'), 100);
|
|
});
|