Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import * as Net from "@t3tools/shared/Net";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as PlatformError from "effect/PlatformError";
11import * as Sink from "effect/Sink";
12import * as Stream from "effect/Stream";
13import * as HttpClient from "effect/http/HttpClient";
14import * as HttpClientResponse from "effect/http/HttpClientResponse";
15import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16import * as TestClock from "effect/testing/TestClock";
17import * as ServerConfig from "../config.ts";
18import * as DeviceHost from "./DeviceHost.ts";
19import * as SshDeviceHost from "./SshDeviceHost.ts";
20
21it.effect("preserves installed status after probes and cleans failed agent activation", () =>
22 Effect.gen(function* () {
23 const fs = yield* FileSystem.FileSystem;
24 const home = yield* fs.makeTempDirectoryScoped();
25 const modes: string[] = [];
26 const owners: string[] = [];
27 const tunnelArgs: string[][] = [];
28 const tunnelStdins: unknown[] = [];
29 const commandArgs: string[][] = [];
30 let forwards = 0;
31 let failForward = true;
32 let rejectConfig = true;
33 const releases: Array<string | undefined> = [];
34 const spawner = ChildProcessSpawner.make((command) =>
35 Effect.gen(function* () {
36 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
37 const forwarding = command.args.includes("-L");
38 (forwarding ? tunnelArgs : commandArgs).push(command.args.map(String));
39 if (forwarding) tunnelStdins.push(command.options.stdin);
40 let output = "";
41 let exitCode = 0;
42 if (forwarding) {
43 if (failForward) {
44 failForward = false;
45 return yield* PlatformError.systemError({
46 _tag: "AlreadyExists",
47 module: "ChildProcess",
48 method: "spawn",
49 description: "Port already bound",
50 });
51 }
52 forwards++;
53 yield* Effect.addFinalizer(() =>
54 Effect.sync(() => {
55 forwards--;
56 }),
57 );
58 } else {
59 const stdin = command.options.stdin;
60 if (
61 !stdin ||
62 typeof stdin !== "object" ||
63 !("stream" in stdin) ||
64 !Stream.isStream(stdin.stream)
65 )
66 return yield* Effect.die("Missing script");
67 const script = yield* stdin.stream.pipe(
68 Stream.decodeText(),
69 Stream.runFold(
70 () => "",
71 (a, b) => a + b,
72 ),
73 );
74 const mode = /const mode = "([^"]+)"/.exec(script)?.[1] ?? "";
75 modes.push(mode);
76 owners.push(/const owner = "([^"]+)"/.exec(script)?.[1] ?? "");
77 output = JSON.stringify({
78 nodePath: "/node",
79 platforms: [{ platform: "ios", available: true }],
80 hubPort: 1234,
81 hubToken: "hub-token",
82 helpers: { serveSimAxSettings: null, serveSimCli: null },
83 ...(mode === "agent-start"
84 ? { daemonPort: 1235, token: "fixture", entryPath: "/agent.mjs" }
85 : {}),
86 });
87 if (mode === "") {
88 output = `${"verbose-prefix ".repeat(400)}PANIC: emulator SDK is broken\n`;
89 exitCode = 1;
90 }
91 }
92 return ChildProcessSpawner.makeHandle({
93 pid: ChildProcessSpawner.ProcessId(123),
94 stdout: Stream.make(new TextEncoder().encode(output)),
95 stderr: Stream.empty,
96 all: Stream.empty,
97 exitCode: forwarding
98 ? Effect.never
99 : Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
100 isRunning: Effect.succeed(forwarding),
101 kill: () => Effect.void,
102 stdin: Sink.drain,
103 getInputFd: () => Sink.drain,
104 getOutputFd: () => Stream.empty,
105 unref: Effect.succeed(Effect.void),
106 });
107 }),
108 );
109 const host = yield* SshDeviceHost.make(
110 { kind: "ssh", id: "test", label: "Test", target: "test.example" },
111 () =>
112 rejectConfig
113 ? Effect.fail(
114 new DeviceHost.DeviceHostError({
115 hostId: "test",
116 step: "configuring agent access",
117 cause: new Error("fixture failure"),
118 }),
119 )
120 : Effect.void,
121 ).pipe(
122 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
123 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
124 Effect.provideService(
125 HttpClient.HttpClient,
126 HttpClient.make((request) =>
127 Effect.sync(() => {
128 if (!request.url.endsWith("/rpc"))
129 return HttpClientResponse.fromWeb(request, new Response("ok"));
130 releases.push(request.headers.authorization);
131 return HttpClientResponse.fromWeb(request, Response.json({ result: { ok: true } }));
132 }),
133 ),
134 ),
135 );
136 yield* host.ensureReady(() => Effect.void);
137 yield* SshDeviceHost.probe({
138 kind: "ssh",
139 id: "test",
140 label: "Test",
141 target: "test.example",
142 }).pipe(
143 Effect.provide(ServerConfig.layerTest(home, home)),
144 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
145 );
146 expect(new Set(owners).size).toBe(1);
147 expect(owners[0]).toMatch(/^[a-f0-9]{24}$/);
148 expect(forwards).toBe(1);
149 expect(modes.filter((mode) => mode === "start")).toHaveLength(2);
150 const ready = yield* host.current;
151 expect(ready).not.toBeNull();
152 if (ready) {
153 const failed = yield* ready.run("emulator", ["-list-avds"]);
154 expect(failed.code).toBe(1);
155 expect(failed.stdout).toMatch(/^\[truncated\]/);
156 expect(failed.stderr).toBe(failed.stdout.trim());
157 expect(failed.stderr).toMatch(/PANIC: emulator SDK is broken$/);
158 }
159 yield* host.platformAvailability("ios");
160 expect((yield* host.summary).hubInstalled).toBe(true);
161 const failed = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.result);
162 expect(failed._tag).toBe("Failure");
163 expect(forwards).toBe(0);
164 expect(modes.at(-1)).toBe("stop-agent");
165 expect(yield* host.current).toBeNull();
166 rejectConfig = false;
167 yield* host.ensureAgentReady(() => Effect.void);
168 yield* host.platformAvailability("ios");
169 expect((yield* host.summary).agentDeviceInstalled).toBe(true);
170 // Close releases agent sessions through the forwarded daemon.
171 const agentReady = yield* host.current;
172 yield* agentReady!.releaseAgentSession!("t3-session", "device");
173 expect(releases).toEqual(["Bearer fixture"]);
174 yield* host.stopAgent;
175 expect(forwards).toBe(1);
176 const multiplexingOff = ["ControlMaster=no", "ControlPath=none", "ControlPersist=no"];
177 expect(tunnelArgs.length).toBeGreaterThan(0);
178 for (const args of tunnelArgs) {
179 for (const option of multiplexingOff) {
180 const index = args.indexOf(option);
181 expect(index).toBeGreaterThan(0);
182 expect(args[index - 1]).toBe("-o");
183 }
184 // The tunnel lives only as long as the server holds the write end of its stdin.
185 expect(args).not.toContain("-N");
186 expect(args.slice(args.indexOf("test.example") + 1)).toEqual([
187 "sh",
188 "-c",
189 "'exec cat >/dev/null'",
190 ]);
191 }
192 expect(tunnelStdins).toEqual(tunnelArgs.map(() => "pipe"));
193 expect(commandArgs.length).toBeGreaterThan(0);
194 for (const args of commandArgs) {
195 for (const option of multiplexingOff) expect(args).not.toContain(option);
196 }
197 yield* host.stop;
198 expect(forwards).toBe(0);
199 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
200);
201
202it.effect("releases the forward of a bootstrap interrupted before its hub answered", () =>
203 Effect.gen(function* () {
204 const fs = yield* FileSystem.FileSystem;
205 const home = yield* fs.makeTempDirectoryScoped();
206 const probing = yield* Deferred.make<void>();
207 let holdReadiness = true;
208 let forwards = 0;
209 const spawner = ChildProcessSpawner.make((command) =>
210 Effect.gen(function* () {
211 const forwarding = command._tag === "StandardCommand" && command.args.includes("-L");
212 if (forwarding) {
213 forwards++;
214 yield* Effect.addFinalizer(() =>
215 Effect.sync(() => {
216 forwards--;
217 }),
218 );
219 }
220 const output = forwarding
221 ? ""
222 : JSON.stringify({
223 nodePath: "/node",
224 platforms: [{ platform: "ios", available: true }],
225 hubPort: 1234,
226 hubToken: "hub-token",
227 helpers: { serveSimAxSettings: null, serveSimCli: null },
228 });
229 return ChildProcessSpawner.makeHandle({
230 pid: ChildProcessSpawner.ProcessId(123),
231 stdout: Stream.make(new TextEncoder().encode(output)),
232 stderr: Stream.empty,
233 all: Stream.empty,
234 exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
235 isRunning: Effect.succeed(forwarding),
236 kill: () => Effect.void,
237 stdin: Sink.drain,
238 getInputFd: () => Sink.drain,
239 getOutputFd: () => Stream.empty,
240 unref: Effect.succeed(Effect.void),
241 });
242 }),
243 );
244 const publishing = yield* Deferred.make<void>();
245 let holdReadyStatus = false;
246 const host = yield* SshDeviceHost.make(
247 {
248 kind: "ssh",
249 id: "test",
250 label: "Test",
251 target: "test.example",
252 },
253 undefined,
254 (status) =>
255 status === "ready" && holdReadyStatus
256 ? Deferred.succeed(publishing, undefined).pipe(Effect.andThen(Effect.never))
257 : Effect.void,
258 ).pipe(
259 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
260 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
261 Effect.provideService(
262 HttpClient.HttpClient,
263 HttpClient.make((request) =>
264 Effect.gen(function* () {
265 if (holdReadiness) {
266 yield* Deferred.succeed(probing, undefined);
267 return yield* Effect.never;
268 }
269 return HttpClientResponse.fromWeb(request, new Response("ok"));
270 }),
271 ),
272 ),
273 );
274 const first = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild);
275 yield* Deferred.await(probing);
276 yield* Fiber.interrupt(first);
277 expect(forwards).toBe(0);
278 holdReadiness = false;
279 yield* host.ensureReady(() => Effect.void);
280 yield* host.stop;
281 expect(forwards).toBe(0);
282 holdReadyStatus = true;
283 const second = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild);
284 yield* Deferred.await(publishing);
285 yield* Fiber.interrupt(second);
286 holdReadyStatus = false;
287 yield* host.ensureReady(() => Effect.void);
288 expect(forwards).toBe(1);
289 yield* host.stop;
290 expect(forwards).toBe(0);
291 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
292);
293
294it.effect("lists Android devices without probing the host again once it reports Android", () =>
295 Effect.gen(function* () {
296 const fs = yield* FileSystem.FileSystem;
297 const home = yield* fs.makeTempDirectoryScoped();
298 const modes: string[] = [];
299 let androidInstalled = false;
300 const spawner = ChildProcessSpawner.make((command) =>
301 Effect.gen(function* () {
302 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
303 const forwarding = command.args.includes("-L");
304 let output = "";
305 if (!forwarding) {
306 const stdin = command.options.stdin;
307 const script =
308 stdin && typeof stdin === "object" && "stream" in stdin && Stream.isStream(stdin.stream)
309 ? yield* stdin.stream.pipe(
310 Stream.decodeText(),
311 Stream.runFold(
312 () => "",
313 (a, b) => a + b,
314 ),
315 )
316 : "";
317 const mode = /const mode = "([^"]+)"/.exec(script)?.[1] ?? "";
318 modes.push(mode);
319 output =
320 mode === ""
321 ? "Pixel_8\n"
322 : JSON.stringify({
323 nodePath: "/node",
324 platforms: [
325 { platform: "ios", available: true },
326 { platform: "android", available: androidInstalled },
327 ],
328 hubPort: 1234,
329 hubToken: "hub-token",
330 helpers: { serveSimAxSettings: null, serveSimCli: null },
331 });
332 }
333 return ChildProcessSpawner.makeHandle({
334 pid: ChildProcessSpawner.ProcessId(123),
335 stdout: Stream.make(new TextEncoder().encode(output)),
336 stderr: Stream.empty,
337 all: Stream.empty,
338 exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
339 isRunning: Effect.succeed(forwarding),
340 kill: () => Effect.void,
341 stdin: Sink.drain,
342 getInputFd: () => Sink.drain,
343 getOutputFd: () => Stream.empty,
344 unref: Effect.succeed(Effect.void),
345 });
346 }),
347 );
348 const host = yield* SshDeviceHost.make({
349 kind: "ssh",
350 id: "test",
351 label: "Test",
352 target: "test.example",
353 }).pipe(
354 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
355 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
356 Effect.provideService(
357 HttpClient.HttpClient,
358 HttpClient.make((request) =>
359 Effect.succeed(
360 HttpClientResponse.fromWeb(
361 request,
362 new Response(
363 request.url.endsWith("/api/devices")
364 ? JSON.stringify({ simulators: [], emulators: [] })
365 : "ok",
366 ),
367 ),
368 ),
369 ),
370 ),
371 );
372 const ready = yield* host.ensureReady(() => Effect.void);
373 const androidNames = ready.devices.list.pipe(
374 Effect.map((list) =>
375 list.devices.filter((device) => device.platform === "android").map((device) => device.name),
376 ),
377 );
378
379 modes.splice(0);
380
381 expect(yield* androidNames).toEqual([]);
382 expect(modes.splice(0)).toEqual(["probe"]);
383 expect(yield* androidNames).toEqual([]);
384 expect(modes.splice(0)).toEqual(["probe"]);
385
386 // The SDK appears after connecting, so the next listing still probes the host.
387 androidInstalled = true;
388 expect(yield* androidNames).toEqual(["Pixel_8"]);
389 expect(modes.splice(0)).toEqual(["probe", ""]);
390
391 // Android is now known to be available, so listing only runs `emulator -list-avds`.
392 expect(yield* androidNames).toEqual(["Pixel_8"]);
393 expect(modes.splice(0)).toEqual([""]);
394
395 androidInstalled = false;
396 expect((yield* host.platformAvailability("android")).available).toBe(false);
397 expect(modes.splice(0)).toEqual(["probe"]);
398 expect(yield* androidNames).toEqual([]);
399 expect(modes.splice(0)).toEqual(["probe"]);
400
401 androidInstalled = true;
402 yield* host.inspect;
403 expect(modes.splice(0)).toEqual(["probe"]);
404 expect(yield* androidNames).toEqual(["Pixel_8"]);
405 expect(modes.splice(0)).toEqual([""]);
406 yield* host.stop;
407 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
408);
409
410it.effect(
411 "keeps the SSH forward through slow health probes and reconnects once the hub stays down",
412 () =>
413 Effect.gen(function* () {
414 const fs = yield* FileSystem.FileSystem;
415 const home = yield* fs.makeTempDirectoryScoped();
416 let forwardSpawns = 0;
417 let hubAnswers = true;
418 const spawner = ChildProcessSpawner.make((command) =>
419 Effect.gen(function* () {
420 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
421 const forwarding = command.args.includes("-L");
422 // A fresh forward reaches a working hub again.
423 if (forwarding && ++forwardSpawns > 1) hubAnswers = true;
424 const output = JSON.stringify({
425 nodePath: "/node",
426 platforms: [{ platform: "ios", available: true }],
427 hubPort: 1234,
428 hubToken: "hub-token",
429 helpers: { serveSimAxSettings: null, serveSimCli: null },
430 });
431 return ChildProcessSpawner.makeHandle({
432 pid: ChildProcessSpawner.ProcessId(123),
433 stdout: Stream.make(new TextEncoder().encode(output)),
434 stderr: Stream.empty,
435 all: Stream.empty,
436 exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
437 isRunning: Effect.succeed(forwarding),
438 kill: () => Effect.void,
439 stdin: Sink.drain,
440 getInputFd: () => Sink.drain,
441 getOutputFd: () => Stream.empty,
442 unref: Effect.succeed(Effect.void),
443 });
444 }),
445 );
446 const host = yield* SshDeviceHost.make({
447 kind: "ssh",
448 id: "test",
449 label: "Test",
450 target: "test.example",
451 }).pipe(
452 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
453 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
454 Effect.provideService(
455 HttpClient.HttpClient,
456 HttpClient.make((request) =>
457 hubAnswers
458 ? Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok")))
459 : Effect.never,
460 ),
461 ),
462 );
463 yield* host.ensureReady(() => Effect.void);
464 expect(forwardSpawns).toBe(1);
465
466 // A loaded host can miss a probe or two without being gone; the forward stays up.
467 hubAnswers = false;
468 yield* TestClock.adjust("30 seconds");
469 hubAnswers = true;
470 yield* TestClock.adjust("2 minutes");
471 expect(forwardSpawns).toBe(1);
472
473 // A hub that keeps missing probes is still replaced.
474 hubAnswers = false;
475 // Recovery reset the miss count, so two new misses do not reconnect yet.
476 yield* TestClock.adjust("30 seconds");
477 expect(forwardSpawns).toBe(1);
478 yield* TestClock.adjust("30 seconds");
479 expect(forwardSpawns).toBe(2);
480 yield* host.stop;
481 }).pipe(
482 Effect.scoped,
483 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, TestClock.layer())),
484 ),
485);
486
487it.effect.each([
488 {
489 name: "a Windows PowerShell login shell",
490 exitCode: 1,
491 stderr: "sh : The term 'sh' is not recognized as the name of a cmdlet\r\n",
492 shellEcho: "%OS%\r\nWindows_NT\r\n",
493 nodePlatform: null,
494 windows: true,
495 },
496 {
497 name: "a Windows cmd login shell",
498 exitCode: 1,
499 stderr: "'sh' is not recognized as an internal or external command,\r\n",
500 shellEcho: "Windows_NT $env:OS\r\n",
501 nodePlatform: null,
502 windows: true,
503 },
504 {
505 name: "a Windows cmd login shell returning 9009",
506 exitCode: 9009,
507 stderr: "'sh' is not recognized as an internal or external command,\r\n",
508 shellEcho: "Windows_NT $env:OS\r\n",
509 nodePlatform: null,
510 windows: true,
511 },
512 {
513 name: "a Windows Git Bash login shell without a spawnable npm",
514 exitCode: 1,
515 stderr: "npm is missing from the non-interactive SSH PATH\n",
516 shellEcho: "%OS% :OS\n",
517 nodePlatform: "win32",
518 windows: true,
519 },
520 {
521 name: "a Linux host without npm",
522 exitCode: 1,
523 stderr: "npm is missing from the non-interactive SSH PATH\n",
524 shellEcho: "%OS% :OS\n",
525 nodePlatform: "linux",
526 windows: false,
527 },
528 {
529 name: "a POSIX host without Node",
530 exitCode: 1,
531 stderr: "Node is missing from the non-interactive SSH PATH\n",
532 shellEcho: "%OS% :OS\n",
533 nodePlatform: null,
534 windows: false,
535 },
536 {
537 name: "an unrecognized SSH failure",
538 exitCode: 255,
539 stderr: "Connection refused\n",
540 shellEcho: "Windows_NT\n",
541 nodePlatform: "win32",
542 windows: false,
543 },
544 {
545 name: "a ProxyCommand permission error",
546 exitCode: 255,
547 stderr: "bash: /usr/local/bin/proxy: Permission denied\n",
548 shellEcho: "",
549 nodePlatform: null,
550 windows: false,
551 },
552 {
553 name: "an untrusted host key",
554 exitCode: 255,
555 stderr: "Host key verification failed.\r\n",
556 shellEcho: "Windows_NT\n",
557 nodePlatform: "win32",
558 windows: false,
559 connect: { reason: "host_key_unknown", detail: "Host key verification failed." },
560 },
561 {
562 name: "a changed host key",
563 exitCode: 255,
564 stderr:
565 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\nHost key verification failed.\n",
566 shellEcho: "",
567 nodePlatform: null,
568 windows: false,
569 connect: {
570 reason: "host_key_changed",
571 detail: "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @",
572 },
573 },
574 {
575 name: "a rejected login",
576 exitCode: 255,
577 stderr: "me@test.example: Permission denied (publickey).\n",
578 shellEcho: "",
579 nodePlatform: null,
580 windows: false,
581 connect: { reason: "auth", detail: "me@test.example: Permission denied (publickey)." },
582 },
583 {
584 name: "an unresolvable host name",
585 exitCode: 255,
586 stderr: "ssh: Could not resolve hostname test.example: Name or service not known\n",
587 shellEcho: "",
588 nodePlatform: null,
589 windows: false,
590 connect: {
591 reason: "resolve",
592 detail: "ssh: Could not resolve hostname test.example: Name or service not known",
593 },
594 },
595 {
596 name: "a refused connection",
597 exitCode: 255,
598 stderr: "ssh: connect to host test.example port 22: Connection refused\n",
599 shellEcho: "",
600 nodePlatform: null,
601 windows: false,
602 connect: {
603 reason: "connect",
604 detail: "ssh: connect to host test.example port 22: Connection refused",
605 },
606 },
607])(
608 "probe failure on $name keeps the SSH cause",
609 ({ exitCode, stderr, shellEcho, nodePlatform, windows, connect }) =>
610 Effect.gen(function* () {
611 const fs = yield* FileSystem.FileSystem;
612 const home = yield* fs.makeTempDirectoryScoped();
613 const shellChecks: string[] = [];
614 const spawner = ChildProcessSpawner.make((command) =>
615 Effect.gen(function* () {
616 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
617 const remote = command.args.slice(command.args.indexOf("test.example") + 1).join(" ");
618 const shellCheck = remote === "echo %OS% $env:OS";
619 const platformCheck = remote === "node -p process.platform";
620 if (shellCheck || platformCheck) shellChecks.push(remote);
621 return ChildProcessSpawner.makeHandle({
622 pid: ChildProcessSpawner.ProcessId(123),
623 stdout: Stream.make(
624 new TextEncoder().encode(
625 shellCheck ? shellEcho : platformCheck ? `${nodePlatform ?? ""}\n` : "",
626 ),
627 ),
628 stderr: Stream.make(
629 new TextEncoder().encode(shellCheck || platformCheck ? "" : stderr),
630 ),
631 all: Stream.empty,
632 exitCode: Effect.succeed(
633 ChildProcessSpawner.ExitCode(
634 shellCheck ? 0 : platformCheck ? (nodePlatform === null ? 1 : 0) : exitCode,
635 ),
636 ),
637 isRunning: Effect.succeed(false),
638 kill: () => Effect.void,
639 stdin: Sink.drain,
640 getInputFd: () => Sink.drain,
641 getOutputFd: () => Stream.empty,
642 unref: Effect.succeed(Effect.void),
643 });
644 }),
645 );
646 const failure = yield* SshDeviceHost.probe({
647 kind: "ssh",
648 id: "test",
649 label: "Test",
650 target: "test.example",
651 }).pipe(
652 Effect.provide(ServerConfig.layerTest(home, home)),
653 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
654 Effect.flip,
655 );
656 expect(failure).toMatchObject({ _tag: "DeviceHostError", step: "probe" });
657 const sshFailure = { _tag: "SshCommandError", exitCode, stderr };
658 expect(failure.cause).toMatchObject(
659 windows
660 ? { _tag: "SshWindowsHostError", cause: sshFailure }
661 : connect
662 ? { _tag: "SshConnectError", ...connect, target: "test.example", cause: sshFailure }
663 : sshFailure,
664 );
665 // A transport failure has no remote shell to ask.
666 if (exitCode === 255) expect(shellChecks).toEqual([]);
667 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
668);