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

RemoteOpenTargets.test.ts

203 lines6.9 KB

1// @effect-diagnostics nodeBuiltinImport:off -- replace the native OS lookup and sync its ESM bindings.
2import * as NodeModule from "node:module";
3import { it } from "@effect/vitest";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as NetService from "@t3tools/shared/Net";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcess from "effect/process/ChildProcess";
11import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12import { afterEach, describe, expect, vi } from "vite-plus/test";
13​
14import * as RemoteOpenTargets from "./RemoteOpenTargets.ts";
15​
16afterEach(() => {
17 vi.restoreAllMocks();
18 NodeModule.syncBuiltinESMExports();
19});
20​
21const encoder = new TextEncoder();
22​
23const TAILSCALE_STATUS_JSON = JSON.stringify({
24 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
25});
26​
27interface CommandResult {
28 readonly exitCode: number;
29 readonly stdout: string;
31​
32const layerSpawner = (input: { readonly status: CommandResult; readonly prefs: CommandResult }) =>
33 Layer.succeed(
34 ChildProcessSpawner.ChildProcessSpawner,
35 ChildProcessSpawner.make((command) => {
36 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
37 const result = args[0] === "debug" ? input.prefs : input.status;
38 return Effect.succeed(
39 ChildProcessSpawner.makeHandle({
40 pid: ChildProcessSpawner.ProcessId(1),
41 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
42 isRunning: Effect.succeed(false),
43 kill: () => Effect.void,
44 unref: Effect.succeed(Effect.void),
45 stdin: Sink.drain,
46 stdout: Stream.make(encoder.encode(result.stdout)),
47 stderr: Stream.empty,
48 all: Stream.empty,
49 getInputFd: () => Sink.drain,
50 getOutputFd: () => Stream.empty,
51 }),
52 );
53 }),
54 );
55​
56const layerNet = (input: {
57 readonly ipv4: boolean;
58 readonly ipv6: boolean;
59 readonly tailnet?: boolean;
60}) =>
61 Layer.succeed(NetService.NetService, {
62 canListenOnHost: () => Effect.succeed(true),
63 probeListenOnHost: () => Effect.succeed(true),
64 isPortAvailableOnLoopback: () => Effect.succeed(true),
65 hasListenerOnHost: (_port, host) =>
66 Effect.succeed(
67 host === "127.0.0.1"
68 ? input.ipv4
69 : host === "::1"
70 ? input.ipv6
71 : (host === "100.64.1.2" || host === "bb-1.tail1234.ts.net") && input.tailnet === true,
72 ),
73 reserveLoopbackPort: () => Effect.succeed(40_000),
74 findAvailablePort: (preferred) => Effect.succeed(preferred),
75 });
76​
77const resolveTargets = (input: {
78 readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean; readonly tailnet?: boolean };
79 readonly tailscale: { readonly status: CommandResult; readonly prefs: CommandResult };
80 readonly hostname: string;
81 readonly username?: string | null;
82}) => {
83 const resolve = Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) =>
84 service.resolveTargets(),
85 ).pipe(
86 Effect.provideService(HostProcess.Hostname, input.hostname),
87 Effect.provide(
88 RemoteOpenTargets.layer.pipe(
89 Layer.provide(Layer.mergeAll(layerNet(input.sshd), layerSpawner(input.tailscale))),
90 ),
91 ),
92 );
93​
94 return input.username === undefined
95 ? resolve
96 : resolve.pipe(Effect.provideService(HostProcess.Username, input.username));
97};
98​
99const prefsJson = (prefs: { readonly RunSSH: boolean; readonly WantRunning: boolean }) => ({
100 exitCode: 0,
101 stdout: JSON.stringify(
102 { ControlURL: "https://controlplane.tailscale.com", ...prefs },
103 null,
104 "\t",
105 ),
106});
107​
108const STATUS_UP = { exitCode: 0, stdout: TAILSCALE_STATUS_JSON };
109const TAILSCALE_UP = { status: STATUS_UP, prefs: prefsJson({ RunSSH: false, WantRunning: true }) };
110const TAILSCALE_SSH = { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: true }) };
111const TAILSCALE_DOWN = { status: { exitCode: 1, stdout: "" }, prefs: { exitCode: 1, stdout: "" } };
112​
113describe("RemoteOpenTargets", () => {
114 it.effect("advertises nothing when no sshd accepts and Tailscale SSH is off", () =>
115 Effect.gen(function* () {
116 const targets = yield* resolveTargets({
117 sshd: { ipv4: false, ipv6: false },
118 tailscale: TAILSCALE_UP,
119 hostname: "bb-1",
120 username: "t3-user",
121 });
122 expect(targets).toEqual([]);
123 }),
124 );
125​
126 it.effect("advertises only the tailnet name when Tailscale SSH serves without sshd", () =>
127 Effect.gen(function* () {
128 const targets = yield* resolveTargets({
129 sshd: { ipv4: false, ipv6: false },
130 tailscale: TAILSCALE_SSH,
131 hostname: "bb-1",
132 username: "t3-user",
133 });
134 expect(targets).toEqual([
135 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
136 ]);
137 }),
138 );
139​
140 it.effect("advertises the tailnet name when sshd listens only on the tailnet address", () =>
141 Effect.gen(function* () {
142 const targets = yield* resolveTargets({
143 sshd: { ipv4: false, ipv6: false, tailnet: true },
144 tailscale: TAILSCALE_UP,
145 hostname: "bb-1",
146 username: "t3-user",
147 });
148 expect(targets).toEqual([
149 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
150 ]);
151 }),
152 );
153​
154 it.effect("ignores a stored RunSSH after `tailscale down`", () =>
155 Effect.gen(function* () {
156 const targets = yield* resolveTargets({
157 sshd: { ipv4: false, ipv6: false },
158 tailscale: { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: false }) },
159 hostname: "bb-1",
160 username: null,
161 });
162 expect(targets).toEqual([]);
163 }),
164 );
165​
166 it.effect("drops the tailnet name after `tailscale down` while sshd listens", () =>
167 Effect.gen(function* () {
168 const targets = yield* resolveTargets({
169 sshd: { ipv4: true, ipv6: false },
170 tailscale: {
171 // After `tailscale down`, status still exits 0 and keeps Self from the last netmap.
172 status: {
173 exitCode: 0,
174 stdout: JSON.stringify({
175 BackendState: "Stopped",
176 Self: {
177 DNSName: "bb-1.tail1234.ts.net.",
178 TailscaleIPs: ["100.64.1.2"],
179 Online: false,
180 },
181 }),
182 },
183 prefs: prefsJson({ RunSSH: false, WantRunning: false }),
184 },
185 hostname: "bb-1",
186 username: "t3-user",
187 });
188 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
189 }),
190 );
191​
192 it.effect("accepts an sshd bound to IPv6 loopback only", () =>
193 Effect.gen(function* () {
194 const targets = yield* resolveTargets({
195 sshd: { ipv4: false, ipv6: true },
196 tailscale: TAILSCALE_DOWN,
197 hostname: "bb-1",
198 username: "t3-user",
199 });
200 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
201 }),
202 );
203});