Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - exercises concurrent real CLI subprocesses.
2import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
3import { describe, expect, it } from "@effect/vitest";
4import * as NodeChildProcess from "node:child_process";
5import * as NodeUtil from "node:util";
6import * as NodeServices from "@effect/platform-node/NodeServices";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Path from "effect/Path";
10import { ensureAgentDeviceShim } from "./AgentDeviceShim.ts";
11import {
12 agentDeviceConfigPath,
13 agentDeviceSession,
14 writeAgentDeviceConfig,
15} from "./AgentDeviceTarget.ts";
16
17const exec = NodeUtil.promisify(NodeChildProcess.execFile);
18
19describe("host-bound agent commands", () => {
20 it.effect("runs two hosts concurrently and only updates the reconnected host", () =>
21 Effect.gen(function* () {
22 const fs = yield* FileSystem.FileSystem;
23 const path = yield* Path.Path;
24 const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-target-" });
25 const platform = yield* HostProcessPlatform;
26 const dir = path.join(
27 temp,
28 platform === "win32" ? "paths with spaces" : "quotes '\" $HOME `literal`",
29 );
30 yield* fs.makeDirectory(dir);
31 const entryPath = path.join(dir, "cli.mjs");
32 yield* fs.writeFileString(
33 entryPath,
34 `import { readFileSync } from 'node:fs';
35const args = process.argv.slice(2);
36console.log(readFileSync(args[args.indexOf('--config') + 1], 'utf8'));
37if (process.env.AGENT_DEVICE_DAEMON_BASE_URL) process.exit(2);`,
38 );
39 const shim = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir });
40 const files = ["mini", "android"].map((host) => agentDeviceConfigPath(dir, host, path));
41 for (const [index, file] of files.entries())
42 yield* writeAgentDeviceConfig(file, {
43 baseUrl: `http://127.0.0.1:${1000 + index}`,
44 token: `token-${index}`,
45 entryPath,
46 });
47 const invoke = (file: string) =>
48 exec(
49 platform === "win32" ? process.execPath : path.join(shim, "agent-device"),
50 [
51 ...(platform === "win32" ? [path.join(shim, "agent-device-launcher.mjs")] : []),
52 "snapshot",
53 "--config",
54 file,
55 "--session",
56 "test-session",
57 ],
58 { env: { ...process.env, AGENT_DEVICE_DAEMON_BASE_URL: "http://wrong-host" } },
59 ).then((result) => JSON.parse(result.stdout));
60 expect(yield* Effect.promise(() => Promise.all(files.map(invoke)))).toEqual([
61 { daemonBaseUrl: "http://127.0.0.1:1000", daemonAuthToken: "token-0" },
62 { daemonBaseUrl: "http://127.0.0.1:1001", daemonAuthToken: "token-1" },
63 ]);
64 const second = yield* fs.readFileString(files[1]!);
65 yield* writeAgentDeviceConfig(files[0]!, {
66 baseUrl: "http://127.0.0.1:2000",
67 token: "new",
68 entryPath,
69 });
70 expect((yield* Effect.promise(() => invoke(files[0]!))).daemonAuthToken).toBe("new");
71 expect(yield* fs.readFileString(files[1]!)).toBe(second);
72 expect(agentDeviceSession("thread", "mini", "same-id")).not.toBe(
73 agentDeviceSession("thread", "android", "same-id"),
74 );
75 for (const args of [
76 ["snapshot"],
77 ["snapshot", "--config", files[0]!],
78 ["snapshot", "--config", "help"],
79 ["snapshot", "--config", files[0]!, "--session"],
80 ]) {
81 yield* Effect.promise(() =>
82 expect(
83 exec(process.execPath, [path.join(shim, "agent-device-launcher.mjs"), ...args]),
84 ).rejects.toThrow("Call device_open first"),
85 );
86 }
87 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
88 );
89});