52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { backendBaseUrl, joinBackendPath, resolveBackendUrl } from "./runtime-config";
|
|
import cases from "./backend-url-cases.json";
|
|
|
|
describe("resolveBackendUrl", () => {
|
|
it("uses the runtime-injected backend URL", () => {
|
|
expect(resolveBackendUrl({ backendBaseUrl: "/api" })).toBe("/api");
|
|
});
|
|
|
|
it("falls back to the Vite backend URL", () => {
|
|
expect(resolveBackendUrl(undefined)).toBe(import.meta.env.VITE_BACKEND_URL ?? "");
|
|
});
|
|
|
|
it("preserves the client default when Vite has no configured backend", () => {
|
|
expect(backendBaseUrl).toBe(import.meta.env.VITE_BACKEND_URL ?? "http://localhost:8787");
|
|
});
|
|
|
|
it.each(["/backend", "api", "//evil.test", "ftp://example.test", "https://user:pass@example.test"])(
|
|
"rejects unsupported backend URL %j",
|
|
(backendBaseUrl) => {
|
|
expect(() => resolveBackendUrl({ backendBaseUrl })).toThrow(/BACKEND_BASE_URL/);
|
|
},
|
|
);
|
|
|
|
it.each(["", "/", "/api", "/api/", "http://localhost:8787", "https://api.example.test/v1"])(
|
|
"accepts supported backend URL %j",
|
|
(backendBaseUrl) => {
|
|
expect(resolveBackendUrl({ backendBaseUrl })).toBe(backendBaseUrl);
|
|
},
|
|
);
|
|
|
|
it.each(cases)("applies the canonical policy to $value", ({ value, valid }) => {
|
|
const resolve = () => resolveBackendUrl({ backendBaseUrl: value });
|
|
if (valid) expect(resolve()).toBe(value);
|
|
else expect(resolve).toThrow(/BACKEND_BASE_URL/);
|
|
});
|
|
});
|
|
|
|
describe("joinBackendPath", () => {
|
|
it.each([
|
|
["", "/sessions/s1", "/sessions/s1"],
|
|
["/", "/sessions/s1", "/sessions/s1"],
|
|
["/api", "/sessions/s1", "/api/sessions/s1"],
|
|
["/api/", "/sessions/s1", "/api/sessions/s1"],
|
|
["https://example.test/api", "/sessions/s1", "https://example.test/api/sessions/s1"],
|
|
["https://example.test/api/", "sessions/s1", "https://example.test/api/sessions/s1"],
|
|
])("joins base %j and path %j", (base, path, expected) => {
|
|
expect(joinBackendPath(base, path)).toBe(expected);
|
|
});
|
|
});
|