76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { TelemetryService } from '../src/relay/telemetry-service.js';
|
|
|
|
test('TelemetryService - Grava e recupera eventos com retenção e filtros', (t) => {
|
|
const tmpDir = path.resolve('.tmp-test-traces-' + Date.now());
|
|
const telemetry = new TelemetryService({ tracesDir: tmpDir });
|
|
|
|
t.after(() => {
|
|
telemetry.destroy();
|
|
try {
|
|
if (fs.existsSync(tmpDir)) {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
}
|
|
} catch (_) {}
|
|
});
|
|
|
|
// 1. Grava evento unitário do Daemon
|
|
const ev1 = telemetry.logEvent({
|
|
source: 'gateway',
|
|
pin: '1234',
|
|
agentName: 'BotAssistente',
|
|
category: 'terminal',
|
|
action: 'terminal_output',
|
|
summary: 'Polling terminal: isThinking=false',
|
|
payload: { isThinking: false, isPaused: false, output: 'Prompt pronto' }
|
|
});
|
|
|
|
assert.ok(ev1.id, 'Evento deve conter um ID gerado');
|
|
assert.strictEqual(ev1.source, 'gateway');
|
|
assert.strictEqual(ev1.pin, '1234');
|
|
assert.strictEqual(ev1.agentName, 'BotAssistente');
|
|
|
|
// 2. Grava evento de Snapshot de Estado de Tela do Mobile
|
|
const ev2 = telemetry.logEvent({
|
|
source: 'mobile',
|
|
pin: '1234',
|
|
agentName: 'BotAssistente',
|
|
category: 'screen_state',
|
|
action: 'state_change',
|
|
summary: 'Snapshot tela: thinking=false, msgs=3',
|
|
payload: { isThinking: false, bannerVisible: false, chatBubblesCount: 3 }
|
|
});
|
|
|
|
assert.strictEqual(ev2.category, 'screen_state');
|
|
|
|
// 3. Consulta de traces com filtros
|
|
const allTraces = telemetry.getTraces({ pin: '1234' });
|
|
assert.strictEqual(allTraces.length, 2, 'Deve conter os 2 eventos registrados');
|
|
|
|
const daemonTraces = telemetry.getTraces({ category: 'terminal' });
|
|
assert.strictEqual(daemonTraces.length, 1);
|
|
assert.strictEqual(daemonTraces[0].source, 'gateway');
|
|
|
|
const mobileTraces = telemetry.getTraces({ category: 'screen_state' });
|
|
assert.strictEqual(mobileTraces.length, 1);
|
|
assert.strictEqual(mobileTraces[0].source, 'mobile');
|
|
|
|
// 4. Teste de Purge: cria arquivo simulando 4 dias atrás (>72h)
|
|
const oldDate = new Date(Date.now() - (4 * 24 * 60 * 60 * 1000));
|
|
const yyyy = oldDate.getUTCFullYear();
|
|
const mm = String(oldDate.getUTCMonth() + 1).padStart(2, '0');
|
|
const dd = String(oldDate.getUTCDate()).padStart(2, '0');
|
|
const oldFilename = `traces-${yyyy}-${mm}-${dd}.ndjson`;
|
|
const oldFilePath = path.join(tmpDir, oldFilename);
|
|
|
|
fs.writeFileSync(oldFilePath, JSON.stringify({ old: true }) + '\n');
|
|
assert.ok(fs.existsSync(oldFilePath), 'Arquivo de 4 dias atrás deve existir antes do purge');
|
|
|
|
// Executa o purge
|
|
telemetry.purgeExpiredLogs();
|
|
assert.ok(!fs.existsSync(oldFilePath), 'Arquivo com mais de 3 dias (72h) deve ter sido purgado/deletado com sucesso');
|
|
});
|