|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off -- Exercise real shells and OpenSSH with isolated fixtures. |
| 2 | +import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient"; |
| 3 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | +import { assert, describe, it } from "@effect/vitest"; |
| 5 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 6 | +import * as NetService from "@t3tools/shared/Net"; |
| 7 | +import { SshCommandError } from "@t3tools/ssh/errors"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | +import * as FileSystem from "effect/FileSystem"; |
| 10 | +import * as Layer from "effect/Layer"; |
| 11 | +import * as Path from "effect/Path"; |
| 12 | +import * as ChildProcess from "effect/process/ChildProcess"; |
| 13 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 14 | +import * as NodeOS from "node:os"; |
| 15 | +import { vi } from "vite-plus/test"; |
| 16 | + |
| 17 | +import * as DesktopConfig from "../app/DesktopConfig.ts"; |
| 18 | +import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 19 | +import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts"; |
| 20 | +import * as DesktopSshEnvironment from "./DesktopSshEnvironment.ts"; |
| 21 | +import * as DesktopSshPasswordPrompts from "./DesktopSshPasswordPrompts.ts"; |
| 22 | + |
| 23 | +vi.mock("node:os", { spy: true }); |
| 24 | + |
| 25 | +const target = { |
| 26 | + alias: "managed-workspace", |
| 27 | + hostname: "helper-missing.invalid", |
| 28 | + username: null, |
| 29 | + port: null, |
| 30 | +}; |
| 31 | + |
| 32 | +function runRecoveryScenario( |
| 33 | + operation: "resolve" | "connect" | "still-unavailable" | "fallback-shell" | "launchctl", |
| 34 | +) { |
| 35 | + return Effect.gen(function* () { |
| 36 | + const fs = yield* FileSystem.FileSystem; |
| 37 | + const path = yield* Path.Path; |
| 38 | + const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-path-" }); |
| 39 | + const bin = path.join(home, "bin"); |
| 40 | + const helpers = path.join(home, "workspace tools"); |
| 41 | + const probes = path.join(home, "probes"); |
| 42 | + const ready = path.join(home, "ready"); |
| 43 | + const proxyReached = path.join(home, "proxy-reached"); |
| 44 | + const config = path.join(home, "ssh_config"); |
| 45 | + yield* fs.makeDirectory(bin); |
| 46 | + yield* fs.makeDirectory(helpers); |
| 47 | + const inheritedPath = `${bin}:/usr/bin:/bin:/usr/sbin:/sbin`; |
| 48 | + const shellPath = `${helpers}:${inheritedPath}`; |
| 49 | + const preferredShell = path.join(bin, "preferred-shell"); |
| 50 | + yield* fs.writeFileString( |
| 51 | + preferredShell, |
| 52 | + '#!/bin/sh\nexport T3_TEST_PREFERRED=1\nexec /bin/zsh "$@"\n', |
| 53 | + ); |
| 54 | + yield* fs.chmod(preferredShell, 0o700); |
| 55 | + yield* fs.writeFileString( |
| 56 | + path.join(home, ".zshenv"), |
| 57 | + [ |
| 58 | + "[[ -o login && -o interactive ]] || return 0", |
| 59 | + `print probe >> '${probes}'`, |
| 60 | + `if [[ ! -f '${ready}' ]]; then`, |
| 61 | + ' [[ "$T3_TEST_ALLOW_FALLBACK" = 1 && -z "$T3_TEST_PREFERRED" ]] || exit 1', |
| 62 | + "fi", |
| 63 | + "", |
| 64 | + ].join("\n"), |
| 65 | + ); |
| 66 | + yield* fs.writeFileString( |
| 67 | + path.join(home, ".zprofile"), |
| 68 | + `if [[ -f '${ready}' ]]; then\nexport PATH='${shellPath}'\nelse\nexport PATH='${inheritedPath}'\nfi\n`, |
| 69 | + ); |
| 70 | + yield* fs.writeFileString( |
| 71 | + path.join(bin, "ssh"), |
| 72 | + `#!/bin/sh\nexec /usr/bin/ssh -F '${config}' "$@"\n`, |
| 73 | + ); |
| 74 | + yield* fs.chmod(path.join(bin, "ssh"), 0o700); |
| 75 | + yield* fs.writeFileString(path.join(helpers, "workspace-check"), "#!/bin/sh\nexit 0\n"); |
| 76 | + yield* fs.chmod(path.join(helpers, "workspace-check"), 0o700); |
| 77 | + yield* fs.writeFileString( |
| 78 | + path.join(helpers, "workspace-proxy"), |
| 79 | + `#!/bin/sh\nprintf 'proxy invoked\\n' > '${proxyReached}'\nexit 1\n`, |
| 80 | + ); |
| 81 | + yield* fs.chmod(path.join(helpers, "workspace-proxy"), 0o700); |
| 82 | + yield* fs.writeFileString( |
| 83 | + config, |
| 84 | + [ |
| 85 | + 'Match originalhost managed-workspace exec "workspace-check"', |
| 86 | + " HostName helper-ready.invalid", |
| 87 | + "Host managed-workspace", |
| 88 | + " HostName helper-missing.invalid", |
| 89 | + " ProxyCommand workspace-proxy", |
| 90 | + " ControlMaster no", |
| 91 | + " ControlPath none", |
| 92 | + "", |
| 93 | + ].join("\n"), |
| 94 | + ); |
| 95 | + |
| 96 | + const layerEnvironment = DesktopEnvironment.layer({ |
| 97 | + dirname: home, |
| 98 | + homeDirectory: home, |
| 99 | + platform: "darwin", |
| 100 | + processArch: yield* HostProcess.Architecture, |
| 101 | + appVersion: "0.1.0", |
| 102 | + appPath: home, |
| 103 | + isPackaged: true, |
| 104 | + resourcesPath: home, |
| 105 | + runningUnderArm64Translation: false, |
| 106 | + }).pipe(Layer.provide(DesktopConfig.layerTest({ T3CODE_HOME: home }))); |
| 107 | + const layerShell = DesktopShellEnvironment.layer.pipe(Layer.provide(layerEnvironment)); |
| 108 | + const layerSsh = DesktopSshEnvironment.layer({ |
| 109 | + resolveCliRunner: Effect.succeed({ archiveVersion: "0.1.0" }), |
| 110 | + }).pipe( |
| 111 | + Layer.provideMerge(layerShell), |
| 112 | + Layer.provide( |
| 113 | + Layer.succeed(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, { |
| 114 | + request: () => Effect.die("Unexpected authentication prompt"), |
| 115 | + resolve: () => Effect.die("Unexpected authentication response"), |
| 116 | + }), |
| 117 | + ), |
| 118 | + ); |
| 119 | + const program = Effect.gen(function* () { |
| 120 | + const shell = yield* DesktopShellEnvironment.DesktopShellEnvironment; |
| 121 | + const ssh = yield* DesktopSshEnvironment.DesktopSshEnvironment; |
| 122 | + assert.equal(yield* shell.installIntoProcess, false); |
| 123 | + if (operation !== "still-unavailable") yield* fs.writeFileString(ready, ""); |
| 124 | + if (operation === "resolve" || operation === "fallback-shell" || operation === "launchctl") { |
| 125 | + assert.equal((yield* ssh.resolveHost(target.alias)).hostname, "helper-ready.invalid"); |
| 126 | + assert.equal((yield* ssh.resolveHost(target.alias)).hostname, "helper-ready.invalid"); |
| 127 | + } else { |
| 128 | + const result = yield* Effect.result(ssh.ensureEnvironment(target)); |
| 129 | + assert.equal(result._tag, "Failure"); |
| 130 | + if (result._tag === "Failure") assert.instanceOf(result.failure, SshCommandError); |
| 131 | + if (operation === "connect") { |
| 132 | + assert.equal(yield* fs.readFileString(proxyReached), "proxy invoked\n"); |
| 133 | + assert.equal((yield* ssh.resolveHost(target.alias)).hostname, "helper-ready.invalid"); |
| 134 | + } else { |
| 135 | + assert.equal(yield* fs.exists(proxyReached), false); |
| 136 | + if (result._tag === "Failure") { |
| 137 | + assert.include(result.failure.message, "workspace-proxy"); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + assert.equal( |
| 142 | + yield* fs.readFileString(probes), |
| 143 | + operation === "fallback-shell" ? "probe\nprobe\nprobe\n" : "probe\nprobe\n", |
| 144 | + ); |
| 145 | + assert.equal(process.env.PATH, operation === "still-unavailable" ? inheritedPath : shellPath); |
| 146 | + }).pipe( |
| 147 | + Effect.provide( |
| 148 | + layerSsh.pipe( |
| 149 | + Layer.provideMerge(Layer.mergeAll(NodeHttpClient.layerUndici, NetService.layer)), |
| 150 | + ), |
| 151 | + ), |
| 152 | + ); |
| 153 | + |
| 154 | + const nativeSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 155 | + const spawner = ChildProcessSpawner.make((command) => |
| 156 | + nativeSpawner.spawn( |
| 157 | + operation === "launchctl" && |
| 158 | + ChildProcess.isStandardCommand(command) && |
| 159 | + command.command === "/bin/launchctl" |
| 160 | + ? ChildProcess.make("/bin/sh", ["-c", `printf '%s' '${inheritedPath}'`]) |
| 161 | + : command, |
| 162 | + ), |
| 163 | + ); |
| 164 | + yield* Effect.acquireUseRelease( |
| 165 | + Effect.sync(() => { |
| 166 | + const previous = process.env; |
| 167 | + process.env = { |
| 168 | + ...previous, |
| 169 | + HOME: home, |
| 170 | + ZDOTDIR: home, |
| 171 | + SHELL: operation === "fallback-shell" ? preferredShell : "/bin/zsh", |
| 172 | + T3_TEST_ALLOW_FALLBACK: operation === "fallback-shell" ? "1" : "0", |
| 173 | + PATH: inheritedPath, |
| 174 | + }; |
| 175 | + const spy = vi.spyOn(NodeOS, "userInfo").mockReturnValue({ |
| 176 | + ...NodeOS.userInfo(), |
| 177 | + shell: "/bin/zsh", |
| 178 | + }); |
| 179 | + return { previous, spy }; |
| 180 | + }), |
| 181 | + () => program.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)), |
| 182 | + ({ previous, spy }) => |
| 183 | + Effect.sync(() => { |
| 184 | + spy.mockRestore(); |
| 185 | + process.env = previous; |
| 186 | + }), |
| 187 | + ); |
| 188 | + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped); |
| 189 | +} |
| 190 | + |
| 191 | +describe("SSH shell PATH recovery with real OpenSSH", () => { |
| 192 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "darwin")( |
| 193 | + "retries the preferred shell when a fallback shell supplied only a degraded PATH", |
| 194 | + () => runRecoveryScenario("fallback-shell"), |
| 195 | + ); |
| 196 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "darwin")( |
| 197 | + "retries the login shell when launchctl supplied only a degraded PATH", |
| 198 | + () => runRecoveryScenario("launchctl"), |
| 199 | + ); |
| 200 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "darwin")( |
| 201 | + "recovers a failed startup capture before resolving a host and caches the recovered PATH", |
| 202 | + () => runRecoveryScenario("resolve"), |
| 203 | + ); |
| 204 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "darwin")( |
| 205 | + "recovers a failed startup capture before invoking a saved host's ProxyCommand", |
| 206 | + () => runRecoveryScenario("connect"), |
| 207 | + ); |
| 208 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "darwin")( |
| 209 | + "preserves the SSH error when PATH capture is still unavailable", |
| 210 | + () => runRecoveryScenario("still-unavailable"), |
| 211 | + ); |
| 212 | +}); |
0 commit comments