mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-04 19:21:51 +00:00
853610bbbf
Co-authored-by: Pablo F.G <pablo.fernandez@prowler.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { readEnv } from "./runtime-env";
|
|
|
|
describe("readEnv", () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it("returns the primary value when it is set", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", "https://primary.example.com");
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL")).toBe("https://primary.example.com");
|
|
});
|
|
|
|
it("returns null when the primary is unset and no legacy is given", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", undefined);
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL")).toBeNull();
|
|
});
|
|
|
|
it("treats an empty or whitespace-only primary as unset", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", " ");
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL")).toBeNull();
|
|
});
|
|
|
|
it("falls back to the legacy var when the primary is unset", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", undefined);
|
|
vi.stubEnv("NEXT_PUBLIC_API_BASE_URL", "https://legacy.example.com");
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL", "NEXT_PUBLIC_API_BASE_URL")).toBe(
|
|
"https://legacy.example.com",
|
|
);
|
|
});
|
|
|
|
it("falls back to the legacy var when the primary is empty", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", "");
|
|
vi.stubEnv("NEXT_PUBLIC_API_BASE_URL", "https://legacy.example.com");
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL", "NEXT_PUBLIC_API_BASE_URL")).toBe(
|
|
"https://legacy.example.com",
|
|
);
|
|
});
|
|
|
|
it("prefers the primary over the legacy when both are set", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", "https://primary.example.com");
|
|
vi.stubEnv("NEXT_PUBLIC_API_BASE_URL", "https://legacy.example.com");
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL", "NEXT_PUBLIC_API_BASE_URL")).toBe(
|
|
"https://primary.example.com",
|
|
);
|
|
});
|
|
|
|
it("returns null when neither the primary nor the legacy is set", () => {
|
|
// Given
|
|
vi.stubEnv("UI_API_BASE_URL", undefined);
|
|
vi.stubEnv("NEXT_PUBLIC_API_BASE_URL", undefined);
|
|
|
|
// When / Then
|
|
expect(readEnv("UI_API_BASE_URL", "NEXT_PUBLIC_API_BASE_URL")).toBeNull();
|
|
});
|
|
});
|