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

localSshDeviceHost.test.ts

118 lines3.8 KB

1import { expect, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";
6import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7import {
8 isLocalSshDeviceHost,
9 LocalDeviceHostAddresses,
10 remoteSshDeviceHosts,
11} from "./localSshDeviceHost.ts";
12​
13const host = (target: string, port?: number) => ({
14 kind: "ssh" as const,
15 id: target,
16 label: target,
17 target,
18 ...(port ? { port } : {}),
19});
20const spawner = ChildProcessSpawner.make((command) =>
21 Effect.gen(function* () {
22 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
23 expect(command.args).toContain("-G");
24 const target = command.args.map(String).at(-1);
25 const configs: Record<string, string> = {
26 "mac-mini": "hostname 100.65.180.100\nport 22\n",
27 remote: "hostname 192.0.2.1\nport 22\n",
28 loopback: "hostname 127.0.1.1\nport 22\n",
29 ipv6: "hostname ::1\nport 22\n",
30 "ipv6-expanded": "hostname 0:0:0:0:0:0:0:1\nport 22\n",
31 "ipv6-upper-local": "hostname FD7A:115C:A1E0::1\nport 22\n",
32 "ipv6-remote": "hostname 2001:DB8::1\nport 22\n",
33 mapped: "hostname ::ffff:127.0.0.1\nport 22\n",
34 forwarded: "hostname 127.0.0.1\nport 2222\n",
35 proxy: "hostname 127.0.0.1\nport 22\nproxyjump bastion\n",
36 command: "hostname 127.0.0.1\nport 22\nproxycommand nc remote 22\n",
37 unresolved: "hostname example.invalid\nport 22\n",
38 };
39 return ChildProcessSpawner.makeHandle({
40 pid: ChildProcessSpawner.ProcessId(123),
41 stdout: Stream.make(new TextEncoder().encode(configs[target ?? ""] ?? "")),
42 stderr: Stream.empty,
43 all: Stream.empty,
44 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
45 exitStatus: Effect.succeed({ _tag: "Exited", code: ChildProcessSpawner.ExitCode(0) }),
46 isRunning: Effect.succeed(false),
47 kill: () => Effect.void,
48 stdin: Sink.drain,
49 getInputFd: () => Sink.drain,
50 getOutputFd: () => Stream.empty,
51 unref: Effect.succeed(Effect.void),
52 });
53 }),
54);
55const provide = <A, E>(
56 effect: Effect.Effect<A, E, Effect.Services<ReturnType<typeof isLocalSshDeviceHost>>>,
57) =>
58 effect.pipe(
59 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
60 Effect.provideService(
61 LocalDeviceHostAddresses,
62 new Set(["100.65.180.100", "fd7a:115c:a1e0::1"]),
63 ),
64 Effect.provide(NodeServices.layer),
65 );
66​
67it.effect("skips SSH aliases resolving to this machine, including loopback", () =>
68 provide(
69 Effect.gen(function* () {
70 for (const target of [
71 "mac-mini",
72 "loopback",
73 "ipv6",
74 "ipv6-expanded",
75 "ipv6-upper-local",
76 "mapped",
77 ]) {
78 expect(yield* isLocalSshDeviceHost(host(target))).toBe(true);
79 }
80 }),
81 ),
82);
83​
84it.effect("keeps remote, forwarded, proxied, and unresolved destinations", () =>
85 provide(
86 Effect.gen(function* () {
87 for (const target of [
88 "remote",
89 "ipv6-remote",
90 "forwarded",
91 "proxy",
92 "command",
93 "unresolved",
94 ]) {
95 expect(yield* isLocalSshDeviceHost(host(target))).toBe(false);
96 }
97 }),
98 ),
99);
100​
101it.effect("removes only self targets from a fanned-out host list", () =>
102 provide(
103 Effect.gen(function* () {
104 const eas = {
105 kind: "eas" as const,
106 id: "eas",
107 label: "Expo Simulators",
108 accessToken: "",
109 maxSessionMinutes: 30,
110 idleMinutes: 10,
111 maxConcurrentSessions: 2,
112 };
113 expect(
114 yield* remoteSshDeviceHosts([host("mac-mini"), eas, host("remote"), host("forwarded")]),
115 ).toEqual([eas, host("remote"), host("forwarded")]);
116 }),
117 ),
118);