Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

cursor-motion.test.ts

80 lines3.2 KB

1import { describe, it, expect } from "vitest";
2import { Vector3 } from "three";
3import { CursorMotion } from "../src/client/cursor-motion";
4​
5describe("remote cursor animation", () => {
6 it("starts at the received surface point and preserves position and velocity across new samples", () => {
7 const motion = new CursorMotion(new Vector3(2, 4, 6));
8 expect(motion.position).toEqual(new Vector3(2, 4, 6));
9 motion.receive(new Vector3(4, 8, 12), 200);
10 motion.advance(0.1);
11 const before = motion.position.clone();
12 const velocity = motion.velocity.clone();
13 motion.receive(new Vector3(6, 9, 15), 550);
14 expect(motion.position).toEqual(before);
15 expect(motion.velocity).toEqual(velocity);
16 motion.advance(1 / 60);
17 expect(motion.position.x).toBeGreaterThan(before.x);
18 });
19​
20 it.each([200, 350, 500])(
21 "keeps moving between %i ms samples instead of stopping between packets",
22 (period) => {
23 const motion = new CursorMotion(new Vector3());
24 const speeds: number[] = [];
25 // A cursor travelling one stud per second, observed at a low packet rate.
26 for (let time = 0; time < 6000; time += 10) {
27 if (time % period === 0)
28 motion.receive(new Vector3(time / 1000, 0, 0), time);
29 const before = motion.position.x;
30 motion.advance(0.01);
31 if (time > 2000) speeds.push((motion.position.x - before) / 0.01);
32 }
33 expect(Math.min(...speeds)).toBeGreaterThan(0.6);
34 expect(Math.max(...speeds)).toBeLessThan(1.3);
35 },
36 );
37​
38 it("settles on the actual point without oscillating or overshooting after jittery delivery", () => {
39 const motion = new CursorMotion(new Vector3());
40 let arrival = 0;
41 for (const gap of [200, 370, 190, 480, 290]) {
42 arrival += gap;
43 motion.receive(new Vector3(arrival / 100, 2, -3), arrival);
44 for (let i = 0; i < 10; i++) motion.advance(gap / 10000);
45 }
46 const target = new Vector3(arrival / 100, 2, -3);
47 for (let i = 0; i < 180; i++) {
48 const before = motion.position.x;
49 motion.advance(1 / 60);
50 expect(motion.position.x).toBeGreaterThanOrEqual(before);
51 expect(motion.position.x).toBeLessThanOrEqual(target.x);
52 }
53 expect(motion.position.distanceTo(target)).toBeLessThan(0.001);
54 expect(motion.velocity.length()).toBeLessThan(0.001);
55 });
56​
57 it("moves equally at different frame rates and ignores identical stationary refreshes", () => {
58 const fast = new CursorMotion(new Vector3());
59 const slow = new CursorMotion(new Vector3());
60 for (const motion of [fast, slow])
61 motion.receive(new Vector3(4, 2, -3), 100);
62 for (let i = 0; i < 30; i++) {
63 fast.advance(1 / 120);
64 if (i === 10) fast.receive(new Vector3(4, 2, -3), 200);
65 }
66 for (let i = 0; i < 15; i++) slow.advance(1 / 60);
67 expect(fast.position.distanceTo(slow.position)).toBeLessThan(1e-10);
68 expect(fast.velocity.distanceTo(slow.velocity)).toBeLessThan(1e-10);
69 });
70​
71 it("resumes at the current target after a long backgrounded frame", () => {
72 const motion = new CursorMotion(new Vector3());
73 const target = new Vector3(4, 2, -3);
74 motion.receive(target, 200);
75 motion.advance(0.1);
76 motion.advance(3);
77 expect(motion.position).toEqual(target);
78 expect(motion.velocity.length()).toBe(0);
79 });
80});