feat(api): add resolveApiExampleUrl function and related tests for dynamic API URL resolution

This commit is contained in:
KodekaTeam committed 2026-09-13 11:47:37 +07:00
1 parent c51c17e590
commit 14411e0cf8
3 files changed
+131 -1

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import assert from "node:assert/strict";
import test from "node:test";
import { resolveApiExampleUrl } from "../src/lib/api-example-url";
test("uses the Panel Access URL stored in settings", () => {
assert.equal(
resolveApiExampleUrl({
panelUrl: "https://panel.example.com/",
browserOrigin: "http://192.168.1.20:3000",
apiBaseUrl: "http://localhost:4000/api/v1",
}),
"https://panel.example.com/api/v1/servers",
);
});
test("uses the active browser origin when the stored panel URL is a stale loopback default", () => {
assert.equal(
resolveApiExampleUrl({
panelUrl: "http://localhost:3000",
browserOrigin: "http://192.168.1.20:3000",
apiBaseUrl: "http://localhost:4000/api/v1",
}),
"http://192.168.1.20:3000/api/v1/servers",
);
});
test("keeps the stored loopback URL during local access", () => {
assert.equal(
resolveApiExampleUrl({
panelUrl: "http://localhost:3000",
browserOrigin: "http://127.0.0.1:3000",
apiBaseUrl: "http://localhost:4000/api/v1",
}),
"http://localhost:3000/api/v1/servers",
);
});
test("falls back to the configured API base when no public panel origin is available", () => {
assert.equal(
resolveApiExampleUrl({
panelUrl: "not-a-url",
browserOrigin: null,
apiBaseUrl: "https://api.example.com/api/v1/",
}),
"https://api.example.com/api/v1/servers",
);
});