Files
client/scripts/probe-stream.mjs
T

129 lines
6.7 KiB
JavaScript

// Watches a real TS6 screenshare with the real TeamsPeach UI and records protocol/media evidence.
// Microphone is muted before connecting and push-to-talk is never pressed: nothing is transmitted.
// No video pixels are saved: the video element is hidden for the UI screenshot.
//
// node scripts/probe-stream.mjs --server 10.10.10.160:9987 --channel cs --out docs/evidence/runtime/<name> [--seconds 40]
import { launchApp } from './lib/electron-app.mjs';
import { mkdirSync, writeFileSync, mkdtempSync, readFileSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const argv = process.argv.slice(2);
const opt = (n, d) => (argv.indexOf(`--${n}`) >= 0 ? argv[argv.indexOf(`--${n}`) + 1] : d);
const server = opt('server');
const channelName = opt('channel', 'cs');
const seconds = Number(opt('seconds', '40'));
const nickname = opt('nick', 'TeamsPeach-Probe');
const out = path.resolve(root, opt('out', 'probe-stream-out'));
const profile = opt('profile') ? path.resolve(opt('profile')) : mkdtempSync(path.join(tmpdir(), 'teamspeach-probe-'));
mkdirSync(out, { recursive: true });
const result = { startedAt: new Date().toISOString(), server, channelName, profile, steps: [], samples: [], console: [] };
const step = (name, data = {}) => {
result.steps.push({ name, at: new Date().toISOString(), ...data });
console.log(`[probe] ${name}`, JSON.stringify(data));
};
const diag = (page) => page.evaluate(() => window.teamspeachDiagnostics.snapshot());
let app;
let page;
let failed = null;
try {
app = await launchApp({ profile });
page = await app.firstWindow();
page.on('console', (m) => result.console.push(`${m.type()}: ${m.text()}`));
page.on('pageerror', (e) => result.console.push(`pageerror: ${e.message}`));
await page.setViewportSize({ width: 1280, height: 800 }).catch(() => {});
const onboarding = await page.waitForSelector('#view-onboarding, #view-app', { state: 'visible', timeout: 20000 });
if ((await onboarding.getAttribute('id')) === 'view-onboarding') {
await page.fill('#onboarding-nick', nickname);
await page.click('#onboarding-submit');
await page.waitForSelector('#view-app', { state: 'visible', timeout: 20000 });
}
// Mute before connecting (applied by the core at connect).
await page.click('#btn-mute');
await page.waitForFunction(() => window.teamspeachDiagnostics.snapshot().audio.input_muted === true, null, { timeout: 10000 });
step('muted before connect', { profile: (await diag(page)).profile });
await page.fill('#connect-address', server);
await page.click('#connect-submit');
await page.waitForFunction(() => window.teamspeachDiagnostics.snapshot().conn === 'connected', null, { timeout: 60000 });
let d = await diag(page);
step('connected', { server: d.server, ownClientId: d.ownClientId, channel: d.ownChannel, audio: d.audio });
const findRow = (name) => Array.from(document.querySelectorAll('#tree .channel-row')).some((r) => r.querySelector('.row-label')?.textContent.trim().toLowerCase() === name.toLowerCase());
await page.waitForFunction(findRow, channelName, { timeout: 20000 }).catch(() => {});
const joined = await page.evaluate((name) => {
const rows = Array.from(document.querySelectorAll('#tree .channel-row'));
const row = rows.find((r) => r.querySelector('.row-label')?.textContent.trim().toLowerCase() === name.toLowerCase());
if (!row) return false;
row.querySelector('.row-btn').click();
return true;
}, channelName);
if (!joined) throw new Error(`channel "${channelName}" not found`);
await page.waitForFunction((name) => (window.teamspeachDiagnostics.snapshot().ownChannel ?? '').toLowerCase() === name.toLowerCase(), channelName, { timeout: 15000 });
d = await diag(page);
step('joined channel', { channel: d.ownChannel, members: d.clients.filter((c) => c.channel === d.clients.find((x) => x.id === d.ownClientId).channel).map((c) => c.nickname) });
await page.waitForFunction(() => window.teamspeachDiagnostics.snapshot().streams.length > 0, null, { timeout: 25000 });
d = await diag(page);
step('streams discovered', { streams: d.streams });
const stream = d.streams[0];
await page.click(`#tree [data-key="u:${stream.clientId}"] .watch-btn`);
step('watch clicked', { streamId: stream.id, clientId: stream.clientId });
const deadline = Date.now() + seconds * 1000;
let firstFrameAt = null;
while (Date.now() < deadline) {
await page.waitForTimeout(2000);
d = await diag(page);
result.samples.push({ at: new Date().toISOString(), ...d.watch });
if (!firstFrameAt && d.watch.framesDecoded > 0) {
firstFrameAt = new Date().toISOString();
step('first decoded frame', { ...d.watch });
await page.evaluate(() => { document.querySelector('#stream-video').style.visibility = 'hidden'; });
await page.screenshot({ path: path.join(out, '20-watching-video-redacted.png') });
await page.evaluate(() => { document.querySelector('#stream-video').style.visibility = ''; });
}
if (['failed', 'declined', 'ended', 'removed', 'unsupported'].includes(d.watch.state)) {
step('session ended early', { ...d.watch, overlay: await page.textContent('#stream-overlay') });
await page.screenshot({ path: path.join(out, '21-watch-ended.png') });
break;
}
}
d = await diag(page);
step('final watch stats', { ...d.watch });
if (!(d.watch.framesDecoded > 0)) throw new Error('no video frames decoded');
await page.click('#stream-stop');
await page.waitForFunction(() => window.teamspeachDiagnostics.snapshot().watch.state === 'idle', null, { timeout: 10000 });
step('stopped watching');
} catch (err) {
failed = err;
step('FAILED', { error: String(err.stack || err) });
if (page) {
await page.evaluate(() => { const v = document.querySelector('#stream-video'); if (v) v.style.visibility = 'hidden'; }).catch(() => {});
await page.screenshot({ path: path.join(out, 'fail.png') }).catch(() => {});
}
} finally {
if (page) {
await page.click('#btn-disconnect').catch(() => {});
await page.waitForTimeout(800);
}
if (app) await app.close().catch(() => {});
}
for (const name of ['renderer.log', 'core.log']) {
const file = path.join(profile, 'logs', name);
if (existsSync(file)) {
const lines = readFileSync(file, 'utf8').split(/\r?\n/).filter((l) => /watch|stream|state|error|warn/i.test(l));
writeFileSync(path.join(out, `${name}.filtered.txt`), lines.join('\n'));
}
}
result.finishedAt = new Date().toISOString();
result.ok = !failed;
writeFileSync(path.join(out, 'result.json'), JSON.stringify(result, null, 2));
console.log(`[probe] ${result.ok ? 'OK' : 'FAILED'} → ${out}`);
process.exit(result.ok ? 0 : 1);