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

AgentDeviceTarget.test.ts

100 lines4.0 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 = yield* Effect.forEach(["mini", "android"], (host) =>
45 agentDeviceConfigPath(dir, host, path),
46 );
47 for (const [index, file] of files.entries())
48 yield* writeAgentDeviceConfig(file, {
49 baseUrl: `http://127.0.0.1:${1000 + index}`,
50 token: `token-${index}`,
51 entryPath,
52 });
53 const invoke = (file: string) =>
54 exec(
55 platform === "win32" ? process.execPath : path.join(shim, "agent-device"),
56 [
57 ...(platform === "win32" ? [path.join(shim, "agent-device-launcher.mjs")] : []),
58 "snapshot",
59 "--config",
60 file,
61 "--session",
62 "test-session",
63 ],
64 { env: { ...process.env, AGENT_DEVICE_DAEMON_BASE_URL: "http://wrong-host" } },
65 ).then((result) => JSON.parse(result.stdout));
66 expect(yield* Effect.promise(() => Promise.all(files.map(invoke)))).toEqual([
67 { daemonBaseUrl: "http://127.0.0.1:1000", daemonAuthToken: "token-0" },
68 { daemonBaseUrl: "http://127.0.0.1:1001", daemonAuthToken: "token-1" },
69 ]);
70 const second = yield* fs.readFileString(files[1]!);
71 yield* writeAgentDeviceConfig(files[0]!, {
72 baseUrl: "http://127.0.0.1:2000",
73 token: "new",
74 entryPath,
75 });
76 expect((yield* Effect.promise(() => invoke(files[0]!))).daemonAuthToken).toBe("new");
77 expect(yield* fs.readFileString(files[1]!)).toBe(second);
78 expect(yield* agentDeviceSession("thread", "mini", "same-id")).not.toBe(
79 yield* agentDeviceSession("thread", "android", "same-id"),
80 );
81 for (const args of [
82 ["snapshot"],
83 ["snapshot", "--config", files[0]!],
84 ["snapshot", "--config", "help"],
85 ["snapshot", "--config", files[0]!, "--session"],
86 ]) {
87 yield* Effect.promise(() =>
88 expect(
89 exec(process.execPath, [path.join(shim, "agent-device-launcher.mjs"), ...args]),
90 ).rejects.toThrow("Call device_open first"),
91 );
92 }
93 }).pipe(
94 Effect.scoped,
95 Effect.provideService(HostProcessIsExecutable, true),
96 Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
97 Effect.provide(NodeServices.layer),
98 ),
99 );
100});