Files
gibbz/test.js
T
mats dee8ef4f7c
Build and Publish / Metadata (push) Successful in 1s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 24s
Show users in voice channels before joining
2026-09-22 10:45:08 +02:00

94 lines
3.3 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert');
const http = require('node:http');
test('server module syntax and exports', () => {
assert.ok(true);
});
test('static files exist', () => {
const fs = require('fs');
assert.ok(fs.existsSync('./public/index.html'), 'index.html exists');
assert.ok(fs.existsSync('./public/app.js'), 'app.js exists');
assert.ok(fs.existsSync('./public/style.css'), 'style.css exists');
});
test('server HTTP and WebSocket signaling endpoints', async (t) => {
const { server, wss, interval, startServer } = require('./server');
await startServer(0, '127.0.0.1');
const port = server.address().port;
t.after(() => {
clearInterval(interval);
wss.close();
server.close();
});
// test /api/ice endpoint
const resIce = await fetch(`http://127.0.0.1:${port}/api/ice`);
assert.strictEqual(resIce.status, 200);
const ice = await resIce.json();
assert.ok(Array.isArray(ice.iceServers), 'iceServers array returned');
const stunUrls = ice.iceServers.flatMap((s) => (Array.isArray(s.urls) ? s.urls : [s.urls]));
assert.ok(stunUrls.some((u) => u.includes('turn.k6n.net')), 'cluster stun/turn included');
// test /healthz
const resHealth = await fetch(`http://127.0.0.1:${port}/healthz`);
assert.strictEqual(resHealth.status, 200);
const healthText = await resHealth.text();
assert.strictEqual(healthText, 'ok');
// test /api/rooms initially empty
const resRoomsInit = await fetch(`http://127.0.0.1:${port}/api/rooms`);
assert.strictEqual(resRoomsInit.status, 200);
const roomsInit = await resRoomsInit.json();
assert.deepStrictEqual(roomsInit, {});
// test WebSocket signaling /ws
const { WebSocket } = require('ws');
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws?room=testroom&name=tester`);
const messages = [];
const helloPromise = new Promise((resolve, reject) => {
ws.on('message', (raw) => {
try {
const msg = JSON.parse(raw.toString());
messages.push(msg);
if (msg.type === 'hello') resolve(msg);
} catch (err) {
reject(err);
}
});
ws.on('error', reject);
});
const msg = await helloPromise;
assert.strictEqual(msg.type, 'hello');
assert.strictEqual(msg.room, 'testroom');
assert.ok(msg.id, 'client id assigned');
// test /api/rooms while tester is connected
const resRoomsActive = await fetch(`http://127.0.0.1:${port}/api/rooms`);
assert.strictEqual(resRoomsActive.status, 200);
const roomsActive = await resRoomsActive.json();
assert.ok(roomsActive.testroom, 'testroom present in /api/rooms');
assert.strictEqual(roomsActive.testroom.length, 1);
assert.strictEqual(roomsActive.testroom[0].name, 'tester');
ws.close();
});
test('TURN credentials generation with TURN_SECRET', () => {
const { iceConfig } = require('./server');
process.env.TURN_SECRET = 'secret123';
try {
const config = iceConfig();
const turnServer = config.iceServers.find((s) => s.urls && (Array.isArray(s.urls) ? s.urls : [s.urls]).some((u) => u.startsWith('turn')));
assert.ok(turnServer, 'turnServer configured');
assert.ok(turnServer.username.endsWith(':gibbz'), 'ephemeral username generated');
assert.ok(turnServer.credential.length > 0, 'HMAC credential generated');
} finally {
delete process.env.TURN_SECRET;
}
});