| 1 | import { expect, it } from "vite-plus/test"; |
| 2 | import { Box3, Vector3 } from "three"; |
| 3 | import { createDeviceFraming } from "./deviceFraming.ts"; |
| 4 | |
| 5 | const box = (x: number, width: number) => |
| 6 | new Box3(new Vector3(x - width / 2, -1, -0.2), new Vector3(x + width / 2, 1, 0.2)); |
| 7 | it("fits both axes through a moving pivot, freezes for contact, and stops once framing settles", () => { |
| 8 | const framing = createDeviceFraming(); |
| 9 | framing.setBounds(box(0, 1), Math.PI / 8, 0.7, 0); |
| 10 | const initialDistance = framing.distance(); |
| 11 | const next = box(1.5, 3); |
| 12 | framing.setBounds(next, Math.PI / 8, 0.7, 10); |
| 13 | framing.advance(50); |
| 14 | expect(framing.center.x).toBeGreaterThan(0); |
| 15 | expect(framing.center.x).toBeLessThan(1.5); |
| 16 | for (const x of [next.min.x, next.max.x]) |
| 17 | expect( |
| 18 | Math.abs(x - framing.center.x) / |
| 19 | ((framing.distance() - next.max.z) * Math.tan(Math.PI / 8) * 0.7), |
| 20 | ).toBeLessThan(1); |
| 21 | framing.hold(true, 50); |
| 22 | const center = framing.center.clone(), |
| 23 | distance = framing.distance(); |
| 24 | framing.advance(10000); |
| 25 | expect(framing.center.equals(center)).toBe(true); |
| 26 | expect(framing.distance()).toBe(distance); |
| 27 | expect(framing.needsFrame()).toBe(false); |
| 28 | framing.hold(false, 10000); |
| 29 | framing.advance(10016); |
| 30 | expect(framing.center.x).toBeLessThan(1.5); |
| 31 | framing.advance(12000); |
| 32 | expect(framing.center.x).toBe(1.5); |
| 33 | expect(framing.distance()).toBeGreaterThan(initialDistance); |
| 34 | expect(framing.needsFrame()).toBe(false); |
| 35 | }); |
| 36 | |
| 37 | it("uses elapsed time independently of frame partitions and fits a changed viewport immediately", () => { |
| 38 | const run = (step: number) => { |
| 39 | const framing = createDeviceFraming(); |
| 40 | framing.setBounds(box(0, 1), Math.PI / 8, 1, 0); |
| 41 | framing.setBounds(box(-1, 2), Math.PI / 8, 1, 0); |
| 42 | for (let time = step; time < 400; time += step) framing.advance(time); |
| 43 | framing.advance(400); |
| 44 | return framing; |
| 45 | }; |
| 46 | const fast = run(8), |
| 47 | slow = run(33); |
| 48 | expect(fast.center.distanceTo(slow.center)).toBeLessThan(1e-8); |
| 49 | expect(fast.distance()).toBeCloseTo(slow.distance(), 8); |
| 50 | fast.setBounds(box(-1, 2), Math.PI / 8, 0.4, 400, true); |
| 51 | expect(fast.distance()).toBeGreaterThan(slow.distance()); |
| 52 | expect(fast.needsFrame()).toBe(false); |
| 53 | }); |