Remove the duplicate channel-preview route and move its launcher, owner-link, acceptance and documentation callers to the existing root URL. Query and invitation fragments retain their meaning. App launcher and component preview routes stay separate. Route, session and built-server protocol checks pass.
262 lines
21 KiB
TypeScript
262 lines
21 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { spawn } from 'node:child_process';
|
|
import { writeFile } from 'node:fs/promises';
|
|
import { existsSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { createHash } from 'node:crypto';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { fixture } from '../../prototype/channel-server/browser-fixture.mjs';
|
|
import { readPacketFlows } from './packets';
|
|
import { connectedNativePair } from './scenarios';
|
|
import { command } from './network';
|
|
import type { LabNetwork } from './network';
|
|
import type { LabProcess } from './process';
|
|
|
|
export interface MediaLab {
|
|
network: LabNetwork; output: string; source: string; executable: string; signal: AbortSignal;
|
|
result: any;
|
|
start(name: string, ns: string, program: string, args: string[], env?: NodeJS.ProcessEnv): LabProcess;
|
|
}
|
|
|
|
// Real ChannelSession/UI; synthetic capture is shared with normal acceptance.
|
|
export async function media(lab: MediaLab, mode: 'ordinary' | 'media' | 'recovery' | 'browser-relay'): Promise<void> {
|
|
const { network, output, source, executable, start, signal, result } = lab;
|
|
const prepared = mode === 'media', relayed = mode === 'browser-relay';
|
|
const remapped = mode === 'recovery', nativeTransit = prepared || remapped;
|
|
const origin = 'http://198.51.100.1:9000', room = 'kernel-lab-room';
|
|
const indices = relayed ? [0, 1, 2] : [0, 1];
|
|
const sessions = indices.map(index => network.prefix+'-browser'+index);
|
|
const sourceBrowser = 0, browserRelay = 1, viewerBrowser = relayed ? 2 : 1;
|
|
const browserOpened = new Set<number>();
|
|
const cdp = new Map<number, string>();
|
|
const browser = async (index: number, ...args: string[]) => {
|
|
const commandArgs = ['netns', 'exec', network.endpoint(index + (nativeTransit ? 2 : 1)), process.env.AGENT_BROWSER_BIN ?? 'agent-browser',
|
|
'--session', sessions[index]!, '--json', '--executable-path', process.env.CHROMIUM_BIN ?? '/usr/bin/chromium',
|
|
'--args', `--no-sandbox,--autoplay-policy=no-user-gesture-required,--disable-dev-shm-usage,--unsafely-treat-insecure-origin-as-secure=${origin}`,
|
|
'--init-script', join(output, `${index}.js`), ...args];
|
|
return new Promise<any>((resolve, reject) => {
|
|
const child = spawn('ip', commandArgs, { stdio: ['ignore', 'pipe', 'pipe'] }); let stdout = '', stderr = '';
|
|
const finish = (error?: unknown, data?: any) => {
|
|
clearTimeout(timer); child.removeAllListeners();
|
|
child.stdout.removeAllListeners(); child.stderr.removeAllListeners();
|
|
child.stdout.destroy(); child.stderr.destroy();
|
|
if (error) reject(error); else resolve(data);
|
|
};
|
|
// A daemon can retain the CLI's pipe handles after exit. Require the
|
|
// complete JSON response and exit, rather than waiting for inherited EOF.
|
|
const complete = (closed = false) => {
|
|
if (child.exitCode === null && !child.signalCode && !closed) return;
|
|
let value: any;
|
|
try { value = JSON.parse(stdout); } catch (error) { if (!closed) return; return finish(error); }
|
|
if (child.exitCode !== 0 || !value?.success) return finish(Error(stderr || value?.error || stdout));
|
|
finish(undefined, value.data);
|
|
};
|
|
child.stdout.on('data', bytes => { stdout += bytes; complete(); });
|
|
child.stderr.on('data', bytes => { stderr += bytes; });
|
|
const timer = setTimeout(() => {
|
|
result.browserFailure ??= { index, phase: result.phase, command: args[0], atMs: Date.now(),
|
|
exitCode: child.exitCode, signalCode: child.signalCode, stdoutBytes: stdout.length, stderrBytes: stderr.length };
|
|
child.kill(); finish(Error('owned Browser command deadline'));
|
|
}, 35000);
|
|
child.once('error', error => finish(error));
|
|
child.once('exit', () => complete()); child.once('close', () => complete(true));
|
|
});
|
|
};
|
|
const f = fixture(browser);
|
|
const ice = (index: number) => f.evaluate(index, `(async()=>{const rows=[];for(const pc of window.__channelProbe.peers){const stats=await pc.getStats();rows.push({connectionState:pc.connectionState,iceConnectionState:pc.iceConnectionState,candidates:[...stats.values()].filter(row=>row.type==='candidate-pair'||row.type==='local-candidate'||row.type==='remote-candidate')});}return rows;})()`);
|
|
let recoveryCapture: LabProcess | undefined;
|
|
await writeFile(join(output, 'vite.config.mts'),
|
|
'import { mergeConfig } from '+JSON.stringify(join(source, 'node_modules/vite/dist/node/index.js'))+';\n'
|
|
+ 'import base from '+JSON.stringify(join(source, 'vite.config.ts'))+';\n'
|
|
+ 'export default mergeConfig(base,{root:'+JSON.stringify(source)+',cacheDir:'+JSON.stringify(join(output, 'vite-cache'))+'});\n');
|
|
try {
|
|
result.phase = 'Browser room setup';
|
|
const vite = start('vite', network.wan, process.execPath, [join(source, 'node_modules/vite/bin/vite.js'), '--host', '0.0.0.0', '--port', '9000', '--strictPort', '--config', join(output, 'vite.config.mts')],
|
|
{ ...process.env, PIIK_CHANNEL_PORT: '9001' });
|
|
await vite.wait(() => vite.stdout.includes('9000'), signal);
|
|
start('native-host', network.endpoint(0), executable, ['host', join(output, 'host')]);
|
|
const relay = nativeTransit ? start('native-relay', network.endpoint(1), executable, ['participant']) : undefined;
|
|
if (relay) await relay.wait(() => relay.records.some(row => row.event?.type === 'admitted'), signal);
|
|
for (const index of indices) {
|
|
await writeFile(join(output, `${index}.js`), f.captureProbe(index ? 'Viewer' : 'Source'));
|
|
browserOpened.add(index);
|
|
await browser(index, 'open', `${origin}/#invite=owned-kernel-lab-invite`);
|
|
cdp.set(index, (await browser(index, 'get', 'cdp-url')).cdpUrl);
|
|
await f.until(`Browser${index} admitted`, () => f.view(index), (value: any) => value.connection === 'connected');
|
|
const role = index === sourceBrowser ? 'source' : index === viewerBrowser ? 'viewer' : 'relay';
|
|
await f.action(index, `updateProfile({name:${JSON.stringify('Owned ' + role)},avatar:null})`);
|
|
await f.until('Native room available', () => f.view(index), (value: any) => value.state.channel.rooms.some((entry: any) => entry.id === room && entry.available));
|
|
await browser(index, 'click', '.c3-room-link[aria-label="Kernel room"]');
|
|
}
|
|
for (const index of indices) await f.action(index, `joinVoice('${room}')`);
|
|
result.initial = await Promise.all(indices.map(index => f.view(index)));
|
|
const hostEndpoint = result.initial[0].state.channel.rooms.find((entry: any) => entry.id === room).delivery.hostSessionId;
|
|
const transitEndpoint = relay?.records.find(row => row.event?.type === 'admitted')?.event.sessionId;
|
|
assert(hostEndpoint && (!nativeTransit || (transitEndpoint && transitEndpoint !== hostEndpoint)));
|
|
result.nativeHostEndpoint = hostEndpoint;
|
|
result.nativeEndpoint = nativeTransit ? transitEndpoint : hostEndpoint;
|
|
if (relayed) {
|
|
result.roles = { sourceBrowser, browserRelay, viewerBrowser };
|
|
result.mediaEdges = [[sourceBrowser + 1, 0], [0, browserRelay + 1], [browserRelay + 1, viewerBrowser + 1]];
|
|
}
|
|
if (mode === 'ordinary') {
|
|
await f.action(0, `sendMessage('${room}', 'Confirmed across calibrated NAT')`);
|
|
await f.until('Native confirmed chat', () => f.evaluate(1, `${f.hook}.snapshot().messagesByRoom['${room}']`), (messages: any[]) => messages?.some(message => message.text === 'Confirmed across calibrated NAT'));
|
|
await f.action(1, `loadLatestHistory('${room}')`);
|
|
result.history = await f.evaluate(1, `${f.hook}.snapshot().messagesByRoom['${room}'].map(message => ({seq:message.seq,text:message.text}))`);
|
|
const stored = new DatabaseSync(join(output, 'host/room.sqlite'), { readOnly: true });
|
|
try { result.nativeConfirmedMessages = stored.prepare('SELECT count(*) AS count FROM message WHERE room_id=?').get(room)!.count; assert.equal(result.nativeConfirmedMessages, 1); } finally { stored.close(); }
|
|
}
|
|
// Each blocked discovery edge can take a full attempt before Core finds the transit path.
|
|
await f.until('bidirectional Native-forwarded voice', () => Promise.all([sourceBrowser, viewerBrowser].map(index => f.sample(index))), (values: any[]) => values.every(value => value.inbound.some((row: any) => row.kind === 'audio' && row.samples > 0 && row.energy > 0)), 90_000);
|
|
await f.evaluate(sourceBrowser, 'window.__channelProbe.captureAudio(true)');
|
|
const publication = await f.capturedScreen(sourceBrowser, room); result.publication = publication;
|
|
await f.action(viewerBrowser, `watch(${JSON.stringify(publication)})`);
|
|
result.phase = 'viewer playing'; await f.playing(viewerBrowser, publication);
|
|
if (prepared && relay) {
|
|
result.preparedNativePairs = relay.records.filter(row => row.event?.type === 'plan')
|
|
.flatMap(row => row.event.pairs).filter((pair: any) => pair.peerId === hostEndpoint && pair.preparation === 'required'
|
|
&& pair.incoming.some((flow: any) => flow.publicationId === publication && flow.selected));
|
|
assert(result.preparedNativePairs.length, 'Selected publication must cross the prepared Native pair');
|
|
assert(relay.records.some(row => row.event?.type === 'native-signal' && row.event.udp === 'complete'
|
|
&& result.preparedNativePairs.some((pair: any) => pair.id === row.event.pairId)), 'Matching Native pair must complete preparation');
|
|
result.probeBoundary = 'Native transit follows real voice and selected publication demand';
|
|
}
|
|
result.phase = 'viewer source audio progressing'; await f.audioProgressing(viewerBrowser, publication);
|
|
result.phase = 'initial decoded continuity';
|
|
const mediaSample = () => f.evaluate(viewerBrowser, `(async () => {
|
|
const snap=${f.hook}.snapshot(), stream=snap.streams.get(${JSON.stringify(publication)});
|
|
const video=[...document.querySelectorAll('video')].find(value=>value.srcObject===stream);
|
|
if(!video || video.readyState<2)return null;
|
|
const canvas=document.createElement('canvas');canvas.width=16;canvas.height=9;
|
|
const ctx=canvas.getContext('2d');ctx.drawImage(video,0,0,16,9);
|
|
const pixels=Array.from(ctx.getImageData(0,0,16,9).data);
|
|
const stats=await window.__channelProbe.sample();
|
|
const track=stream.getAudioTracks()[0];
|
|
const pc=window.__channelProbe.peers.find(pc=>pc.connectionState==='connected'&&pc.getReceivers().some(receiver=>receiver.track===track));
|
|
const audio=pc ? [...(await pc.getReceivers().find(receiver=>receiver.track===track).getStats()).values()]
|
|
.filter(row=>row.type==='inbound-rtp'&&row.kind==='audio').map(row=>({id:row.id,samples:row.totalSamplesReceived,energy:row.totalAudioEnergy})) : [];
|
|
return {atMs:Date.now(),width:video.videoWidth,height:video.videoHeight,frames:video.getVideoPlaybackQuality().totalVideoFrames,time:video.currentTime,pixels,
|
|
audio,inbound:stats.inbound,connections:stats.connections,pairs:snap.state.pairs};})()`);
|
|
const selectedPath = async (value: any) => {
|
|
const parent = relayed ? result.initial[browserRelay].sessionId : result.nativeEndpoint;
|
|
assert(value.pairs.some((pair: any) => pair.peerId === parent && pair.incoming.some((flow: any) => flow.publicationId === publication && flow.selected)), 'selected picture must use the observed parent');
|
|
if (relayed) {
|
|
const middle = await f.view(browserRelay);
|
|
assert(middle.state.pairs.some((pair: any) => pair.peerId === result.nativeEndpoint && pair.incoming.some((flow: any) => flow.publicationId === publication && flow.selected)), 'Browser relay must receive the selected Native output');
|
|
assert(middle.state.pairs.some((pair: any) => pair.peerId === result.initial[viewerBrowser].sessionId && pair.outgoing.some((flow: any) => flow.publicationId === publication && flow.selected)), 'Browser relay must send that publication to the viewer');
|
|
}
|
|
if (relay) {
|
|
const plan = relay.records.findLast(row => row.event?.type === 'plan')?.event;
|
|
assert(plan?.pairs.some((pair: any) => pair.peerId === hostEndpoint && pair.incoming.some((flow: any) => flow.publicationId === publication && flow.selected)), 'Native relay must receive that publication from the room host');
|
|
}
|
|
};
|
|
result.mediaBefore = await mediaSample(); await delay(3000, undefined, { signal }); result.mediaAfter = await mediaSample();
|
|
assert(result.mediaAfter.frames > result.mediaBefore.frames && result.mediaAfter.time > result.mediaBefore.time);
|
|
assert.notDeepEqual(result.mediaAfter.pixels, result.mediaBefore.pixels, 'actual decoded pixels must change');
|
|
await selectedPath(result.mediaAfter);
|
|
if (relayed || nativeTransit) {
|
|
const node = relayed ? browserRelay + 1 : 1;
|
|
const route = relayed ? 'Browser relay' : 'Native transit';
|
|
const initialPair = remapped && relay ? connectedNativePair(relay.records) : undefined;
|
|
if (remapped) {
|
|
assert(initialPair?.peerId === hostEndpoint, 'Native room-host pair must be connected before blackout');
|
|
result.nativePair = initialPair;
|
|
}
|
|
result.sourceBeforeOutage = await f.sample(sourceBrowser);
|
|
result.recovery = { blockedAt: Date.now() };
|
|
result.phase = `${route} UDP blackout`;
|
|
network.setFault(node, 'udp-block', 1);
|
|
await delay(50_000, undefined, { signal });
|
|
result.blocked = network.counters(node);
|
|
assert(result.blocked.faultDrops > 0, `${route} blackout must drop actual traffic`);
|
|
let previous = await mediaSample();
|
|
recoveryCapture = start('recovery-capture', network.wan, 'tcpdump', ['-i', 'internet', '-n', '-s', '96', '-U', '-w', join(output, 'recovery.pcap'), '-c', '10000', 'udp']);
|
|
await recoveryCapture.wait(() => recoveryCapture!.stderr.includes('listening on'), signal);
|
|
if (remapped) result.remap = network.remapUDP(node);
|
|
result.recovery.restoredAt = Date.now(); network.setFault(node, 'none', 1);
|
|
result.phase = `${route} decoded picture and source audio recovery`;
|
|
for (const [from, to] of result.blockedShortcuts.filter(([from, to]: number[]) => from === node || (remapped && to === node))) network.exec(network.router(from), 'nft', [
|
|
`add rule inet lab_filter faults ip daddr ${network.publicIP(to)} meta l4proto udp counter drop`,
|
|
]);
|
|
const recovered = await f.until(`${route} decoded picture and source sound recover`, mediaSample, (value: any) => {
|
|
const advanced = value && previous && value.frames > previous.frames && value.time > previous.time && value.audio.some((row: any) =>
|
|
previous.audio.some((before: any) => row.id === before.id && row.samples > before.samples && row.energy > before.energy));
|
|
previous = value;
|
|
if (!initialPair || !relay) return advanced;
|
|
const pair = connectedNativePair(relay.records, { since: result.recovery.restoredAt, replaces: initialPair });
|
|
if (pair) result.recovery.nativePair = pair;
|
|
return advanced && Boolean(pair);
|
|
}, 30_000);
|
|
Object.assign(result.recovery, { recoveredAt: Date.now(), before: recovered });
|
|
await delay(3000, undefined, { signal }); result.recovery.after = await mediaSample();
|
|
assert(result.recovery.after.frames > recovered.frames && result.recovery.after.time > recovered.time);
|
|
assert.notDeepEqual(result.recovery.after.pixels, recovered.pixels);
|
|
await selectedPath(result.recovery.after);
|
|
result.sourceAfterOutage = await f.sample(sourceBrowser);
|
|
assert.deepEqual(result.sourceAfterOutage.captures, result.sourceBeforeOutage.captures, 'The original capture must stay live through transport recovery');
|
|
if (relay) result.recovery.nativePlans = relay.records.filter(row => row.atMs >= result.recovery.blockedAt && row.event?.type === 'plan')
|
|
.flatMap(row => row.event.pairs);
|
|
}
|
|
await browser(viewerBrowser, 'screenshot', join(output, 'decoded-native-transit.png'));
|
|
result.ice = await Promise.all(indices.map(index => f.evaluate(index, `(async()=>{const rows=[];for(const pc of window.__channelProbe.peers){const stats=await pc.getStats();for(const row of stats.values())if(row.type==='candidate-pair'&&row.state==='succeeded'&&row.nominated)rows.push({pair:row,local:stats.get(row.localCandidateId),remote:stats.get(row.remoteCandidateId)});}return rows;})()`)));
|
|
if (mode === 'ordinary') {
|
|
const bytes = 1024 * 1024 + 17;
|
|
const expected = createHash('sha256').update(Buffer.from(Array.from({ length: bytes }, (_, index) => index % 251))).digest('hex');
|
|
await f.evaluate(sourceBrowser, `(async()=>{const bytes=Uint8Array.from({length:${bytes}},(_,i)=>i%251);await ${f.hook}.session.sendMessage('${room}','File through Native',[{id:crypto.randomUUID(),file:new File([bytes],'owned-proof.bin')}]);return true;})()`);
|
|
const fileMessage = await f.until('Native confirms file description', () => f.evaluate(viewerBrowser, `${f.hook}.snapshot().messagesByRoom['${room}']?.find(message=>message.text==='File through Native')`), Boolean);
|
|
assert.equal(fileMessage.attachments[0].sha256, expected);
|
|
result.file = await f.evaluate(viewerBrowser, `(async()=>{const message=${JSON.stringify(fileMessage)},attachment=message.attachments[0];const file=await ${f.hook}.session.attachments.content({roomId:'${room}',messageSeq:message.seq,attachmentId:attachment.id},attachment,new AbortController().signal);const digest=await crypto.subtle.digest('SHA-256',await file.arrayBuffer());return {size:file.size,sha256:[...new Uint8Array(digest)].map(byte=>byte.toString(16).padStart(2,'0')).join('')};})()`);
|
|
assert.equal(result.file.size, bytes); assert.equal(result.file.sha256, expected);
|
|
}
|
|
await f.progressing(viewerBrowser, publication); await f.audioProgressing(viewerBrowser, publication);
|
|
result.final = await Promise.all(indices.map(index => f.view(index)));
|
|
await f.action(sourceBrowser, `stopScreen(${JSON.stringify(publication)})`);
|
|
await f.until('source retirement reaches viewer', () => f.view(viewerBrowser), (value: any) => !value.state.publications.some((entry: any) => entry.id === publication));
|
|
for (const index of indices) await f.action(index, `leaveVoice('${room}')`);
|
|
for (const index of indices) await f.assertNoError(index, 'kernel NAT media');
|
|
result.stop = await Promise.all(indices.map(index => f.sample(index)));
|
|
assert(result.stop.every((value: any) => value.captures.every((track: any) => track.state === 'ended')));
|
|
} catch (error) {
|
|
result.failure = { atMs: Date.now(), phase: result.phase,
|
|
memory: command('cat', ['/proc/meminfo']),
|
|
memoryPressure: existsSync('/proc/pressure/memory') ? command('cat', ['/proc/pressure/memory']) : null,
|
|
cpuPressure: existsSync('/proc/pressure/cpu') ? command('cat', ['/proc/pressure/cpu']) : null,
|
|
processes: await Promise.allSettled(network.namespaces.map(async namespace => {
|
|
const pids = command('ip', ['netns', 'pids', namespace]).split(/\s+/).filter(Boolean);
|
|
return { namespace, pids, status: pids.length ? command('ps', ['-p', pids.join(','), '-o', 'pid,ppid,stat,pcpu,rss,etime,comm']) : '' };
|
|
})),
|
|
cdp: await Promise.allSettled([...cdp].map(async ([index, url]) => ({ index,
|
|
// Standard CDP, independently of an agent command or awaited page Promise.
|
|
health: JSON.parse(network.exec(network.endpoint(index + (nativeTransit ? 2 : 1)), process.execPath, ['--input-type=module', '-e', `
|
|
const ws=new WebSocket(process.argv[1]), replies=[];let done=false;
|
|
const finish=()=>{if(done)return;done=true;console.log(JSON.stringify(replies));ws.close();};
|
|
const timer=setTimeout(finish,2000);
|
|
ws.onopen=()=>{ws.send(JSON.stringify({id:1,method:'Browser.getVersion'}));ws.send(JSON.stringify({id:2,method:'Target.getTargets'}));};
|
|
ws.onmessage=({data})=>{const row=JSON.parse(data);if(!row.id)return;replies.push(row);
|
|
if(row.id===2){const page=row.result?.targetInfos.find(t=>t.type==='page');if(page)ws.send(JSON.stringify({id:3,method:'Target.attachToTarget',params:{targetId:page.targetId,flatten:true}}));}
|
|
if(row.id===3&&row.result?.sessionId)ws.send(JSON.stringify({id:4,sessionId:row.result.sessionId,method:'Runtime.evaluate',params:{expression:'1',returnByValue:true,awaitPromise:false}}));
|
|
if(row.id===4){clearTimeout(timer);finish();}};
|
|
ws.onerror=()=>{clearTimeout(timer);finish();};
|
|
`, url])),
|
|
}))),
|
|
browsers: await Promise.allSettled([...browserOpened].map(async index => ({
|
|
index, view: await f.view(index), sample: await f.sample(index), ice: await ice(index),
|
|
}))) };
|
|
if (relayed && result.roles) result.failure.routers = await Promise.allSettled(indices.map(async index => ({ index: index + 1,
|
|
counters: network.counters(index + 1),
|
|
nat: JSON.parse(network.exec(network.router(index + 1), 'nft', ['-j', 'list', 'table', 'ip', 'lab_nat'])),
|
|
})));
|
|
throw error;
|
|
} finally {
|
|
const closed = await Promise.allSettled([...browserOpened].map(index => browser(index, 'close')));
|
|
if (recoveryCapture) {
|
|
await recoveryCapture.close('SIGINT');
|
|
result.recoveryPacketFlows = await readPacketFlows(join(output, 'recovery.pcap'));
|
|
}
|
|
const errors = closed.filter(value => value.status === 'rejected').map(value => value.reason);
|
|
if (errors.length) throw new AggregateError(errors, 'Owned Browser cleanup failed');
|
|
}
|
|
}
|