|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as NetService from "@t3tools/shared/Net"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as PlatformError from "effect/PlatformError"; |
| 7 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 8 | + |
| 9 | +import * as ProcessRunner from "../processRunner.ts"; |
| 10 | +import * as AutomaticPort from "./automaticPort.ts"; |
| 11 | + |
| 12 | +const WSL_KERNEL = "6.6.87.2-microsoft-standard-WSL2\n"; |
| 13 | +const NETSTAT_PATH = "/windows/c/Windows/System32/netstat.exe"; |
| 14 | + |
| 15 | +const output = (stdout: string, code: number | null = null): ProcessRunner.ProcessRunOutput => ({ |
| 16 | + stdout, |
| 17 | + stderr: "", |
| 18 | + code: code === null ? null : ChildProcessSpawner.ExitCode(code), |
| 19 | + timedOut: false, |
| 20 | + stdoutTruncated: false, |
| 21 | + stderrTruncated: false, |
| 22 | + stdoutInvalidUtf8: false, |
| 23 | + stderrInvalidUtf8: false, |
| 24 | +}); |
| 25 | + |
| 26 | +const listening = (ports: ReadonlyArray<number>, host = "127.0.0.1", foreign = "0.0.0.0:0") => |
| 27 | + ports.map((port) => ` TCP ${host}:${port} ${foreign} ABHÖREN 4242`).join("\r\n"); |
| 28 | + |
| 29 | +interface Scenario { |
| 30 | + readonly kernelRelease?: string; |
| 31 | + readonly windowsPorts?: ReadonlyArray<number>; |
| 32 | + readonly windowsPortsV6?: ReadonlyArray<number>; |
| 33 | + readonly tcpv6ExitCode?: number; |
| 34 | + readonly wslpathMissing?: boolean; |
| 35 | + readonly reservedPorts?: ReadonlyArray<number>; |
| 36 | +} |
| 37 | + |
| 38 | +const select = (scenario: Scenario) => { |
| 39 | + const commands: string[] = []; |
| 40 | + const reserved = [...(scenario.reservedPorts ?? [50_001])]; |
| 41 | + const net: NetService.NetServiceShape = { |
| 42 | + canListenOnHost: () => Effect.succeed(true), |
| 43 | + hasListenerOnHost: () => Effect.succeed(false), |
| 44 | + isPortAvailableOnLoopback: () => Effect.succeed(true), |
| 45 | + reserveLoopbackPort: () => Effect.sync(() => reserved.shift() ?? 60_000), |
| 46 | + findAvailablePort: Effect.succeed, |
| 47 | + }; |
| 48 | + const run: ProcessRunner.ProcessRunner["Service"]["run"] = (input) => { |
| 49 | + commands.push(input.command); |
| 50 | + if (input.command === "wslpath") { |
| 51 | + if (scenario.wslpathMissing) { |
| 52 | + return Effect.fail( |
| 53 | + new ProcessRunner.ProcessSpawnError({ |
| 54 | + command: input.command, |
| 55 | + argumentCount: input.args.length, |
| 56 | + cause: PlatformError.systemError({ |
| 57 | + _tag: "NotFound", |
| 58 | + module: "ChildProcess", |
| 59 | + method: "spawn", |
| 60 | + }), |
| 61 | + }), |
| 62 | + ); |
| 63 | + } |
| 64 | + return Effect.succeed(output(`${NETSTAT_PATH}\n`)); |
| 65 | + } |
| 66 | + if (input.args.includes("tcpv6")) { |
| 67 | + return Effect.succeed( |
| 68 | + output( |
| 69 | + listening(scenario.windowsPortsV6 ?? [], "[::1]", "[::]:0"), |
| 70 | + scenario.tcpv6ExitCode ?? null, |
| 71 | + ), |
| 72 | + ); |
| 73 | + } |
| 74 | + return Effect.succeed(output(listening(scenario.windowsPorts ?? []))); |
| 75 | + }; |
| 76 | + const layer = Layer.mergeAll( |
| 77 | + Layer.succeed(NetService.NetService, net), |
| 78 | + Layer.succeed(ProcessRunner.ProcessRunner, { run }), |
| 79 | + FileSystem.layerNoop({ |
| 80 | + readFileString: (path) => |
| 81 | + path === "/proc/sys/kernel/osrelease" && scenario.kernelRelease !== undefined |
| 82 | + ? Effect.succeed(scenario.kernelRelease) |
| 83 | + : Effect.fail( |
| 84 | + PlatformError.systemError({ |
| 85 | + _tag: "NotFound", |
| 86 | + module: "FileSystem", |
| 87 | + method: "readFileString", |
| 88 | + }), |
| 89 | + ), |
| 90 | + }), |
| 91 | + ); |
| 92 | + return Effect.gen(function* () { |
| 93 | + const selectAutomaticPort = yield* AutomaticPort.make; |
| 94 | + const port = yield* selectAutomaticPort(3773); |
| 95 | + return { port, commands }; |
| 96 | + }).pipe(Effect.provide(layer)); |
| 97 | +}; |
| 98 | + |
| 99 | +describe("automatic port selection", () => { |
| 100 | + it.effect("moves off the default port only when Windows already listens on it", () => |
| 101 | + Effect.gen(function* () { |
| 102 | + const free = yield* select({ kernelRelease: WSL_KERNEL, windowsPorts: [8080] }); |
| 103 | + expect(free.port).toBe(3773); |
| 104 | + expect(free.commands).toEqual(["wslpath", NETSTAT_PATH, NETSTAT_PATH]); |
| 105 | + |
| 106 | + const owned = yield* select({ kernelRelease: WSL_KERNEL, windowsPorts: [3773] }); |
| 107 | + expect(owned.port).toBe(50_001); |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + it.effect("retries a fallback port that Windows already listens on", () => |
| 112 | + Effect.gen(function* () { |
| 113 | + const result = yield* select({ |
| 114 | + kernelRelease: WSL_KERNEL, |
| 115 | + windowsPorts: [3773], |
| 116 | + windowsPortsV6: [50_001], |
| 117 | + reservedPorts: [50_001, 50_002], |
| 118 | + }); |
| 119 | + expect(result.port).toBe(50_002); |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + it.effect("keeps the local choice when the Windows snapshot is unavailable", () => |
| 124 | + Effect.gen(function* () { |
| 125 | + const partial = yield* select({ |
| 126 | + kernelRelease: WSL_KERNEL, |
| 127 | + windowsPorts: [3773], |
| 128 | + tcpv6ExitCode: 1, |
| 129 | + }); |
| 130 | + expect(partial.port).toBe(3773); |
| 131 | + |
| 132 | + const noInterop = yield* select({ kernelRelease: WSL_KERNEL, wslpathMissing: true }); |
| 133 | + expect(noInterop.port).toBe(3773); |
| 134 | + }), |
| 135 | + ); |
| 136 | + |
| 137 | + it.effect("never queries Windows outside WSL", () => |
| 138 | + Effect.gen(function* () { |
| 139 | + for (const scenario of [ |
| 140 | + { kernelRelease: "6.8.0-45-generic\n" }, |
| 141 | + // macOS and Windows have no kernel release file. |
| 142 | + {}, |
| 143 | + ] satisfies ReadonlyArray<Scenario>) { |
| 144 | + const result = yield* select(scenario); |
| 145 | + expect(result.port).toBe(3773); |
| 146 | + expect(result.commands).toEqual([]); |
| 147 | + } |
| 148 | + }), |
| 149 | + ); |
| 150 | +}); |
0 commit comments