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
+2 -1
View File
@@ -20,7 +20,8 @@
"pack": "npm run build && electron-builder --dir", "pack": "npm run build && electron-builder --dir",
"dist": "npm run build && electron-builder", "dist": "npm run build && electron-builder",
"rebuild:native": "node scripts/rebuild-nodecg-native.mjs", "rebuild:native": "node scripts/rebuild-nodecg-native.mjs",
"rebuild:better-sqlite3": "electron-rebuild --version 39.5.1 --module-dir lib/nodecg/workspaces/database-adapter-sqlite-legacy --only better-sqlite3 -f" "rebuild:better-sqlite3": "electron-rebuild --version 39.5.1 --module-dir lib/nodecg/workspaces/database-adapter-sqlite-legacy --only better-sqlite3 -f",
"test": "npm run build && node --test dist/tests/**/*.test.js"
}, },
"build": { "build": {
"appId": "com.scoreko.desktop", "appId": "com.scoreko.desktop",
+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(); const value = process.env[name]?.trim();
return value && value.length > 0 ? value : undefined; 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; return getOptionalEnv(name) ?? fallback;
} }
function parseEnvInt(name: string, fallback: number): number { export function parseEnvInt(name: string, fallback: number): number {
const rawValue = process.env[name]; const rawValue = process.env[name];
if (!rawValue) { if (!rawValue) {
return fallback; 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);
});
});