Open History to see the last commit
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 id: target,
15 label: target,
16 target,
17 ...(port ? { port } : {}),
18});
19const spawner = ChildProcessSpawner.make((command) =>
20 Effect.gen(function* () {
21 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
22 expect(command.args).toContain("-G");
23 const target = command.args.at(-1);
24 const configs: Record<string, string> = {
25 "mac-mini": "hostname 100.65.180.100\nport 22\n",
26 remote: "hostname 192.0.2.1\nport 22\n",
27 loopback: "hostname 127.0.1.1\nport 22\n",
28 ipv6: "hostname ::1\nport 22\n",
29 forwarded: "hostname 127.0.0.1\nport 2222\n",
30 proxy: "hostname 127.0.0.1\nport 22\nproxyjump bastion\n",
31 command: "hostname 127.0.0.1\nport 22\nproxycommand nc remote 22\n",
32 unresolved: "hostname example.invalid\nport 22\n",
33 };
34 return ChildProcessSpawner.makeHandle({
35 pid: ChildProcessSpawner.ProcessId(123),
36 stdout: Stream.make(new TextEncoder().encode(configs[target ?? ""] ?? "")),
37 stderr: Stream.empty,
38 all: Stream.empty,
39 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
40 isRunning: Effect.succeed(false),
41 kill: () => Effect.void,
42 stdin: Sink.drain,
43 getInputFd: () => Sink.drain,
44 getOutputFd: () => Stream.empty,
45 unref: Effect.succeed(Effect.void),
46 });
47 }),
48);
49const provide = <A, E>(
50 effect: Effect.Effect<A, E, Effect.Services<ReturnType<typeof isLocalSshDeviceHost>>>,
51) =>
52 effect.pipe(
53 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
54 Effect.provideService(LocalDeviceHostAddresses, new Set(["100.65.180.100"])),
55 Effect.provide(NodeServices.layer),
56 );
57
58it.effect("skips SSH aliases resolving to this machine, including loopback", () =>
59 provide(
60 Effect.gen(function* () {
61 for (const target of ["mac-mini", "loopback", "ipv6"]) {
62 expect(yield* isLocalSshDeviceHost(host(target))).toBe(true);
63 }
64 }),
65 ),
66);
67
68it.effect("keeps remote, forwarded, proxied, and unresolved destinations", () =>
69 provide(
70 Effect.gen(function* () {
71 for (const target of ["remote", "forwarded", "proxy", "command", "unresolved"]) {
72 expect(yield* isLocalSshDeviceHost(host(target))).toBe(false);
73 }
74 }),
75 ),
76);
77
78it.effect("removes only self targets from a fanned-out host list", () =>
79 provide(
80 Effect.gen(function* () {
81 expect(
82 yield* remoteSshDeviceHosts([host("mac-mini"), host("remote"), host("forwarded")]),
83 ).toEqual([host("remote"), host("forwarded")]);
84 }),
85 ),
86);