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

SimlockDeviceHost.test.ts

512 lines20.4 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { SecretProviderId, type SimlockDeviceHostConfig } from "@t3tools/contracts";
4import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Deferred from "effect/Deferred";
9import * as Exit from "effect/Exit";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as Scope from "effect/Scope";
14import * as TestClock from "effect/testing/TestClock";
15import { HttpClient, HttpClientResponse } from "effect/http";
16import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17​
18import * as ServerConfig from "../config.ts";
19import * as ProcessRunner from "../processRunner.ts";
20import * as SimlockDeviceHost from "./SimlockDeviceHost.ts";
21​
22const GATEWAY = "https://simlock.test:4700";
23​
24const config = (overrides: Partial<SimlockDeviceHostConfig> = {}): SimlockDeviceHostConfig => ({
25 kind: "simlock",
26 id: "pool",
27 label: "Device pool",
28 gatewayUrl: `${GATEWAY}/`,
29 agentToken: "agent-token",
30 maxLeases: 2,
31 maxLeaseMinutes: 60,
32 ...overrides,
33});
34​
35const CATALOG = {
36 platforms: [
37 {
38 platform: "ios",
39 modelRuntimes: { "iPhone 17 Pro": ["27.0"], "Apple TV 4K": ["27.0"], "iPhone 12": [] },
40 modelClasses: { "iPhone 17 Pro": "phone", "Apple TV 4K": "tv", "iPhone 12": "phone" },
41 },
42 {
43 platform: "android",
44 modelRuntimes: { "Pixel 9": ["36"], "Pixel 8": ["36"] },
45 modelWorkers: { "Pixel 9": ["worker-a"], "Pixel 8": [] },
46 },
47 ],
48};
49​
50/** The gateway's HTTP API and T3's agent-device CLI, recording what was asked of them. */
51const fakePool = (
52 options: {
53 /** What the agent-device CLI prints before exiting 1 from `open`. */
54 readonly failOpen?: unknown;
55 readonly holdOpen?: {
56 readonly entered: Deferred.Deferred<void>;
57 readonly proceed: Deferred.Deferred<void>;
58 };
59 } = {},
60) =>
61 Effect.gen(function* () {
62 const fs = yield* FileSystem.FileSystem;
63 const path = yield* Path.Path;
64 const requests: Array<string> = [];
65 const commands: Array<{ readonly args: ReadonlyArray<string>; readonly cwd?: string }> = [];
66 let leases = 0;
67 let renewStatus = 200;
68 let catalog: unknown = CATALOG;
69 const http = HttpClient.make((request) =>
70 Effect.sync(() => {
71 const url = new URL(request.url);
72 requests.push(`${request.method} ${url.pathname}`);
73 const json = (body: unknown, status = 200) =>
74 HttpClientResponse.fromWeb(request, Response.json(body, { status }));
75 if (request.headers.authorization !== "Bearer agent-token")
76 return json({ error: { code: "UNAUTHENTICATED", message: "Unknown bearer token" } }, 401);
77 if (url.pathname === "/v1/catalog") return json(catalog);
78 if (url.pathname.endsWith("/renew")) return json({}, renewStatus);
79 if (request.method === "DELETE") return json({}, 202);
80 return json({}, 404);
81 }),
82 );
83 const runner = ProcessRunner.ProcessRunner.of({
84 run: (input) =>
85 Effect.gen(function* () {
86 const args = input.args.slice(1);
87 commands.push({ args, ...(input.cwd ? { cwd: input.cwd } : {}) });
88 const command = args[0];
89 const session = args[args.indexOf("--session") + 1]!;
90 const configFile = args[args.indexOf("--config") + 1]!;
91 const stateDir = (
92 JSON.parse(yield* fs.readFileString(configFile)) as { stateDir: string }
93 ).stateDir;
94 if (command === "open") {
95 if (options.failOpen) {
96 return {
97 stdout: JSON.stringify(options.failOpen),
98 stderr: "",
99 code: ChildProcessSpawner.ExitCode(1),
100 timedOut: false,
101 stdoutTruncated: false,
102 stderrTruncated: false,
103 stdoutInvalidUtf8: false,
104 stderrInvalidUtf8: false,
105 };
106 }
107 if (options.holdOpen) {
108 yield* Deferred.succeed(options.holdOpen.entered, undefined);
109 yield* Deferred.await(options.holdOpen.proceed);
110 }
111 const dir = path.join(stateDir, "remote-connections");
112 yield* fs.makeDirectory(dir, { recursive: true });
113 yield* fs.writeFileString(
114 path.join(dir, `${session}.json`),
115 JSON.stringify({ leaseId: `lease-${++leases}` }),
116 );
117 }
118 if (command === "screenshot") yield* fs.writeFile(args[1]!, new Uint8Array([1, 2, 3]));
119 return {
120 stdout: "{}",
121 stderr: "",
122 code: ChildProcessSpawner.ExitCode(0),
123 timedOut: false,
124 stdoutTruncated: false,
125 stderrTruncated: false,
126 stdoutInvalidUtf8: false,
127 stderrInvalidUtf8: false,
128 };
129 }).pipe(Effect.orDie),
130 });
131 const ran = (command: string) => commands.filter(({ args }) => args[0] === command);
132 return {
133 http,
134 runner,
135 requests,
136 ran,
137 endLeases: () => {
138 renewStatus = 404;
139 },
140 setCatalog: (next: unknown) => {
141 catalog = next;
142 },
143 };
144 });
145​
146const makeSecretResolver = (values: Record<string, string> = {}) =>
147 Layer.succeed(SecretResolver.SecretResolver, {
148 read: (references) =>
149 Effect.succeed(references.map((reference) => values[reference.reference])),
150 invalidate: () => Effect.void,
151 label: (provider) => Effect.succeed(provider),
152 });
153​
154const layerWith = (secrets: Layer.Layer<SecretResolver.SecretResolver>) =>
155 Layer.mergeAll(
156 ServerConfig.layerTest(process.cwd(), { prefix: "t3-simlock-host-" }),
157 secrets,
158 ).pipe(Layer.provideMerge(NodeServices.layer));
159​
160const layer = layerWith(makeSecretResolver());
161​
162const tokenSource = { provider: SecretProviderId.make("homelab"), reference: "simlock#token" };
163​
164const makeHost = (
165 pool: Effect.Success<ReturnType<typeof fakePool>>,
166 hostConfig: SimlockDeviceHostConfig,
167 scope: Scope.Scope,
168) =>
169 SimlockDeviceHost.make(
170 hostConfig,
171 Effect.succeed({ nodePath: "node", entryPath: "/tools/agent-device.mjs" }),
172 ).pipe(
173 Effect.provideService(HttpClient.HttpClient, pool.http),
174 Effect.provideService(ProcessRunner.ProcessRunner, pool.runner),
175 Effect.provideService(Scope.Scope, scope),
176 );
177​
178describe("Simlock device host", () => {
179 it.effect("leases a pool device for agents and serves its screen and input to the panel", () =>
180 Effect.gen(function* () {
181 const pool = yield* fakePool();
182 const fs = yield* FileSystem.FileSystem;
183 const workspace = yield* fs.makeTempDirectoryScoped();
184 const scope = yield* Scope.make();
185 const host = yield* makeHost(pool, config(), scope);
186 const ready = yield* host.ensureReady(() => Effect.void);
187​
188 const catalog = (yield* ready.devices.list).devices;
189 // Only models a worker can make right now, and only phones and tablets.
190 expect(catalog.map((device) => [device.id, device.version])).toEqual([
191 ["simlock:ios:iPhone 17 Pro", "iOS 27.0"],
192 ["simlock:android:Pixel 9", "Android 36"],
193 ]);
194​
195 const leaseId = yield* ready.devices.boot(catalog[1]!, { workspaceRoot: workspace });
196 expect(leaseId).toBe("lease-1");
197 const [open] = pool.ran("open");
198 expect(open?.args.slice(0, 5)).toEqual([
199 "open",
200 "--platform",
201 "android",
202 "--device",
203 "Pixel 9",
204 ]);
205 expect(open?.cwd).toBe(yield* fs.realPath(workspace));
206 expect(pool.ran("connect")[0]?.args).toContain(`${GATEWAY}/v1/agent-device`);
207​
208 const binding = yield* ready.devices.agentEndpoint!(leaseId);
209 expect(binding).toMatchObject({
210 baseUrl: `${GATEWAY}/v1/agent-device`,
211 token: "agent-token",
212 agentDeviceId: null,
213 session: open?.args[open.args.indexOf("--session") + 1],
214 installDirectory: yield* fs.realPath(workspace),
215 });
216 expect((yield* ready.devices.list).devices.at(-1)).toMatchObject({
217 id: "lease-1",
218 name: "Pixel 9",
219 booted: true,
220 });
221​
222 expect(
223 yield* ready.upstream!(
224 "/vendor/serve-sim/helper/lease-1/stream.avcc",
225 new URLSearchParams(),
226 ),
227 ).toEqual({
228 url: `${GATEWAY}/v1/leases/lease-1/stream`,
229 headers: { authorization: "Bearer agent-token" },
230 protocols: [],
231 });
232 expect(
233 yield* ready.upstream!(
234 "/vendor/serve-sim/helper/ws",
235 new URLSearchParams({ device: "lease-1" }),
236 ),
237 ).toEqual({
238 url: `${GATEWAY}/v1/leases/lease-1/input`,
239 headers: { authorization: "Bearer agent-token" },
240 protocols: [],
241 });
242 expect(
243 yield* ready.upstream!("/vendor/serve-sim/helper/lease-1/config", new URLSearchParams()),
244 ).toBeNull();
245 expect(
246 yield* ready.upstream!(
247 "/vendor/serve-sim/helper/ws",
248 new URLSearchParams({ device: "lease-2" }),
249 ),
250 ).toBeNull();
251 expect([...(yield* ready.devices.screenshot({ ...catalog[1]!, id: leaseId }))]).toEqual([
252 1, 2, 3,
253 ]);
254​
255 yield* ready.devices.shutdown({ id: leaseId, platform: "android" });
256 expect(pool.ran("close")).toHaveLength(1);
257 expect((yield* ready.devices.list).devices.some((device) => device.booted)).toBe(false);
258 yield* Scope.close(scope, Exit.void);
259 expect(pool.ran("close")).toHaveLength(1);
260 }).pipe(Effect.scoped, Effect.provide(layer)),
261 );
262​
263 it.effect("caps leases at the limit", () =>
264 Effect.gen(function* () {
265 const pool = yield* fakePool();
266 const scope = yield* Scope.make();
267 const host = yield* makeHost(pool, config({ maxLeases: 1 }), scope);
268 const ready = yield* host.ensureReady(() => Effect.void);
269 const [iphone] = (yield* ready.devices.list).devices;
270 yield* ready.devices.boot(iphone!);
271​
272 const refused = yield* ready.devices.boot(iphone!).pipe(Effect.flip);
273 expect(refused.message).toContain("limited to 1 device");
274 expect(pool.ran("open")).toHaveLength(1);
275 yield* Scope.close(scope, Exit.void);
276 expect(pool.ran("close")).toHaveLength(1);
277 }).pipe(Effect.scoped, Effect.provide(layer)),
278 );
279​
280 it.effect("reports its platforms unavailable when the gateway refuses the agent token", () =>
281 Effect.gen(function* () {
282 const pool = yield* fakePool();
283 const scope = yield* Scope.make();
284 const host = yield* makeHost(pool, config({ agentToken: "revoked" }), scope);
285 expect(yield* host.platformAvailability("ios")).toEqual({
286 platform: "ios",
287 available: false,
288 reason: "The Simlock gateway refused the agent token for Device pool.",
289 });
290 expect((yield* host.summary).platforms.every((platform) => !platform.available)).toBe(true);
291 yield* Scope.close(scope, Exit.void);
292 }).pipe(Effect.scoped, Effect.provide(layer)),
293 );
294​
295 it.effect("sends saved and machine tokens only to the gateway each belongs to", () =>
296 Effect.gen(function* () {
297 const pool = yield* fakePool();
298 const availability = (baseUrl: string) =>
299 Effect.gen(function* () {
300 const scope = yield* Scope.make();
301 const host = yield* makeHost(pool, config({ agentToken: "" }), scope).pipe(
302 Effect.provideService(HostProcess.Environment, {
303 AGENT_DEVICE_DAEMON_BASE_URL: baseUrl,
304 AGENT_DEVICE_DAEMON_AUTH_TOKEN: "agent-token",
305 }),
306 );
307 const result = yield* host.platformAvailability("ios");
308 yield* Scope.close(scope, Exit.void);
309 return result;
310 });
311​
312 expect(
313 yield* SimlockDeviceHost.resolveSimlockToken(
314 yield* SecretResolver.SecretResolver,
315 "https://elsewhere.example:4700",
316 config({ agentToken: "saved-token" }),
317 {},
318 ),
319 ).toBeNull();
320​
321 expect(yield* availability(`${GATEWAY}/v1/agent-device`)).toEqual({
322 platform: "ios",
323 available: true,
324 });
325 const requests = pool.requests.length;
326 expect(yield* availability("https://elsewhere.example:4700/v1/agent-device")).toEqual({
327 platform: "ios",
328 available: false,
329 reason: "Add an agent token for Device pool in Settings.",
330 });
331 expect(pool.requests).toHaveLength(requests);
332 }).pipe(Effect.scoped, Effect.provide(layer)),
333 );
334​
335 it.effect("reads its agent token from the secret provider the setting names", () =>
336 Effect.gen(function* () {
337 const pool = yield* fakePool();
338 const scope = yield* Scope.make();
339 const host = yield* makeHost(pool, config({ agentToken: tokenSource }), scope);
340 const ready = yield* host.ensureReady(() => Effect.void);
341 const [iphone] = (yield* ready.devices.list).devices;
342 const leaseId = yield* ready.devices.boot(iphone!);
343 expect(yield* ready.devices.agentEndpoint!(leaseId)).toMatchObject({ token: "agent-token" });
344 yield* Scope.close(scope, Exit.void);
345 expect(pool.ran("close")).toHaveLength(1);
346 }).pipe(
347 Effect.scoped,
348 Effect.provide(layerWith(makeSecretResolver({ "simlock#token": "agent-token" }))),
349 ),
350 );
351​
352 it.effect("fails instead of using this machine's token when its secret cannot be read", () =>
353 Effect.gen(function* () {
354 const pool = yield* fakePool();
355 const scope = yield* Scope.make();
356 const host = yield* makeHost(pool, config({ agentToken: tokenSource }), scope).pipe(
357 Effect.provideService(HostProcess.Environment, {
358 AGENT_DEVICE_DAEMON_BASE_URL: `${GATEWAY}/v1/agent-device`,
359 AGENT_DEVICE_DAEMON_AUTH_TOKEN: "agent-token",
360 }),
361 );
362 const availability = yield* host.platformAvailability("ios");
363 expect(availability.available).toBe(false);
364 expect(availability.reason).toContain('secret provider "homelab"');
365 expect(pool.requests).toEqual([]);
366 yield* Scope.close(scope, Exit.void);
367 }).pipe(Effect.scoped, Effect.provide(layer)),
368 );
369​
370 it.effect("lists a platform again as soon as its workers return", () =>
371 Effect.gen(function* () {
372 const pool = yield* fakePool();
373 const scope = yield* Scope.make();
374 const host = yield* makeHost(pool, config(), scope);
375 const ready = yield* host.ensureReady(() => Effect.void);
376 pool.setCatalog({ platforms: [] });
377 expect((yield* ready.devices.list).devices).toEqual([]);
378 // Mid-reconnect, the gateway knows the Android models but no worker that makes them.
379 pool.setCatalog({
380 platforms: [
381 CATALOG.platforms[0]!,
382 { ...CATALOG.platforms[1]!, modelWorkers: { "Pixel 9": [], "Pixel 8": [] } },
383 ],
384 });
385 const during = (yield* ready.devices.list).devices;
386 expect(during.some((device) => device.platform === "android")).toBe(false);
387 pool.setCatalog(CATALOG);
388 const after = (yield* ready.devices.list).devices;
389 expect(after.some((device) => device.platform === "android")).toBe(true);
390 yield* Scope.close(scope, Exit.void);
391 }).pipe(Effect.scoped, Effect.provide(layer)),
392 );
393​
394 it.effect("closes the session of a failed open so no lease is left behind", () =>
395 Effect.gen(function* () {
396 const pool = yield* fakePool({
397 failOpen: { error: { message: "No device matches.", hint: "Retry." } },
398 });
399 const scope = yield* Scope.make();
400 const host = yield* makeHost(pool, config(), scope);
401 const ready = yield* host.ensureReady(() => Effect.void);
402 const [iphone] = (yield* ready.devices.list).devices;
403 const failure = yield* ready.devices.boot(iphone!).pipe(Effect.flip);
404 expect(String(failure.cause)).toContain("No device matches. Retry.");
405 expect(pool.ran("close")).toHaveLength(1);
406 // The failed open freed its slot.
407 expect(yield* ready.devices.boot(iphone!).pipe(Effect.flip)).toBeDefined();
408 yield* Scope.close(scope, Exit.void);
409 }).pipe(Effect.scoped, Effect.provide(layer)),
410 );
411​
412 it.effect("tells the user what agent-device reported when a device fails to open", () =>
413 Effect.gen(function* () {
414 const pool = yield* fakePool({
415 failOpen: {
416 success: false,
417 error: {
418 code: "COMMAND_FAILED",
419 message: "Invalid daemon response",
420 cause: {
421 message: "Unexpected non-whitespace character after JSON at position 4",
422 },
423 hint: "Retry with --debug and inspect diagnostics log for details.",
424 details: { requestId: "22a6bf10a567b2a1", line: "404 Not Found" },
425 },
426 },
427 });
428 const scope = yield* Scope.make();
429 const host = yield* makeHost(pool, config(), scope);
430 const ready = yield* host.ensureReady(() => Effect.void);
431 const pixel = (yield* ready.devices.list).devices.find(({ name }) => name === "Pixel 9");
432 const failure = yield* ready.devices.boot(pixel!).pipe(Effect.flip);
433 expect(failure.message).not.toContain("The device host sent no details.");
434 expect(failure.message).toContain("Invalid daemon response");
435 expect(failure.message).toContain("404 Not Found");
436 yield* Scope.close(scope, Exit.void);
437​
438 // A details bag without a usable response line still keeps agent-device's message.
439 const oddPool = yield* fakePool({
440 failOpen: { error: { message: "Invalid daemon response", details: { line: 17 } } },
441 });
442 const oddScope = yield* Scope.make();
443 const oddReady = yield* (yield* makeHost(oddPool, config(), oddScope)).ensureReady(
444 () => Effect.void,
445 );
446 const [oddDevice] = (yield* oddReady.devices.list).devices;
447 const oddFailure = yield* oddReady.devices.boot(oddDevice!).pipe(Effect.flip);
448 expect(oddFailure.message).toContain("Invalid daemon response");
449 yield* Scope.close(oddScope, Exit.void);
450 }).pipe(Effect.scoped, Effect.provide(layer)),
451 );
452​
453 it.effect("keeps a watched lease renewed and stops listing it once Simlock ends it", () =>
454 Effect.gen(function* () {
455 const pool = yield* fakePool();
456 const scope = yield* Scope.make();
457 const host = yield* makeHost(pool, config(), scope);
458 const ready = yield* host.ensureReady(() => Effect.void);
459 const [iphone] = (yield* ready.devices.list).devices;
460 const leaseId = yield* ready.devices.boot(iphone!);
461​
462 yield* TestClock.adjust("2 minutes");
463 expect(pool.requests).toContain(`POST /v1/leases/${leaseId}/renew`);
464 expect((yield* ready.devices.list).devices.some((device) => device.id === leaseId)).toBe(
465 true,
466 );
467​
468 pool.endLeases();
469 yield* TestClock.adjust("2 minutes");
470 expect((yield* ready.devices.list).devices.some((device) => device.id === leaseId)).toBe(
471 false,
472 );
473 yield* Scope.close(scope, Exit.void);
474 expect(pool.ran("close")).toHaveLength(0);
475 }).pipe(Effect.scoped, Effect.provide(layer)),
476 );
477​
478 it.effect("returns a lease whose open finishes after the host stopped", () =>
479 Effect.gen(function* () {
480 const holdOpen = {
481 entered: yield* Deferred.make<void>(),
482 proceed: yield* Deferred.make<void>(),
483 };
484 const pool = yield* fakePool({ holdOpen });
485 const host = yield* makeHost(pool, config(), yield* Scope.make());
486 const ready = yield* host.ensureReady(() => Effect.void);
487 const [iphone] = (yield* ready.devices.list).devices;
488 const opening = yield* Effect.forkChild(ready.devices.boot(iphone!));
489 yield* Deferred.await(holdOpen.entered);
490 yield* host.stop;
491 yield* Deferred.succeed(holdOpen.proceed, undefined);
492 expect(Exit.isFailure(yield* Fiber.await(opening))).toBe(true);
493 expect(pool.ran("close")).toHaveLength(1);
494 expect((yield* ready.devices.list).devices.every((device) => !device.booted)).toBe(true);
495 }).pipe(Effect.scoped, Effect.provide(layer)),
496 );
497​
498 it.effect("holds a lease for its full maximum age, then returns it", () =>
499 Effect.gen(function* () {
500 const pool = yield* fakePool();
501 const host = yield* makeHost(pool, config({ maxLeaseMinutes: 5 }), yield* Scope.make());
502 const ready = yield* host.ensureReady(() => Effect.void);
503 const [iphone] = (yield* ready.devices.list).devices;
504 yield* ready.devices.boot(iphone!);
505​
506 yield* TestClock.adjust("299 seconds");
507 expect(pool.ran("close")).toHaveLength(0);
508 yield* TestClock.adjust("1 second");
509 expect(pool.ran("close")).toHaveLength(1);
510 }).pipe(Effect.scoped, Effect.provide(layer)),
511 );
512});