diff --git a/src/painel.js b/src/painel.js index 24b9732..7ac9b4d 100644 --- a/src/painel.js +++ b/src/painel.js @@ -309,7 +309,8 @@ for (const w of Object.values(known)) { } } // O mesmo share chega por mais de um caminho (pool com 2 casas, proxy com mais): 1410,95 e 1410,9534 são um recorde só -const sameShare = (a, b) => Math.abs(a - b) <= Math.max(Math.min(0.011, Math.max(a, b) * 0.01), Math.max(a, b) * 1e-4); +// function (e não const): é usada logo acima, na carga dos recordes, antes desta linha rodar +function sameShare(a, b) { return Math.abs(a - b) <= Math.max(Math.min(0.011, Math.max(a, b) * 0.01), Math.max(a, b) * 1e-4); } function dedupeRecords(list) { const out = []; for (const r of [...list].sort((a, b) => new Date(a.at || 0) - new Date(b.at || 0))) { diff --git a/tests/painel.unit.test.js b/tests/painel.unit.test.js index 2ca4098..efae7e5 100644 --- a/tests/painel.unit.test.js +++ b/tests/painel.unit.test.js @@ -728,3 +728,16 @@ test('recordes com dificuldade fracionária (< 1, ex.: NerdMiner / ESP32)', () = fs.rmSync(dir, { recursive: true, force: true }); } }); + +// Regressão (#44): com workers.json e records.json já preenchidos (qualquer instalação em uso), carregar o painel quebrava +// ("Cannot access 'sameShare' before initialization") e o serviço ficava reiniciando. +test('carrega com workers.json e records.json existentes', () => { + const { execFileSync } = require('child_process'); + const os = require('os'), fs = require('fs'), path = require('path'); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pn-')); + fs.writeFileSync(path.join(dir, 'workers.json'), JSON.stringify({ nerd: { name: 'nerd', bestDifficulty: 0.42, lastSeen: new Date().toISOString() } })); + fs.writeFileSync(path.join(dir, 'records.json'), JSON.stringify([{ worker: 'nerd', value: 0.3, at: new Date().toISOString() }])); + const out = execFileSync(process.execPath, ['-e', "const p=require('./src/painel'); console.log(JSON.stringify(p.recordsList ? p.recordsList() : 'ok'))"], { env: { ...process.env, DATA_DIR: dir }, encoding: 'utf8' }); + assert.ok(out.trim().length > 0); + fs.rmSync(dir, { recursive: true, force: true }); +});