Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off globalFetchInEffect:off - verifies generated remote scripts using real shell and Node processes.
2import * as Effect from "effect/Effect";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import { expect, it } from "@effect/vitest";
5import * as NodeChildProcess from "node:child_process";
6import * as NodeFSP from "node:fs/promises";
7import * as NodeOS from "node:os";
8import * as NodePath from "node:path";
9import * as NodeUtil from "node:util";
10import { quoteRemoteArg, remoteDeviceEnvironment, remoteDeviceScript } from "./sshDeviceScript.ts";
11
12const exec = NodeUtil.promisify(NodeChildProcess.execFile);
13
14it.effect("finds Android Studio Java for a non-interactive SSH session", () =>
15 Effect.gen(function* () {
16 if ((yield* HostProcess.Platform) === "win32") return;
17 yield* Effect.promise(async () => {
18 const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-java-"));
19 try {
20 const javaHome = NodePath.join(home, ".local/opt/android-studio/jbr");
21 await NodeFSP.mkdir(NodePath.join(javaHome, "bin"), { recursive: true });
22 await NodeFSP.writeFile(
23 NodePath.join(javaHome, "bin/java"),
24 "#!/bin/sh\necho test-java\n",
25 { mode: 0o755 },
26 );
27 const result = await exec("/bin/sh", ["-c", `${remoteDeviceEnvironment}\njava`], {
28 env: { HOME: home, PATH: "/nonexistent", JAVA_HOME: "" },
29 });
30 expect(result.stdout.trim()).toBe("test-java");
31 } finally {
32 await NodeFSP.rm(home, { recursive: true, force: true });
33 }
34 });
35 }),
36);
37
38it.effect("preserves shell metacharacters and newlines in remote arguments", () =>
39 Effect.gen(function* () {
40 if ((yield* HostProcess.Platform) === "win32") return;
41 yield* Effect.promise(async () => {
42 const value = "quotes ' \" ; $(echo expanded) $HOME\nnext line";
43 const result = await exec("sh", ["-c", `printf %s ${quoteRemoteArg(value)}`]);
44 expect(result.stdout).toBe(value);
45 });
46 }),
47);
48
49it.effect("stops a daemon left for its owner when starting without agent tools", () =>
50 Effect.gen(function* () {
51 if ((yield* HostProcess.Platform) === "win32") return;
52 yield* Effect.promise(async () => {
53 const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-start-"));
54 try {
55 const state = NodePath.join(home, ".t3/device/hosts/owner");
56 const daemonFile = NodePath.join(state, "daemon.json");
57 const calls = NodePath.join(home, "calls");
58 const failMarker = NodePath.join(home, "fail-stop");
59 const entry = NodePath.join(home, "agent-device.mjs");
60 await NodeFSP.mkdir(NodePath.join(home, "bin"));
61 await NodeFSP.mkdir(state, { recursive: true });
62 await NodeFSP.writeFile(
63 entry,
64 `import * as fs from "node:fs";
65fs.appendFileSync(${JSON.stringify(calls)}, process.argv.slice(2).join(" ") + "\\n");
66if (fs.existsSync(${JSON.stringify(failMarker)})) process.exitCode = 1;`,
67 );
68 await NodeFSP.writeFile(
69 NodePath.join(state, "agent.json"),
70 JSON.stringify({ entryPath: entry }),
71 );
72 await NodeFSP.writeFile(daemonFile, JSON.stringify({ httpPort: 1, token: "claims" }));
73 // An empty PATH leaves no platforms, so the script ends right after the daemon check.
74 const start = () =>
75 NodeChildProcess.spawnSync(process.execPath, [], {
76 input: remoteDeviceScript("owner", "start"),
77 env: { HOME: home, PATH: NodePath.join(home, "bin") },
78 encoding: "utf8",
79 });
80 await NodeFSP.writeFile(failMarker, "");
81 expect(start().stderr).toContain("Could not stop the agent-device daemon");
82 expect(await NodeFSP.readFile(daemonFile, "utf8")).toContain("claims");
83 await NodeFSP.rm(failMarker);
84 expect(start().stderr).toContain("Android SDK missing");
85 await expect(NodeFSP.access(daemonFile)).rejects.toThrow();
86 expect(await NodeFSP.readFile(calls, "utf8")).toBe(
87 `daemon stop --state-dir ${state}\ndaemon stop --state-dir ${state}\n`,
88 );
89 } finally {
90 await NodeFSP.rm(home, { recursive: true, force: true });
91 }
92 });
93 }),
94);
95
96it.effect("keeps a daemon record its missing agent CLI cannot stop", () =>
97 Effect.gen(function* () {
98 if ((yield* HostProcess.Platform) === "win32") return;
99 yield* Effect.promise(async () => {
100 const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-start-"));
101 try {
102 const state = NodePath.join(home, ".t3/device/hosts/owner");
103 const daemonFile = NodePath.join(state, "daemon.json");
104 await NodeFSP.mkdir(NodePath.join(home, "bin"));
105 await NodeFSP.mkdir(state, { recursive: true });
106 await NodeFSP.writeFile(
107 NodePath.join(state, "agent.json"),
108 JSON.stringify({ entryPath: NodePath.join(home, "missing/agent-device.mjs") }),
109 );
110 const start = async (pid: number) => {
111 await NodeFSP.writeFile(
112 daemonFile,
113 JSON.stringify({ pid, httpPort: 1, token: "claims" }),
114 );
115 return NodeChildProcess.spawnSync(process.execPath, [], {
116 input: remoteDeviceScript("owner", "start"),
117 env: { HOME: home, PATH: NodePath.join(home, "bin") },
118 encoding: "utf8",
119 });
120 };
121 // This test process stands in for a daemon that is still running.
122 expect((await start(process.pid)).stderr).toContain(
123 "Could not stop the agent-device daemon",
124 );
125 expect(await NodeFSP.readFile(daemonFile, "utf8")).toContain("claims");
126 const exited = NodeChildProcess.spawnSync(process.execPath, ["-e", ""]).pid;
127 expect((await start(exited)).stderr).toContain("Android SDK missing");
128 await expect(NodeFSP.access(daemonFile)).rejects.toThrow();
129 } finally {
130 await NodeFSP.rm(home, { recursive: true, force: true });
131 }
132 });
133 }),
134);