'use strict'; // Pestaña Motores (080): lectura de las métricas de los canales y los números. const test = require('node:test'); const assert = require('node:assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'orbisubs-motores-')); process.env.DATA_DIR = tmp; process.env.LOG_DIR = tmp; const M = require('../src/engine/motores'); const { db } = require('../src/db'); test('parseLine: asr con y sin pedido, con error, y líneas viejas sin hora', () => { assert.deepStrictEqual(M.parseLine('asr', { t: 10, http_s: 1.2, audio_s: 7 }), { ts: 10, dur_s: 1.2, audio_s: 7, ok: 1, error: null }); assert.strictEqual(M.parseLine('asr', { t: 10, asr_s: 0 }), null); // sin pedido assert.strictEqual(M.parseLine('asr', { http_s: 1.2 }), null); // de antes de la 080 assert.strictEqual(M.parseLine('asr', { t: 10, http_s: 0.01, error: 'Connection refused' }).ok, 0); }); test('parseLine: llm, traducción vacía cuenta como falla', () => { assert.strictEqual(M.parseLine('llm', { t: 1, es: 'hola', en: 'hi', qwen_s: 0.6 }).ok, 1); const f = M.parseLine('llm', { t: 1, es: 'hola', en: null, qwen_s: 8 }); assert.strictEqual(f.ok, 0); assert.strictEqual(f.error, 'sin traducción'); assert.strictEqual(M.parseLine('llm', { seg: 5, en_lag_vs_es_s: 6 }), null); // línea de lag, no es pedido }); test('statsOf: mediana, p95 y RTF sobre los pedidos buenos', () => { const rows = [1, 2, 3, 4].map(d => ({ ok: 1, dur_s: d, audio_s: 8 })).concat([{ ok: 0, dur_s: 9 }]); const s = M.statsOf(rows); assert.strictEqual(s.n, 5); assert.strictEqual(s.fails, 1); assert.strictEqual(s.median_s, 2.5); assert.strictEqual(s.p95_s, 3.85); assert.strictEqual(s.rtf, 0.313); }); test('buckets: 60 minutos, fallas aparte', () => { const now = 10000; const b = M.buckets([{ ts: now - 30, ok: 1, dur_s: 1 }, { ts: now - 20, ok: 0, dur_s: 0 }, { ts: now - 4000, ok: 1, dur_s: 5 }], now); assert.strictEqual(b.length, 60); assert.strictEqual(b[59].med, 1); assert.strictEqual(b[59].fails, 1); assert.strictEqual(b.filter(x => x.n).length, 1); }); test('engineList: una tarjeta por URL en uso; sin canales, las de gpu02', () => { const ch = (slug, asr, llm, langs = ['en']) => ({ slug, cfg: { asr: { url: asr }, tr: { url: llm, langs } } }); const l = M.engineList([ch('a', 'http://g:8004/', 'http://g:8001'), ch('b', 'http://g:8004', 'http://g:8999', [])]); assert.deepStrictEqual(l.map(e => e.key), ['asr|http://g:8004', 'llm|http://g:8001']); assert.deepStrictEqual(l[0].channels, ['a', 'b']); assert.strictEqual(M.engineList([]).length, 2); }); test('ingestFile: solo lo nuevo, y no relee después de un reinicio', () => { const f = path.join(tmp, 'c1.jsonl'); const now = Date.now() / 1000; fs.writeFileSync(f, JSON.stringify({ t: now, http_s: 1, audio_s: 6 }) + '\n' + JSON.stringify({ seq: 1, http_s: 1 }) + '\n'); assert.strictEqual(M.ingestFile(f, 'asr', 'http://g:8004', 'c1'), 1); assert.strictEqual(M.ingestFile(f, 'asr', 'http://g:8004', 'c1'), 0); fs.appendFileSync(f, JSON.stringify({ t: now, http_s: 2, audio_s: 6, error: 'x' }) + '\n{"t": ' + now); // la última a medias assert.strictEqual(M.ingestFile(f, 'asr', 'http://g:8004', 'c1'), 1); fs.appendFileSync(f, ', "http_s": 3}\n'); assert.strictEqual(M.ingestFile(f, 'asr', 'http://g:8004', 'c1'), 1); assert.strictEqual(db.prepare('SELECT COUNT(*) n FROM engine_reqs').get().n, 3); }); test('setConfig: valida rangos', () => { assert.throws(() => M.setConfig({ poll_s: 5 }), /entre 15 y 30/); assert.strictEqual(M.setConfig({ asr: { p95_warn_s: 2.5 } }).asr.p95_warn_s, 2.5); assert.strictEqual(M.config().asr.model, 'large-v3-turbo'); }); test('parseProm: una fila por placa, ordenada por índice', () => { const txt = [ 'nvidia_smi_gpu_info{cuda_version="12.8",driver_version="570.211.01",index="1",name="NVIDIA GeForce RTX 3060",uuid="b"} 1', 'nvidia_smi_gpu_info{cuda_version="12.8",driver_version="570.211.01",index="0",name="NVIDIA GeForce RTX 3080",uuid="a"} 1', 'nvidia_smi_name{uuid="b"} 3060', 'nvidia_smi_index{uuid="b"} 1', 'nvidia_smi_memory_free_bytes{uuid="b"} 1.240465408e+09', 'nvidia_smi_utilization_gpu_ratio{uuid="b"} 0.99', 'nvidia_smi_temperature_gpu{uuid="a"} 56', '# HELP x', 'go_threads 10', ].join('\n'); const g = M.parseProm(txt); assert.deepStrictEqual(g.map(x => x.index), [0, 1]); assert.strictEqual(g[1].name, 'NVIDIA GeForce RTX 3060'); assert.strictEqual(g[1].util, 0.99); assert.strictEqual(g[1].mem_free, 1240465408); assert.strictEqual(g[0].temp, 56); });