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

RemoteOpenTargets.test.ts

207 lines7.1 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 exitStatus: Effect.succeed({
43 _tag: "Exited",
44 code: ChildProcessSpawner.ExitCode(result.exitCode),
45 }),
46 isRunning: Effect.succeed(false),
47 kill: () => Effect.void,
48 unref: Effect.succeed(Effect.void),
49 stdin: Sink.drain,
50 stdout: Stream.make(encoder.encode(result.stdout)),
51 stderr: Stream.empty,
52 all: Stream.empty,
53 getInputFd: () => Sink.drain,
54 getOutputFd: () => Stream.empty,
55 }),
56 );
57 }),
58 );
59​
60const layerNet = (input: {
61 readonly ipv4: boolean;
62 readonly ipv6: boolean;
63 readonly tailnet?: boolean;
64}) =>
65 Layer.succeed(NetService.NetService, {
66 canListenOnHost: () => Effect.succeed(true),
67 probeListenOnHost: () => Effect.succeed(true),
68 isPortAvailableOnLoopback: () => Effect.succeed(true),
69 hasListenerOnHost: (_port, host) =>
70 Effect.succeed(
71 host === "127.0.0.1"
72 ? input.ipv4
73 : host === "::1"
74 ? input.ipv6
75 : (host === "100.64.1.2" || host === "bb-1.tail1234.ts.net") && input.tailnet === true,
76 ),
77 reserveLoopbackPort: () => Effect.succeed(40_000),
78 findAvailablePort: (preferred) => Effect.succeed(preferred),
79 });
80​
81const resolveTargets = (input: {
82 readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean; readonly tailnet?: boolean };
83 readonly tailscale: { readonly status: CommandResult; readonly prefs: CommandResult };
84 readonly hostname: string;
85 readonly username?: string | null;
86}) => {
87 const resolve = Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) =>
88 service.resolveTargets(),
89 ).pipe(
90 Effect.provideService(HostProcess.Hostname, input.hostname),
91 Effect.provide(
92 RemoteOpenTargets.layer.pipe(
93 Layer.provide(Layer.mergeAll(layerNet(input.sshd), layerSpawner(input.tailscale))),
94 ),
95 ),
96 );
97​
98 return input.username === undefined
99 ? resolve
100 : resolve.pipe(Effect.provideService(HostProcess.Username, input.username));
101};
102​
103const prefsJson = (prefs: { readonly RunSSH: boolean; readonly WantRunning: boolean }) => ({
104 exitCode: 0,
105 stdout: JSON.stringify(
106 { ControlURL: "https://controlplane.tailscale.com", ...prefs },
107 null,
108 "\t",
109 ),
110});
111​
112const STATUS_UP = { exitCode: 0, stdout: TAILSCALE_STATUS_JSON };
113const TAILSCALE_UP = { status: STATUS_UP, prefs: prefsJson({ RunSSH: false, WantRunning: true }) };
114const TAILSCALE_SSH = { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: true }) };
115const TAILSCALE_DOWN = { status: { exitCode: 1, stdout: "" }, prefs: { exitCode: 1, stdout: "" } };
116​
117describe("RemoteOpenTargets", () => {
118 it.effect("advertises nothing when no sshd accepts and Tailscale SSH is off", () =>
119 Effect.gen(function* () {
120 const targets = yield* resolveTargets({
121 sshd: { ipv4: false, ipv6: false },
122 tailscale: TAILSCALE_UP,
123 hostname: "bb-1",
124 username: "t3-user",
125 });
126 expect(targets).toEqual([]);
127 }),
128 );
129​
130 it.effect("advertises only the tailnet name when Tailscale SSH serves without sshd", () =>
131 Effect.gen(function* () {
132 const targets = yield* resolveTargets({
133 sshd: { ipv4: false, ipv6: false },
134 tailscale: TAILSCALE_SSH,
135 hostname: "bb-1",
136 username: "t3-user",
137 });
138 expect(targets).toEqual([
139 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
140 ]);
141 }),
142 );
143​
144 it.effect("advertises the tailnet name when sshd listens only on the tailnet address", () =>
145 Effect.gen(function* () {
146 const targets = yield* resolveTargets({
147 sshd: { ipv4: false, ipv6: false, tailnet: true },
148 tailscale: TAILSCALE_UP,
149 hostname: "bb-1",
150 username: "t3-user",
151 });
152 expect(targets).toEqual([
153 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
154 ]);
155 }),
156 );
157​
158 it.effect("ignores a stored RunSSH after `tailscale down`", () =>
159 Effect.gen(function* () {
160 const targets = yield* resolveTargets({
161 sshd: { ipv4: false, ipv6: false },
162 tailscale: { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: false }) },
163 hostname: "bb-1",
164 username: null,
165 });
166 expect(targets).toEqual([]);
167 }),
168 );
169​
170 it.effect("drops the tailnet name after `tailscale down` while sshd listens", () =>
171 Effect.gen(function* () {
172 const targets = yield* resolveTargets({
173 sshd: { ipv4: true, ipv6: false },
174 tailscale: {
175 // After `tailscale down`, status still exits 0 and keeps Self from the last netmap.
176 status: {
177 exitCode: 0,
178 stdout: JSON.stringify({
179 BackendState: "Stopped",
180 Self: {
181 DNSName: "bb-1.tail1234.ts.net.",
182 TailscaleIPs: ["100.64.1.2"],
183 Online: false,
184 },
185 }),
186 },
187 prefs: prefsJson({ RunSSH: false, WantRunning: false }),
188 },
189 hostname: "bb-1",
190 username: "t3-user",
191 });
192 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
193 }),
194 );
195​
196 it.effect("accepts an sshd bound to IPv6 loopback only", () =>
197 Effect.gen(function* () {
198 const targets = yield* resolveTargets({
199 sshd: { ipv4: false, ipv6: true },
200 tailscale: TAILSCALE_DOWN,
201 hostname: "bb-1",
202 username: "t3-user",
203 });
204 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
205 }),
206 );
207});