d600150088
Revisión externa 2.1, 2.2 y 2.4: asyncHandler + errorHandler (500 y log) en vez de tumbar el panel con un rechazo. PUT, DELETE y start marcan el canal ocupado antes de esperar a systemd. Puerto SRT del modal hasta 55000, como el servidor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JAUCv2ADyD2NSg4ub9YPr1
81 lines
3.4 KiB
JavaScript
81 lines
3.4 KiB
JavaScript
'use strict';
|
|
// Rutas de canales (revisión externa, 2.1 y 2.2).
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const http = require('http');
|
|
|
|
process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'orbisubs-rutas-'));
|
|
process.env.ORBISUBS_ETC = path.join(process.env.DATA_DIR, 'etc');
|
|
process.env.LOG_DIR = path.join(process.env.DATA_DIR, 'log');
|
|
const express = require('express');
|
|
const status = require('../src/engine/status');
|
|
const { db } = require('../src/db');
|
|
const { errorHandler } = require('../src/http');
|
|
const router = require('../src/routes/channels');
|
|
|
|
function servidor() {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, res, next) => { req.user = { username: 'test' }; next(); });
|
|
app.use('/api/channels', router);
|
|
app.use(errorHandler);
|
|
return new Promise(r => { const s = app.listen(0, '127.0.0.1', () => r(s)); });
|
|
}
|
|
function pedir(s, method, url) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request({ host: '127.0.0.1', port: s.address().port, method, path: url }, res => {
|
|
let b = '';
|
|
res.on('data', d => { b += d; });
|
|
res.on('end', () => resolve({ status: res.statusCode, body: b ? JSON.parse(b) : null }));
|
|
});
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
const cfg = {
|
|
input: { kind: 'udp', url: 'udp://@239.1.1.1:1234' }, asr: { lang: 'es', url: 'http://127.0.0.1:1' },
|
|
tr: { langs: [] }, hls: { enabled: true, seg_s: 6, list_size: 10 }, srt: { enabled: false },
|
|
};
|
|
|
|
test('2.1: un rechazo en una ruta async responde 500 y el panel sigue andando', async t => {
|
|
const s = await servidor();
|
|
const orig = status.all;
|
|
t.after(() => { status.all = orig; s.close(); });
|
|
const errores = [];
|
|
const ce = console.error;
|
|
console.error = (...a) => errores.push(a.join(' '));
|
|
try {
|
|
status.all = async () => { throw new Error('systemctl colgado'); };
|
|
const r = await pedir(s, 'GET', '/api/channels');
|
|
assert.strictEqual(r.status, 500);
|
|
assert.strictEqual(r.body.error, 'Error interno');
|
|
assert.ok(errores.some(e => e.includes('systemctl colgado')), 'se loguea');
|
|
status.all = orig;
|
|
// Sigue vivo: el pedido siguiente anda.
|
|
status.all = async chs => Object.fromEntries(chs.map(c => [c.id, { state: 'stopped', units: [], notes: [] }]));
|
|
assert.strictEqual((await pedir(s, 'GET', '/api/channels')).status, 200);
|
|
} finally { console.error = ce; }
|
|
});
|
|
|
|
test('2.2: dos DELETE a la vez: el segundo encuentra el canal ocupado', async t => {
|
|
const s = await servidor();
|
|
const orig = status.all;
|
|
t.after(() => { status.all = orig; s.close(); });
|
|
const id = db.prepare('INSERT INTO channels (slug, name, enabled, cfg) VALUES (?, ?, 1, ?)').run('borrame', 'Borrame', JSON.stringify(cfg)).lastInsertRowid;
|
|
let soltar;
|
|
const espera = new Promise(r => { soltar = r; });
|
|
status.all = async chs => { await espera; return Object.fromEntries(chs.map(c => [c.id, { state: 'stopped', units: [], notes: [] }])); };
|
|
const primero = pedir(s, 'DELETE', `/api/channels/${id}`);
|
|
await new Promise(r => setTimeout(r, 50));
|
|
const segundo = await pedir(s, 'DELETE', `/api/channels/${id}`);
|
|
assert.strictEqual(segundo.status, 409);
|
|
assert.match(segundo.body.error, /ocupado/);
|
|
soltar();
|
|
assert.strictEqual((await primero).status, 200);
|
|
assert.strictEqual(db.prepare('SELECT 1 FROM channels WHERE id = ?').get(id), undefined);
|
|
});
|