Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

duoControl.test.ts

190 lines6.8 KB

1import { afterEach, expect, it, vi } from "vite-plus/test";
2import {
3 createDuoControl,
4 createDuoPinch,
5 duoFoldState,
6 duoHoldOrientation,
7 type DuoCommand,
8} from "./duoControl.ts";
9afterEach(() => vi.useRealTimers());
10​
11it("keeps a failed send visible, including a disconnect while draining queued motion", () => {
12 const send = vi.fn((_request: { requestId: number; command: DuoCommand }) => false);
13 const onChange = vi.fn();
14 const queue = createDuoControl({ send, onChange });
15 queue.enqueue({ control: "angle", value: 40 });
16 expect(onChange).toHaveBeenLastCalledWith({
17 pending: false,
18 requested: null,
19 error: "Device is disconnected.",
20 });
21 send.mockReturnValueOnce(true);
22 queue.enqueue({ control: "pose", value: "book" });
23 queue.enqueue({ control: "angle", value: 60 });
24 queue.receive({ requestId: 2, ok: true });
25 expect(onChange).toHaveBeenLastCalledWith({
26 pending: false,
27 requested: null,
28 error: "Device is disconnected.",
29 });
30});
31​
32it("coalesces hinge edits behind acknowledgements and lets a preset replace queued edits", () => {
33 const send = vi.fn((_request: { requestId: number; command: DuoCommand }) => true);
34 const onChange = vi.fn();
35 const queue = createDuoControl({ send, onChange });
36 queue.enqueue({ control: "angle", value: 40 });
37 queue.enqueue({ control: "angle", value: 50 });
38 queue.enqueue({ control: "angle", value: 60 });
39 expect(send).toHaveBeenCalledTimes(1);
40 queue.receive({ requestId: 1, ok: true });
41 expect(send.mock.calls[1]?.[0]).toEqual({
42 requestId: 2,
43 command: { control: "angle", value: 60 },
44 });
45 queue.enqueue({ control: "angle", value: 100 });
46 queue.enqueue({ control: "pose", value: "tent" });
47 queue.receive({ requestId: 2, ok: true });
48 expect(send.mock.calls[2]?.[0]).toEqual({
49 requestId: 3,
50 command: { control: "pose", value: "tent" },
51 });
52 queue.receive({ requestId: 3, ok: true });
53 expect(onChange).toHaveBeenLastCalledWith({ pending: false, requested: null, error: null });
54 queue.clear();
55});
56​
57it("drops queued commands on failure, timeout and disconnect; late replies cannot acknowledge later work", () => {
58 vi.useFakeTimers();
59 const send = vi.fn((_request: { requestId: number; command: DuoCommand }) => true);
60 const onChange = vi.fn();
61 const queue = createDuoControl({ send, onChange, timeoutMs: 100 });
62 queue.enqueue({ control: "angle", value: 90 });
63 queue.enqueue({ control: "angle", value: 100 });
64 vi.advanceTimersByTime(100);
65 expect(onChange.mock.lastCall?.[0].error).toContain("timed out");
66 queue.enqueue({ control: "pose", value: "open" });
67 queue.receive({ requestId: 1, ok: true });
68 expect(onChange.mock.lastCall?.[0].pending).toBe(true);
69 queue.enqueue({ control: "angle", value: 130 });
70 queue.receive({ requestId: 2, ok: false, error: "native refused" });
71 expect(onChange.mock.lastCall?.[0]).toEqual({
72 pending: false,
73 requested: null,
74 error: "native refused",
75 });
76 queue.enqueue({ control: "pose", value: "book" });
77 queue.enqueue({ control: "pose", value: "closed" });
78 queue.clear();
79 queue.receive({ requestId: 3, ok: true });
80 expect(send).toHaveBeenCalledTimes(3);
81 queue.enqueue({ control: "angle", value: Infinity });
82 expect(send).toHaveBeenCalledTimes(3);
83 queue.clear();
84});
85​
86it("pinches only a hit device, accumulates independently of native readback, clamps and cancels", () => {
87 let confirmed = 90;
88 const change = vi.fn();
89 const pinch = createDuoPinch({
90 angle: () => confirmed,
91 contains: (x, y) => x > 0.2 && y > 0.2,
92 change,
93 });
94 expect(pinch.begin(0.1, 0.5)).toBe(false);
95 pinch.move(1);
96 expect(change).not.toHaveBeenCalled();
97 expect(pinch.begin(0.5, 0.5)).toBe(true);
98 pinch.move(0.25);
99 expect(change).toHaveBeenLastCalledWith(120);
100 confirmed = 100;
101 pinch.move(0.25);
102 expect(change).toHaveBeenLastCalledWith(150);
103 pinch.move(2);
104 expect(change).toHaveBeenLastCalledWith(180);
105 pinch.move(-3);
106 expect(change).toHaveBeenLastCalledWith(0);
107 pinch.move(NaN);
108 pinch.end();
109 expect(change).toHaveBeenLastCalledWith(null);
110 const count = change.mock.calls.length;
111 pinch.move(1);
112 pinch.end();
113 expect(change).toHaveBeenCalledTimes(count);
114 expect(pinch.active).toBe(false);
115});
116​
117it.each([
118 [
119 "vertical phone, closed",
120 { screenId: 1, orientation: "portrait", hingeAngle: 0 },
121 "closed",
122 true,
123 ],
124 [
125 "horizontal phone, closed",
126 { screenId: 1, orientation: "landscape_right", hingeAngle: 0 },
127 "closed",
128 false,
129 ],
130 [
131 "vertical phone opened as a book",
132 { screenId: 3, orientation: "landscape_left", hingeAngle: 180 },
133 "open",
134 true,
135 ],
136 [
137 "horizontal phone opened as a laptop",
138 { screenId: 3, orientation: "portrait_upside_down", hingeAngle: 180 },
139 "open",
140 false,
141 ],
142 ["half-open book", { screenId: 3, orientation: "landscape_left", hingeAngle: 90 }, "half", true],
143] as const)("reads a %s", (_name, screen, fold, phoneVertical) => {
144 expect(duoFoldState(screen)).toEqual({ fold, stand: false, phoneVertical, settled: true });
145});
146​
147it("marks a display handoff unsettled while the cover reports the inner display's orientation", () => {
148 const handoff = [
149 { screenId: 1, orientation: "portrait", hingeAngle: 90 },
150 { screenId: 1, orientation: "landscape_left", hingeAngle: 90 },
151 { screenId: 3, orientation: "landscape_left", hingeAngle: 90 },
152 ] as const;
153 expect(handoff.map((screen) => duoFoldState(screen).settled)).toEqual([false, false, true]);
154 expect(duoFoldState({ screenId: 3, orientation: "landscape_left", hingeAngle: 0 }).settled).toBe(
155 false,
156 );
157});
158​
159it("marks native stands so the fold group does not also claim them", () => {
160 expect(
161 duoFoldState({ screenId: 3, orientation: "portrait", hingeAngle: 90, hingePose: "laptop" }),
162 ).toEqual({ fold: "half", stand: true, phoneVertical: false, settled: true });
163});
164​
165it("falls back like the 3D view when hinge fields are missing", () => {
166 expect(duoFoldState({ screenId: 1, orientation: "portrait" })).toMatchObject({
167 fold: "closed",
168 phoneVertical: true,
169 });
170 expect(duoFoldState({ orientation: "landscape_left" })).toMatchObject({
171 fold: "open",
172 phoneVertical: true,
173 });
174});
175​
176it("rotates a stand back to how the phone was held, in the frame of the display receiving it", () => {
177 // Recorded from the iPhone Duo simulator: Tent can rest on either display.
178 // Rotating the inner display to landscape_left, or the cover to portrait,
179 // leaves a phone that opens and closes vertical.
180 expect(duoHoldOrientation(true, 3)).toBe("landscape_left");
181 expect(duoHoldOrientation(true, 1)).toBe("portrait");
182 expect(duoHoldOrientation(false, 1)).toBe("landscape_left");
183 expect(duoHoldOrientation(false, 3)).toBe("portrait_upside_down");
184 for (const screenId of [1, 3])
185 for (const vertical of [true, false])
186 expect(
187 duoFoldState({ screenId, orientation: duoHoldOrientation(vertical, screenId) })
188 .phoneVertical,
189 ).toBe(vertical);
190});