1import { describe, expect, it } from "vite-plus/test";
2
3import { createShakeDetector } from "./shakeDetector";
4
5const resting = (timestamp: number) => ({ x: 0, y: 0, z: -1, timestamp });
6const jolt = (timestamp: number) => ({ x: 2.2, y: 0.4, z: -1, timestamp });
7
8describe("shake detector", () => {
9 it("ignores gravity and a single bump", () => {
10 const detect = createShakeDetector();
11 expect(detect(resting(0))).toBe(false);
12 expect(detect(jolt(100))).toBe(false);
13 expect(detect(resting(200))).toBe(false);
14 expect(detect(jolt(900))).toBe(false);
15 });
16
17 it("reports two jolts within the window once, then cools down", () => {
18 const detect = createShakeDetector();
19 expect(detect(jolt(0))).toBe(false);
20 expect(detect(jolt(300))).toBe(true);
21 expect(detect(jolt(400))).toBe(false);
22 expect(detect(jolt(600))).toBe(false);
23 expect(detect(jolt(1_400))).toBe(false);
24 expect(detect(jolt(1_500))).toBe(true);
25 });
26});