mirror of
https://github.com/Pandipipas/scoreko-electron-dev.git
synced 2026-06-06 05:32:06 +00:00
test(config): cubrir utilidades env con node:test
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user