refactor: validação de inputs e melhorias de código

- Adiciona validação em calcularCustoTotal, calcularSecagem, calcularCEV, calcularCisalhamento, calcularPreaquecimento
- Cria Logger com níveis (debug, info, warn, error)
- Consolida 5 arquivos de teste em 1 (test-suite.js)
- Adiciona safe DOM access em funções de cálculo
This commit is contained in:
2026-04-03 21:39:20 +00:00
parent 85c053ff30
commit 0d8797c80c
8 changed files with 311 additions and 789 deletions

View File

@@ -0,0 +1,109 @@
/**
* SteelBase Test Suite
* Console commands available:
* - runTestSuite() - Run all tests
* - testPersistence() - Test persistence system
* - testBackup() - Test backup/restore
* - testConfig() - Test admin config
*/
(function() {
'use strict';
const TestRunner = {
results: [],
log(msg, type = 'info') {
const prefix = { info: '', success: '✅', error: '❌', warn: '⚠️' }[type] || '';
console.log(`${prefix} ${msg}`);
},
async waitForModules(timeout = 5000) {
const start = Date.now();
while (Date.now() - start < timeout) {
if (window.adminConfigManager && window.backupManager) {
return true;
}
await new Promise(r => setTimeout(r, 100));
}
return false;
},
async testPersistence() {
this.log('Testando persistência...', 'info');
try {
if (!await this.waitForModules()) {
throw new Error('Módulos não carregados');
}
const config = window.adminConfigManager.getConfig();
window.adminConfigManager.updateConfig('test.run', Date.now());
const updated = window.adminConfigManager.getConfig();
const passed = updated.test?.run > 0;
this.log(passed ? 'Persistência OK' : 'Falha na persistência', passed ? 'success' : 'error');
return passed;
} catch (e) {
this.log(`Erro: ${e.message}`, 'error');
return false;
}
},
async testBackup() {
this.log('Testando backup...', 'info');
try {
const backup = await window.backupManager.createBackup('Teste automático');
const list = window.backupManager.getBackups();
const passed = list.some(b => b.id === backup.id);
if (passed) {
window.backupManager.removeBackup(backup.id);
}
this.log(passed ? 'Backup OK' : 'Falha no backup', passed ? 'success' : 'error');
return passed;
} catch (e) {
this.log(`Erro: ${e.message}`, 'error');
return false;
}
},
async testConfig() {
this.log('Testando configuração...', 'info');
try {
const cfg = window.adminConfigManager?.getConfig();
const passed = cfg && cfg.app?.name;
this.log(passed ? 'Configuração OK' : 'Falha na configuração', passed ? 'success' : 'error');
return passed;
} catch (e) {
this.log(`Erro: ${e.message}`, 'error');
return false;
}
},
async runAllTests() {
this.log('🧪 SteelBase Test Suite', 'info');
this.log('='.repeat(30), 'info');
const results = {
persistence: await this.testPersistence(),
backup: await this.testBackup(),
config: await this.testConfig()
};
const total = Object.values(results).filter(Boolean).length;
const totalTests = Object.keys(results).length;
this.log('='.repeat(30), 'info');
this.log(`Resultado: ${total}/${totalTests} testes passaram`, total === totalTests ? 'success' : 'warn');
return results;
}
};
window.runTestSuite = () => TestRunner.runAllTests();
window.testPersistence = () => TestRunner.testPersistence();
window.testBackup = () => TestRunner.testBackup();
window.testConfig = () => TestRunner.testConfig();
console.log('✅ Test Suite carregado. Use runTestSuite() no console para executar.');
})();