Repository navigation
Expand file tree
/
Copy pathAgentDeviceTarget.test.ts
More file actions
483 lines (473 loc) · 19.5 KB
/
Copy pathAgentDeviceTarget.test.ts
File metadata and controls
483 lines (473 loc) · 19.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
471
472
473
474
475
476
477
478
479
480
481
482
483
// @effect-diagnostics nodeBuiltinImport:off - exercises concurrent real CLI subprocesses.
import * as HostProcess from "@t3tools/shared/HostProcess";
import { describe, expect, it } from "@effect/vitest";
import * as NodeChildProcess from "node:child_process";
import * as NodeNet from "node:net";
import * as NodeUtil from "node:util";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Layer from "effect/Layer";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import {
agentDeviceCloseOutcome,
bindAgentDeviceLauncher,
ensureAgentDeviceShim,
pruneAgentDeviceLaunchers,
} from "./AgentDeviceShim.ts";
import {
agentDeviceConfigPath,
agentDeviceSession,
writeAgentDeviceConfig,
} from "./AgentDeviceTarget.ts";
const exec = NodeUtil.promisify(NodeChildProcess.execFile);
/** A loopback port that accepts connections until the scope closes, like T3's device connection. */
const listeningPort = Effect.acquireRelease(
Effect.promise(
() =>
new Promise<NodeNet.Server>((resolve) => {
const server = NodeNet.createServer((socket) => socket.destroy());
server.listen(0, "127.0.0.1", () => resolve(server));
}),
),
(server) => Effect.promise(() => new Promise<void>((resolve) => server.close(() => resolve()))),
).pipe(Effect.map((server) => (server.address() as NodeNet.AddressInfo).port));
describe("host-bound agent commands", () => {
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
"runs both desktop hops as Node with a stripped environment, preserving arguments and exit code",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-target-" });
const dir = path.join(temp, "quotes '\" $HOME `literal`");
yield* fs.makeDirectory(dir);
const desktopApp = path.join(dir, "T3 Code");
// Dropping Node mode after checking it forces each hop to set it independently.
yield* fs.writeFileString(
desktopApp,
`#!/bin/sh
if [ "\${ELECTRON_RUN_AS_NODE:-}" != "1" ]; then
echo "started the desktop app" >&2
exit 134
fi
unset ELECTRON_RUN_AS_NODE
exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@"
`,
);
yield* fs.chmod(desktopApp, 0o755);
const entryPath = path.join(dir, "cli.mjs");
yield* fs.writeFileString(
entryPath,
"console.log(JSON.stringify(process.argv.slice(2))); process.exitCode = 23;",
);
const shim = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir }).pipe(
Effect.provideService(HostProcess.ExecutablePath, desktopApp),
Effect.provideService(HostProcess.IsExecutable, false),
);
const args = [
"snapshot",
"--config",
path.join(dir, "config.json"),
"--session",
"a!b!c",
"",
];
const hostPath = (yield* HostProcess.Environment).PATH ?? "";
yield* Effect.promise(() =>
expect(
exec(path.join(shim, "agent-device"), args, {
env: { PATH: hostPath, HOME: temp },
}),
).rejects.toMatchObject({ code: 23, stdout: `${JSON.stringify(args)}\n`, stderr: "" }),
);
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect("binds one device's flags into its launcher and leaves help alone", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-bound-" });
const entryPath = path.join(dir, "cli.mjs");
yield* fs.writeFileString(entryPath, "console.log(JSON.stringify(process.argv.slice(2)));");
const target = [
"--platform",
"ios",
"--udid",
"UDID-1",
"--config",
"/h.json",
"--session",
"t3-a",
];
const command = yield* bindAgentDeviceLauncher({
entryPath,
stateDir: dir,
session: "t3-a",
targetArgs: target,
});
const shared = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir });
const launch = (launcher: string, args: ReadonlyArray<string>) =>
exec(process.execPath, [path.join(launcher, "agent-device-launcher.mjs"), ...args]).then(
(result) => JSON.parse(result.stdout),
);
const bound = path.dirname(command);
expect(yield* Effect.promise(() => launch(bound, ["snapshot", "-i"]))).toEqual([
"snapshot",
"-i",
...target,
]);
// A flag the agent passes itself wins over the bound one.
expect(yield* Effect.promise(() => launch(bound, ["open", "--session", "mine"]))).toEqual([
"open",
"--session",
"mine",
"--platform",
"ios",
"--udid",
"UDID-1",
"--config",
"/h.json",
]);
expect(yield* Effect.promise(() => launch(bound, ["snapshot", "--session=mine"]))).toEqual([
"snapshot",
"--session=mine",
"--platform",
"ios",
"--udid",
"UDID-1",
"--config",
"/h.json",
]);
// After `--` everything is text: no help, no overrides, and bound flags go before it.
for (const text of [["--help"], ["literal text", "--session", "mine"]]) {
expect(yield* Effect.promise(() => launch(bound, ["fill", "@e5", "--", ...text]))).toEqual([
"fill",
"@e5",
...target,
"--",
...text,
]);
}
expect(
yield* Effect.promise(() =>
launch(shared, ["snapshot", "--config=/h.json", "--session=t3-a"]),
),
).toEqual(["snapshot", "--config=/h.json", "--session=t3-a"]);
yield* Effect.promise(() =>
expect(
launch(shared, ["fill", "@e5", "--", "--config", "/h.json", "--session", "t3-a"]),
).rejects.toThrow("Call device_open first"),
);
for (const args of [["help", "gesture"], ["open", "--help"], ["record", "-h"], []]) {
expect(yield* Effect.promise(() => launch(bound, args))).toEqual(args);
expect(yield* Effect.promise(() => launch(shared, args))).toEqual(args);
}
yield* fs.remove(entryPath);
yield* Effect.promise(() =>
expect(launch(bound, ["snapshot"])).rejects.toThrow("Call device_open again"),
);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.ExecutablePath, process.execPath),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
it.effect("passes each command only the bound flags agent-device accepts for it", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-commands-" });
const entryPath = path.join(dir, "cli.mjs");
yield* fs.writeFileString(entryPath, "console.log(JSON.stringify(process.argv.slice(2)));");
const command = yield* bindAgentDeviceLauncher({
entryPath,
stateDir: dir,
session: "t3-a",
targetArgs: [
"--platform",
"ios",
"--udid",
"UDID-1",
"--config",
"/h.json",
"--session",
"t3-a",
],
});
const launch = (args: ReadonlyArray<string>) =>
exec(process.execPath, [
path.join(path.dirname(command), "agent-device-launcher.mjs"),
...args,
]).then((result) => JSON.parse(result.stdout));
// agent-device 0.21.31 rejects these flags with "unknown option" or "not supported for command".
const cdp = ["cdp", "target", "list", "--url", "http://127.0.0.1:8081"];
expect(yield* Effect.promise(() => launch(cdp))).toEqual(cdp);
expect(yield* Effect.promise(() => launch(["connection"]))).toEqual([
"connection",
"--config",
"/h.json",
"--session",
"t3-a",
]);
// The command is found past leading flags and their values, in either form.
for (const config of [["--config", "/o.json"], ["--config=/o.json"]]) {
expect(yield* Effect.promise(() => launch([...config, "connection"]))).toEqual([
...config,
"connection",
"--session",
"t3-a",
]);
// device status reads claims on the machine it runs on, which is not the device's host.
yield* Effect.promise(() =>
expect(launch([...config, "device", "status"])).rejects.toThrow(
"`device status` reads this machine's device claims, not those on the device's host",
),
);
}
expect(yield* Effect.promise(() => launch(["--json", ...cdp]))).toEqual(["--json", ...cdp]);
for (const managed of [
["connect"],
["disconnect"],
["auth", "status"],
["daemon", "stop"],
["--config", "/o.json", "auth", "status"],
["--", "connect"],
]) {
yield* Effect.promise(() =>
expect(launch(managed)).rejects.toThrow(
`T3 Code manages this command's connection to the device, so \`${managed.find((arg) => ["connect", "disconnect", "auth", "daemon"].includes(arg))}\` is not available through it.`,
),
);
}
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.ExecutablePath, process.execPath),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
it("tells a released session from a close that failed or timed out", () => {
const result = (code: number | null, stderr = "", timedOut = false) => ({
stdout: "",
stderr,
code,
timedOut,
});
expect(agentDeviceCloseOutcome(result(0))).toBe("closed");
// A session that never ran `open` has nothing to release.
expect(
agentDeviceCloseOutcome(
result(1, "Error (SESSION_NOT_FOUND): No active session. Run open first."),
),
).toBe("no-session");
expect(
agentDeviceCloseOutcome(result(1, "Error (COMMAND_FAILED): Remote daemon is unavailable")),
).toBe("failed");
expect(agentDeviceCloseOutcome(result(null, "", true))).toBe("timed-out");
});
it.live("stops before agent-device when T3's device connection is down", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-endpoint-" });
const entryPath = path.join(dir, "cli.mjs");
yield* fs.writeFileString(entryPath, "console.log(JSON.stringify(process.argv.slice(2)));");
const configPath = path.join(dir, "host.json");
// A port that was just free: nothing listens on it, so a connect is refused.
const port = yield* Effect.promise(
() =>
new Promise<number>((resolve) => {
const server = NodeNet.createServer();
server.listen(0, "127.0.0.1", () => {
const address = server.address() as NodeNet.AddressInfo;
server.close(() => resolve(address.port));
});
}),
);
yield* writeAgentDeviceConfig(configPath, {
baseUrl: `http://127.0.0.1:${port}`,
token: "secret",
});
const command = yield* bindAgentDeviceLauncher({
entryPath,
stateDir: dir,
session: "t3-a",
targetArgs: ["--config", configPath, "--session", "t3-a"],
});
const launch = (args: ReadonlyArray<string>) =>
exec(process.execPath, [
path.join(path.dirname(command), "agent-device-launcher.mjs"),
...args,
]);
yield* Effect.promise(() =>
expect(launch(["screenshot", "/tmp/shot.png"])).rejects.toMatchObject({
code: 1,
stdout: "",
stderr: `T3 Code's device connection at 127.0.0.1:${port} is not reachable from this shell (connect ECONNREFUSED). Call device_open again to reconnect; if that succeeds and this repeats, this shell runs in a network sandbox, so run the command with network access, outside the sandbox.\n`,
}),
);
// An explicit --daemon-base-url wins over the config, as it does in agent-device.
const override = `http://127.0.0.1:${yield* listeningPort}`;
for (const flag of [["--daemon-base-url", override], [`--daemon-base-url=${override}`]]) {
expect(
JSON.parse(
(yield* Effect.promise(() => launch(["screenshot", "/tmp/shot.png", ...flag]))).stdout,
),
).toEqual([
"screenshot",
"/tmp/shot.png",
...flag,
"--config",
configPath,
"--session",
"t3-a",
]);
}
// cdp talks to Metro, not to T3's connection, so it still runs.
expect(
JSON.parse((yield* Effect.promise(() => launch(["cdp", "target", "list"]))).stdout),
).toEqual(["cdp", "target", "list"]);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.ExecutablePath, process.execPath),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
it.effect("prunes the launchers of ended sessions and keeps the shared one", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-prune-" });
const entryPath = path.join(stateDir, "cli.mjs");
const shared = yield* ensureAgentDeviceShim({ entryPath, stateDir });
const bind = (session: string) =>
bindAgentDeviceLauncher({ entryPath, stateDir, session, targetArgs: [] });
const kept = yield* bind("t3-open");
const ended = yield* bind("t3-closed");
yield* pruneAgentDeviceLaunchers({ stateDir, live: new Set(["t3-open"]) });
expect(yield* fs.exists(kept)).toBe(true);
expect(yield* fs.exists(path.dirname(ended))).toBe(false);
expect(yield* fs.exists(shared)).toBe(true);
yield* pruneAgentDeviceLaunchers({ stateDir, live: new Set() });
expect(yield* fs.readDirectory(path.join(stateDir, "device", "targets"))).toEqual([]);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.ExecutablePath, process.execPath),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
"tells the agent to reopen the device when the runtime it was bound to is gone",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-stale-" });
const runtime = path.join(dir, "old-node");
yield* fs.writeFileString(runtime, "#!/bin/sh\nexit 0\n");
yield* fs.chmod(runtime, 0o755);
const command = yield* bindAgentDeviceLauncher({
entryPath: path.join(dir, "cli.mjs"),
stateDir: dir,
session: "t3-a",
targetArgs: ["--config", "/h.json", "--session", "t3-a"],
}).pipe(Effect.provideService(HostProcess.ExecutablePath, runtime));
yield* fs.remove(runtime);
yield* Effect.promise(() =>
expect(exec(command, ["snapshot"])).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining("Call device_open again"),
}),
);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
it.effect("runs two hosts concurrently and only updates the reconnected host", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-target-" });
const platform = yield* HostProcess.Platform;
const dir = path.join(
temp,
platform === "win32" ? "paths with spaces" : "quotes '\" $HOME `literal`",
);
yield* fs.makeDirectory(dir);
const entryPath = path.join(dir, "cli.mjs");
yield* fs.writeFileString(
entryPath,
`import { readFileSync } from 'node:fs';
const args = process.argv.slice(2);
console.log(readFileSync(args[args.indexOf('--config') + 1], 'utf8'));
if (process.env.AGENT_DEVICE_DAEMON_BASE_URL) process.exit(2);`,
);
const shim = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir });
const files = yield* Effect.forEach(["mini", "android"], (host) =>
agentDeviceConfigPath(dir, host, path),
);
const ports = [yield* listeningPort, yield* listeningPort, yield* listeningPort];
for (const [index, file] of files.entries())
yield* writeAgentDeviceConfig(file, {
baseUrl: `http://127.0.0.1:${ports[index]}`,
token: `token-${index}`,
});
const invoke = (file: string) =>
exec(
platform === "win32" ? process.execPath : path.join(shim, "agent-device"),
[
...(platform === "win32" ? [path.join(shim, "agent-device-launcher.mjs")] : []),
"snapshot",
"--config",
file,
"--session",
"test-session",
],
{ env: { ...process.env, AGENT_DEVICE_DAEMON_BASE_URL: "http://wrong-host" } },
).then((result) => JSON.parse(result.stdout));
expect(yield* Effect.promise(() => Promise.all(files.map(invoke)))).toEqual([
{ daemonBaseUrl: `http://127.0.0.1:${ports[0]}`, daemonAuthToken: "token-0" },
{ daemonBaseUrl: `http://127.0.0.1:${ports[1]}`, daemonAuthToken: "token-1" },
]);
const second = yield* fs.readFileString(files[1]!);
yield* writeAgentDeviceConfig(files[0]!, {
baseUrl: `http://127.0.0.1:${ports[2]}`,
token: "new",
});
expect((yield* Effect.promise(() => invoke(files[0]!))).daemonAuthToken).toBe("new");
expect(yield* fs.readFileString(files[1]!)).toBe(second);
// Hosts with a daemon per device get a file per device, apart from the host's own.
const perDevice = yield* Effect.forEach(["udid-1", "udid-2"], (device) =>
agentDeviceConfigPath(dir, "eas", path, device),
);
expect(new Set([...perDevice, yield* agentDeviceConfigPath(dir, "eas", path)]).size).toBe(3);
expect(yield* agentDeviceSession("thread", "mini", "same-id")).not.toBe(
yield* agentDeviceSession("thread", "android", "same-id"),
);
for (const args of [
["snapshot"],
["snapshot", "--config", files[0]!],
["snapshot", "--config", "help"],
["snapshot", "--config", files[0]!, "--session"],
]) {
yield* Effect.promise(() =>
expect(
exec(process.execPath, [path.join(shim, "agent-device-launcher.mjs"), ...args]),
).rejects.toThrow("Call device_open first"),
);
}
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.IsExecutable, true),
Effect.provideService(HostProcess.ExecutablePath, "/packaged/t3"),
Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
),
);
});