fuzzyworld

nordic-skating / mistral-medium-35-littlejs / files / test / input.test.ts

Branch mistral-medium-35-littlejs · default branch · all branches

input.test.ts

1.7 KB · history · blame

1import { describe, expect, it } from 'vitest'
2import { padSteer, tiltSteer } from '../src/input.js'
3
4describe('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})
32
33describe('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})