feat(v2): add parseConfig with clamped TOP_N and safe defaults

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gustavo
2026-08-24 15:13:48 -03:00
co-authored by Claude Sonnet 5
parent ff5620c95c
commit 32ebe0d4f2
2 changed files with 62 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
function toBool(value, defaultValue) {
if (value === undefined || value === null || value === '') return defaultValue;
return String(value).toLowerCase() !== 'false';
}
function toNumber(value, defaultValue) {
const n = Number(value);
return Number.isFinite(n) ? n : defaultValue;
}
function parseConfig(env) {
const e = env || {};
return {
aiEnabled: toBool(e.AI_ENABLED, true),
aiProvider: e.AI_PROVIDER || 'openai',
aiModel: e.AI_MODEL || 'gpt-4o-mini',
candidatePoolSize: toNumber(e.CANDIDATE_POOL_SIZE, 60),
aiMaxCandidates: toNumber(e.AI_MAX_CANDIDATES, 30),
topN: Math.min(toNumber(e.TOP_N, 10), 10),
cooldownDays: toNumber(e.COOLDOWN_DAYS, 1),
aiConfidenceThreshold: toNumber(e.AI_CONFIDENCE_THRESHOLD, 0.6),
aiMaxContextChars: toNumber(e.AI_MAX_CONTEXT_CHARS, 4000),
devMode: toBool(e.DEV_MODE, true),
shadowMode: toBool(e.SHADOW_MODE, true),
diasSemInteracaoAlerta: toNumber(e.DIAS_SEM_INTERACAO_ALERTA, 2),
horasSlaRisco: toNumber(e.HORAS_SLA_RISCO, 8),
};
}
module.exports = { parseConfig, toBool, toNumber };
+32
View File
@@ -0,0 +1,32 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { parseConfig } = require('./config');
test('valores padrao quando env vazio', () => {
const config = parseConfig({});
assert.equal(config.aiEnabled, true);
assert.equal(config.aiProvider, 'openai');
assert.equal(config.candidatePoolSize, 60);
assert.equal(config.aiMaxCandidates, 30);
assert.equal(config.topN, 10);
assert.equal(config.aiConfidenceThreshold, 0.6);
assert.equal(config.shadowMode, true);
assert.equal(config.devMode, true);
});
test('AI_ENABLED=false desliga a IA', () => {
const config = parseConfig({ AI_ENABLED: 'false' });
assert.equal(config.aiEnabled, false);
});
test('TOP_N nunca passa de 10 mesmo se configurado maior', () => {
const config = parseConfig({ TOP_N: '25' });
assert.equal(config.topN, 10);
});
test('overrides numericos sao respeitados', () => {
const config = parseConfig({ CANDIDATE_POOL_SIZE: '80', AI_MAX_CANDIDATES: '15', COOLDOWN_DAYS: '2' });
assert.equal(config.candidatePoolSize, 80);
assert.equal(config.aiMaxCandidates, 15);
assert.equal(config.cooldownDays, 2);
});