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