|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 2 | import { it as effectIt } from "@effect/vitest"; |
| 3 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | +import * as NetService from "@t3tools/shared/Net"; |
3 | 5 | import type * as NodeOS from "node:os"; |
4 | 6 | import * as Effect from "effect/Effect"; |
| 7 | +import * as Fiber from "effect/Fiber"; |
5 | 8 | import * as Layer from "effect/Layer"; |
| 9 | +import * as Option from "effect/Option"; |
6 | 10 | import * as Sink from "effect/Sink"; |
7 | 11 | import * as Stream from "effect/Stream"; |
8 | 12 | import * as HttpClient from "effect/http/HttpClient"; |
9 | 13 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 14 | +import * as ChildProcess from "effect/process/ChildProcess"; |
10 | 15 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 16 | +import * as TestClock from "effect/testing/TestClock"; |
11 | 17 | import { describe, expect, it } from "vite-plus/test"; |
12 | 18 |
|
13 | 19 | import * as ServerConfig from "../config.ts"; |
14 | 20 | import * as DirectEndpoints from "./DirectEndpoints.ts"; |
15 | 21 | import { resolveBoundEndpoints } from "./DirectEndpoints.ts"; |
| 22 | +import * as RemoteOpenTargets from "./RemoteOpenTargets.ts"; |
16 | 23 |
|
17 | 24 | const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = { |
18 | 25 | lo0: [ |
@@ -239,3 +246,93 @@ describe("DirectEndpoints Tailscale Serve", () => { |
239 | 246 | }), |
240 | 247 | ); |
241 | 248 | }); |
| 249 | + |
| 250 | +// A CLI that cannot reach tailscaled: `tailscale status` never answers, so each read waits out its timeout. |
| 251 | +const layerTailscaleHung = (onStatusSpawn: () => void) => |
| 252 | + Layer.succeed( |
| 253 | + ChildProcessSpawner.ChildProcessSpawner, |
| 254 | + ChildProcessSpawner.make((command) => { |
| 255 | + if (ChildProcess.isStandardCommand(command) && command.args[0] === "status") onStatusSpawn(); |
| 256 | + return Effect.succeed( |
| 257 | + ChildProcessSpawner.makeHandle({ |
| 258 | + pid: ChildProcessSpawner.ProcessId(1), |
| 259 | + exitCode: Effect.never, |
| 260 | + isRunning: Effect.succeed(true), |
| 261 | + kill: () => Effect.void, |
| 262 | + unref: Effect.succeed(Effect.void), |
| 263 | + stdin: Sink.drain, |
| 264 | + stdout: Stream.empty, |
| 265 | + stderr: Stream.empty, |
| 266 | + all: Stream.empty, |
| 267 | + getInputFd: () => Sink.drain, |
| 268 | + getOutputFd: () => Stream.empty, |
| 269 | + }), |
| 270 | + ); |
| 271 | + }), |
| 272 | + ); |
| 273 | + |
| 274 | +const layerSshdListening = Layer.succeed(NetService.NetService, { |
| 275 | + canListenOnHost: () => Effect.succeed(true), |
| 276 | + isPortAvailableOnLoopback: () => Effect.succeed(true), |
| 277 | + hasListenerOnHost: () => Effect.succeed(true), |
| 278 | + reserveLoopbackPort: () => Effect.succeed(40_000), |
| 279 | + findAvailablePort: (preferred) => Effect.succeed(preferred), |
| 280 | +}); |
| 281 | + |
| 282 | +const MDNS_ONLY = [{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]; |
| 283 | + |
| 284 | +// What ws.loadServerConfig resolves for every client that connects. |
| 285 | +const connectWithTailscaleHung = (onStatusSpawn: () => void) => |
| 286 | + Effect.gen(function* () { |
| 287 | + const remoteOpenTargets = yield* RemoteOpenTargets.RemoteOpenTargets; |
| 288 | + const directEndpoints = yield* DirectEndpoints.DirectEndpoints; |
| 289 | + return Effect.all([remoteOpenTargets.resolveTargets(), directEndpoints.resolve()], { |
| 290 | + concurrency: "unbounded", |
| 291 | + }).pipe( |
| 292 | + Effect.provideService(HostProcess.Hostname, "bb-1"), |
| 293 | + Effect.provideService(HostProcess.Username, "t3-user"), |
| 294 | + ); |
| 295 | + }).pipe( |
| 296 | + Effect.provide( |
| 297 | + Layer.mergeAll(DirectEndpoints.layer, RemoteOpenTargets.layer).pipe( |
| 298 | + Layer.provide( |
| 299 | + Layer.mergeAll( |
| 300 | + layerServeConfig, |
| 301 | + layerTailscaleHung(onStatusSpawn), |
| 302 | + layerServeProbe(200), |
| 303 | + layerSshdListening, |
| 304 | + ), |
| 305 | + ), |
| 306 | + ), |
| 307 | + ), |
| 308 | + ); |
| 309 | + |
| 310 | +describe("Tailscale status shared by DirectEndpoints and RemoteOpenTargets", () => { |
| 311 | + effectIt.effect("reads `tailscale status` once per connect, not once per service", () => |
| 312 | + Effect.gen(function* () { |
| 313 | + let statusReads = 0; |
| 314 | + const connect = yield* connectWithTailscaleHung(() => statusReads++); |
| 315 | + const first = yield* Effect.forkChild(connect); |
| 316 | + yield* TestClock.adjust("4 seconds"); |
| 317 | + expect(yield* Fiber.join(first)).toEqual([MDNS_ONLY, []]); |
| 318 | + expect(statusReads, "tailscale status spawns for one connect").toBe(1); |
| 319 | + }), |
| 320 | + ); |
| 321 | + |
| 322 | + effectIt.effect( |
| 323 | + "answers a later connect from the settled failed read without waiting again", |
| 324 | + () => |
| 325 | + Effect.gen(function* () { |
| 326 | + const connect = yield* connectWithTailscaleHung(() => {}); |
| 327 | + const first = yield* Effect.forkChild(connect); |
| 328 | + yield* TestClock.adjust("4 seconds"); |
| 329 | + expect(yield* Fiber.join(first)).toEqual([MDNS_ONLY, []]); |
| 330 | + |
| 331 | + const later = yield* connect.pipe(Effect.timeoutOption("1 second"), Effect.forkChild); |
| 332 | + yield* TestClock.adjust("1 second"); |
| 333 | + expect(yield* Fiber.join(later), "later connect still waiting on tailscale status").toEqual( |
| 334 | + Option.some([MDNS_ONLY, []]), |
| 335 | + ); |
| 336 | + }), |
| 337 | + ); |
| 338 | +}); |
0 commit comments