AvanzaCast/packages/studio-panel/scripts/e2e_session_http_runner.mjs

60 lines
2.4 KiB
JavaScript

import fetch from 'node-fetch';
import fs from 'fs';
const LOG = '/tmp/e2e_http_runner.log';
function writeLog(...args){ try{ fs.appendFileSync(LOG, args.map(a=>typeof a==='string'?a:JSON.stringify(a)).join(' ')+'\n') }catch(e){}
console.log(...args);
}
const TOKEN_SERVER = process.env.TOKEN_SERVER_URL || 'http://localhost:4000';
const STUDIO_URL = process.env.STUDIO_URL || 'http://localhost:3020';
const TIMEOUT = Number(process.env.E2E_TIMEOUT_MS || 15000);
(async ()=>{
try{
writeLog('START HTTP runner', new Date().toISOString(), 'TOKEN_SERVER='+TOKEN_SERVER);
// 1) create session
const resp = await fetch(`${TOKEN_SERVER}/api/session`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ room: 'http-e2e-room', username: 'http-runner' })
});
writeLog('POST /api/session status', resp.status);
const json = await resp.json();
writeLog('POST response', json);
const redirectUrl = json.redirectUrl || json.studioUrl || null;
if (!redirectUrl){ writeLog('No redirectUrl in response, abort'); process.exit(2); }
writeLog('Redirect URL:', redirectUrl);
// 2) GET redirect URL
const r2 = await fetch(redirectUrl, { method: 'GET' });
writeLog('GET redirect status', r2.status, 'content-type', r2.headers.get('content-type'));
const bodyText = await r2.text();
writeLog('GET redirect body length', bodyText.length);
// 3) checks
const hasTokenInUrl = /token=/.test(redirectUrl);
const hasTokenInBody = /token=/.test(bodyText) || /Token recibido/.test(bodyText) || /LIVEKIT_ACK/.test(bodyText);
writeLog('hasTokenInUrl', hasTokenInUrl, 'hasTokenInBody', hasTokenInBody);
// 4) session lookup if id present
if (json.id){
try{
const s = await fetch(`${TOKEN_SERVER}/api/session/${json.id}`);
writeLog('/api/session/:id status', s.status);
const sjson = await s.json(); writeLog('session lookup', sjson);
}catch(e){ writeLog('session lookup error', String(e)); }
}
// save an excerpt of body to file
const out = '/tmp/e2e_http_runner_body.html';
try{ fs.writeFileSync(out, bodyText.slice(0, 20000)); writeLog('Saved redirect HTML excerpt to', out); }catch(e){ writeLog('Failed writing excerpt', String(e)); }
writeLog('FINISH OK');
process.exit(0);
}catch(err){ writeLog('Runner error', String(err)); process.exit(1); }
})();