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