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

DeviceActions.test.ts

131 lines4.9 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3​
4import { readDeviceDetail, runDeviceAction } from "./DeviceActions.ts";
5import type { DeviceHostReady } from "./DeviceHost.ts";
6​
7type Call = { command: string; args: ReadonlyArray<string>; stdin?: string };
8​
9const makeReady = (
10 respond: (call: Call) => { stdout?: string; stderr?: string; code?: number } = () => ({}),
11 helpers: DeviceHostReady["helpers"] = {
12 serveSimAxSettings: "/hub/simax/serve-sim-ax-settings",
13 serveSimCli: "/hub/serve-sim.js",
14 },
15) => {
16 const calls: Call[] = [];
17 const unused = Effect.die("Device actions run host commands, never device operations");
18 const ready: DeviceHostReady = {
19 nodePath: process.execPath,
20 hub: { origin: "http://127.0.0.1:1" },
21 devices: {
22 list: unused,
23 boot: () => unused,
24 attach: () => unused,
25 shutdown: () => unused,
26 screenshot: () => unused,
27 },
28 helpers,
29 run: (command, args, options) => {
30 const call = { command, args, ...(options?.stdin ? { stdin: options.stdin } : {}) };
31 calls.push(call);
32 const result = respond(call);
33 return Effect.succeed({
34 stdout: result.stdout ?? "",
35 stderr: result.stderr ?? "",
36 code: result.code ?? 0,
37 });
38 },
39 };
40 return { ready, calls };
41};
42​
43const udid = "SIM-1";
44​
45describe("runDeviceAction", () => {
46 it.effect.each([
47 ["emulator-5554", "Physical size: 2560x1600", "portrait", "-9.81:0:0"],
48 ["emulator-5554", "Physical size: 2560x1600", "landscape_left", "0:9.81:0"],
49 ["R5CT1234", "Physical size: 2560x1600", "portrait", "3"],
50 ["R5CT1234", "Physical size: 2560x1600", "landscape_left", "0"],
51 ["R5CT1234", "Physical size: 1080x1920\nOverride size: 1920x1080", "portrait", "3"],
52 ] as const)(
53 "rotates from the natural orientation reported by wm size: %s %j %s",
54 ([deviceId, wmSize, value, expected]) =>
55 Effect.gen(function* () {
56 const { ready, calls } = makeReady((call) =>
57 call.args.join(" ").endsWith("shell wm size") ? { stdout: `${wmSize}\n` } : {},
58 );
59 yield* runDeviceAction(ready, "android", { type: "setOrientation", deviceId, value });
60 expect(calls.at(-1)?.args.at(-1)).toBe(expected);
61 }),
62 );
63});
64​
65describe("readDeviceDetail", () => {
66 it.effect("reads iOS settings from simctl and the accessibility helper", () =>
67 Effect.gen(function* () {
68 const { ready } = makeReady((call) => {
69 const key = call.args.join(" ");
70 if (key.endsWith("ui SIM-1 appearance")) return { stdout: "dark\n" };
71 if (key.endsWith("ui SIM-1 content_size")) return { stdout: "extra-extra-large\n" };
72 if (key.endsWith("ui SIM-1 increase_contrast")) return { stdout: "enabled\n" };
73 if (key.includes("serve-sim-ax-settings status")) {
74 return {
75 stdout:
76 '{"reduce-motion":"on","reduce-transparency":"off","show-borders":"off","voiceover":"off","liquid-glass":"tinted","color-filter":"grayscale"}',
77 };
78 }
79 return { code: 1 };
80 });
81 const detail = yield* readDeviceDetail(ready, "ios", udid);
82 expect(detail.settings).toEqual({
83 appearance: "dark",
84 textSize: "large",
85 increaseContrast: true,
86 reduceMotion: true,
87 reduceTransparency: false,
88 showBorders: false,
89 voiceOver: false,
90 liquidGlass: "tinted",
91 colorFilter: "grayscale",
92 });
93 }),
94 );
95​
96 it.effect("degrades unreadable values to unknown instead of failing", () =>
97 Effect.gen(function* () {
98 const { ready } = makeReady(() => ({ code: 1, stderr: "boom" }));
99 const detail = yield* readDeviceDetail(ready, "ios", udid);
100 expect(detail.settings).toEqual({});
101 expect(detail.foregroundApp).toBeNull();
102 }),
103 );
104​
105 it.effect("reads Android settings and the focused package", () =>
106 Effect.gen(function* () {
107 const { ready } = makeReady((call) => {
108 const key = call.args.join(" ");
109 if (key.endsWith("cmd uimode night")) return { stdout: "Night mode: yes\n" };
110 if (key.endsWith("font_scale")) return { stdout: "0.85\n" };
111 if (key.endsWith("animator_duration_scale")) return { stdout: "0\n" };
112 if (key.endsWith("wifi_on")) return { stdout: "1\n" };
113 if (key.endsWith("dumpsys window")) {
114 return {
115 stdout:
116 " mFocusedApp=ActivityRecord{155579877 u0 com.example.app/.MainActivity t15}\n mCurrentFocus=Window{1a2b u0 com.example.app/com.example.app.MainActivity}\n",
117 };
118 }
119 return { code: 1 };
120 });
121 const detail = yield* readDeviceDetail(ready, "android", "emulator-5554");
122 expect(detail.settings).toEqual({
123 appearance: "dark",
124 textSize: "small",
125 reduceMotion: true,
126 networkEnabled: true,
127 });
128 expect(detail.foregroundApp).toEqual({ id: "com.example.app" });
129 }),
130 );
131});