Files
Rosea/packages/ui/test/gestures.test.ts

78 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { GESTURE, inside, isPull, isSweep, pourRate, strokeOf, tiltOf, type Sample } from '../src/scene/gestures.js';
const at = (x: number, y: number, t = 0): Sample => ({ x, y, t });
/** A straight line from a to b in `n` steps over `ms`. */
const line = (a: Sample, b: Sample, ms: number, n = 10): Sample[] =>
Array.from({ length: n + 1 }, (_, i) => ({ x: a.x + ((b.x - a.x) * i) / n, y: a.y + ((b.y - a.y) * i) / n, t: (ms * i) / n }));
describe('tilting the can (planet-garden.md G4)', () => {
it('a little movement does not tip it; far enough tips it fully; either way round', () => {
expect(tiltOf(at(100, 100), at(108, 100))).toBe(0);
expect(tiltOf(at(100, 100), at(100 + GESTURE.tiltFull, 100))).toBe(1);
expect(tiltOf(at(100, 100), at(100 - GESTURE.tiltFull, 100))).toBe(1);
expect(tiltOf(at(100, 100), at(176, 100))).toBeCloseTo(0.5, 1);
});
it('tilt stays within 0–1 and never falls as the hand moves further (every distance to 500)', () => {
let previous = 0;
for (let d = 0; d <= 500; d++) {
const t = tiltOf(at(0, 0), at(d, 0));
expect(t).toBeGreaterThanOrEqual(previous);
expect(t).toBeLessThanOrEqual(1);
previous = t;
}
});
it('pours slower than a steady hold when barely tipped, faster when fully tipped', () => {
expect(pourRate(0)).toBeLessThan(1);
expect(pourRate(0.55)).toBeCloseTo(1, 1);
expect(pourRate(1)).toBeGreaterThan(1);
expect(pourRate(5)).toBe(pourRate(1));
});
});
describe('pulling a weed', () => {
it('up and quick enough pulls it; sideways, too short or too slow does not', () => {
expect(isPull(line(at(100, 200), at(104, 140), 300))).toBe(true);
expect(isPull(line(at(100, 200), at(180, 180), 300))).toBe(false);
expect(isPull(line(at(100, 200), at(100, 170), 300))).toBe(false);
expect(isPull(line(at(100, 200), at(100, 120), GESTURE.pullMs + 1))).toBe(false);
expect(isPull(line(at(100, 200), at(100, 280), 300))).toBe(false);
});
it('far enough up but more sideways than up is not a pull (only the direction decides)', () => {
expect(isPull(line(at(100, 200), at(200, 140), 300))).toBe(false);
expect(isPull(line(at(100, 200), at(150, 140), 300))).toBe(true);
});
});
describe('sweeping the petals', () => {
it('a long sideways swipe across all of them gathers them, either direction', () => {
expect(isSweep(line(at(60, 300), at(260, 310), 400), 100, 220)).toBe(true);
expect(isSweep(line(at(260, 300), at(60, 290), 400), 100, 220)).toBe(true);
});
it('one that misses some, wanders up and down, or is too short does not', () => {
expect(isSweep(line(at(60, 300), at(180, 300), 400), 100, 220)).toBe(false);
expect(isSweep(line(at(60, 300), at(260, 420), 400), 100, 220)).toBe(false);
expect(isSweep([at(100, 300)], 100, 220)).toBe(false);
});
});
describe('touching her', () => {
it('a slow stroke across her petals is a stroke; a short press is a tap; a flick is neither', () => {
expect(strokeOf(line(at(100, 100), at(160, 104), 600))).toBe('stroke');
expect(strokeOf(line(at(100, 100), at(104, 102), 80))).toBe('tap');
expect(strokeOf(line(at(100, 100), at(160, 100), 60))).toBe('none');
});
});
describe('dropping the windbreak', () => {
it('lands inside a box or not', () => {
const box = { x0: 40, y0: 100, x1: 120, y1: 200 };
expect(inside(at(80, 150), box)).toBe(true);
expect(inside(at(130, 150), box)).toBe(false);
});
});