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 parseSshResolveOutput,
17 resolveRemoteT3CliPackageSpec,
18 runSshCommand,
19} from "./command.ts";
20
21const makeNeverFinishingProcess = () => {
22 let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
23 return ChildProcessSpawner.makeHandle({
24 pid: ChildProcessSpawner.ProcessId(123),
25 stdout: Stream.empty,
26 stderr: Stream.empty,
27 all: Stream.empty,
28 exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
29 finish = (exitCode) => resume(Effect.succeed(exitCode));
30 return Effect.sync(() => {
31 finish = null;
32 });
33 }),
34 isRunning: Effect.succeed(true),
35 kill: () =>
36 Effect.sync(() => {
37 finish?.(ChildProcessSpawner.ExitCode(143));
38 }),
39 stdin: Sink.drain,
40 getInputFd: () => Sink.drain,
41 getOutputFd: () => Stream.empty,
42 unref: Effect.succeed(Effect.void),
43 });
44};
45
46describe("ssh command", () => {
47 it.effect("parses resolved ssh config output into a target", () =>
48 Effect.sync(() => {
49 assert.deepEqual(
50 parseSshResolveOutput(
51 "devbox",
52 ["hostname devbox.example.com", "user julius", "port 2222", ""].join("\n"),
53 ),
54 {
55 alias: "devbox",
56 hostname: "devbox.example.com",
57 username: "julius",
58 port: 2222,
59 },
60 );
61 }),
62 );
63
64 it.effect("builds interactive ssh args without forcing batch mode", () =>
65 Effect.sync(() => {
66 assert.deepEqual(
67 baseSshArgs(
68 {
69 alias: "devbox",
70 hostname: "devbox.example.com",
71 username: "julius",
72 port: 2222,
73 },
74 { batchMode: "no" },
75 ),
76 ["-o", "BatchMode=no", "-o", "ConnectTimeout=10", "-p", "2222"],
77 );
78 }),
79 );
80
81 it.effect("resolves the remote t3 package spec from the desktop release channel", () =>
82 Effect.sync(() => {
83 assert.equal(
84 resolveRemoteT3CliPackageSpec({
85 appVersion: "0.0.17",
86 updateChannel: "latest",
87 }),
88 "t3@0.0.17",
89 );
90 assert.equal(
91 resolveRemoteT3CliPackageSpec({
92 appVersion: "0.0.17-nightly.20260415.44",
93 updateChannel: "nightly",
94 }),
95 "t3@0.0.17-nightly.20260415.44",
96 );
97 assert.equal(
98 resolveRemoteT3CliPackageSpec({
99 appVersion: "0.0.0-dev",
100 updateChannel: "nightly",
101 isDevelopment: true,
102 }),
103 "t3@nightly",
104 );
105 assert.equal(
106 resolveRemoteT3CliPackageSpec({
107 appVersion: "0.0.0-dev",
108 updateChannel: "latest",
109 isDevelopment: true,
110 }),
111 "t3@nightly",
112 );
113 }),
114 );
115
116 it.effect("reads the last non-empty ssh output line", () =>
117 Effect.sync(() => {
118 assert.equal(
119 getLastNonEmptyOutputLine(
120 ["Welcome to the host", "", '{"credential":"pairing-token"}', ""].join("\n"),
121 ),
122 '{"credential":"pairing-token"}',
123 );
124 }),
125 );
126
127 it.effect("fails commands that never finish", () => {
128 const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeNeverFinishingProcess()));
129 const spawnerLayer = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
130 const processLayer = Layer.mergeAll(NodeServices.layer, spawnerLayer, TestClock.layer());
131
132 return Effect.gen(function* () {
133 const fiber = yield* Effect.forkChild(
134 Effect.result(
135 runSshCommand(
136 {
137 alias: "devbox",
138 hostname: "devbox.example.com",
139 username: "julius",
140 port: 2222,
141 },
142 { timeoutMs: 1 },
143 ),
144 ),
145 );
146 yield* Effect.yieldNow;
147 yield* TestClock.adjust(Duration.millis(1));
148
149 const result = yield* Fiber.join(fiber);
150
151 assert.isTrue(Result.isFailure(result));
152 if (Result.isFailure(result)) {
153 assert.include(result.failure.message, "SSH command timed out after 1ms.");
154 }
155 }).pipe(Effect.provide(processLayer));
156 });
157});