/* The two radio routes, against the real server: who may call them, and what `/now` agrees to look up. What a genre actually returns is api/test/radio.test.js, which owns the directory logic and stubs the network — this file is the guards, because a guard is exactly the thing that quietly stops guarding. The second one matters more than it looks. `/api/radio/now` takes a source and an id *from the request* and uses them to build a URL this process then fetches; if the source stopped being checked, the api container (which sits on the owner's own network) would become a request proxy for anyone holding a session. */ import { test } from 'node:test'; import assert from 'node:assert/strict'; import crypto from 'node:crypto'; import net from 'node:net'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; const API = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const SECRET = crypto.randomBytes(32).toString('hex'); function mintSession(uid, sv = 0) { const payload = `${uid}:${Date.now() + 86400000}:${sv}`; return payload + '.' + crypto.createHmac('sha256', SECRET).update(payload).digest('base64url'); } const cookie = uid => ({ Cookie: `gymsid=${mintSession(uid)}` }); const freePort = () => new Promise(r => { const s = net.createServer(); s.listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => r(p)); }); }); async function startServer(t) { const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-radio-')); fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 }); fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users: [{ id: 'u_r', name: 'One', created: '2026-01-01T00:00:00.000Z' }], creds: [], subs: [], invites: [] })); const port = await freePort(); const child = spawn(process.execPath, ['server.js'], { cwd: API, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, PORT: String(port), DATA_DIR: dataDir, ORIGIN: 'http://localhost:8080', RP_ID: 'localhost' } }); const h = { api: `http://127.0.0.1:${port}`, log: '' }; child.stdout.on('data', d => h.log += d); child.stderr.on('data', d => h.log += d); t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); }); let up = false; for (let i = 0; i < 100 && !up; i++) { try { up = (await fetch(`${h.api}/api/health`)).ok; } catch { /* not up yet */ } if (!up) await new Promise(r => setTimeout(r, 100)); } assert.ok(up, `server never came up:\n${h.log}`); return h; } test('the station list needs a session', async t => { const h = await startServer(t); // Not an open station directory: the list is what this instance's owner pays attention to, and // the audio itself never passes through here. const r = await fetch(`${h.api}/api/radio`); assert.equal(r.status, 401); }); test('what is playing needs a session too', async t => { const h = await startServer(t); const r = await fetch(`${h.api}/api/radio/now?src=soma&id=beatblender`); assert.equal(r.status, 401); }); test('a station that is not one of the two known sources is refused, unfetched', async t => { const h = await startServer(t); for (const q of [ 'src=http://127.0.0.1:3000&id=health', // a source that is not one of ours 'src=soma', // no id at all 'src=soma&id=' + encodeURIComponent('../../x'),// an id that would walk out of the path 'src=soma&id=' + encodeURIComponent('a b c'), 'id=beatblender' // no source ]) { const r = await fetch(`${h.api}/api/radio/now?${q}`, { headers: cookie('u_r') }); assert.equal(r.status, 400, `${q} should be refused`); assert.match((await r.json()).error, /no such station/); } });