Files
DeepTutor/web/tests/api-resolve-base.test.ts
2026-05-22 20:19:32 +08:00

82 lines
2.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
// Must be set before importing the module under test, since API_BASE_URL is
// read at module-load time and the module throws if it's missing.
process.env.NEXT_PUBLIC_API_BASE = "http://localhost:8001/api";
let apiModulePromise: Promise<typeof import("../lib/api")> | null = null;
async function loadApiModule(): Promise<typeof import("../lib/api")> {
apiModulePromise ??= import("../lib/api");
return apiModulePromise;
}
function setWindow(hostname: string | undefined): void {
if (hostname === undefined) {
delete (globalThis as { window?: unknown }).window;
return;
}
(globalThis as { window?: unknown }).window = {
location: { hostname },
} as unknown;
}
test("resolveBase returns the build-time base in SSR (no window)", async () => {
const { resolveBase } = await loadApiModule();
setWindow(undefined);
assert.equal(resolveBase(), "http://localhost:8001/api");
});
test("isApiBasePlaceholder detects the unsubstituted Docker placeholder", async () => {
const { isApiBasePlaceholder } = await loadApiModule();
assert.equal(
isApiBasePlaceholder("__NEXT_PUBLIC_API_BASE_PLACEHOLDER__"),
true,
);
});
test("isApiBasePlaceholder accepts a substituted API URL", async () => {
const { isApiBasePlaceholder } = await loadApiModule();
assert.equal(isApiBasePlaceholder("http://localhost:3781"), false);
});
test("resolveBase returns base unchanged when client is also on localhost", async () => {
const { resolveBase } = await loadApiModule();
setWindow("localhost");
assert.equal(resolveBase(), "http://localhost:8001/api");
});
test("resolveBase rewrites loopback hostname to remote LAN host and preserves path", async () => {
const { resolveBase } = await loadApiModule();
setWindow("192.168.1.10");
assert.equal(resolveBase(), "http://192.168.1.10:8001/api");
});
test("resolveBase treats IPv6 loopback as loopback (no swap when client is also ::1)", async () => {
const { resolveBase } = await loadApiModule();
setWindow("::1");
assert.equal(resolveBase(), "http://localhost:8001/api");
});
test("apiUrl composes correctly after rewrite, without losing the base path", async () => {
const { apiUrl } = await loadApiModule();
setWindow("10.0.0.5");
assert.equal(
apiUrl("/api/v1/knowledge/list"),
"http://10.0.0.5:8001/api/api/v1/knowledge/list",
);
});
test("wsUrl converts http to ws and respects rewritten host", async () => {
const { wsUrl } = await loadApiModule();
setWindow("10.0.0.5");
assert.equal(wsUrl("/api/v1/ws"), "ws://10.0.0.5:8001/api/api/v1/ws");
});
test("wsUrl keeps original loopback when client is also loopback", async () => {
const { wsUrl } = await loadApiModule();
setWindow("127.0.0.1");
assert.equal(wsUrl("/api/v1/ws"), "ws://localhost:8001/api/api/v1/ws");
});