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