Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

use explicit localhost ports in server tests

Raine Virta4e5f0b39abd1
diff --git a/src/server.test.ts b/src/server.test.ts
index a1dd99b..ff862f7 100644
--- a/src/server.test.ts
+++ b/src/server.test.ts
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it } from "bun:test";
import { loadConfig } from "./config.ts";
import { startServer, normalizeIncomingModel } from "./server.ts";
+import { startTestServer } from "./test/server.ts";
const servers: Array<{ stop: () => void }> = [];
@@ -31,7 +32,7 @@ describe("normalizeIncomingModel", () => {
describe("server session-aware alias routing", () => {
it("routes aliases to the concrete provider used earlier in the session", async () => {
loadConfig({ env: { CCP_CODEX_SERVICE_TIER: "standard" }, forceReload: true });
- const server = startServer({ port: 0 });
+ const server = startTestServer(startServer);
servers.push(server);
const sessionId = crypto.randomUUID();
diff --git a/src/test/server.test.ts b/src/test/server.test.ts
new file mode 100644
index 0000000..3f99cfd
--- /dev/null
+++ b/src/test/server.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "bun:test";
+import { startTestServer } from "./server.ts";
+
+function bindError(): Error & { code: string } {
+ const err = new Error("address already in use") as Error & { code: string };
+ err.code = "EADDRINUSE";
+ return err;
+}
+
+describe("startTestServer", () => {
+ it("uses explicit non-zero ports and retries bind failures", () => {
+ const seen: number[] = [];
+ const server = startTestServer(({ port }) => {
+ seen.push(port);
+ if (seen.length === 1) throw bindError();
+ return { port, stop() {} };
+ }, [41000, 41001]);
+
+ expect(server.port).toBe(41001);
+ expect(seen).toEqual([41000, 41001]);
+ });
+
+ it("rejects port 0 candidates", () => {
+ expect(() => startTestServer(({ port }) => ({ port, stop() {} }), [0])).toThrow(
+ "Invalid test server port: 0",
+ );
+ });
+
+ it("rethrows non-bind failures", () => {
+ expect(() =>
+ startTestServer(() => {
+ throw new Error("boom");
+ }, [41000]),
+ ).toThrow("boom");
+ });
+});
diff --git a/src/test/server.ts b/src/test/server.ts
new file mode 100644
index 0000000..f87ef6c
--- /dev/null
+++ b/src/test/server.ts
@@ -0,0 +1,48 @@
+export interface TestServer {
+ stop: () => void;
+ port: number;
+}
+
+type StartServer = (opts: { port: number }) => TestServer;
+
+const ATTEMPTS = 50;
+const MIN_PORT = 40_000;
+const PORT_SPAN = 20_000;
+
+export function startTestServer(
+ startServer: StartServer,
+ portCandidates: Iterable<number> = defaultPortCandidates(),
+): TestServer {
+ let lastError: unknown;
+ let attempts = 0;
+
+ for (const port of portCandidates) {
+ attempts += 1;
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
+ throw new Error(`Invalid test server port: ${port}`);
+ }
+
+ try {
+ return startServer({ port });
+ } catch (err) {
+ if (!isPortUnavailableError(err)) throw err;
+ lastError = err;
+ }
+ }
+
+ throw new Error(`Unable to start test server after ${attempts} explicit port attempts`, {
+ cause: lastError,
+ });
+}
+
+function defaultPortCandidates(): number[] {
+ const start = Math.abs(process.pid) % PORT_SPAN;
+ return Array.from({ length: ATTEMPTS }, (_, i) => MIN_PORT + ((start + i) % PORT_SPAN));
+}
+
+function isPortUnavailableError(err: unknown): boolean {
+ if (!(err instanceof Error)) return false;
+ const code = (err as Error & { code?: unknown }).code;
+ if (code === "EADDRINUSE") return true;
+ return /EADDRINUSE|address already in use/i.test(err.message);
+}