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

cursors.test.ts

123 lines3.9 KB

1import { describe, it, expect, vi } from "vitest";
2import * as THREE from "three";
3import { installCursorTracking } from "../src/client/cursors";
4​
5function setup() {
6 const canvas = Object.assign(new EventTarget(), {
7 getBoundingClientRect: () => ({
8 left: 0,
9 top: 0,
10 right: 100,
11 bottom: 100,
12 width: 100,
13 height: 100,
14 }),
15 });
16 const keys = new EventTarget();
17 const page = Object.assign(new EventTarget(), { visibilityState: "visible" });
18 const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 100);
19 camera.position.set(0, 8, 10);
20 camera.lookAt(0, 0, 0);
21 camera.updateMatrixWorld();
22 const scene = new THREE.Scene();
23 const board = new THREE.Mesh(new THREE.PlaneGeometry(32, 32));
24 board.rotation.x = -Math.PI / 2;
25 board.userData.cursorSurface = true;
26 const brick = new THREE.InstancedMesh(
27 new THREE.BoxGeometry(2, 1, 2),
28 new THREE.MeshBasicMaterial(),
29 1,
30 );
31 brick.setMatrixAt(0, new THREE.Matrix4().makeTranslation(0, 0.5, 0));
32 brick.userData.partAt = () => ({ id: "brick" });
33 // This closer object represents a handle or a collaborator's preview.
34 const overlay = new THREE.Mesh(new THREE.BoxGeometry(2, 1, 2));
35 overlay.position.y = 2;
36 scene.add(board, brick, overlay);
37 scene.updateMatrixWorld();
38 const onCursor = vi.fn();
39 const tracking = installCursorTracking(
40 canvas as unknown as HTMLCanvasElement,
41 camera,
42 scene,
43 onCursor,
44 keys as unknown as Window,
45 page as unknown as Document,
46 );
47 const move = (x = 50, y = 50, pointerType = "mouse") => {
48 canvas.dispatchEvent(
49 Object.assign(new Event("pointermove"), {
50 clientX: x,
51 clientY: y,
52 pointerType,
53 }),
54 );
55 tracking.update();
56 };
57 return { canvas, keys, page, camera, brick, board, onCursor, tracking, move };
59​
60describe("collaborative surface cursors", () => {
61 it("picks the frontmost real brick in database units, ignoring overlays", () => {
62 const s = setup();
63 s.move();
64 expect(s.onCursor).toHaveBeenLastCalledWith({ x: 0, y: 4.8, z: 6 });
65 s.move();
66 expect(s.onCursor).toHaveBeenCalledTimes(1);
67 s.tracking.dispose();
68 });
69 it("picks the board and clears when the ray misses or a captured pointer leaves", () => {
70 const s = setup();
71 s.brick.visible = false;
72 s.move();
73 expect(s.onCursor).toHaveBeenLastCalledWith({ x: 0, y: 0, z: 0 });
74 s.board.visible = false;
75 s.tracking.invalidate();
76 s.tracking.update();
77 expect(s.onCursor).toHaveBeenLastCalledWith(null);
78 s.board.visible = true;
79 s.move();
80 s.move(101, 50);
81 expect(s.onCursor).toHaveBeenLastCalledWith(null);
82 s.tracking.dispose();
83 });
84 it("reprojects a stationary pointer when the camera moves", () => {
85 const s = setup();
86 s.move();
87 s.camera.position.x = 8;
88 s.camera.lookAt(0, 0, 0);
89 s.camera.updateMatrixWorld();
90 s.tracking.update();
91 expect(s.onCursor.mock.calls.at(-1)?.[0]).not.toEqual(
92 s.onCursor.mock.calls[0][0],
93 );
94 s.tracking.dispose();
95 });
96 it.each(["pointerleave", "pointercancel", "blur", "hidden", "dispose"])(
97 "clears on %s without reviving the old pointer",
98 (reason) => {
99 const s = setup();
100 s.move();
101 if (reason === "hidden") {
102 s.page.visibilityState = "hidden";
103 s.page.dispatchEvent(new Event("visibilitychange"));
104 } else if (reason === "dispose") s.tracking.dispose();
105 else
106 (reason === "blur" ? s.keys : s.canvas).dispatchEvent(
107 new Event(reason),
108 );
109 expect(s.onCursor).toHaveBeenLastCalledWith(null);
110 const count = s.onCursor.mock.calls.length;
111 s.tracking.update();
112 expect(s.onCursor).toHaveBeenCalledTimes(count);
113 s.tracking.dispose();
114 },
115 );
116 it("does not share a persistent mouse cursor for touch input", () => {
117 const s = setup();
118 s.move();
119 s.move(50, 50, "touch");
120 expect(s.onCursor).toHaveBeenLastCalledWith(null);
121 s.tracking.dispose();
122 });
123});