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