|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - executes the generated POSIX launcher against an isolated filesystem fixture. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeFS from "node:fs"; |
| 4 | +import * as NodeOS from "node:os"; |
| 5 | +import * as NodePath from "node:path"; |
1 | 6 | import { assert, describe, it } from "@effect/vitest"; |
2 | 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 8 | import * as NetService from "@t3tools/shared/Net"; |
@@ -82,6 +87,68 @@ function commandArgs(command: ChildProcess.Command): ReadonlyArray<string> { |
82 | 87 | const ARCHIVE = { archiveVersion: "1.2.3-preview.20260911.4" } as const; |
83 | 88 |
|
84 | 89 | describe("ssh tunnel scripts", () => { |
| 90 | + it.each(["unavailable", "recovering", "discovered", "missing-port", "fresh"] as const)( |
| 91 | + "handles SSH launch with %s external service state", |
| 92 | + (scenario) => { |
| 93 | + const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-owner-")); |
| 94 | + try { |
| 95 | + const state = NodePath.join(home, ".t3/ssh-launch/test"); |
| 96 | + const runtime = NodePath.join(home, ".t3/runtime/versions", ARCHIVE.archiveVersion); |
| 97 | + NodeFS.mkdirSync(state, { recursive: true }); |
| 98 | + NodeFS.mkdirSync(runtime, { recursive: true }); |
| 99 | + if (scenario !== "discovered" && scenario !== "fresh") { |
| 100 | + NodeFS.writeFileSync(NodePath.join(state, "managed"), "external\n"); |
| 101 | + if (scenario !== "missing-port") |
| 102 | + NodeFS.writeFileSync(NodePath.join(state, "port"), "3773\n"); |
| 103 | + } |
| 104 | + NodeFS.writeFileSync(NodePath.join(runtime, ".install-complete"), ARCHIVE.archiveVersion); |
| 105 | + NodeFS.writeFileSync( |
| 106 | + NodePath.join(runtime, "t3"), |
| 107 | + `#!/bin/sh |
| 108 | +case "$1 $2" in |
| 109 | + '--version ') exit 0 ;; |
| 110 | + '__ssh-helper runtime-port') |
| 111 | + ${scenario === "discovered" ? `printf '${process.pid} 3773'` : "exit 1"} ;; |
| 112 | + '__ssh-helper wait-ready') |
| 113 | + printf '%s\\n' "$4" >> "$HOME/probes" |
| 114 | + ${scenario === "fresh" ? "exit 0" : scenario === "recovering" ? '[ "$4" -gt 2000 ]' : "exit 1"} ;; |
| 115 | + '__ssh-helper pick-port') printf 'pick-port\\n' >> "$HOME/spawns"; printf '3774' ;; |
| 116 | + 'serve --host') printf 'serve\\n' >> "$HOME/spawns" ;; |
| 117 | + *) exit 2 ;; |
| 118 | +esac |
| 119 | +`, |
| 120 | + { mode: 0o700 }, |
| 121 | + ); |
| 122 | + const result = NodeChildProcess.spawnSync("sh", ["-s", "--", "test"], { |
| 123 | + input: SshTunnel.buildRemoteLaunchScript(ARCHIVE), |
| 124 | + env: { ...process.env, HOME: home }, |
| 125 | + encoding: "utf8", |
| 126 | + timeout: 10_000, |
| 127 | + }); |
| 128 | + assert.isUndefined(result.error); |
| 129 | + if (scenario === "fresh") { |
| 130 | + assert.equal(result.status, 0, result.stderr); |
| 131 | + assert.deepEqual(JSON.parse(result.stdout), { remotePort: 3774, serverKind: "managed" }); |
| 132 | + return; |
| 133 | + } |
| 134 | + assert.isFalse(NodeFS.existsSync(NodePath.join(home, "spawns")), result.stderr); |
| 135 | + assert.equal( |
| 136 | + NodeFS.readFileSync(NodePath.join(state, "managed"), "utf8").trim(), |
| 137 | + "external", |
| 138 | + ); |
| 139 | + if (scenario === "recovering") { |
| 140 | + assert.equal(result.status, 0, result.stderr); |
| 141 | + assert.deepEqual(JSON.parse(result.stdout), { remotePort: 3773, serverKind: "external" }); |
| 142 | + } else { |
| 143 | + assert.equal(result.status, 1); |
| 144 | + assert.include(result.stderr, "external T3 server"); |
| 145 | + } |
| 146 | + } finally { |
| 147 | + NodeFS.rmSync(home, { recursive: true, force: true }); |
| 148 | + } |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
85 | 152 | it("rejects archive versions that are not a single exact version segment", () => { |
86 | 153 | for (const archiveVersion of [ |
87 | 154 | "../other", |
|
0 commit comments