Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

test(cli): use portable loopback fixtures for reset redirects

Joseph Mikhailf7fe16c052c9
diff --git a/ix-cli/src/client/__tests__/reset-redirect.test.ts b/ix-cli/src/client/__tests__/reset-redirect.test.ts
index 9006eee0..8c62efc2 100644
--- a/ix-cli/src/client/__tests__/reset-redirect.test.ts
+++ b/ix-cli/src/client/__tests__/reset-redirect.test.ts
@@ -2,7 +2,7 @@
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
import type { AddressInfo } from "node:net";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { IxClient } from "../api.js";
import { isBackendUnreachable } from "../../cli/errors.js";
@@ -10,9 +10,15 @@ const opId = "11111111-1111-4111-8111-111111111111";
const servers: Server[] = [];
type Seen = { method: string; path: string; body: string };
-// 127.0.0.2 is loopback, but deliberately selects the remote async branch of
-// IxClient. Real fetch and real servers are used; no network error is mocked.
-async function fixture(reply: (request: Seen, response: ServerResponse) => void, host = "127.0.0.2") {
+// Use standard localhost on every platform. Select the async branch separately
+// so macOS does not need an extra loopback alias; fetch and its errors are real.
+function remoteClient(endpoint: string) {
+ const client = new IxClient(endpoint);
+ vi.spyOn(client as unknown as { isLocalEndpoint(): boolean }, "isLocalEndpoint").mockReturnValue(false);
+ return client;
+}
+
+async function fixture(reply: (request: Seen, response: ServerResponse) => void, host = "127.0.0.1") {
const seen: Seen[] = [];
const server = createServer((request: IncomingMessage, response: ServerResponse) => {
let body = "";
@@ -43,7 +49,10 @@ async function close(server: Server) {
await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()));
}
-afterEach(async () => { await Promise.all(servers.splice(0).map(close)); });
+afterEach(async () => {
+ try { await Promise.all(servers.splice(0).map(close)); }
+ finally { vi.restoreAllMocks(); }
+});
describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirect", method => {
const stem = method === "reset" ? "/v1/reset" : "/v1/reset/code";
@@ -53,7 +62,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
if (request.path === `${stem}/async`) json(response, { opId }, 202);
else json(response, { opId, state: "done" });
});
- await expect(new IxClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });
+ await expect(remoteClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });
expect(remote.seen).toEqual([
{ method: "POST", path: `${stem}/async`, body: "{}" },
{ method: "GET", path: `/v1/reset/status/${opId}`, body: "" },
@@ -71,7 +80,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
if (request.path === `${stem}/async`) json(response, {}, 404);
else json(response, { ok: true });
});
- await expect(new IxClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });
+ await expect(remoteClient(remote.endpoint)[method]()).resolves.toMatchObject({ ok: true });
expect(remote.seen).toEqual([
{ method: "POST", path: `${stem}/async`, body: "{}" },
{ method: "POST", path: stem, body: "{}" },
@@ -81,7 +90,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
it("preserves unreachable classification when the initial connection is refused", async () => {
const closed = await fixture((_request, response) => json(response, {}));
await close(closed.server);
- const failure = await new IxClient(closed.endpoint)[method]().catch((error: unknown) => error);
+ const failure = await remoteClient(closed.endpoint)[method]().catch((error: unknown) => error);
expect(closed.seen).toEqual([]);
expect(failure).toMatchObject({ cause: { code: "ECONNREFUSED" } });
expect(isBackendUnreachable(failure)).toBe(true);
@@ -94,7 +103,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
} else if (request.path === "/redirected-reset") json(response, { opId }, 202);
else json(response, { opId, state: "done" });
});
- await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
+ await expect(remoteClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);
});
@@ -105,7 +114,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
} else if (request.path === "/missing") json(response, {}, 404);
else json(response, { ok: true, message: "synthetic reset" });
});
- await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
+ await expect(remoteClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);
});
@@ -115,7 +124,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
const remote = await fixture((_request, response) => {
response.writeHead(307, { location: `${closed.endpoint}/redirected-reset` }); response.end();
});
- const failure = await new IxClient(remote.endpoint)[method]().catch((error: unknown) => error);
+ const failure = await remoteClient(remote.endpoint)[method]().catch((error: unknown) => error);
expect(remote.seen).toEqual([{ method: "POST", path: `${stem}/async`, body: "{}" }]);
expect(failure).toMatchObject({ name: "ResetReconciliationError" });
expect((failure as Error).message).toContain("Do not repeat the reset");
@@ -129,7 +138,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
response.writeHead(302, { location: "/unrelated-status" }); response.end();
} else json(response, { opId, state: "done" });
});
- const failure = new IxClient(remote.endpoint)[method]();
+ const failure = remoteClient(remote.endpoint)[method]();
await expect(failure).rejects.toThrow(opId);
await expect(failure).rejects.toThrow("Do not repeat the reset");
expect(remote.seen.map(({ method, path }) => ({ method, path }))).toEqual([
@@ -154,7 +163,7 @@ describe.each(["reset", "resetCode"] as const)("%s never follows a reset redirec
response.writeHead(307, { location: "/redirected-reset" }); response.end();
} else json(response, { ok: true, message: "synthetic reset" });
});
- await expect(new IxClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
+ await expect(remoteClient(remote.endpoint)[method]()).rejects.toThrow("Do not repeat the reset");
expect(remote.seen.map(({ method, path }) => ({ method, path }))).toEqual([
{ method: "POST", path: `${stem}/async` }, { method: "POST", path: stem },
]);