diff --git a/logic/config.js b/logic/config.js new file mode 100644 index 0000000..804a723 --- /dev/null +++ b/logic/config.js @@ -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 }; diff --git a/logic/config.test.js b/logic/config.test.js new file mode 100644 index 0000000..1610a50 --- /dev/null +++ b/logic/config.test.js @@ -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); +});