test(config): cubrir utilidades env con node:test

This commit is contained in:
Pandipipas
2026-02-21 18:33:38 +01:00
parent e4e3ea4459
commit e3b78cf6ba
3 changed files with 66 additions and 4 deletions
+3 -3
View File
@@ -26,16 +26,16 @@ export function getRuntimeConfig(): AppRuntimeConfig {
};
}
function getOptionalEnv(name: string): string | undefined {
export function getOptionalEnv(name: string): string | undefined {
const value = process.env[name]?.trim();
return value && value.length > 0 ? value : undefined;
}
function getEnv(name: string, fallback: string): string {
export function getEnv(name: string, fallback: string): string {
return getOptionalEnv(name) ?? fallback;
}
function parseEnvInt(name: string, fallback: number): number {
export function parseEnvInt(name: string, fallback: number): number {
const rawValue = process.env[name];
if (!rawValue) {
return fallback;
+61
View File
@@ -0,0 +1,61 @@
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);
});
});