Files
piik/scripts/room-voice-gate.ts
T
AAA 499789671c feat(app): bring Android sharing and touch control into the shared runtime (#504)
## Problem and outcome / 问题与结果

Bring Android into Piik's shared App workflow and make phone control usable without a separate Web UI. One signed APK covers ARM64 and x64; screen capture, room authority, routing, voice and site approval reuse the existing Go and Browser owners. Android opens the user's system browser rather than shipping an embedded browser.

This phase also fixes capture replacement and encoding-clock failures, improves source-audio startup, contains recoverable input and transport failures, adds a bounded relay-volunteer setting and supports hostnames in `SFU_PUBLIC_IP`. The website separates desktop and mobile downloads, gives iOS users an online entry and aligns the bilingual installation guides.

Related reports: #495, #499, #500, #501, #502 and #503. For #502, input execution failures now preserve authorization, and temporary target observations suspend rather than revoke it; the original Windows report remains open for candidate feedback.

## Release notes

**Piik, now in your pocket.**

Piik App now supports Android screen sharing, with touch controls for a friend's computer.

**手机,也能开播。**

Piik App 现已支持 Android 屏幕分享,也能用手机控制朋友的电脑。

### English

Piik v2.5.0 brings the App to Android and improves capture reliability, touch control and everyday startup. The existing 2.x compatibility baseline is unchanged.

- **Android App:** share your screen, join room voice or control a supported computer. One APK supports ARM64 and x64, using your own browser and the same Piik interface.
- **Phone controls:** use direct touch or a compact floating mouse with dragging, left/right buttons and a scroll wheel. Pinch to zoom and pan the picture while controlling.
- **More reliable sharing:** keep video progressing through source and encoder-output changes, and improve low-bitrate recovery and stationary-screen keyframes. Correct Windows window-size handling and Linux NVENC settings; start optional source sound after video is ready.
- **Smoother App entry:** retry failed startup in place, reopen the running room and retain the correct App connection when returning from the browser. Compress shared page assets to reduce loading traffic and keep microphone choices stable during ordinary page updates.
- **Recovery stays local:** a rejected input or canceled source replacement no longer revokes remote-control permission. Full ICE candidate queues and failed audio-parameter updates retain healthy connections. A failed local room leaves the App and unrelated approved sites running.
- **Connection settings:** relay volunteers can cap their downstream viewers. Self-hosted SFU setups can use a DNS hostname in `SFU_PUBLIC_IP`.
- **Clearer help:** refine sharing-error guidance, preserve input-failure diagnostics without recording input content, and update the bilingual platform and network guides.

Update both the App and your self-hosted site to use the new features. Android can control a supported computer; the Android device itself is not a controlled endpoint.

If you previously installed a test APK, uninstall it once before installing this release: the official signing key is different. Uninstalling clears App-local site approvals and starred-site choices; browser settings remain in the browser. Later official releases can be installed over this one.

### 中文

Piik v2.5.0 带来 Android App,并改善采集稳定性、手机远控与日常启动体验。沿用现有 2.x 兼容基线。

- **Android App:** 分享手机画面、加入房间语音,或控制支持远控的电脑。一个安装包同时支持 ARM64 与 x64,使用自己的浏览器和熟悉的 Piik 界面。
- **手机远控:** 直接触摸操作,或展开小巧的悬浮鼠标,使用拖拽、左右键与滚轮;控制时也能双指缩放、移动画面。
- **分享更稳定:** 修复切换采集来源、编码输出时画面停止推进的问题,改善低码率恢复与静止画面的关键帧响应。修正 Windows 窗口尺寸处理和 Linux NVENC 设置,来源声音会在画面就绪后启动。
- **App 进出更顺畅:** 启动失败可原地重试,再次打开会回到正在运行的房间;从浏览器返回时保持连接到正确的 App。共用页面资源启用压缩,减少加载流量,麦克风选项不再随普通页面更新反复加载。
- **局部问题就地恢复:** 单次操作失败或取消换源不再撤销远控授权;ICE 候选队列满或音频参数调整失败时保留健康连接。本地房间故障不会连带停止 App 和其他已授权站点的分享。
- **连接设置:** 帮助转发的观众可设置承接人数上限;自部署 SFU 的 `SFU_PUBLIC_IP` 现在支持 DNS 主机名。
- **排查更直接:** 打磨分享失败提示,补充不记录输入内容的远控故障诊断,并更新中英文平台说明与网络指南。

使用新功能时,请一起更新 App 与自部署站点。Android 可作为控制端操作电脑,手机本身不作为被控端。

曾安装测试 APK 的用户需先卸载一次,再安装正式版,两者签名不同。卸载会清除 App 内的站点授权和收藏,浏览器设置仍保留在浏览器中;后续正式版可直接覆盖安装。

## Verification / 验证

- Local full Web/type/build checks passed: 104 test files, 1,848 tests; the website and documentation builds passed.
- Android full-App emulator acceptance covered real decoded screen sharing, concurrent 1080p/540p encoder allocation, live bitrate changes, stationary keyframes, Viewer departure, consent reuse/reentry and final capture-resource retirement.
- A local universal release APK passed signing, package identity, version-code, ABI and non-debuggable checks using the permanent release key. GitHub Actions receives the same key through encrypted Secrets; manual candidate runs use a disposable candidate signer.
- Windows real capture checks covered H264/VP8 window resizing and restoration. Source-audio startup and failure/close ordering passed focused runtime checks; Linux race checks passed.
- Desktop/mobile website layouts were reviewed in English and Chinese, both themes, including grouped downloads, the iOS online entry and self-hosting links.
- All three Server and six App candidate targets, including the signed Android universal APK, passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40350004792. This uses the same release packaging path with publication disabled.
- The complete network laboratory passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40354253735: 43 expected scenario outcomes (37 media connections, six expected-unavailable controls), six negative controls, and complete process/network cleanup.
- Recoverable-error checks cover real encrypted input replies and legacy opt-in, retained grants and held-key cleanup, real Windows target geometry recovery, candidate queue overflow, and audio-parameter retry on the existing edge. Android room failure and full shutdown use real launcher, control WebSocket and supervised-process checks; unrelated site resources remain intact.
- The lifecycle fixes at `7eb04de373567cfe7aff2db6c93adf2a38e0b1aa` passed all nine package targets and Go/vulnerability/race/Web checks in https://github.com/TNTcraftHIM/Piik/actions/runs/40362794088, including macOS x64/ARM64 cgo and Android. Publication was skipped.
- Final cross-review found and fixed the same candidate-overflow false failure in the SFU voice owner. Its real-Pion regression verifies the first 64 candidates remain queued, a valid answer drains them, and renegotiation succeeds. Independent reviews confirmed real SDP and authorization failures still propagate.
- A real-browser before/after check reduced microphone enumeration from nine calls to one across eight ordinary Host rerenders. Changing the native client or switching to Browser capture still enumerates the new devices. The separate focused contribution #507 preserves input focus during queued floating-panel opening and is not attributed to this phase.
- The verified Windows candidate and reproduction steps have been updated in #502. macOS/Linux physical-device coverage and iOS device acceptance keep their documented boundaries.

## Impact / 影响

- Planned release: **v2.5.0**; no public wire or storage version bump.
- Android is a sharing and controlling endpoint, not a remotely controlled Android device. iOS is held outside this release.
- New Android release target joins the existing complete-target, checksum, GitHub and Gitee artifact pipeline. Signing material stays outside Git; optional input SDKs/drivers are not bundled.
- The owner authorized closure and publication after the final design review and acceptance checks. Restore the publication gate after the held focused contribution and merge this complete phase once; CI publishes only after its full checks and registered packages pass.
- Current TODO, platform guidance, media/routing evidence and release operations retain their existing document owners; no additional audit ledger or changelog archive is introduced.
2026-10-26 13:37:33 -04:00

570 lines
44 KiB
TypeScript

// Normal Host/Viewer acceptance: room activation, capture and real WebRTC.
import { spawn, execFile, type ChildProcessWithoutNullStreams } from "node:child_process";
import { createSocket } from "node:dgram";
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { promisify } from "node:util";
import { createServer as createViteServer, type ViteDevServer } from "vite";
import type { RoomVoiceState } from "../src/shared/protocol";
import { CdpConnection, cleanupRun, evaluate, fetchJsonBefore, launchChrome,
reservePort, waitForSample, waitForVersion, type PageHandle } from "./browser-gate-harness";
const ROOT = resolve(import.meta.dirname, "..");
const OUTPUT = join(ROOT, "build", "voice-integration");
const runFile = promisify(execFile);
const redact = (value: string) => value.replace(/#v=[A-Za-z0-9_-]+/g, "#v=[redacted]");
type Page = PageHandle & { targetId: string; session: string };
type Sample = { voice: RoomVoiceState | null; peerId: string | null; inviteUrl: string | null; authentications: number;
energy: number; samples: number; bytes: number; pictureAudioEnergy: number; pictureAudioSamples: number;
playing: number; mutedPlayback: number; enabled: boolean; liveMicrophones: number; videoFrames: number; text: string; controls: string[] };
// Observe the normal browser boundary; no alternate session or control protocol.
const probe = (name: string) => `(() => {
localStorage.setItem('piik:ui-lang','en'); localStorage.setItem('piik:ui-mode','text');
localStorage.setItem('piik:display-name:v1',${JSON.stringify(name)});
const peers=[], sockets=[], captures=[], NativePC=RTCPeerConnection, NativeWS=WebSocket;
const gate=window.__roomVoiceGate={peers,sockets,captures,voice:null,peerId:null,inviteUrl:null,wire:[],authentications:0};
const record=(direction,m)=>{
if(!m.type.startsWith('room-voice-')&&!['room-interaction','route-diagnostic-snapshot','peer-state'].includes(m.type))return;
const summary={...m};
if(m.description)summary.description={revision:m.description.revision,type:m.description.sdp.type,tracks:m.description.tracks,
media:m.description.sdp.sdp.split('\\r\\n').filter(line=>/^m=|^a=mid:|^a=(sendrecv|sendonly|recvonly|inactive)/.test(line))};
if(m.candidate)summary.candidate=m.candidate.candidate.split(' ').slice(0,8).join(' ');
gate.wire.push({direction,time:Date.now(),...summary});if(gate.wire.length>120)gate.wire.shift();
};
window.RTCPeerConnection=class extends NativePC { constructor(...a){super(...a);const index=peers.length;peers.push(this);
this.addEventListener('iceconnectionstatechange',async()=>{
const state={type:'peer-state',index,ice:this.iceConnectionState,connection:this.connectionState,
gathering:this.iceGatheringState,signaling:this.signalingState};
try{const stats=await this.getStats();state.stats=[...stats.values()].filter(row=>['transport','candidate-pair','local-candidate','remote-candidate'].includes(row.type))
.map(row=>Object.fromEntries(['id','type','state','iceState','dtlsState','selectedCandidatePairId','localCandidateId','remoteCandidateId','candidateType','address','port','protocol','nominated','requestsSent','responsesReceived','requestsReceived','responsesSent'].filter(key=>key in row).map(key=>[key,row[key]])));}catch{}
record('browser',state);
});
}};
window.WebSocket=class extends NativeWS { constructor(...a){super(...a);sockets.push(this);
this.addEventListener('message',event=>{try{const m=JSON.parse(event.data);
record('receive',m);if(m.type==='room-voice-state')gate.voice=m;
if(m.type==='authenticated'){gate.peerId=m.peerId;gate.authentications++;gate.voice=null;}
}catch{}});
}send(data){try{record('send',JSON.parse(data));}catch{}return super.send(data);}};
const originalFetch=window.fetch;
window.fetch=async(...a)=>{const response=await originalFetch(...a);
if(String(a[0]).endsWith('/api/rooms')&&a[1]?.method==='POST') {
const created=await response.clone().json();gate.inviteUrl=created.inviteUrl;
}return response;
};
const gum=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
navigator.mediaDevices.getUserMedia=async(...a)=>{const stream=await gum(...a);captures.push(stream);return stream;};
gate.sample=async()=>{
const audio=[...document.querySelectorAll('audio')].filter(node=>node.srcObject);
const ids=new Set(audio.flatMap(node=>node.srcObject.getAudioTracks().map(track=>track.id)));
const pictureIds=new Set([...document.querySelectorAll('video')].flatMap(node=>node.srcObject?.getAudioTracks().map(track=>track.id)??[]));
let energy=0, samples=0, bytes=0, pictureAudioEnergy=0, pictureAudioSamples=0;
for(const peer of peers)if(peer.connectionState==='connected')for(const row of (await peer.getStats()).values())
if(row.type==='inbound-rtp'&&row.kind==='audio'){
if(ids.has(row.trackIdentifier)){energy+=row.totalAudioEnergy||0;samples+=row.totalSamplesReceived||0;bytes+=row.bytesReceived||0;}
if(pictureIds.has(row.trackIdentifier)){pictureAudioEnergy+=row.totalAudioEnergy||0;pictureAudioSamples+=row.totalSamplesReceived||0;}
}
return {voice:gate.voice,peerId:gate.peerId,inviteUrl:gate.inviteUrl,authentications:gate.authentications,energy,samples,bytes,pictureAudioEnergy,pictureAudioSamples,wire:gate.wire,
playing:audio.length,mutedPlayback:audio.filter(node=>node.muted).length,enabled:audio.every(node=>node.srcObject.getAudioTracks().every(t=>t.enabled)),
tracks:audio.flatMap(node=>node.srcObject.getAudioTracks().map(t=>({readyState:t.readyState,muted:t.muted,currentTime:node.currentTime}))),
connections:peers.map(p=>({state:p.connectionState,signaling:p.signalingState,senders:p.getSenders().map(s=>s.track&&({kind:s.track.kind,readyState:s.track.readyState,enabled:s.track.enabled,muted:s.track.muted}))})),
liveMicrophones:captures.flatMap(s=>s.getAudioTracks()).filter(t=>t.readyState==='live').length,
videoFrames:[...document.querySelectorAll('video')].reduce((sum,node)=>sum+(node.getVideoPlaybackQuality?.().totalVideoFrames||0),0),
text:document.body.innerText.slice(-1200),controls:[...document.querySelectorAll('button[aria-label]')].map(node=>node.getAttribute('aria-label'))};
};
})()`;
function audioFixture(): Buffer {
const rate = 48000, samples = rate * 2, data = Buffer.alloc(44 + samples * 2);
data.write("RIFF"); data.writeUInt32LE(data.length - 8, 4); data.write("WAVEfmt ", 8);
data.writeUInt32LE(16, 16); data.writeUInt16LE(1, 20); data.writeUInt16LE(1, 22);
data.writeUInt32LE(rate, 24); data.writeUInt32LE(rate * 2, 28); data.writeUInt16LE(2, 32);
data.writeUInt16LE(16, 34); data.write("data", 36); data.writeUInt32LE(samples * 2, 40);
for (let i = 0; i < samples; i++) {
const time = i / rate, pulse = .4 + .6 * Math.sin(2 * Math.PI * 3.3 * time) ** 2;
const pitch = 180 * time + 13 * Math.sin(time * 5);
data.writeInt16LE(Math.round(2400 * pulse * (Math.sin(2 * Math.PI * pitch) + .4 * Math.sin(4 * Math.PI * pitch))), 44 + i * 2);
}
return data;
}
async function udpPort(): Promise<number> {
const socket = createSocket("udp4");
return new Promise((accept, reject) => {
socket.once("error", reject);
socket.bind(0, "127.0.0.1", () => { const port = socket.address().port; socket.close(() => accept(port)); });
});
}
async function main(): Promise<void> {
if (process.env.PIIK_ROOM_VOICE_GATE !== "true") throw new Error("Set PIIK_ROOM_VOICE_GATE=true for this local acceptance gate");
const executable = process.env.PIIK_SERVER_EXE || join(OUTPUT, `piik-server${process.platform === "win32" ? ".exe" : ""}`);
const chromePath = process.env.CHROME_PATH;
if (!chromePath) throw new Error("CHROME_PATH is required");
const agentBrowser = process.env.AGENT_BROWSER_PATH || (process.platform === "win32"
? join(process.env.APPDATA!, "npm/node_modules/agent-browser/bin/agent-browser-win32-x64.exe") : "agent-browser");
const sfuOnly = process.env.PIIK_ROOM_VOICE_SFU === "true";
const arm = sfuOnly ? "sfu" : "peer";
const serverPort = Number(process.env.PIIK_ROOM_VOICE_PORT || (sfuOnly ? 19162 : 19161));
const [vitePort, debugPort, mediaPort] = await Promise.all([reservePort(), reservePort(), udpPort()]);
const origin = `http://127.0.0.1:${vitePort}`, backend = `http://127.0.0.1:${serverPort}`;
const legacyRoot = process.env.PIIK_ROOM_VOICE_LEGACY_WEB;
const legacyPort = legacyRoot ? await reservePort() : null;
const legacyOrigin = legacyPort ? `http://127.0.0.1:${legacyPort}` : null;
const legacyRevision = legacyRoot ? (await runFile("git", ["-C", legacyRoot, "rev-parse", "HEAD"], { windowsHide: true })).stdout.trim() : null;
const profile = await mkdtemp(join(tmpdir(), "piik-client-media-"));
await mkdir(OUTPUT, { recursive: true });
const audio = join(OUTPUT, "microphone.wav");
await writeFile(audio, audioFixture());
let server: ChildProcessWithoutNullStreams | null = null, chrome: ChildProcessWithoutNullStreams | null = null;
let cdp: CdpConnection | null = null, vite: ViteDevServer | null = null, legacyVite: ViteDevServer | null = null;
let serverLog = "";
const pages: Page[] = [];
const result = { arm, legacyRevision, passed: false, stage: "start", checks: [] as string[], error: null as string | null,
diagnostics: [] as unknown[], cleanup: null as unknown };
const ui = async (page: Page, ...args: string[]) => {
if (args[0] === "click" || args[0] === "focus") await waitForSample(() => evaluate<boolean>(cdp!, page,
`!!document.querySelector(${JSON.stringify(args[1])})`, Date.now() + 3000), Boolean, 15_000);
const response = await runFile(agentBrowser, ["--session", page.session, "--cdp", String(debugPort), ...args],
{ cwd: ROOT, timeout: 15_000, windowsHide: true, maxBuffer: 1024 * 1024 });
return response.stdout;
};
const sample = (page: Page) => evaluate<Sample>(cdp!, page, "window.__roomVoiceGate.sample()", Date.now() + 4000);
const until = async (page: Page, accept: (state: Sample) => boolean, label: string) => {
result.stage = label;
let last: Sample | null = null;
try { await waitForSample(() => sample(page), value => { last = value; return accept(value); }, 25_000, 200); }
catch { const failed = last as Sample | null; throw new Error(`${label}: ${JSON.stringify(failed && {
revision: failed.voice?.revision, connections: failed.voice?.connections.length, unavailable: failed.voice?.unavailable.length,
energy: failed.energy, samples: failed.samples, bytes: failed.bytes, liveMicrophones: failed.liveMicrophones, text: failed.text,
})}`); }
return last!;
};
const button = (page: Page, name: string) => ui(page, "find", "role", "button", "click", "--name", name, "--exact");
const memberButton = async (page: Page, name: string, action: string) => {
let index = -1;
await waitForSample(() => evaluate<number>(cdp!, page,
`[...document.querySelectorAll('.lr-voice-member')].findIndex(node=>node.querySelector('b')?.textContent.includes(${JSON.stringify(name)})&&node.querySelector('button[aria-label=${JSON.stringify(action)}]'))`,
Date.now() + 3000), value => { index = value; return value >= 0; }, 5000);
const selector = `.lr-voice-member:nth-child(${index + 1}) button[aria-label=${JSON.stringify(action)}]`;
await ui(page, "scrollintoview", selector);
return ui(page, "click", selector);
};
const openVoice = async (page: Page) => {
await button(page, "Interact");
await waitForSample(() => evaluate<boolean>(cdp!, page,
`!!document.querySelector('[role="tab"][id$="-voice-tab"]')`, Date.now() + 3000), Boolean, 5000);
await ui(page, "find", "role", "tab", "click", "--name", "Voice", "--exact");
};
const check = (label: string) => { result.checks.push(label); process.stdout.write(`${arm}: ${label}\n`); };
const inspectVoiceRoute = async (participant: Page, name: string) => {
await waitForSample(() => evaluate<boolean>(cdp!, participant, `(() => {
const state=window.__roomVoiceGate.voice, tree=document.getElementById('room-topology');
const members=state?.members.filter(member=>member.peerId!==state.hostPeerId&&member.connected&&member.joined&&member.upstream.kind!=='none');
const seats=[...document.querySelectorAll('.lr-couch [data-room-peer]')];
return members?.length>0&&tree?.querySelectorAll('.lr-route-edge:not(.is-pending)').length>=members.length&&
members.every(member=>seats.find(seat=>seat.dataset.roomPeer===member.peerId)?.querySelector('.lr-pawn')?.getAttribute('aria-label')?.includes('Voice connected'));
})()`, Date.now() + 3000), Boolean, 5000);
await button(participant, "Show connection details");
await waitForSample(() => evaluate<boolean>(cdp!, participant, `(() => {
const details=[...document.querySelectorAll('.lr-media-detail')].filter(node=>node.getAttribute('aria-label')?.startsWith('Voice'));
return details.length>0&&details.every(node=>!node.textContent.includes(' fps'))&&
details.some(node=>/\\d+ kbps$/.test(node.querySelector('[data-metric="stats.totalBitrate"] b')?.textContent??''))&&
details.some(node=>/\\d+ ms$/.test(node.querySelector('[data-metric="stats.avgRtt"] b')?.textContent??''));
})()`, Date.now() + 3000), Boolean, 5000);
await ui(participant, "screenshot", join(OUTPUT, `${arm}-${name}.png`), "--full");
await button(participant, "Hide connection details");
};
try {
server = spawn(executable, [], { cwd: ROOT, stdio: "pipe", windowsHide: true,
env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, PIIK_ENV: "development",
LISTEN_HOST: "127.0.0.1", PORT: String(serverPort), PIIK_LOG_DIR: join(OUTPUT, `${arm}-logs`),
ROOM_DATABASE_PATH: join(profile, "rooms.sqlite"), PIIK_DEBUG: "route",
PUBLIC_BASE_URL: origin, ALLOWED_ORIGINS: [origin, legacyOrigin].filter(Boolean).join(","), SFU_LISTEN_HOST: "127.0.0.1",
SFU_UDP_PORT: String(mediaPort), SFU_PUBLIC_IP: "127.0.0.1", SFU_ONLY: String(sfuOnly),
ENDPOINT_MEDIA_COPY_CAPACITY: "1" } });
server.stdout.resume();
server.stderr.on("data", (data: Buffer) => { serverLog = (serverLog + data.toString()).slice(-12000); });
await waitForSample(deadline => fetchJsonBefore(`${backend}/healthz`, deadline), () => true, 15_000);
vite = await createViteServer({ root: ROOT, logLevel: "error", server: { host: "127.0.0.1", port: vitePort, watch: null,
strictPort: true, proxy: { "/api": { target: backend }, "/healthz": { target: backend }, "/signal": { target: backend, ws: true } } } });
await vite.listen();
if (legacyRoot && legacyPort) {
legacyVite = await createViteServer({ root: legacyRoot, logLevel: "error", optimizeDeps: { entries: ["index.html"] }, server: { host: "127.0.0.1", port: legacyPort,
watch: null, strictPort: true, proxy: { "/api": { target: backend }, "/healthz": { target: backend }, "/signal": { target: backend, ws: true } } } });
await legacyVite.listen();
}
chrome = await launchChrome(chromePath, debugPort, profile, ["--headless=new", "--no-first-run",
"--window-size=1280,900",
// Chromium excludes loopback ICE by default; this gate's SFU binds only localhost.
// https://chromium.googlesource.com/chromium/src/+/HEAD/content/public/common/content_switches.cc
"--allow-loopback-in-peer-connection",
"--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream", "--autoplay-policy=no-user-gesture-required",
`--use-file-for-fake-audio-capture=${audio}`]);
const version = await waitForVersion(debugPort, chrome);
cdp = await CdpConnection.connect(version.webSocketDebuggerUrl, Date.now() + 10_000);
const page = async (url: string, name: string): Promise<Page> => {
const { browserContextId } = await cdp!.call<{ browserContextId: string }>("Target.createBrowserContext", {}, undefined, Date.now() + 5000);
const { targetId } = await cdp!.call<{ targetId: string }>("Target.createTarget", { url: "about:blank", browserContextId }, undefined, Date.now() + 5000);
const { sessionId } = await cdp!.call<{ sessionId: string }>("Target.attachToTarget", { targetId, flatten: true }, undefined, Date.now() + 5000);
const current = { targetId, sessionId, session: `piik-voice-${arm}-${process.pid}-${pages.length}` };
pages.push(current);
await cdp!.call("Page.enable", {}, sessionId, Date.now() + 5000);
await cdp!.call("Page.addScriptToEvaluateOnNewDocument", { source: probe(name) }, sessionId, Date.now() + 5000);
await cdp!.call("Page.navigate", { url }, sessionId, Date.now() + 5000);
await ui(current, "--pin-tab", "tab", targetId);
return current;
};
const host = await page(origin, "Voice Host");
await until(host, state => state.controls.includes("Create room"), "idle Host");
if ((await sample(host)).inviteUrl) throw new Error("Page visit allocated a room");
await ui(host, "screenshot", join(OUTPUT, `${arm}-idle-room.png`), "--full");
await button(host, "Create room");
const created = await until(host, state => !!state.inviteUrl && !!state.voice, "create idle room");
if (created.liveMicrophones) throw new Error("Room creation started a microphone");
await openVoice(host);
const viewer = await page(created.inviteUrl!, "Voice Viewer");
await until(viewer, state => !!state.voice, "Viewer authenticates before sharing");
await openVoice(viewer);
if (await evaluate<boolean>(cdp, viewer,
`!document.querySelector('button[aria-label="Join voice"]')?.matches(':disabled,[aria-disabled="true"]')`, Date.now() + 3000)) throw new Error("Viewer can join before Host opens room voice");
for (const [index, participant] of [host, viewer].entries()) {
await button(participant, "Hide interaction window");
await button(participant, "Show connection topology");
await waitForSample(() => evaluate<boolean>(cdp!, participant, `(() => {
const tree=document.getElementById('room-topology');
return !!tree?.querySelector('.lr-route-idle')&&tree.querySelectorAll('.lr-route-node').length===2&&
!tree.querySelector('.lr-route-edge,.is-pending');
})()`, Date.now() + 3000), Boolean, 5000);
await ui(participant, "screenshot", join(OUTPUT, `${arm}-idle-topology-${index}.png`), "--full");
await button(participant, "Hide connection topology");
await openVoice(participant);
}
check("idle room members have no pending treatment or media edges before sharing and voice");
await button(host, "Open room voice");
await until(viewer, state => !!state.voice?.enabled, "Host opens room voice");
await button(viewer, "Join voice");
await until(viewer, state => !!state.voice?.members.find(member => member.peerId === state.peerId)?.joined, "Viewer joins without approval");
if ((await sample(host)).liveMicrophones || (await sample(viewer)).liveMicrophones) throw new Error("Joining started a microphone");
await button(host, "Microphone");
await button(viewer, "Microphone");
await until(host, state => state.energy > 0 && state.voice?.publications.length === 2, "Host receives Viewer");
const initial = await until(viewer, state => state.energy > 0 && state.voice?.publications.length === 2, "Viewer receives Host");
if (!initial.voice?.connections.some(connection => connection.transport === (sfuOnly ? "sfu" : "peer"))) throw new Error("Wrong transport");
check("Host opens voice, Viewer joins freely, explicit microphones carry bidirectional audio");
result.stage = "microphone touch hold";
await cdp.call("Page.bringToFront", {}, host.sessionId, Date.now() + 3000);
await cdp.call("Input.dispatchMouseEvent", { type: "mouseMoved", x: 1, y: 1 }, host.sessionId, Date.now() + 3000);
await waitForSample(() => evaluate<boolean>(cdp!, host,
`!document.querySelector('.lr-audio-volume-popover:popover-open')`, Date.now() + 3000), Boolean, 5000);
await cdp.call("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 1 }, host.sessionId, Date.now() + 3000);
const microphonePoint = await evaluate<{ x: number; y: number }>(cdp, host,
`(()=>{const r=document.querySelector('.lr-voice-quick-controls button[aria-label="Microphone"]').getBoundingClientRect();return{x:r.x+r.width/2,y:r.y+r.height/2}})()`, Date.now() + 3000);
await cdp.call("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [microphonePoint] }, host.sessionId, Date.now() + 3000);
await waitForSample(() => evaluate<boolean>(cdp!, host,
`!!document.querySelector('.lr-audio-volume-popover:popover-open')`, Date.now() + 3000), Boolean, 5000, 50);
result.stage = "microphone touch release and keyboard volume";
await cdp.call("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] }, host.sessionId, Date.now() + 3000);
await waitForSample(() => evaluate<boolean>(cdp!, host,
`!!document.querySelector('.lr-audio-volume-popover:popover-open')`, Date.now() + 3000), Boolean, 5000);
await ui(host, "focus", '.lr-audio-volume-popover:popover-open input[aria-label="Microphone volume"]');
await ui(host, "press", "End");
await ui(host, "press", "Escape");
await cdp.call("Emulation.setTouchEmulationEnabled", { enabled: false }, host.sessionId, Date.now() + 3000);
const afterVolume = await sample(host);
if (afterVolume.liveMicrophones !== 1 || afterVolume.voice?.publications.length !== 2 || !await evaluate<boolean>(cdp, host,
`!document.querySelector('.lr-audio-volume-popover:popover-open')&&!!document.querySelector('.lr-interaction-panel:popover-open')`, Date.now() + 3000)) {
throw new Error("Touch volume adjustment toggled the microphone or closed its containing voice window");
}
check("touch hold opens volume without muting; keyboard adjustment and Escape retain the voice window");
let leaf: Page | null = null;
if (!sfuOnly) {
leaf = await page(created.inviteUrl!, "Voice Listener");
await until(leaf, state => !!state.voice, "third normal Viewer authenticates");
await openVoice(leaf);
await button(leaf, "Join voice");
const relay = await sample(viewer);
await until(leaf, state => state.playing === 2 && state.energy > 0 && !!state.voice?.connections.some(connection =>
connection.parentPeerId === relay.peerId && !connection.pending && connection.receive.length === 2), "two speakers through normal Viewer relay");
check("two simultaneous publications decoded by a third Viewer through shared relay graph");
}
let legacyViewer: Page | null = null;
if (legacyOrigin) {
const invitation = new URL(created.inviteUrl!);
legacyViewer = await page(`${legacyOrigin}${invitation.pathname}${invitation.hash}`, "Older Viewer");
await until(legacyViewer, state => state.authentications === 1 && state.controls.includes("Interact"), "older Viewer joins before picture sharing");
}
result.stage = "voice-only topology";
for (const [index, participant] of [host, viewer].entries()) {
await button(participant, "Hide interaction window");
await button(participant, "Show connection topology");
await waitForSample(() => evaluate<boolean>(cdp!, participant, `(() => {
const state=window.__roomVoiceGate.voice, tree=document.getElementById('room-topology');
const expected=state?.members.filter(member=>member.peerId!==state.hostPeerId&&member.connected&&member.joined&&member.upstream.kind!=='none').length;
return expected>0&&tree?.querySelectorAll('.lr-route-edge:not(.is-pending)').length>=expected;
})()`, Date.now() + 3000), Boolean, 5000);
await ui(participant, "screenshot", join(OUTPUT, `${arm}-voice-topology-${index}.png`), "--full");
await inspectVoiceRoute(participant, `voice-room-details-${index}`);
await button(participant, "Hide connection topology");
await openVoice(participant);
}
check("Host and Viewer topology display connected voice routes before picture sharing");
await button(host, "Interact with Voice Viewer");
await waitForSample(() => evaluate<boolean>(cdp!, host, `(() => {
const menu=document.querySelector('.lr-quick-reactions'), roster=document.querySelector('.lr-voice-history');
return !!menu?.matches(':popover-open')&&menu.querySelectorAll('.lr-reaction-grid button').length===4&&
!!roster?.getClientRects().length&&!!document.querySelector('.lr-voice-toolbar')?.getClientRects().length;
})()`, Date.now() + 3000), Boolean, 5000);
await button(host, "Toss poop");
await waitForSample(() => evaluate<boolean>(cdp!, viewer,
`window.__roomVoiceGate.wire.some(message=>message.direction==='receive'&&message.type==='room-interaction'&&message.payload.kind==='reaction'&&message.payload.reaction==='poop'&&message.payload.targetPeerId===window.__roomVoiceGate.peerId)`,
Date.now() + 3000), Boolean, 5000);
await ui(viewer, "screenshot", join(OUTPUT, `${arm}-voice-reaction.png`));
check("voice participant picker sends a correctly targeted reaction to the other normal page");
await button(host, "Voice settings");
await button(host, "Back");
const beforeMute = await sample(host);
const oldPublication = beforeMute.voice!.publications.find(publication => publication.peerId === beforeMute.peerId)!.id;
const voiceConnections = beforeMute.voice!.connections.filter(connection => !connection.pending).map(connection => connection.connectionId).sort();
await button(host, "Microphone");
await until(host, state => state.liveMicrophones === 0 && state.voice?.publications.length === 1 &&
!!state.voice.members.find(member => member.peerId === state.peerId)?.joined, "microphone off retains voice membership");
await until(viewer, state => state.playing === 0 && state.voice?.publications.length === 1, "retired microphone removed from playback");
await button(host, "Microphone");
await until(host, state => state.liveMicrophones === 1 && state.voice?.publications.length === 2, "microphone restarts within joined channel");
const restarted = await until(viewer, state => state.playing === 1 && state.energy > 0 && state.voice?.publications.length === 2,
"replacement microphone produces decoded audio");
await until(viewer, state => state.samples > restarted.samples, "replacement microphone continues producing samples");
const afterMute = await sample(host);
if (afterMute.voice!.publications.find(publication => publication.peerId === afterMute.peerId)!.id === oldPublication ||
JSON.stringify(afterMute.voice!.connections.filter(connection => !connection.pending).map(connection => connection.connectionId).sort()) !== JSON.stringify(voiceConnections)) {
throw new Error("Microphone restart reused retired publication or replaced the room route");
}
if (leaf) await until(leaf, state => state.playing === 2 && state.voice?.publications.length === 2, "relay receives replacement microphone exactly once");
check("microphone restart replaces its publication without leaving voice, duplicating playback or replacing the route");
await memberButton(viewer, "Voice Host", "Listen to this person");
const muted = await sample(viewer);
await until(viewer, state => state.enabled && state.samples > muted.samples, "local mute retains received media");
if (leaf) {
const before = await sample(leaf);
await until(leaf, state => state.playing === 2 && state.samples > before.samples, "relay continues after local playback mute");
}
check("local mute retains enabled incoming track and media progress");
await button(viewer, "Listen to voice");
const deafened = await until(viewer, state => state.liveMicrophones === 1 && state.playing > 0 &&
state.mutedPlayback === state.playing && state.voice?.publications.length === 2, "pause listening preserves microphone");
await until(viewer, state => state.enabled && state.samples > deafened.samples, "deafen retains incoming media");
if (leaf) {
const before = await sample(leaf);
await until(leaf, state => state.playing === 2 && state.samples > before.samples, "relay still forwards both speakers while not listening");
}
await button(viewer, "Listen to voice");
const listening = await sample(viewer);
if (listening.liveMicrophones !== 1 || listening.mutedPlayback !== listening.playing) throw new Error("Resuming listening changed microphone or per-person mute");
await memberButton(viewer, "Voice Host", "Listen to this person");
await until(viewer, state => state.mutedPlayback === 0, "individual mute remains independent of deafen");
if (leaf) await until(leaf, state => state.playing === 2, "relay listeners regain both speakers");
check("local listening, microphone and per-person mute remain independent and preserve forwarding");
await button(host, "Voice settings");
const routesBeforeBitrate = (await sample(viewer)).voice!.connections.map(connection => connection.connectionId).sort();
await ui(host, "fill", '.lr-voice-settings input[name="audioBitrate"]', "96");
await ui(host, "press", "Enter");
for (const participant of [host, viewer, ...(leaf ? [leaf] : [])]) {
await until(participant, state => state.voice?.bitrate === 96_000, "room bitrate reaches each participant");
await waitForSample(() => evaluate<boolean>(cdp!, participant, `(() => {
const senders=window.__roomVoiceGate.peers.filter(peer=>peer.connectionState==='connected'&&
peer.getTransceivers().every(transceiver=>transceiver.receiver.track.kind==='audio'))
.flatMap(peer=>peer.getSenders()).filter(sender=>sender.track);
return senders.length===0||senders.every(sender=>sender.getParameters().encodings[0]?.maxBitrate===96000);
})()`, Date.now() + 3000), Boolean, 5000);
}
if (JSON.stringify((await sample(viewer)).voice!.connections.map(connection => connection.connectionId).sort()) !== JSON.stringify(routesBeforeBitrate)) {
throw new Error("Changing voice bitrate replaced the room route");
}
const afterBitrate = await sample(viewer);
await until(viewer, state => state.samples > afterBitrate.samples, "audio continues after bitrate change");
check("Host room bitrate applies live to source and relay senders without replacing the route");
await ui(host, "focus", '.lr-interaction-settings input[aria-label="Microphone volume"]');
await ui(host, "press", "End");
await button(host, "Back");
check("one room microphone setting surface supplies device, gain and speech processing");
const beforeVideo = await sample(viewer);
await button(host, "Hide interaction window");
await button(host, "Start sharing");
await ui(host, "find", "role", "tab", "click", "--name", "Camera", "--exact");
await ui(host, "click", 'button[aria-label^="Share camera:"]');
await until(host, state => state.text.includes("Stop sharing"), "camera sharing starts alongside voice");
await until(viewer, state => state.samples > beforeVideo.samples && state.voice?.publications.length === 2, "voice survives video start");
await button(host, "Sharing settings");
await ui(host, "find", "role", "tab", "click", "--name", "Sound", "--exact");
if (!await evaluate<boolean>(cdp, host,
`document.querySelector('button[role="switch"][aria-label^="Include my microphone"]')?.getAttribute('aria-checked')==='false'`,
Date.now() + 3000) || (await sample(host)).liveMicrophones !== 1) {
throw new Error("Source commentary started without an explicit action or replaced the room microphone");
}
const roomMicrophonePublication = (await sample(host)).voice!.publications.find(publication => publication.peerId === created.peerId)!.id;
const beforeCommentary = await sample(viewer);
const commentaryToggle = 'button[role="switch"][aria-label^="Include my microphone"]';
await ui(host, "click", commentaryToggle);
await until(host, state => state.liveMicrophones === 2 && state.voice?.publications.length === 2 &&
state.voice.publications.some(publication => publication.id === roomMicrophonePublication), "source commentary and room microphone run independently");
const commentaryReceived = await until(viewer, state => state.pictureAudioEnergy > beforeCommentary.pictureAudioEnergy &&
state.pictureAudioSamples > beforeCommentary.pictureAudioSamples && state.samples > beforeCommentary.samples,
"commentary arrives on shared-picture audio while room voice continues");
await until(viewer, state => state.pictureAudioSamples > commentaryReceived.pictureAudioSamples,
"shared commentary keeps producing decoded samples");
await ui(host, "screenshot", join(OUTPUT, `${arm}-source-commentary.png`), "--full");
await ui(host, "click", commentaryToggle);
await until(host, state => state.liveMicrophones === 1 && state.voice?.publications.length === 2 &&
state.voice.publications.some(publication => publication.id === roomMicrophonePublication), "turning commentary off retains the room microphone");
await ui(host, "click", commentaryToggle);
await until(host, state => state.liveMicrophones === 2, "commentary is active before stopping the share");
await button(host, "Hide sharing settings");
check("source commentary defaults off, sends shared audio alongside room voice, and can stop independently");
await cdp.call("Page.bringToFront", {}, host.sessionId, Date.now() + 3000);
// A stationary hover does not wake idle playback controls.
await ui(host, "hover", ".lr-top-right");
await ui(host, "hover", ".lr-tv video");
await waitForSample(() => evaluate<boolean>(cdp!, host, "!!document.querySelector('.lr-tv video')&&!document.querySelector('.lr-tv video').paused", Date.now() + 3000), Boolean, 5000);
if (!await evaluate<boolean>(cdp, host, "document.querySelector('.lr-tv video').muted", Date.now() + 3000)) throw new Error("Host preview started with sound");
await button(host, "Pause on this device");
const locallyPaused = await sample(viewer);
await until(viewer, state => state.videoFrames > locallyPaused.videoFrames && state.samples > locallyPaused.samples, "local Host pause preserves outgoing picture and voice");
await cdp.call("Page.bringToFront", {}, viewer.sessionId, Date.now() + 3000);
await cdp.call("Page.bringToFront", {}, host.sessionId, Date.now() + 3000);
if (!await evaluate<boolean>(cdp, host, "document.querySelector('.lr-tv video').paused", Date.now() + 3000)) throw new Error("Focus restored an explicitly paused Host preview");
await button(host, "Play");
check("Host local playback starts muted; pause and focus changes preserve sharing and voice");
if (legacyViewer) await until(legacyViewer, state => state.videoFrames > 0 && state.voice !== null
&& state.voice.bitrate === undefined && state.authentications === 1,
"published Viewer receives picture and voice state without the new bitrate field");
await button(host, "Stop sharing");
await until(host, state => state.liveMicrophones === 1 && state.voice?.publications.length === 2 &&
state.voice.publications.some(publication => publication.id === roomMicrophonePublication), "stopping sharing retires only commentary capture");
const stopped = await sample(viewer);
await until(viewer, state => state.samples > stopped.samples && state.voice?.publications.length === 2, "voice survives video stop");
for (const [index, participant] of [host, viewer].entries()) {
if ((await sample(participant)).controls.includes("Hide interaction window")) await button(participant, "Hide interaction window");
await button(participant, "Show connection topology");
await inspectVoiceRoute(participant, `stopped-picture-voice-details-${index}`);
await button(participant, "Hide connection topology");
if (participant === viewer) await openVoice(participant);
}
check("stopping picture preserves voice couch state, topology and voice-only connection details");
await button(host, "Start sharing");
await ui(host, "find", "role", "tab", "click", "--name", "Camera", "--exact");
await ui(host, "click", 'button[aria-label^="Share camera:"]');
await until(host, state => state.text.includes("Stop sharing"), "camera sharing restarts");
await until(viewer, state => state.videoFrames > 0, "Viewer decodes picture alongside room voice");
await button(host, "Sharing settings");
await ui(host, "find", "role", "tab", "click", "--name", "Sound", "--exact");
if (!await evaluate<boolean>(cdp, host, `document.querySelector(${JSON.stringify(commentaryToggle)})?.getAttribute('aria-checked')==='false'`,
Date.now() + 3000) || (await sample(host)).liveMicrophones !== 1) throw new Error("A new share restored commentary without an explicit action");
await button(host, "Hide sharing settings");
check("stopping sharing releases commentary only; the next share starts with commentary off");
await button(host, "Interact");
check("camera share start/stop/restart preserves room microphones and voice");
for (const width of [320, 390, 1280]) {
await cdp.call("Emulation.setDeviceMetricsOverride", { width, height: 840, deviceScaleFactor: 1, mobile: width < 500 }, host.sessionId, Date.now() + 5000);
await ui(host, "screenshot", join(OUTPUT, `${arm}-voice-${width}.png`));
}
check("mobile and desktop voice screenshots");
await button(host, "Open in always-on-top window");
await waitForSample(() => evaluate<boolean>(cdp!, host,
"!!documentPictureInPicture.window?.document.querySelector('.lr-room-voice')", Date.now() + 3000), Boolean, 5000);
const detached = await sample(viewer);
await until(viewer, state => state.samples > detached.samples, "detached panel preserves voice");
const { targetInfos } = await cdp.call<{ targetInfos: { targetId: string; title: string }[] }>("Target.getTargets", {}, undefined, Date.now() + 5000);
const popup = targetInfos.find(target => target.title === "Interactions · Piik");
if (!popup) throw new Error("Detached interaction window target missing");
await ui(host, "--pin-tab", "tab", popup.targetId);
await ui(host, "screenshot", join(OUTPUT, `${arm}-voice-detached.png`));
await ui(host, "--pin-tab", "tab", host.targetId);
await evaluate(cdp, host, "documentPictureInPicture.window.close()", Date.now() + 5000);
await until(host, state => state.controls.includes("Microphone") && state.liveMicrophones === 1, "panel returns from detached window");
check("actual detached shared voice window preserves capture and audio");
await button(host, "Close room voice");
for (const participant of [host, viewer, ...(leaf ? [leaf] : [])]) {
await until(participant, state => !state.voice?.enabled && !state.liveMicrophones && !state.playing && !state.voice?.connections.length,
"Host closes room voice and retires every participant's capture and playback");
}
const closedVoice = await sample(viewer);
await until(viewer, state => state.videoFrames > closedVoice.videoFrames, "picture continues after channel close");
if (legacyViewer) {
const legacyBefore = await sample(legacyViewer);
await until(legacyViewer, state => state.videoFrames > legacyBefore.videoFrames && state.voice?.enabled === false
&& state.voice.bitrate === undefined && state.authentications === 1,
"older Viewer retains picture after voice closes");
check("published Web keeps picture and existing voice state without the unrequested bitrate field");
}
await button(host, "Open room voice");
await until(viewer, state => !!state.voice?.enabled, "Host reopens room voice");
if ((await sample(viewer)).liveMicrophones || (await sample(viewer)).voice?.members.find(member => member.peerId === (beforeVideo.peerId))?.joined) {
throw new Error("Reopening restored Viewer membership or microphone automatically");
}
await button(viewer, "Join voice");
await until(viewer, state => !!state.voice?.members.find(member => member.peerId === state.peerId)?.joined, "Viewer explicitly rejoins");
await button(host, "Microphone");
await button(viewer, "Microphone");
await until(viewer, state => state.liveMicrophones === 1 && state.voice?.publications.length === 2, "explicit microphone restart");
const restartedVoice = await sample(viewer);
await until(viewer, state => state.samples > restartedVoice.samples && state.videoFrames > restartedVoice.videoFrames, "voice activates on an existing picture route");
check("closing voice preserves picture; reopening adds voice to the existing picture route");
for (const participant of [viewer, host]) {
await evaluate(cdp, participant, `(() => {
const connection=window.__roomVoiceGate.peers.find(peer=>peer.connectionState==='connected'&&peer.getTransceivers().length&&
peer.getTransceivers().every(transceiver=>transceiver.receiver.track.kind==='audio'));
if(!connection)throw new Error('No independent voice transport');
connection.close();connection.dispatchEvent(new Event('connectionstatechange'));
})()`, Date.now() + 5000);
const failedVoice = await sample(viewer);
await until(viewer, state => state.videoFrames > failedVoice.videoFrames, "voice transport failure leaves picture progressing");
await until(viewer, state => state.energy > 0 && state.playing > 0 && !!state.voice?.connections.some(connection => !connection.pending), "voice transport recovers through room route owner");
}
check("independent voice transport failure retains healthy picture and uses ordinary room recovery");
const beforeDisconnect = await sample(viewer);
await evaluate(cdp, viewer, "window.__roomVoiceGate.sockets.at(-1).close()", Date.now() + 5000);
await until(viewer, state => !state.liveMicrophones, "session loss stops capture");
await until(viewer, state => state.authentications > beforeDisconnect.authentications &&
!!state.voice?.members.find(member => member.peerId === state.peerId)?.connected, "normal signaling reconnect");
if ((await sample(viewer)).liveMicrophones) throw new Error("Reconnect restored microphone automatically");
check("closing room voice retires all audio; reopening and reconnect never restart microphones");
if (leaf) {
const departedId = (await sample(leaf)).peerId;
await cdp.call("Target.closeTarget", { targetId: leaf.targetId }, undefined, Date.now() + 3000);
await waitForSample(() => evaluate<boolean>(cdp!, host,
`!document.querySelector('.lr-voice-member[data-room-peer="${departedId}"]')`, Date.now() + 3000), Boolean, 5000);
check("departed voice member leaves the visible roster without waiting for reconnect grace");
}
result.passed = true;
} catch (error) {
result.error = redact(error instanceof Error ? error.message : String(error));
if (pages[0]) try {
await evaluate(cdp!, pages[0], `window.__roomVoiceGate.sockets.at(-1).send(JSON.stringify({type:'request-route-diagnostic'}))`, Date.now() + 3000);
await new Promise(resolveWait => setTimeout(resolveWait, 150));
} catch { /* disconnected sessions cannot request diagnostics */ }
for (const [index, page] of pages.entries()) try {
result.diagnostics.push(await sample(page));
await ui(page, "screenshot", join(OUTPUT, `${arm}-failure-${index}.png`));
} catch { /* page may have closed */ }
process.stderr.write(`${result.error}\n${redact(serverLog)}\n`);
} finally {
for (const page of pages) try { await ui(page, "close"); } catch { /* cleanupRun owns remaining Chrome */ }
if (legacyVite) await legacyVite.close();
if (vite) await vite.close();
const cleanup = await cleanupRun({ cdp, native: null, chrome, server, profile, ports: [serverPort, vitePort, debugPort, ...(legacyPort ? [legacyPort] : [])] });
result.cleanup = cleanup;
if (!Object.values(cleanup).every(Boolean)) {
result.passed = false;
result.error ??= "Gate resource cleanup failed";
}
await writeFile(join(OUTPUT, `${arm}-server.log`), redact(serverLog));
await writeFile(join(OUTPUT, `${arm}-report.json`), `${redact(JSON.stringify(result, null, 2))}\n`);
}
if (!result.passed) process.exitCode = 1;
}
await main();