Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import type { DeviceActionInput } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4
5import { readDeviceDetail, runDeviceAction, supportsAction } from "./DeviceActions.ts";
6import type { DeviceHostReady } from "./DeviceHost.ts";
7
8type Call = { command: string; args: ReadonlyArray<string>; stdin?: string };
9
10const makeReady = (
11 respond: (call: Call) => { stdout?: string; stderr?: string; code?: number } = () => ({}),
12 helpers: DeviceHostReady["helpers"] = {
13 serveSimAxSettings: "/hub/simax/serve-sim-ax-settings",
14 serveSimCli: "/hub/serve-sim.js",
15 },
16) => {
17 const calls: Call[] = [];
18 const unused = Effect.die("Device actions run host commands, never device operations");
19 const ready: DeviceHostReady = {
20 nodePath: process.execPath,
21 hub: { origin: "http://127.0.0.1:1" },
22 devices: {
23 list: unused,
24 boot: () => unused,
25 attach: () => unused,
26 shutdown: () => unused,
27 screenshot: () => unused,
28 },
29 helpers,
30 run: (command, args, options) => {
31 const call = { command, args, ...(options?.stdin ? { stdin: options.stdin } : {}) };
32 calls.push(call);
33 const result = respond(call);
34 return Effect.succeed({
35 stdout: result.stdout ?? "",
36 stderr: result.stderr ?? "",
37 code: result.code ?? 0,
38 });
39 },
40 };
41 return { ready, calls };
42};
43
44const udid = "SIM-1";
45
46describe("supportsAction", () => {
47 it("advertises platform-specific toggles", () => {
48 const toggle = (setting: Extract<DeviceActionInput, { type: "setToggle" }>["setting"]) =>
49 ({ type: "setToggle", deviceId: udid, setting, value: true }) as const;
50 expect(supportsAction("ios", toggle("voiceOver"))).toBe(true);
51 expect(supportsAction("android", toggle("voiceOver"))).toBe(false);
52 expect(supportsAction("android", toggle("networkEnabled"))).toBe(true);
53 expect(supportsAction("ios", toggle("networkEnabled"))).toBe(false);
54 expect(supportsAction("ios", { type: "setLiquidGlass", deviceId: udid, value: "clear" })).toBe(
55 true,
56 );
57 expect(
58 supportsAction("android", { type: "setLiquidGlass", deviceId: udid, value: "clear" }),
59 ).toBe(false);
60 expect(
61 supportsAction("android", { type: "setOrientation", deviceId: udid, value: "portrait" }),
62 ).toBe(true);
63 expect(
64 supportsAction("ios", { type: "setOrientation", deviceId: udid, value: "portrait" }),
65 ).toBe(false);
66 });
67});
68
69describe("runDeviceAction", () => {
70 it.effect("maps shared text sizes onto simctl content-size categories", () =>
71 Effect.gen(function* () {
72 const { ready, calls } = makeReady();
73 yield* runDeviceAction(ready, "ios", {
74 type: "setTextSize",
75 deviceId: udid,
76 value: "extra-large",
77 });
78 expect(calls).toEqual([
79 { command: "xcrun", args: ["simctl", "ui", udid, "content_size", "accessibility-large"] },
80 ]);
81 }),
82 );
83
84 it.effect("maps shared text sizes onto Android font_scale", () =>
85 Effect.gen(function* () {
86 const { ready, calls } = makeReady();
87 yield* runDeviceAction(ready, "android", {
88 type: "setTextSize",
89 deviceId: "emulator-5554",
90 value: "large",
91 });
92 expect(calls[0]?.args).toEqual([
93 "-s",
94 "emulator-5554",
95 "shell",
96 "settings",
97 "put",
98 "system",
99 "font_scale",
100 "1.15",
101 ]);
102 }),
103 );
104
105 it.effect(
106 "rotates emulators through the accelerometer and physical devices through the lock",
107 () =>
108 Effect.gen(function* () {
109 const emulator = makeReady();
110 yield* runDeviceAction(emulator.ready, "android", {
111 type: "setOrientation",
112 deviceId: "emulator-5554",
113 value: "landscape_left",
114 });
115 expect(emulator.calls.at(-1)?.args).toEqual([
116 "-s",
117 "emulator-5554",
118 "emu",
119 "sensor",
120 "set",
121 "acceleration",
122 "9.81:0:0",
123 ]);
124 // A device that refuses `wm size` keeps the portrait-native mapping.
125 const phone = makeReady((call) =>
126 call.args.join(" ").endsWith("shell wm size") ? { code: 1 } : {},
127 );
128 yield* runDeviceAction(phone.ready, "android", {
129 type: "setOrientation",
130 deviceId: "R5CT1234",
131 value: "landscape_right",
132 });
133 expect(phone.calls.at(-1)).toEqual({
134 command: "adb",
135 args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
136 });
137 }),
138 );
139
140 it.effect.each([
141 ["emulator-5554", "Physical size: 2560x1600", "portrait", "-9.81:0:0"],
142 ["emulator-5554", "Physical size: 2560x1600", "landscape_left", "0:9.81:0"],
143 ["R5CT1234", "Physical size: 2560x1600", "portrait", "3"],
144 ["R5CT1234", "Physical size: 2560x1600", "landscape_left", "0"],
145 ["R5CT1234", "Physical size: 1080x1920\nOverride size: 1920x1080", "portrait", "3"],
146 ] as const)(
147 "rotates from the natural orientation reported by wm size: %s %j %s",
148 ([deviceId, wmSize, value, expected]) =>
149 Effect.gen(function* () {
150 const { ready, calls } = makeReady((call) =>
151 call.args.join(" ").endsWith("shell wm size") ? { stdout: `${wmSize}\n` } : {},
152 );
153 yield* runDeviceAction(ready, "android", { type: "setOrientation", deviceId, value });
154 expect(calls.at(-1)?.args.at(-1)).toBe(expected);
155 }),
156 );
157
158 it.effect("runs accessibility toggles through the bundled helper via simctl spawn", () =>
159 Effect.gen(function* () {
160 const { ready, calls } = makeReady();
161 yield* runDeviceAction(ready, "ios", {
162 type: "setToggle",
163 deviceId: udid,
164 setting: "voiceOver",
165 value: true,
166 });
167 expect(calls).toEqual([
168 {
169 command: "xcrun",
170 args: [
171 "simctl",
172 "spawn",
173 udid,
174 "/hub/simax/serve-sim-ax-settings",
175 "set",
176 "voiceover",
177 "on",
178 ],
179 },
180 ]);
181 }),
182 );
183
184 it.effect("fails clearly when the helper is missing", () =>
185 Effect.gen(function* () {
186 const { ready } = makeReady(() => ({}), { serveSimAxSettings: null, serveSimCli: null });
187 const error = yield* Effect.flip(
188 runDeviceAction(ready, "ios", {
189 type: "setColorFilter",
190 deviceId: udid,
191 value: "grayscale",
192 }),
193 );
194 expect(error._tag).toBe("DeviceActionUnavailableError");
195 expect(error.message).toContain("requires a helper");
196 }),
197 );
198
199 it.effect("rejects actions the platform does not support without running anything", () =>
200 Effect.gen(function* () {
201 const { ready, calls } = makeReady();
202 const error = yield* Effect.flip(
203 runDeviceAction(ready, "android", {
204 type: "sendPush",
205 deviceId: "emulator-5554",
206 appId: "com.example",
207 payload: "hi",
208 }),
209 );
210 expect(error.message).toContain("not supported on android");
211 expect(calls).toEqual([]);
212 }),
213 );
214
215 it.effect("surfaces non-zero exit codes as operation errors", () =>
216 Effect.gen(function* () {
217 const { ready } = makeReady(() => ({ code: 1, stderr: "Invalid device: SIM-1" }));
218 const error = yield* Effect.flip(
219 runDeviceAction(ready, "ios", { type: "setAppearance", deviceId: udid, value: "dark" }),
220 );
221 expect(error.operation).toBe("appearance");
222 expect(error.message).toContain("exit code 1");
223 expect(error.message).not.toContain("Invalid device: SIM-1");
224 expect(error._tag === "DeviceOperationError" && error.cause).toMatchObject({
225 stderr: "Invalid device: SIM-1",
226 });
227 }),
228 );
229
230 it.effect("wraps a bare push string in an APNs alert and feeds it on stdin", () =>
231 Effect.gen(function* () {
232 const { ready, calls } = makeReady();
233 yield* runDeviceAction(ready, "ios", {
234 type: "sendPush",
235 deviceId: udid,
236 appId: "com.example.app",
237 payload: "Hello",
238 });
239 expect(calls[0]?.args).toEqual(["simctl", "push", udid, "com.example.app", "-"]);
240 expect(calls[0]?.stdin).toBe('{"aps":{"alert":"Hello"}}');
241 }),
242 );
243});
244
245describe("readDeviceDetail", () => {
246 it.effect("reads iOS settings from simctl and the accessibility helper", () =>
247 Effect.gen(function* () {
248 const { ready } = makeReady((call) => {
249 const key = call.args.join(" ");
250 if (key.endsWith("ui SIM-1 appearance")) return { stdout: "dark\n" };
251 if (key.endsWith("ui SIM-1 content_size")) return { stdout: "extra-extra-large\n" };
252 if (key.endsWith("ui SIM-1 increase_contrast")) return { stdout: "enabled\n" };
253 if (key.includes("serve-sim-ax-settings status")) {
254 return {
255 stdout:
256 '{"reduce-motion":"on","reduce-transparency":"off","show-borders":"off","voiceover":"off","liquid-glass":"tinted","color-filter":"grayscale"}',
257 };
258 }
259 return { code: 1 };
260 });
261 const detail = yield* readDeviceDetail(ready, "ios", udid);
262 expect(detail.settings).toEqual({
263 appearance: "dark",
264 textSize: "large",
265 increaseContrast: true,
266 reduceMotion: true,
267 reduceTransparency: false,
268 showBorders: false,
269 voiceOver: false,
270 liquidGlass: "tinted",
271 colorFilter: "grayscale",
272 });
273 }),
274 );
275
276 it.effect("degrades unreadable values to unknown instead of failing", () =>
277 Effect.gen(function* () {
278 const { ready } = makeReady(() => ({ code: 1, stderr: "boom" }));
279 const detail = yield* readDeviceDetail(ready, "ios", udid);
280 expect(detail.settings).toEqual({});
281 expect(detail.foregroundApp).toBeNull();
282 }),
283 );
284
285 it.effect("reads Android settings and the focused package", () =>
286 Effect.gen(function* () {
287 const { ready } = makeReady((call) => {
288 const key = call.args.join(" ");
289 if (key.endsWith("cmd uimode night")) return { stdout: "Night mode: yes\n" };
290 if (key.endsWith("font_scale")) return { stdout: "0.85\n" };
291 if (key.endsWith("animator_duration_scale")) return { stdout: "0\n" };
292 if (key.endsWith("wifi_on")) return { stdout: "1\n" };
293 if (key.endsWith("dumpsys window")) {
294 return {
295 stdout:
296 " mFocusedApp=ActivityRecord{155579877 u0 com.example.app/.MainActivity t15}\n mCurrentFocus=Window{1a2b u0 com.example.app/com.example.app.MainActivity}\n",
297 };
298 }
299 return { code: 1 };
300 });
301 const detail = yield* readDeviceDetail(ready, "android", "emulator-5554");
302 expect(detail.settings).toEqual({
303 appearance: "dark",
304 textSize: "small",
305 reduceMotion: true,
306 networkEnabled: true,
307 });
308 expect(detail.foregroundApp).toEqual({ id: "com.example.app" });
309 }),
310 );
311});