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

duoControl.test.ts

158 lines5.7 KB

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