Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import * as Result from "effect/Result";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as TestClock from "effect/testing/TestClock";
11import { ChildProcessSpawner } from "effect/unstable/process";
12
13import {
14 baseSshArgs,
15 getLastNonEmptyOutputLine,
16 parseReleaseRepository,
17 parseSshResolveOutput,
18 resolveRemoteT3CliPackageSpec,
19 runSshCommand,
20} from "./command.ts";
21
22const makeNeverFinishingProcess = () => {
23 let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
24 return ChildProcessSpawner.makeHandle({
25 pid: ChildProcessSpawner.ProcessId(123),
26 stdout: Stream.empty,
27 stderr: Stream.empty,
28 all: Stream.empty,
29 exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
30 finish = (exitCode) => resume(Effect.succeed(exitCode));
31 return Effect.sync(() => {
32 finish = null;
33 });
34 }),
35 isRunning: Effect.succeed(true),
36 kill: () =>
37 Effect.sync(() => {
38 finish?.(ChildProcessSpawner.ExitCode(143));
39 }),
40 stdin: Sink.drain,
41 getInputFd: () => Sink.drain,
42 getOutputFd: () => Stream.empty,
43 unref: Effect.succeed(Effect.void),
44 });
45};
46
47describe("ssh command", () => {
48 it.effect("parses resolved ssh config output into a target", () =>
49 Effect.sync(() => {
50 assert.deepEqual(
51 parseSshResolveOutput(
52 "devbox",
53 ["hostname devbox.example.com", "user julius", "port 2222", ""].join("\n"),
54 ),
55 {
56 alias: "devbox",
57 hostname: "devbox.example.com",
58 username: "julius",
59 port: 2222,
60 },
61 );
62 }),
63 );
64
65 it.effect("builds interactive ssh args without forcing batch mode", () =>
66 Effect.sync(() => {
67 assert.deepEqual(
68 baseSshArgs(
69 {
70 alias: "devbox",
71 hostname: "devbox.example.com",
72 username: "julius",
73 port: 2222,
74 },
75 { batchMode: "no" },
76 ),
77 ["-o", "BatchMode=no", "-o", "ConnectTimeout=10", "-p", "2222"],
78 );
79 }),
80 );
81
82 it.effect("resolves the remote t3 package spec from the desktop release channel", () =>
83 Effect.sync(() => {
84 assert.equal(
85 resolveRemoteT3CliPackageSpec({
86 appVersion: "0.0.17",
87 updateChannel: "latest",
88 }),
89 "t3@0.0.17",
90 );
91 assert.equal(
92 resolveRemoteT3CliPackageSpec({
93 appVersion: "0.0.17-nightly.20260415.44",
94 updateChannel: "nightly",
95 }),
96 "t3@0.0.17-nightly.20260415.44",
97 );
98 assert.equal(
99 resolveRemoteT3CliPackageSpec({
100 appVersion: "0.0.0-dev",
101 updateChannel: "nightly",
102 isDevelopment: true,
103 }),
104 "t3@nightly",
105 );
106 assert.equal(
107 resolveRemoteT3CliPackageSpec({
108 appVersion: "0.0.0-dev",
109 updateChannel: "latest",
110 isDevelopment: true,
111 }),
112 "t3@nightly",
113 );
114 }),
115 );
116
117 it.effect(
118 "resolves the remote t3 package spec to a GitHub Release tarball URL when releaseRepository is provided",
119 () =>
120 Effect.sync(() => {
121 const releaseRepository = { owner: "bscholar-tt", repo: "t3code" } as const;
122
123 assert.equal(
124 resolveRemoteT3CliPackageSpec({
125 appVersion: "0.0.17",
126 updateChannel: "latest",
127 releaseRepository,
128 }),
129 "https://github.com/bscholar-tt/t3code/releases/download/v0.0.17/t3-0.0.17.tgz",
130 );
131
132 assert.equal(
133 resolveRemoteT3CliPackageSpec({
134 appVersion: "0.0.17-nightly.20260415.44",
135 updateChannel: "nightly",
136 releaseRepository,
137 }),
138 "https://github.com/bscholar-tt/t3code/releases/download/v0.0.17-nightly.20260415.44/t3-0.0.17-nightly.20260415.44.tgz",
139 );
140
141 // Dev mode / channel fallback both point at the rolling latest alias
142 // so URLs stay stable even when no exact tag matches.
143 assert.equal(
144 resolveRemoteT3CliPackageSpec({
145 appVersion: "0.0.0-dev",
146 updateChannel: "latest",
147 isDevelopment: true,
148 releaseRepository,
149 }),
150 "https://github.com/bscholar-tt/t3code/releases/latest/download/t3-latest.tgz",
151 );
152
153 assert.equal(
154 resolveRemoteT3CliPackageSpec({
155 appVersion: "not-a-version",
156 updateChannel: "nightly",
157 releaseRepository,
158 }),
159 "https://github.com/bscholar-tt/t3code/releases/latest/download/t3-latest.tgz",
160 );
161 }),
162 );
163
164 it.effect("parses release repository slugs and GitHub URLs", () =>
165 Effect.sync(() => {
166 assert.deepEqual(parseReleaseRepository("bscholar-tt/t3code"), {
167 owner: "bscholar-tt",
168 repo: "t3code",
169 });
170 assert.deepEqual(parseReleaseRepository("https://github.com/bscholar-tt/t3code"), {
171 owner: "bscholar-tt",
172 repo: "t3code",
173 });
174 assert.deepEqual(parseReleaseRepository("https://github.com/bscholar-tt/t3code.git"), {
175 owner: "bscholar-tt",
176 repo: "t3code",
177 });
178 assert.equal(parseReleaseRepository(""), undefined);
179 assert.equal(parseReleaseRepository(undefined), undefined);
180 assert.equal(parseReleaseRepository("not a repo"), undefined);
181 }),
182 );
183
184 it.effect("reads the last non-empty ssh output line", () =>
185 Effect.sync(() => {
186 assert.equal(
187 getLastNonEmptyOutputLine(
188 ["Welcome to the host", "", '{"credential":"pairing-token"}', ""].join("\n"),
189 ),
190 '{"credential":"pairing-token"}',
191 );
192 }),
193 );
194
195 it.effect("fails commands that never finish", () => {
196 const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeNeverFinishingProcess()));
197 const spawnerLayer = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
198 const processLayer = Layer.mergeAll(NodeServices.layer, spawnerLayer, TestClock.layer());
199
200 return Effect.gen(function* () {
201 const fiber = yield* Effect.forkChild(
202 Effect.result(
203 runSshCommand(
204 {
205 alias: "devbox",
206 hostname: "devbox.example.com",
207 username: "julius",
208 port: 2222,
209 },
210 { timeoutMs: 1 },
211 ),
212 ),
213 );
214 yield* Effect.yieldNow;
215 yield* TestClock.adjust(Duration.millis(1));
216
217 const result = yield* Fiber.join(fiber);
218
219 assert.isTrue(Result.isFailure(result));
220 if (Result.isFailure(result)) {
221 assert.include(result.failure.message, "SSH command timed out after 1ms.");
222 }
223 }).pipe(Effect.provide(processLayer));
224 });
225});