Files
prowler/ui/lib/runtime-env.test.ts
T
Pablo Fernandez Guerra (PFE) ac3f289de6 feat(ui): gate Sentry, GTM and PostHog behind runtime enable flags (#11682)
Co-authored-by: Pablo F.G <pablo.fernandez@prowler.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 13:43:34 +02:00

124 lines
3.3 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { readBoolEnv, 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();
});
});
describe("readBoolEnv", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it('is true only for the exact string "true"', () => {
// Given
vi.stubEnv("UI_SENTRY_ENABLE", "true");
// When / Then
expect(readBoolEnv("UI_SENTRY_ENABLE")).toBe(true);
});
it("trims surrounding whitespace before comparing", () => {
// Given - clean() does not trim a non-empty value, so readBoolEnv must
vi.stubEnv("UI_SENTRY_ENABLE", " true ");
// When / Then
expect(readBoolEnv("UI_SENTRY_ENABLE")).toBe(true);
});
it("is false when unset", () => {
// Given
vi.stubEnv("UI_SENTRY_ENABLE", undefined);
// When / Then
expect(readBoolEnv("UI_SENTRY_ENABLE")).toBe(false);
});
it('is false for "false"', () => {
// Given
vi.stubEnv("UI_SENTRY_ENABLE", "false");
// When / Then
expect(readBoolEnv("UI_SENTRY_ENABLE")).toBe(false);
});
it('is false for other truthy-looking values ("TRUE", "1", "yes")', () => {
for (const value of ["TRUE", "1", "yes"]) {
// Given
vi.stubEnv("UI_SENTRY_ENABLE", value);
// When / Then
expect(readBoolEnv("UI_SENTRY_ENABLE")).toBe(false);
}
});
});