Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

command.test.ts

237 lines7.5 KB

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 parseSshResolveOutput,
17 resolveRemoteV12CliPackageSpec,
18 runSshCommand,
19} from "./command.ts";
20import { SshCommandError } from "./errors.ts";
21​
22const encoder = new TextEncoder();
23​
24const makeFailedProcess = (input: { readonly stdout: string; readonly stderr?: string }) => {
25 const stdoutStream = Stream.make(encoder.encode(input.stdout));
26 const stderrStream = input.stderr ? Stream.make(encoder.encode(input.stderr)) : Stream.empty;
27 return ChildProcessSpawner.makeHandle({
28 pid: ChildProcessSpawner.ProcessId(123),
29 stdout: stdoutStream,
30 stderr: stderrStream,
31 all: Stream.empty,
32 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
33 isRunning: Effect.succeed(false),
34 kill: () => Effect.void,
35 stdin: Sink.drain,
36 getInputFd: () => Sink.drain,
37 getOutputFd: () => Stream.empty,
38 unref: Effect.succeed(Effect.void),
39 });
40};
41​
42const makeNeverFinishingProcess = () => {
43 let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
44 return ChildProcessSpawner.makeHandle({
45 pid: ChildProcessSpawner.ProcessId(123),
46 stdout: Stream.empty,
47 stderr: Stream.empty,
48 all: Stream.empty,
49 exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
50 finish = (exitCode) => resume(Effect.succeed(exitCode));
51 return Effect.sync(() => {
52 finish = null;
53 });
54 }),
55 isRunning: Effect.succeed(true),
56 kill: () =>
57 Effect.sync(() => {
58 finish?.(ChildProcessSpawner.ExitCode(143));
59 }),
60 stdin: Sink.drain,
61 getInputFd: () => Sink.drain,
62 getOutputFd: () => Stream.empty,
63 unref: Effect.succeed(Effect.void),
64 });
65};
66​
67describe("ssh command", () => {
68 it.effect("parses resolved ssh config output into a target", () =>
69 Effect.sync(() => {
70 assert.deepEqual(
71 parseSshResolveOutput(
72 "devbox",
73 ["hostname devbox.example.com", "user julius", "port 2222", ""].join("\n"),
74 ),
75 {
76 alias: "devbox",
77 hostname: "devbox.example.com",
78 username: "julius",
79 port: 2222,
80 },
81 );
82 }),
83 );
84​
85 it.effect("builds interactive ssh args without forcing batch mode", () =>
86 Effect.sync(() => {
87 assert.deepEqual(
88 baseSshArgs(
89 {
90 alias: "devbox",
91 hostname: "devbox.example.com",
92 username: "julius",
93 port: 2222,
94 },
95 { batchMode: "no" },
96 ),
97 ["-o", "BatchMode=no", "-o", "ConnectTimeout=10", "-p", "2222"],
98 );
99 }),
100 );
101​
102 it.effect("resolves the remote v12 package spec from the desktop release channel", () =>
103 Effect.sync(() => {
104 assert.equal(
105 resolveRemoteV12CliPackageSpec({
106 appVersion: "0.0.17",
107 updateChannel: "latest",
108 }),
109 "v12@0.0.17",
110 );
111 assert.equal(
112 resolveRemoteV12CliPackageSpec({
113 appVersion: "0.0.17-nightly.20260415.44",
114 updateChannel: "nightly",
115 }),
116 "v12@0.0.17-nightly.20260415.44",
117 );
118 assert.equal(
119 resolveRemoteV12CliPackageSpec({
120 appVersion: "0.0.0-dev",
121 updateChannel: "nightly",
122 isDevelopment: true,
123 }),
124 "v12@nightly",
125 );
126 assert.equal(
127 resolveRemoteV12CliPackageSpec({
128 appVersion: "0.0.0-dev",
129 updateChannel: "latest",
130 isDevelopment: true,
131 }),
132 "v12@nightly",
133 );
134 }),
135 );
136​
137 it.effect("reads the last non-empty ssh output line", () =>
138 Effect.sync(() => {
139 assert.equal(
140 getLastNonEmptyOutputLine(
141 ["Welcome to the host", "", '{"credential":"pairing-token"}', ""].join("\n"),
142 ),
143 '{"credential":"pairing-token"}',
144 );
145 }),
146 );
147​
148 it.effect("includes stdout in non-zero command failures when stderr is empty", () => {
149 const spawner = ChildProcessSpawner.make(() =>
150 Effect.succeed(makeFailedProcess({ stdout: "Pairing token creation failed\n" })),
151 );
152 const spawnerLayer = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
153 const processLayer = Layer.mergeAll(NodeServices.layer, spawnerLayer);
154​
155 return Effect.gen(function* () {
156 const result = yield* Effect.result(
157 runSshCommand(
158 {
159 alias: "devbox",
160 hostname: "devbox.example.com",
161 username: "julius",
162 port: 2222,
163 },
164 { remoteCommandArgs: ["sh", "-s"] },
165 ),
166 );
167​
168 assert.isTrue(Result.isFailure(result));
169 if (Result.isFailure(result)) {
170 assert.instanceOf(result.failure, SshCommandError);
171 assert.equal(result.failure.message, "Pairing token creation failed");
172 assert.equal(result.failure.stdout, "Pairing token creation failed\n");
173 assert.equal(result.failure.stderr, "");
174 }
175 }).pipe(Effect.provide(processLayer));
176 });
177​
178 it.effect("redacts credentials from stdout in non-zero command failures", () => {
179 const spawner = ChildProcessSpawner.make(() =>
180 Effect.succeed(makeFailedProcess({ stdout: '{"credential":"pairing-secret"}\n' })),
181 );
182 const spawnerLayer = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
183 const processLayer = Layer.mergeAll(NodeServices.layer, spawnerLayer);
184​
185 return Effect.gen(function* () {
186 const result = yield* Effect.result(
187 runSshCommand(
188 {
189 alias: "devbox",
190 hostname: "devbox.example.com",
191 username: "julius",
192 port: 2222,
193 },
194 { remoteCommandArgs: ["sh", "-s"] },
195 ),
196 );
197​
198 assert.isTrue(Result.isFailure(result));
199 if (Result.isFailure(result)) {
200 assert.instanceOf(result.failure, SshCommandError);
201 assert.equal(result.failure.message, '{"credential":"[redacted]"}');
202 assert.equal(result.failure.stdout, '{"credential":"[redacted]"}\n');
203 }
204 }).pipe(Effect.provide(processLayer));
205 });
206​
207 it.effect("fails commands that never finish", () => {
208 const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeNeverFinishingProcess()));
209 const spawnerLayer = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
210 const processLayer = Layer.mergeAll(NodeServices.layer, spawnerLayer, TestClock.layer());
211​
212 return Effect.gen(function* () {
213 const fiber = yield* Effect.forkChild(
214 Effect.result(
215 runSshCommand(
216 {
217 alias: "devbox",
218 hostname: "devbox.example.com",
219 username: "julius",
220 port: 2222,
221 },
222 { timeoutMs: 1 },
223 ),
224 ),
225 );
226 yield* Effect.yieldNow;
227 yield* TestClock.adjust(Duration.millis(1));
228​
229 const result = yield* Fiber.join(fiber);
230​
231 assert.isTrue(Result.isFailure(result));
232 if (Result.isFailure(result)) {
233 assert.include(result.failure.message, "SSH command timed out after 1ms.");
234 }
235 }).pipe(Effect.provide(processLayer));
236 });
237});