droboticflow/tests/frontend/api-base-url.test.mjs
2026-06-17 03:36:58 +08:00

28 lines
1.4 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { createAPIURLBuilder, normalizeFrontendAPIBaseURL, viteAPIBaseURL } from "../../src/services/api/base-url.js";
test("normalizes empty and absolute frontend api base urls", () => {
assert.equal(normalizeFrontendAPIBaseURL(""), "");
assert.equal(normalizeFrontendAPIBaseURL(" https://api.example.test/// "), "https://api.example.test");
assert.equal(createAPIURLBuilder("")("/api/health"), "/api/health");
assert.equal(createAPIURLBuilder("https://api.example.test/")("/api/health"), "https://api.example.test/api/health");
});
test("rejects invalid explicit frontend api base urls", () => {
assert.throws(() => normalizeFrontendAPIBaseURL("api.example.test"), /empty or an absolute http\(s\) URL/);
assert.throws(() => normalizeFrontendAPIBaseURL("/proxy"), /empty or an absolute http\(s\) URL/);
assert.throws(() => normalizeFrontendAPIBaseURL("ftp://api.example.test"), /empty or an absolute http\(s\) URL/);
});
test("requires api request paths to stay absolute-path shaped", () => {
const buildURL = createAPIURLBuilder("https://api.example.test");
assert.throws(() => buildURL("api/health"), /must start with "\/"/);
});
test("reads vite api base url from explicit meta object", () => {
assert.equal(viteAPIBaseURL({ env: { VITE_API_BASE_URL: "https://api.example.test/" } }), "https://api.example.test");
assert.equal(viteAPIBaseURL({ env: {} }), "");
});