nordic-skating / files / test / input.test.ts
input.test.ts
8876a48d 2026-10-10 Pavel Safronov | 1 | import { describe, expect, it } from 'vitest' |
8bb08e4a 2026-10-10 Pavel Safronov | 2 | import { padSteer, tiltSteer } from '../src/input.js' |
8876a48d 2026-10-10 Pavel Safronov | 3 | |
| 4 | describe('steering by tilt', () => { | |
| 5 | it('is nothing for a phone held level, or nearly', () => { | |
| 6 | for (const roll of [0, 2, -3.9, 4]) expect(tiltSteer(40, roll, 0)).toBe(0) | |
| 7 | }) | |
| 8 | ||
| 9 | it('steers the way the phone is leaned, more the further, and no more than fully', () => { | |
| 10 | expect(tiltSteer(40, 10, 0)).toBeCloseTo(0.25, 5) | |
| 11 | expect(tiltSteer(40, 16, 0)).toBeCloseTo(0.5, 5) | |
| 12 | expect(tiltSteer(40, -16, 0)).toBeCloseTo(-0.5, 5) | |
| 13 | expect(tiltSteer(40, 28, 0)).toBe(1) | |
| 14 | expect(tiltSteer(40, 75, 0)).toBe(1) | |
| 15 | expect(tiltSteer(40, -75, 0)).toBe(-1) | |
| 16 | }) | |
| 17 | ||
| 18 | it('takes no notice of how far back the phone is tipped, held upright', () => { | |
| 19 | for (const tip of [0, 30, 60, 85]) expect(tiltSteer(tip, 16, 0)).toBeCloseTo(0.5, 5) | |
| 20 | }) | |
| 21 | ||
| 22 | it('held on its side, steers by what was its tip, whichever side is up', () => { | |
| 23 | // Top edge to the left: leaning right is the top edge rising. | |
| 24 | expect(tiltSteer(16, 0, 90)).toBeCloseTo(0.5, 5) | |
| 25 | expect(tiltSteer(-16, 0, 90)).toBeCloseTo(-0.5, 5) | |
| 26 | // Top edge to the right: the other way about. | |
| 27 | expect(tiltSteer(16, 0, 270)).toBeCloseTo(-0.5, 5) | |
| 28 | // And upside down, the roll is reversed. | |
| 29 | expect(tiltSteer(0, 16, 180)).toBeCloseTo(-0.5, 5) | |
| 30 | }) | |
| 31 | }) | |
8bb08e4a 2026-10-10 Pavel Safronov | 32 | |
| 33 | describe('steering by a controller', () => { | |
| 34 | it('is nothing for a stick at rest, which is never quite at its middle', () => { | |
| 35 | for (const lean of [0, 0.05, -0.12, 0.15]) expect(padSteer(lean)).toBe(0) | |
| 36 | }) | |
| 37 | ||
| 38 | it('eases in from there to full steering with the stick right over', () => { | |
| 39 | expect(padSteer(0.575)).toBeCloseTo(0.5, 5) | |
| 40 | expect(padSteer(-0.575)).toBeCloseTo(-0.5, 5) | |
| 41 | expect(padSteer(1)).toBe(1) | |
| 42 | expect(padSteer(-1)).toBe(-1) | |
| 43 | }) | |
| 44 | }) |