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

duoControl.test.ts

109 lines4.1 KB

1import { afterEach, expect, it, vi } from "vite-plus/test";
2import { createDuoControl, createDuoPinch, 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});