The best way to control Claude Code, Codex and more from any computer or phone

main
ts 94 lines 3.42 KB
Raw
1 import {
2 BoxGeometry,
3 Group,
4 Mesh,
5 MeshBasicMaterial,
6 PerspectiveCamera,
7 PlaneGeometry,
8 Texture,
9 Vector3,
10 } from "three";
11 import { expect, it } from "vite-plus/test";
12 import { createDuoScene, duoDisplayKey, duoRawPoint } from "./duoScene.ts";
13
14 function fixture() {
15 const asset = new Group();
16 const left = new Group();
17 left.name = "left-half";
18 const right = new Group();
19 right.name = "right-half";
20 const screen = (name: string, x: number, z: number, rear = false) => {
21 const mesh = new Mesh(new PlaneGeometry(1, 2), new MeshBasicMaterial());
22 if (rear) mesh.geometry.rotateY(Math.PI);
23 mesh.geometry.translate(x, 0, z);
24 mesh.name = name;
25 return mesh;
26 };
27 const innerLeft = screen("inner-display-left", -0.5, 0.06);
28 const innerRight = screen("inner-display-right", 0.5, 0.06);
29 const cover = screen("cover-display", -0.5, -0.06, true);
30 left.add(innerLeft, cover);
31 right.add(innerRight);
32 for (const [group, x] of [
33 [left, -0.5],
34 [right, 0.5],
35 ] as const) {
36 const body = new Mesh(new BoxGeometry(1, 2, 0.1), new MeshBasicMaterial());
37 body.geometry.translate(x, 0, 0);
38 group.add(body);
39 }
40 asset.add(left, right);
41 const scene = createDuoScene(asset, { 1: new Texture(), 3: new Texture() });
42 const camera = new PerspectiveCamera(36, 1, 0.1, 50);
43 camera.position.z = 6;
44 camera.updateMatrixWorld();
45 return { scene, camera, left, right, innerLeft, innerRight };
46 }
47
48 it("derives resting views from the hinged display planes independently of the inspection orbit", () => {
49 const { scene } = fixture();
50 scene.setAngle(90);
51 const frames = scene.restFrames(3);
52 expect(frames.map((frame) => frame.face)).toEqual(["inside", "left", "right"]);
53 expect(frames[1]!.normal.x).toBeGreaterThan(0.5);
54 expect(frames[2]!.normal.x).toBeLessThan(-0.5);
55 scene.root.rotation.set(0.5, 1.2, -0.7);
56 scene.root.position.set(3, -2, 1);
57 const rotated = scene.restFrames(3);
58 for (let index = 0; index < frames.length; index++) {
59 expect(rotated[index]!.normal.distanceTo(frames[index]!.normal)).toBeLessThan(1e-6);
60 expect(rotated[index]!.center.distanceTo(frames[index]!.center)).toBeLessThan(1e-6);
61 }
62 scene.setAngle(180);
63 expect(scene.restFrames(3).map((frame) => frame.face)).toEqual(["inside"]);
64 expect(scene.restFrames(1).map((frame) => frame.face)).toEqual(["cover"]);
65 scene.dispose();
66 });
67
68 it("maps active display input through hardware mounting and blocks rear, inactive and stale surfaces", () => {
69 const { scene, camera, innerLeft } = fixture();
70 scene.setAngle(180);
71 const screen = { width: 2007, height: 2853, orientation: "portrait" as const, screenId: 3 };
72 const key = duoDisplayKey(screen);
73 scene.root.updateMatrixWorld();
74 const world = innerLeft.localToWorld(new Vector3(-0.5, -0.5, 0.06)).project(camera);
75 const x = (world.x + 1) / 2,
76 y = (1 - world.y) / 2;
77 const hit = scene.screenPoint(x, y, camera, screen, key);
78 expect(hit?.x).toBeCloseTo(0.75);
79 expect(hit?.y).toBeCloseTo(0.75);
80 expect(
81 scene.screenPoint(
82 x,
83 y,
84 camera,
85 { ...screen, screenId: 1 },
86 duoDisplayKey({ ...screen, screenId: 1 }),
87 ),
88 ).toBeNull();
89 expect(scene.screenPoint(x, y, camera, screen, "old")).toBeNull();
90 expect(scene.screenPoint(x, y, camera, screen, key)).not.toBeNull();
91 expect(scene.screenPoint(x, 1.1, camera, screen, key, true)?.x).toBeGreaterThan(1);
92 expect(duoRawPoint(1, 0.2, 0.7)).toEqual({ x: 0.2, y: 0.7 });
93 scene.dispose();
94 });