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

useDeviceControls.browser.test.tsx

145 lines5.0 KB

1import { EnvironmentId, type DeviceDetail, type DeviceSummary } from "@t3tools/contracts";
2import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access";
3import * as Cause from "effect/Cause";
4import { useEffect } from "react";
5import { expect, it, vi } from "vite-plus/test";
6​
7import { act, render } from "../../../test/render";
8​
9type Result = { _tag: "Success"; value: DeviceDetail } | { _tag: "Failure"; cause: unknown };
10const { read, action, subscribe } = vi.hoisted(() => ({
11 read: vi.fn<() => Promise<Result>>(),
12 action: vi.fn<() => Promise<Result>>(),
13 subscribe:
14 vi.fn<(_target: unknown, onChange: (app: { id: string } | null) => void) => () => void>(),
15}));
16vi.mock(import("~/state/use-atom-command"), async (original) => {
17 const { deviceEnvironment } = await import("~/state/device");
18 return {
19 ...(await original()),
20 useAtomCommand: ((command: unknown) =>
21 command === deviceEnvironment.detail ? read : action) as never,
22 };
23});
24vi.mock(import("./deviceHubApi"), async (original) => ({
25 ...(await original()),
26 subscribeDeviceForeground: subscribe as never,
27}));
28​
29import { useDeviceControls, type DeviceControls } from "./useDeviceControls";
30​
31const offline = Cause.fail(new Error("Device offline"));
32const device: DeviceSummary = {
33 hostId: "remote",
34 id: "phone",
35 name: "Phone",
36 platform: "ios",
37 version: "iOS",
38 booted: true,
39 physical: false,
40};
41const snapshot = (appearance: "light" | "dark"): Result => ({
42 _tag: "Success",
43 value: {
44 hostId: "remote",
45 deviceId: "phone",
46 settings: { appearance },
47 foregroundApp: null,
48 readAt: "2026-09-20T00:00:00Z",
49 },
50});
51function deferred() {
52 let resolve!: (value: Result) => void;
53 const promise = new Promise<Result>((done) => {
54 resolve = done;
55 });
56 return { promise, resolve };
58let controls: DeviceControls;
59function Probe({ visible, access = null }: { visible: boolean; access?: DeviceHubAccess | null }) {
60 const next = useDeviceControls({
61 environmentId: EnvironmentId.make("environment"),
62 device,
63 access,
64 visible,
65 });
66 useEffect(() => {
67 controls = next;
68 }, [next]);
69 return null;
71function setup() {
72 read.mockReset();
73 action.mockReset();
74 subscribe.mockReset();
75 subscribe.mockReturnValue(() => {});
76 read.mockResolvedValue(snapshot("light"));
78​
79it("serializes rapid actions and retains confirmed settings while pending or after failure", async () => {
80 setup();
81 await using _screen = await render(<Probe visible />);
82 const response = deferred();
83 action.mockReturnValueOnce(response.promise);
84 let pending!: Promise<void>;
85 await act(async () => {
86 pending = controls.act({ type: "setAppearance", value: "dark" });
87 await controls.act({ type: "setAppearance", value: "light" });
88 });
89 expect(action).toHaveBeenCalledOnce();
90 expect(controls.disabled).toBe(true);
91 expect(controls.detail?.settings.appearance).toBe("light");
92 await act(async () => {
93 response.resolve(snapshot("dark"));
94 await pending;
95 });
96 expect(controls.detail?.settings.appearance).toBe("dark");
97 action.mockResolvedValueOnce({ _tag: "Failure", cause: offline });
98 await act(async () => controls.act({ type: "setAppearance", value: "light" }));
99 expect(controls.detail?.settings.appearance).toBe("dark");
100 expect(controls.error).toBe("Device offline");
101 expect(controls.disabled).toBe(false);
102});
103​
104it("keeps actions disabled when settings cannot be confirmed after a detached command", async () => {
105 setup();
106 await using screen = await render(<Probe visible />);
107 const response = deferred();
108 action.mockReturnValueOnce(response.promise);
109 let pending!: Promise<void>;
110 await act(async () => {
111 pending = controls.act({ type: "setAppearance", value: "dark" });
112 });
113 await screen.rerender(<Probe visible={false} />);
114 await screen.rerender(<Probe visible />);
115 read.mockResolvedValueOnce({ _tag: "Failure", cause: offline });
116 await act(async () => {
117 response.resolve(snapshot("dark"));
118 await pending;
119 });
120 expect(controls.detail).toBeNull();
121 expect(controls.disabled).toBe(true);
122 expect(controls.error).toBe("Device offline");
123 await act(async () => controls.act({ type: "setAppearance", value: "light" }));
124 expect(action).toHaveBeenCalledOnce();
125 await screen.rerender(<Probe visible={false} />);
126 await screen.rerender(<Probe visible />);
127 expect(controls.disabled).toBe(false);
128 expect(controls.error).toBeNull();
129});
130​
131it("does not let an older refresh overwrite a newer confirmed action", async () => {
132 setup();
133 await using screen = await render(<Probe visible />);
134 await screen.rerender(<Probe visible={false} />);
135 const refresh = deferred();
136 read.mockReturnValueOnce(refresh.promise);
137 await screen.rerender(<Probe visible />);
138 action.mockResolvedValueOnce(snapshot("dark"));
139 await act(async () => controls.act({ type: "setAppearance", value: "dark" }));
140 await act(async () => {
141 refresh.resolve(snapshot("light"));
142 await refresh.promise;
143 });
144 expect(controls.detail?.settings.appearance).toBe("dark");
145});