The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off globalFetchInEffect:off - verifies generated remote scripts using real shell and Node processes. |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import { expect, it } from "@effect/vitest"; |
| 5 | import * as NodeChildProcess from "node:child_process"; |
| 6 | import * as NodeFSP from "node:fs/promises"; |
| 7 | import * as NodeOS from "node:os"; |
| 8 | import * as NodePath from "node:path"; |
| 9 | import * as NodeUtil from "node:util"; |
| 10 | import { quoteRemoteArg, remoteDeviceEnvironment, remoteDeviceScript } from "./sshDeviceScript.ts"; |
| 11 | |
| 12 | const exec = NodeUtil.promisify(NodeChildProcess.execFile); |
| 13 | |
| 14 | const executable = async (file: string, body = "exit 0") => { |
| 15 | await NodeFSP.mkdir(NodePath.dirname(file), { recursive: true }); |
| 16 | await NodeFSP.writeFile(file, `#!/bin/sh\n${body}\n`, { mode: 0o755 }); |
| 17 | }; |
| 18 | |
| 19 | const completeSdk = async (sdk: string, adb = "echo Android Debug Bridge") => { |
| 20 | await executable(NodePath.join(sdk, "platform-tools/adb"), adb); |
| 21 | await executable(NodePath.join(sdk, "emulator/emulator")); |
| 22 | await executable(NodePath.join(sdk, "cmdline-tools/latest/bin/avdmanager")); |
| 23 | }; |
| 24 | |
| 25 | /** Runs the probe through the SSH shell setup, then `after` in that same shell. */ |
| 26 | const probeAndroid = (setup: (home: string) => Promise<Record<string, string>>, after = "true") => |
| 27 | Effect.promise(async () => { |
| 28 | const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-sdk-")); |
| 29 | try { |
| 30 | const env = await setup(home); |
| 31 | await executable(NodePath.join(home, "bin/npm"), "echo 10.0.0"); |
| 32 | const probe = NodePath.join(home, "probe.cjs"); |
| 33 | await NodeFSP.writeFile(probe, remoteDeviceScript("fixture", "probe")); |
| 34 | const result = await exec( |
| 35 | "/bin/sh", |
| 36 | ["-c", `${remoteDeviceEnvironment}\n"$NODE" "$PROBE" && ${after}`], |
| 37 | { |
| 38 | env: { |
| 39 | HOME: home, |
| 40 | PATH: `${NodePath.join(home, "bin")}:/usr/bin:/bin`, |
| 41 | NODE: process.execPath, |
| 42 | PROBE: probe, |
| 43 | ...env, |
| 44 | }, |
| 45 | }, |
| 46 | ); |
| 47 | const [inventory = "", ...rest] = result.stdout.trim().split("\n"); |
| 48 | const android = JSON.parse(inventory).platforms.find( |
| 49 | (entry: { platform: string }) => entry.platform === "android", |
| 50 | ); |
| 51 | return { android, after: rest.join("\n") }; |
| 52 | } finally { |
| 53 | await NodeFSP.rm(home, { recursive: true, force: true }); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | it.effect("does not offer Android when the SDK adb cannot run", () => |
| 58 | Effect.gen(function* () { |
| 59 | if ((yield* HostProcess.Platform) === "win32") return; |
| 60 | const { android } = yield* probeAndroid(async (home) => { |
| 61 | const sdk = NodePath.join(home, "sdk"); |
| 62 | await completeSdk(sdk, "exit 1"); |
| 63 | return { ANDROID_HOME: sdk }; |
| 64 | }); |
| 65 | expect(android).toEqual({ platform: "android", available: false, reason: expect.any(String) }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | it.effect("does not offer Android when SDK tools are directories", () => |
| 70 | Effect.gen(function* () { |
| 71 | if ((yield* HostProcess.Platform) === "win32") return; |
| 72 | const { android } = yield* probeAndroid(async (home) => { |
| 73 | const sdk = NodePath.join(home, "sdk"); |
| 74 | await executable(NodePath.join(sdk, "platform-tools/adb"), "echo Android Debug Bridge"); |
| 75 | await NodeFSP.mkdir(NodePath.join(sdk, "emulator/emulator"), { recursive: true }); |
| 76 | await NodeFSP.mkdir(NodePath.join(sdk, "cmdline-tools/latest/bin/avdmanager"), { |
| 77 | recursive: true, |
| 78 | }); |
| 79 | return { ANDROID_HOME: sdk }; |
| 80 | }); |
| 81 | expect(android).toEqual({ |
| 82 | platform: "android", |
| 83 | available: false, |
| 84 | reason: expect.stringContaining("Android Emulator"), |
| 85 | }); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | it.effect("uses the SDK the probe accepted for later SSH commands", () => |
| 90 | Effect.gen(function* () { |
| 91 | if ((yield* HostProcess.Platform) === "win32") return; |
| 92 | let sdk = ""; |
| 93 | const result = yield* probeAndroid(async (home) => { |
| 94 | // Incomplete default locations must not shadow the configured SDK. |
| 95 | await NodeFSP.mkdir(NodePath.join(home, "Library/Android/sdk"), { recursive: true }); |
| 96 | await NodeFSP.mkdir(NodePath.join(home, "Android/Sdk"), { recursive: true }); |
| 97 | sdk = NodePath.join(home, "custom-sdk"); |
| 98 | await completeSdk(sdk); |
| 99 | return { ANDROID_SDK_ROOT: sdk }; |
| 100 | }, "command -v emulator"); |
| 101 | expect(result.android).toEqual({ platform: "android", available: true }); |
| 102 | expect(result.after).toBe(NodePath.join(sdk, "emulator/emulator")); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | it.effect("finds Android Studio Java for a non-interactive SSH session", () => |
| 107 | Effect.gen(function* () { |
| 108 | if ((yield* HostProcess.Platform) === "win32") return; |
| 109 | yield* Effect.promise(async () => { |
| 110 | const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-java-")); |
| 111 | try { |
| 112 | const javaHome = NodePath.join(home, ".local/opt/android-studio/jbr"); |
| 113 | await NodeFSP.mkdir(NodePath.join(javaHome, "bin"), { recursive: true }); |
| 114 | await NodeFSP.writeFile( |
| 115 | NodePath.join(javaHome, "bin/java"), |
| 116 | "#!/bin/sh\necho test-java\n", |
| 117 | { mode: 0o755 }, |
| 118 | ); |
| 119 | const result = await exec("/bin/sh", ["-c", `${remoteDeviceEnvironment}\njava`], { |
| 120 | env: { HOME: home, PATH: "/nonexistent", JAVA_HOME: "" }, |
| 121 | }); |
| 122 | expect(result.stdout.trim()).toBe("test-java"); |
| 123 | } finally { |
| 124 | await NodeFSP.rm(home, { recursive: true, force: true }); |
| 125 | } |
| 126 | }); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | it.effect("preserves shell metacharacters and newlines in remote arguments", () => |
| 131 | Effect.gen(function* () { |
| 132 | if ((yield* HostProcess.Platform) === "win32") return; |
| 133 | yield* Effect.promise(async () => { |
| 134 | const value = "quotes ' \" ; $(echo expanded) $HOME\nnext line"; |
| 135 | const result = await exec("sh", ["-c", `printf %s ${quoteRemoteArg(value)}`]); |
| 136 | expect(result.stdout).toBe(value); |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | it.effect("rejects a Node release below the pinned agent-device engine floor", () => |
| 142 | Effect.gen(function* () { |
| 143 | if ((yield* HostProcess.Platform) === "win32") return; |
| 144 | const probe = NodeChildProcess.spawnSync(process.execPath, [], { |
| 145 | input: |
| 146 | `Object.defineProperty(process.versions, "node", { value: "22.11.0" });\n` + |
| 147 | remoteDeviceScript("owner", "probe"), |
| 148 | env: { HOME: NodeOS.tmpdir(), PATH: NodePath.dirname(process.execPath) }, |
| 149 | encoding: "utf8", |
| 150 | }); |
| 151 | expect(probe.status).not.toBe(0); |
| 152 | expect(probe.stderr).toContain("Node 22.12 or newer"); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | it.effect("stops a daemon left for its owner when starting without agent tools", () => |
| 157 | Effect.gen(function* () { |
| 158 | if ((yield* HostProcess.Platform) === "win32") return; |
| 159 | yield* Effect.promise(async () => { |
| 160 | const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-start-")); |
| 161 | try { |
| 162 | const state = NodePath.join(home, ".t3/device/hosts/owner"); |
| 163 | const daemonFile = NodePath.join(state, "daemon.json"); |
| 164 | const calls = NodePath.join(home, "calls"); |
| 165 | const failMarker = NodePath.join(home, "fail-stop"); |
| 166 | const entry = NodePath.join(home, "agent-device.mjs"); |
| 167 | await NodeFSP.mkdir(NodePath.join(home, "bin")); |
| 168 | await NodeFSP.mkdir(state, { recursive: true }); |
| 169 | await NodeFSP.writeFile( |
| 170 | entry, |
| 171 | `import * as fs from "node:fs"; |
| 172 | fs.appendFileSync(${JSON.stringify(calls)}, process.argv.slice(2).join(" ") + "\\n"); |
| 173 | if (fs.existsSync(${JSON.stringify(failMarker)})) process.exitCode = 1;`, |
| 174 | ); |
| 175 | await NodeFSP.writeFile( |
| 176 | NodePath.join(state, "agent.json"), |
| 177 | JSON.stringify({ entryPath: entry }), |
| 178 | ); |
| 179 | await NodeFSP.writeFile(daemonFile, JSON.stringify({ httpPort: 1, token: "claims" })); |
| 180 | // An empty PATH leaves no platforms, so the script ends right after the daemon check. |
| 181 | const start = () => |
| 182 | NodeChildProcess.spawnSync(process.execPath, [], { |
| 183 | input: remoteDeviceScript("owner", "start"), |
| 184 | env: { HOME: home, PATH: NodePath.join(home, "bin") }, |
| 185 | encoding: "utf8", |
| 186 | }); |
| 187 | await NodeFSP.writeFile(failMarker, ""); |
| 188 | expect(start().stderr).toContain("Could not stop the agent-device daemon"); |
| 189 | expect(await NodeFSP.readFile(daemonFile, "utf8")).toContain("claims"); |
| 190 | await NodeFSP.rm(failMarker); |
| 191 | expect(start().stderr).toContain("Android SDK was not found"); |
| 192 | await expect(NodeFSP.access(daemonFile)).rejects.toThrow(); |
| 193 | expect(await NodeFSP.readFile(calls, "utf8")).toBe( |
| 194 | `daemon stop --state-dir ${state}\ndaemon stop --state-dir ${state}\n`, |
| 195 | ); |
| 196 | } finally { |
| 197 | await NodeFSP.rm(home, { recursive: true, force: true }); |
| 198 | } |
| 199 | }); |
| 200 | }), |
| 201 | ); |
| 202 | |
| 203 | it.effect("keeps a daemon record its missing agent CLI cannot stop", () => |
| 204 | Effect.gen(function* () { |
| 205 | if ((yield* HostProcess.Platform) === "win32") return; |
| 206 | yield* Effect.promise(async () => { |
| 207 | const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-ssh-start-")); |
| 208 | try { |
| 209 | const state = NodePath.join(home, ".t3/device/hosts/owner"); |
| 210 | const daemonFile = NodePath.join(state, "daemon.json"); |
| 211 | await NodeFSP.mkdir(NodePath.join(home, "bin")); |
| 212 | await NodeFSP.mkdir(state, { recursive: true }); |
| 213 | await NodeFSP.writeFile( |
| 214 | NodePath.join(state, "agent.json"), |
| 215 | JSON.stringify({ entryPath: NodePath.join(home, "missing/agent-device.mjs") }), |
| 216 | ); |
| 217 | const start = async (pid: number) => { |
| 218 | await NodeFSP.writeFile( |
| 219 | daemonFile, |
| 220 | JSON.stringify({ pid, httpPort: 1, token: "claims" }), |
| 221 | ); |
| 222 | return NodeChildProcess.spawnSync(process.execPath, [], { |
| 223 | input: remoteDeviceScript("owner", "start"), |
| 224 | env: { HOME: home, PATH: NodePath.join(home, "bin") }, |
| 225 | encoding: "utf8", |
| 226 | }); |
| 227 | }; |
| 228 | // This test process stands in for a daemon that is still running. |
| 229 | expect((await start(process.pid)).stderr).toContain( |
| 230 | "Could not stop the agent-device daemon", |
| 231 | ); |
| 232 | expect(await NodeFSP.readFile(daemonFile, "utf8")).toContain("claims"); |
| 233 | const exited = NodeChildProcess.spawnSync(process.execPath, ["-e", ""]).pid; |
| 234 | expect((await start(exited)).stderr).toContain("Android SDK was not found"); |
| 235 | await expect(NodeFSP.access(daemonFile)).rejects.toThrow(); |
| 236 | } finally { |
| 237 | await NodeFSP.rm(home, { recursive: true, force: true }); |
| 238 | } |
| 239 | }); |
| 240 | }), |
| 241 | ); |