Files
scoreko-electron-dev/src/tests/runtime-config.test.ts
T
2026-02-21 18:33:38 +01:00

62 lines
1.6 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { getEnv, getOptionalEnv, parseEnvInt } from "../main/config/runtime-config";
function withEnv(name: string, value: string | undefined, run: () => void): void {
const previousValue = process.env[name];
if (value === undefined) {
delete process.env[name];
} else {
process.env[name] = value;
}
try {
run();
} finally {
if (previousValue === undefined) {
delete process.env[name];
return;
}
process.env[name] = previousValue;
}
}
test("getOptionalEnv devuelve undefined para variable ausente", () => {
withEnv("TEST_OPTIONAL_ENV", undefined, () => {
assert.equal(getOptionalEnv("TEST_OPTIONAL_ENV"), undefined);
});
});
test("getOptionalEnv recorta espacios y devuelve valor", () => {
withEnv("TEST_OPTIONAL_ENV", " scoreko ", () => {
assert.equal(getOptionalEnv("TEST_OPTIONAL_ENV"), "scoreko");
});
});
test("getEnv devuelve fallback para valor vacío", () => {
withEnv("TEST_ENV", " ", () => {
assert.equal(getEnv("TEST_ENV", "fallback"), "fallback");
});
});
test("getEnv devuelve el valor cuando existe", () => {
withEnv("TEST_ENV", "valor", () => {
assert.equal(getEnv("TEST_ENV", "fallback"), "valor");
});
});
test("parseEnvInt devuelve fallback para valores inválidos", () => {
withEnv("TEST_ENV_INT", "abc", () => {
assert.equal(parseEnvInt("TEST_ENV_INT", 100), 100);
});
});
test("parseEnvInt parsea enteros válidos", () => {
withEnv("TEST_ENV_INT", "4500", () => {
assert.equal(parseEnvInt("TEST_ENV_INT", 100), 4500);
});
});