import { test } from 'node:test';
import assert from 'node:assert/strict';
const { twoFinger, Touches } = await import('../js/gestures.js');
const close = (a, b, eps = 1e-9) => assert.ok(Math.abs(a - b) < eps, `${a} ≉ ${b}`);
test('spreading the fingers to twice the distance is scale 2, no turn, no tilt', () => {
const g = twoFinger({ x: 90, y: 100 }, { x: 110, y: 100 }, { x: 80, y: 100 }, { x: 120, y: 100 });
close(g.scale, 2); close(g.rot, 0); close(g.tilt, 0);
assert.deepEqual(g.mid, { x: 100, y: 100 }); assert.deepEqual(g.move, { x: 0, y: 0 });
});
test('turning the pair a quarter clockwise on screen (y down) is +90°', () => {
const g = twoFinger({ x: -10, y: 0 }, { x: 10, y: 0 }, { x: 0, y: -10 }, { x: 0, y: 10 });
close(g.rot, 90); close(g.scale, 1);
});
test('turns take the short way across ±180°', () => {
const g = twoFinger({ x: 0, y: 0 }, { x: -10, y: 1 }, { x: 0, y: 0 }, { x: -10, y: -1 });
assert.ok(Math.abs(g.rot) < 12, String(g.rot));
});
test('both fingers moving up together tilt by the smaller move; a vertical pinch does not', () => {
close(twoFinger({ x: 0, y: 100 }, { x: 50, y: 100 }, { x: 0, y: 80 }, { x: 50, y: 70 }).tilt, -20);
close(twoFinger({ x: 0, y: 100 }, { x: 0, y: 150 }, { x: 0, y: 90 }, { x: 0, y: 160 }).tilt, 0);
});
test('Touches reports a gesture only while two fingers are down', () => {
const t = new Touches();
t.down(1, { x: 0, y: 0 });
assert.equal(t.move(1, { x: 5, y: 0 }), null);
t.down(2, { x: 20, y: 0 });
const g = t.move(2, { x: 35, y: 0 }); // 15 → 30 apart
close(g.scale, 2);
assert.equal(t.move(9, { x: 0, y: 0 }), null, 'unknown pointer');
t.up(1);
assert.equal(t.size, 1);
assert.deepEqual(t.other(1), { id: 2, at: { x: 35, y: 0 } });
});
test('fingers moving up one event at a time still tilt (browsers send one move per finger)', () => {
const t = new Touches();
t.down(1, { x: 0, y: 100 }); t.down(2, { x: 50, y: 100 });
let tilt = 0;
for (let s = 1; s <= 5; s++) {
tilt += t.move(1, { x: 0, y: 100 - 10 * s }).tilt;
tilt += t.move(2, { x: 50, y: 100 - 10 * s }).tilt;
}
close(tilt, -50);
});
test('a vertical pinch, one finger at a time, does not tilt', () => {
const t = new Touches();
t.down(1, { x: 0, y: 100 }); t.down(2, { x: 0, y: 200 });
let tilt = 0;
for (let s = 1; s <= 5; s++) {
tilt += t.move(1, { x: 0, y: 100 - 10 * s }).tilt;
tilt += t.move(2, { x: 0, y: 200 + 10 * s }).tilt;
}
close(tilt, 0);
});