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