From e3b78cf6ba2a59264c803e692589cba5fb3ce527 Mon Sep 17 00:00:00 2001 From: Pandipipas <62224708+Pandipipas@users.noreply.github.com> Date: Sat, 21 Feb 2026 18:33:38 +0100 Subject: [PATCH] test(config): cubrir utilidades env con node:test --- package.json | 3 +- src/main/config/runtime-config.ts | 6 +-- src/tests/runtime-config.test.ts | 61 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/tests/runtime-config.test.ts diff --git a/package.json b/package.json index 52f3833..2de8d55 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "pack": "npm run build && electron-builder --dir", "dist": "npm run build && electron-builder", "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": { "appId": "com.scoreko.desktop", diff --git a/src/main/config/runtime-config.ts b/src/main/config/runtime-config.ts index e51aa52..c2668d5 100644 --- a/src/main/config/runtime-config.ts +++ b/src/main/config/runtime-config.ts @@ -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; diff --git a/src/tests/runtime-config.test.ts b/src/tests/runtime-config.test.ts new file mode 100644 index 0000000..5a8ed4f --- /dev/null +++ b/src/tests/runtime-config.test.ts @@ -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); + }); +});