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

DesktopSshEnvironment.test.ts

118 lines3.9 KB

1import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as NetService from "@t3tools/shared/Net";
5import { SshPasswordPromptError } from "@t3tools/ssh/errors";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10​
11import * as DesktopSshEnvironment from "./DesktopSshEnvironment.ts";
12import * as DesktopSshPasswordPrompts from "./DesktopSshPasswordPrompts.ts";
13​
14function makeTempHomeDir() {
15 return Effect.gen(function* () {
16 const fs = yield* FileSystem.FileSystem;
17 return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-env-test-" });
18 });
20​
21describe("sshEnvironment", () => {
22 it("treats password prompt timeouts as cancellable authentication prompts", () => {
23 assert.equal(
24 DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation(
25 new SshPasswordPromptError({
26 message: "SSH authentication timed out for devbox.",
27 cause: new DesktopSshPasswordPrompts.DesktopSshPromptTimedOutError({
28 requestId: "prompt-1",
29 destination: "devbox",
30 }),
31 }),
32 ),
33 true,
34 );
35 });
36​
37 it.effect("wires desktop host discovery through the ssh package runtime", () =>
38 Effect.gen(function* () {
39 const fs = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 const homeDir = yield* makeTempHomeDir();
42 const sshDir = path.join(homeDir, ".ssh");
43 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
44 yield* fs.writeFileString(
45 path.join(sshDir, "config"),
46 ["Host devbox", " HostName devbox.example.com", "Include config.d/*.conf", ""].join("\n"),
47 );
48 yield* fs.writeFileString(
49 path.join(sshDir, "config.d", "team.conf"),
50 [
51 "Host staging",
52 " HostName staging.example.com",
53 "Host *",
54 " ServerAliveInterval 30",
55 "",
56 ].join("\n"),
57 );
58 yield* fs.writeFileString(
59 path.join(sshDir, "known_hosts"),
60 [
61 "known.example.com ssh-ed25519 AAAA",
62 "|1|hashed|entry ssh-ed25519 AAAA",
63 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
64 "",
65 ].join("\n"),
66 );
67​
68 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
69 const hosts = yield* sshEnvironment.discoverHosts({ homeDir });
70 assert.deepEqual(hosts, [
71 {
72 alias: "bastion.example.com",
73 hostname: "bastion.example.com",
74 username: null,
75 port: null,
76 source: "known-hosts",
77 },
78 {
79 alias: "devbox",
80 hostname: "devbox",
81 username: null,
82 port: null,
83 source: "ssh-config",
84 },
85 {
86 alias: "known.example.com",
87 hostname: "known.example.com",
88 username: null,
89 port: null,
90 source: "known-hosts",
91 },
92 {
93 alias: "staging",
94 hostname: "staging",
95 username: null,
96 port: null,
97 source: "ssh-config",
98 },
99 ]);
100 }).pipe(
101 Effect.provide(
102 DesktopSshEnvironment.layer().pipe(
103 Layer.provideMerge(
104 Layer.succeed(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {
105 request: () => Effect.die("unexpected password prompt request"),
106 resolve: () => Effect.die("unexpected password prompt resolution"),
107 cancelPending: () => Effect.void,
108 }),
109 ),
110 Layer.provideMerge(NodeServices.layer),
111 Layer.provideMerge(NodeHttpClient.layerUndici),
112 Layer.provideMerge(NetService.layer),
113 ),
114 ),
115 Effect.scoped,
116 ),
117 );
118});