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

AgentDeviceTarget.test.ts

98 lines3.9 KB

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