mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-10-06 03:38:23 +00:00
feat(nests): T16 Phase 4.A+B — browser-side cross-stack interop scaffold + I1 forward
Lands the bun + Playwright + headless Chromium harness for the T16 cross-stack interop suite, parallel to the existing Rust hang-listen tier. New top-level `nestsClient-browser-interop/` directory with `@moq/lite` + `@moq/hang` 0.2.x pinned, a bun static + WebSocket back-channel server, and a Playwright runner that opens `listen.html` against the same `NativeMoqRelayHarness` moq-relay subprocess the Rust scenarios use. Kotlin side: `PlaywrightDriver` shells out to `bun x playwright test`, forwards the relay URL + leaf-cert SHA-256 (captured via a custom `CertCapturingValidator` during the speaker's QUIC handshake), and reads back Float32 LE PCM frames from a tempfile the bun WS server appends to. `BrowserInteropTest` ships I1 forward — Amethyst Kotlin speaker → Chromium `@moq/lite` listener, asserting FFT 440 Hz on the captured tail. Why pin via `serverCertificateHashes` instead of `--ignore-certificate-errors`: Chromium's flag does NOT bypass QUIC cert validation (crbug.com/1190655). `serverCertificateHashes` is the supported path; moq-relay's `--tls-generate` produces a 14-day ECDSA P-256 cert that satisfies the spec. Two Gradle tasks added: `interopBuildBrowserHarness` (bun install + bun build → dist/) and `interopInstallPlaywrightChromium` (skipped when `PLAYWRIGHT_BROWSERS_PATH` already has a chromium build, as on the agent runner). Verification: - `./gradlew :nestsClient:jvmTest --tests com.vitorpamplona.nestsclient.interop.native.BrowserInteropTest -DnestsHangInterop=true -DnestsBrowserInterop=true` green. - I1 forward asserts ≥ 1 s of decoded PCM with 440 Hz FFT peak. Looser sample-count bound than the hang-tier I1 because Chromium cold-launch + WebTransport handshake (3–5 s) + the publisher's `framesPerGroup = 5` per-subscriber cache cliff means the page captures only the broadcast tail. Phase 4.C (I2/I3/I4/I13/I14/I15) and 4.D (CI) are separate follow-up commits per the plan's per-scenario commit guidance. See: nestsClient/plans/2026-05-06-phase4-browser-harness.md https://claude.ai/code/session_01ERJPUYfdLPwZ99pr5EcEcV
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
node_modules/
|
||||
dist/
|
||||
test-results/
|
||||
playwright-report/
|
||||
.bun/
|
||||
@@ -0,0 +1,31 @@
|
||||
# Pinned upstream npm package versions for the browser-side cross-stack
|
||||
# interop harness (Phase 4 of T16).
|
||||
#
|
||||
# These versions are what `nestsClient-browser-interop/package.json` pins
|
||||
# and what the bun build resolves at install time. Bumping requires
|
||||
# touching package.json + bun.lockb + this file together so a silent
|
||||
# upstream rev change can't mask a regression.
|
||||
#
|
||||
# See: nestsClient/plans/2026-05-06-phase4-browser-harness.md
|
||||
#
|
||||
# Source: https://github.com/kixelated/moq , workspace published to npm
|
||||
# under the @moq/* scope. The `@moq/lite` 0.2.x line implements
|
||||
# `moq-lite-03` (see /tmp/moq/js/lite/src/lite/), matching the pin in
|
||||
# nestsClient/tests/hang-interop/REV (KIXELATED_MOQ_GIT_REV).
|
||||
|
||||
# Browser listener: builds Watch.Broadcast on top of @moq/lite +
|
||||
# @moq/hang. We use @moq/lite + @moq/hang directly for the harness path
|
||||
# (closer to nestsClient's own moq-lite stack) but keep @moq/watch
|
||||
# pinned in case a future scenario wants the higher-level reactive
|
||||
# Broadcast wrapper.
|
||||
MOQ_WATCH_VERSION=0.2.10
|
||||
|
||||
# Browser publisher: same story for @moq/publish.
|
||||
MOQ_PUBLISH_VERSION=0.2.6
|
||||
|
||||
# Lower-level moq-lite-03 client + hang catalog/container.
|
||||
MOQ_LITE_VERSION=0.2.2
|
||||
MOQ_HANG_VERSION=0.2.4
|
||||
|
||||
# Playwright Chromium driver.
|
||||
PLAYWRIGHT_VERSION=1.56.1
|
||||
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"configVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "nests-browser-interop",
|
||||
"dependencies": {
|
||||
"@moq/hang": "0.2.4",
|
||||
"@moq/lite": "0.2.2",
|
||||
"@moq/publish": "0.2.6",
|
||||
"@moq/watch": "0.2.10",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "1.56.1",
|
||||
"@types/bun": "latest",
|
||||
"typescript": "^5.6.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@kixelated/libavjs-webcodecs-polyfill": ["@kixelated/libavjs-webcodecs-polyfill@0.5.5", "", { "dependencies": { "@libav.js/types": "^6.7.7", "@ungap/global-this": "^0.4.4" } }, "sha512-Q1zgnTMMQ2F7IE9ylx3C1XzVbg5vYN18jiDINO5U3kNPBOHdYuUlJsMhtBoqr1M6ocLtoiqdHmLs7tHFgrw5KA=="],
|
||||
|
||||
"@libav.js/types": ["@libav.js/types@6.8.8", "", {}, "sha512-Lbik/0Q3x2R8cI7mOtRgt+nUWLqGXh7UinMndmpdXSDY4YEjYyVUDsq6fxkuriL78+LCYx8frZIN1r+oDsvYCQ=="],
|
||||
|
||||
"@libav.js/variant-opus-af": ["@libav.js/variant-opus-af@6.8.8", "", {}, "sha512-8KBQyA8n5goN7lyctOaPxpcx7dapOgqKh8dWW/NAcl87AgM/WoUGSex3fFc46oCtTHYrUKEm1OmZUrtkt3Q56A=="],
|
||||
|
||||
"@moq/hang": ["@moq/hang@0.2.4", "", { "dependencies": { "@kixelated/libavjs-webcodecs-polyfill": "^0.5.5", "@libav.js/variant-opus-af": "^6.8.8", "@moq/lite": "^0.2.2", "@moq/signals": "^0.1.6", "@svta/cml-iso-bmff": "^1.0.0-alpha.9", "zod": "^4.1.5" } }, "sha512-I7OzutII+Sp5oWKd33t6b1SSY9Tu2dpu6pEaUp7CAKzNRpIaE7O6DhWBsBlcGAMTuDj/zA3CkR3iGx7WW2WW6w=="],
|
||||
|
||||
"@moq/lite": ["@moq/lite@0.2.2", "", { "dependencies": { "@moq/qmux": "^0.0.6", "@moq/signals": "^0.1.6", "async-mutex": "^0.5.0" }, "peerDependencies": { "zod": "^4.0.0" } }, "sha512-o5X4qQlfhO8xWcWwpYsEEWWHr66SIPQKFY++ZxgMYhU+77rTfe1vWgomYjqoQcCZT3flRY2iTRLJriHgyDX/gA=="],
|
||||
|
||||
"@moq/msf": ["@moq/msf@0.1.0", "", { "dependencies": { "@moq/lite": "^0.2.1", "zod": "^4.1.5" } }, "sha512-5Y/RcxxofBXQSdy6IexzB8s2rpRI7xFiut1Zh6WO6hjNNqM+WKPPt+CTGKqnnnx/vhecgKrvpHnN228Zc0bakg=="],
|
||||
|
||||
"@moq/publish": ["@moq/publish@0.2.6", "", { "dependencies": { "@moq/hang": "^0.2.4", "@moq/lite": "^0.2.2", "@moq/signals": "^0.1.6", "@moq/ui-core": "^0.1.0" } }, "sha512-cAHt8ZRMKOZh/yd1CX8wS6N5XLCGxzsKB/hU7R9PtmlJ40U3hDKokMGSd+jYWstDOgppoMwr4qU//yjDyeOiNw=="],
|
||||
|
||||
"@moq/qmux": ["@moq/qmux@0.0.6", "", {}, "sha512-ISuGz05lUvf1hzHW3Aw3VnsGRJe1w9Qdog3LQ66KS+l+5mzQsPANvW8yOioEe1Z9dJO2G3sAHoGPnzwnsY9SIQ=="],
|
||||
|
||||
"@moq/signals": ["@moq/signals@0.1.6", "", { "peerDependencies": { "@types/react": "^19.1.8", "react": "^19.0.0", "solid-js": "^1.9.7" }, "optionalPeers": ["@types/react", "react", "solid-js"] }, "sha512-ic7ttiz6dHXOPoVAfhz4K6LGT2LWdDGTi1x2u8sYSGZ5nOKGWfqDkwYcGvCPlcVQetn3PaeXYSPFiMAC6RO3tQ=="],
|
||||
|
||||
"@moq/ui-core": ["@moq/ui-core@0.1.0", "", { "peerDependencies": { "@moq/signals": "^0.1.2" } }, "sha512-DJNBpUNQDyh7Tou324fbJ5/pT08UPghH3OxcVdLEo9IQeX//8NiEzJwcX7iuacR32nzgdiBThIbIpeFa60U3/g=="],
|
||||
|
||||
"@moq/watch": ["@moq/watch@0.2.10", "", { "dependencies": { "@moq/hang": "^0.2.4", "@moq/lite": "^0.2.2", "@moq/msf": "^0.1.0", "@moq/signals": "^0.1.6", "@moq/ui-core": "^0.1.0" } }, "sha512-uLVwdtx0XIvJ20c1dYJ5NIVLXBA/cbTNzvM1mujvYLVKGQnwPkrrllEFlNpWfwV9SN1Kb8BnDvA6LLtYTFAhkQ=="],
|
||||
|
||||
"@playwright/test": ["@playwright/test@1.56.1", "", { "dependencies": { "playwright": "1.56.1" }, "bin": { "playwright": "cli.js" } }, "sha512-vSMYtL/zOcFpvJCW71Q/OEGQb7KYBPAdKh35WNSkaZA75JlAO8ED8UN6GUNTm3drWomcbcqRPFqQbLae8yBTdg=="],
|
||||
|
||||
"@svta/cml-iso-bmff": ["@svta/cml-iso-bmff@1.0.1", "", { "peerDependencies": { "@svta/cml-utils": "1.4.0" } }, "sha512-MOhATJYQ6cVrIcoY3nj8p/vGYDpG3wjQIIhBPHNt9yjFijdwFdBNqdZbCXv3aFhRjdx5Saca5TkgNJusKhnI/w=="],
|
||||
|
||||
"@svta/cml-utils": ["@svta/cml-utils@1.4.0", "", {}, "sha512-vNtHtv/z+9I9ysxFwNrgwxic1oceVPr8TpcpV/NA1l8Gy4phynwtOppkCIBB+PmoyKDcqE4lO85g+lfsuSTBBA=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.3.13", "", { "dependencies": { "bun-types": "1.3.13" } }, "sha512-9fqXWk5YIHGGnUau9TEi+qdlTYDAnOj+xLCmSTwXfAIqXr2x4tytJb43E9uCvt09zJURKXwAtkoH4nLQfzeTXw=="],
|
||||
|
||||
"@types/node": ["@types/node@25.6.0", "", { "dependencies": { "undici-types": "~7.19.0" } }, "sha512-+qIYRKdNYJwY3vRCZMdJbPLJAtGjQBudzZzdzwQYkEPQd+PJGixUL5QfvCLDaULoLv+RhT3LDkwEfKaAkgSmNQ=="],
|
||||
|
||||
"@ungap/global-this": ["@ungap/global-this@0.4.4", "", {}, "sha512-mHkm6FvepJECMNthFuIgpAEFmPOk71UyXuIxYfjytvFTnSDBIz7jmViO+LfHI/AjrazWije0PnSP3+/NlwzqtA=="],
|
||||
|
||||
"async-mutex": ["async-mutex@0.5.0", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA=="],
|
||||
|
||||
"bun-types": ["bun-types@1.3.13", "", { "dependencies": { "@types/node": "*" } }, "sha512-QXKeHLlOLqQX9LgYaHJfzdBaV21T63HhFJnvuRCcjZiaUDpbs5ED1MgxbMra71CsryN/1dAoXuJJJwIv/2drVA=="],
|
||||
|
||||
"fsevents": ["fsevents@2.3.2", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="],
|
||||
|
||||
"playwright": ["playwright@1.56.1", "", { "dependencies": { "playwright-core": "1.56.1" }, "optionalDependencies": { "fsevents": "2.3.2" }, "bin": { "playwright": "cli.js" } }, "sha512-aFi5B0WovBHTEvpM3DzXTUaeN6eN0qWnTkKx4NQaH4Wvcmc153PdaY2UBdSYKaGYw+UyWXSVyxDUg5DoPEttjw=="],
|
||||
|
||||
"playwright-core": ["playwright-core@1.56.1", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-hutraynyn31F+Bifme+Ps9Vq59hKuUCz7H1kDOcBs+2oGguKkWTU50bBWrtz34OUWmIwpBTWDxaRPXrIXkgvmQ=="],
|
||||
|
||||
"tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
|
||||
|
||||
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
||||
|
||||
"undici-types": ["undici-types@7.19.2", "", {}, "sha512-qYVnV5OEm2AW8cJMCpdV20CDyaN3g0AjDlOGf1OW4iaDEx8MwdtChUp4zu4H0VP3nDRF/8RKWH+IPp9uW0YGZg=="],
|
||||
|
||||
"zod": ["zod@4.4.3", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="],
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "nests-browser-interop",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Phase 4 (T16) browser-side cross-stack interop harness — headless Chromium running @moq/lite + @moq/hang against the same NativeMoqRelayHarness moq-relay subprocess that drives HangInteropTest. Lands behind -DnestsBrowserInterop=true.",
|
||||
"scripts": {
|
||||
"build": "bun build src/listen.ts src/publish.ts --outdir dist --target browser && cp src/listen.html src/publish.html src/pcm-tap-worklet.js dist/",
|
||||
"serve": "bun run src/server.ts",
|
||||
"playwright": "playwright test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@moq/hang": "0.2.4",
|
||||
"@moq/lite": "0.2.2",
|
||||
"@moq/publish": "0.2.6",
|
||||
"@moq/watch": "0.2.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "1.56.1",
|
||||
"@types/bun": "latest",
|
||||
"typescript": "^5.6.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { defineConfig } from "@playwright/test";
|
||||
|
||||
// Phase 4 (T16) browser-interop Playwright config.
|
||||
//
|
||||
// One-off Chromium spawn per Kotlin test. We disable the default test
|
||||
// projects + reporters (the runner is invoked headlessly from the
|
||||
// PlaywrightDriver Kotlin shim with `--reporter list` for stdout
|
||||
// streaming).
|
||||
//
|
||||
// Chromium flags:
|
||||
// --enable-quic — required for WebTransport.
|
||||
// --ignore-certificate-errors — accept the self-signed
|
||||
// cert moq-relay generates
|
||||
// with --tls-generate.
|
||||
// --enable-features=AutoplayPolicy=NoUserGestureRequired
|
||||
// — let AudioContext.resume()
|
||||
// succeed without a user
|
||||
// gesture (we're headless).
|
||||
// --enable-blink-features=WebTransport
|
||||
// — defensively re-enable in
|
||||
// case the build disables
|
||||
// the blink feature flag
|
||||
// by default.
|
||||
|
||||
export default defineConfig({
|
||||
testDir: "./tests",
|
||||
fullyParallel: false,
|
||||
workers: 1,
|
||||
forbidOnly: !!process.env.CI,
|
||||
retries: 0,
|
||||
reporter: process.env.PLAYWRIGHT_REPORTER ?? "list",
|
||||
timeout: 120_000,
|
||||
use: {
|
||||
headless: true,
|
||||
trace: "off",
|
||||
video: "off",
|
||||
screenshot: "off",
|
||||
launchOptions: {
|
||||
args: [
|
||||
"--enable-quic",
|
||||
"--ignore-certificate-errors",
|
||||
"--enable-features=AutoplayPolicy=NoUserGestureRequired",
|
||||
"--enable-blink-features=WebTransport",
|
||||
// Disable network sandbox so WebTransport over loopback
|
||||
// doesn't trip the network service sandbox in headless.
|
||||
"--disable-features=IsolateOrigins,site-per-process",
|
||||
],
|
||||
},
|
||||
},
|
||||
projects: [
|
||||
{
|
||||
name: "chromium",
|
||||
use: { browserName: "chromium" },
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>nests browser-interop listener</title>
|
||||
<style>
|
||||
body { font: 14px monospace; padding: 8px; }
|
||||
body[data-state="error"] { background: #fdd; }
|
||||
body[data-state="done"] { background: #dfd; }
|
||||
</style>
|
||||
</head>
|
||||
<body data-state="init">
|
||||
<h1>nests-browser-interop / listen</h1>
|
||||
<div id="status">init</div>
|
||||
<script type="module" src="./listen.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,228 @@
|
||||
// Phase 4 (T16) browser-listener harness. Connects to the
|
||||
// `NativeMoqRelayHarness` moq-relay subprocess via WebTransport, subscribes
|
||||
// to the `<broadcast>/audio/data` track produced by the Amethyst Kotlin
|
||||
// speaker, decodes each Opus packet via WebCodecs AudioDecoder, and posts
|
||||
// the resulting Float32 PCM samples back to the bun WS server (`ws://`),
|
||||
// which appends them to a file on disk for the Kotlin test to read.
|
||||
//
|
||||
// Reads its parameters from `location.search`:
|
||||
//
|
||||
// relay — the relay's WebTransport URL,
|
||||
// e.g. `https://127.0.0.1:43219/nests/<kind>:<host>:<room>?jwt=`.
|
||||
// Pass the FULL connection target (path + query) — Amethyst's
|
||||
// nests namespace is part of the relay path per `NestsConnect.kt`.
|
||||
// broadcast — the publisher's moq-lite broadcast path
|
||||
// (= `speakerPubkeyHex` per `MoqLiteNestsSpeaker.kt`).
|
||||
// track — the audio track name. Defaults to `audio/data`.
|
||||
// wsPort — the bun WS back-channel port; we POST PCM here.
|
||||
// duration — broadcast capture window in seconds.
|
||||
//
|
||||
// Mirrors the data path of `kixelated/moq` `js/watch/src/audio/decoder.ts`
|
||||
// (`#runLegacyDecoder`) with the `@moq/hang` `Container.Legacy.Format` consumer
|
||||
// and the WebCodecs AudioDecoder warmup-skip semantics. Verbatim matching
|
||||
// the watcher's per-frame behaviour is what catches a Chromium-side
|
||||
// regression that wire-byte tests can't see.
|
||||
|
||||
import * as Moq from "@moq/lite";
|
||||
import * as Container from "@moq/hang/container";
|
||||
import { PRIORITY as CATALOG_PRIORITY } from "@moq/hang/catalog";
|
||||
|
||||
const params = new URLSearchParams(location.search);
|
||||
const relayParam = params.get("relay");
|
||||
const broadcastParam = params.get("broadcast");
|
||||
const trackParam = params.get("track") ?? "audio/data";
|
||||
const wsPort = Number(params.get("wsPort") ?? "0");
|
||||
const durationSec = Number(params.get("duration") ?? "5");
|
||||
const certSha256B64 = params.get("certSha256"); // Base64 SHA-256 of leaf DER cert.
|
||||
|
||||
function required(v: string | null, name: string): string {
|
||||
if (!v) throw new Error(`listen.html: missing ?${name}=`);
|
||||
return v;
|
||||
}
|
||||
const relayUrlString = required(relayParam, "relay");
|
||||
const broadcastName = required(broadcastParam, "broadcast");
|
||||
if (!wsPort) throw new Error("listen.html: missing ?wsPort=");
|
||||
|
||||
const status = (msg: string) => {
|
||||
const el = document.getElementById("status");
|
||||
if (el) el.textContent = msg;
|
||||
console.log("[listen]", msg);
|
||||
};
|
||||
|
||||
const fail = (msg: string) => {
|
||||
status(`ERROR: ${msg}`);
|
||||
document.body.dataset.state = "error";
|
||||
throw new Error(msg);
|
||||
};
|
||||
|
||||
async function main() {
|
||||
// -- WS back-channel ------------------------------------------------
|
||||
// The bun server appends every binary message we send to a PCM file
|
||||
// on disk. We open it BEFORE the WebTransport so the very first frame
|
||||
// (which decodes via WebCodecs after Container.Legacy strips the 2-byte
|
||||
// timestamp) is captured even if it arrives before the page reaches
|
||||
// its `done` state.
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${wsPort}/pcm`);
|
||||
ws.binaryType = "arraybuffer";
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
ws.addEventListener("open", () => resolve(), { once: true });
|
||||
ws.addEventListener("error", () => reject(new Error("ws connect failed")), { once: true });
|
||||
});
|
||||
status("ws connected");
|
||||
|
||||
const sendPcm = (chunk: Float32Array) => {
|
||||
// Float32 LE matches the format hang-listen writes; the Kotlin
|
||||
// test reads it via `readFloat32Pcm`.
|
||||
if (ws.readyState === WebSocket.OPEN) ws.send(chunk.buffer);
|
||||
};
|
||||
const sendDone = () => {
|
||||
if (ws.readyState === WebSocket.OPEN) ws.send("done");
|
||||
};
|
||||
|
||||
// -- Connect to the relay ------------------------------------------
|
||||
// `relayParam` already includes the namespace path + ?jwt=… query
|
||||
// (built by `buildRelayConnectTarget` in NestsConnect.kt). We pass it
|
||||
// straight to `Connection.connect` which feeds it to `new WebTransport(url)`
|
||||
// verbatim. Self-signed cert pinning is via Chromium's
|
||||
// `--ignore-certificate-errors` flag — we do NOT compute a SHA-256 hash
|
||||
// since the relay's auto-generated cert isn't deterministic.
|
||||
const relayUrl = new URL(relayUrlString);
|
||||
status(`connecting to ${relayUrl.toString()}`);
|
||||
// If the test driver passed a leaf-cert SHA-256, pin it via
|
||||
// `serverCertificateHashes`. Chromium's `--ignore-certificate-errors`
|
||||
// does NOT bypass QUIC cert validation (crbug.com/1190655), so this
|
||||
// is the supported path for self-signed test certs over WebTransport.
|
||||
// The hash is base64 — convert to a Uint8Array. Fail loudly if the
|
||||
// hash is malformed; falling back to no-pin would just produce a
|
||||
// QUIC_TLS_CERTIFICATE_UNKNOWN error one round-trip later.
|
||||
const webtransportOpts: WebTransportOptions = {};
|
||||
if (certSha256B64) {
|
||||
const raw = Uint8Array.from(atob(certSha256B64), (c) => c.charCodeAt(0));
|
||||
webtransportOpts.serverCertificateHashes = [
|
||||
{ algorithm: "sha-256", value: raw },
|
||||
];
|
||||
}
|
||||
const conn = await Moq.Connection.connect(relayUrl, {
|
||||
// Disable the WebSocket fallback — the harness relay only speaks QUIC.
|
||||
websocket: { enabled: false },
|
||||
webtransport: webtransportOpts,
|
||||
});
|
||||
status(`connected, alpn=${conn.version}`);
|
||||
|
||||
// Expose for Playwright to read post-hoc.
|
||||
(window as any).__moqVersion = conn.version;
|
||||
|
||||
// -- Subscribe to the audio track ----------------------------------
|
||||
const broadcastPath = Moq.Path.from(broadcastName);
|
||||
const broadcast = conn.consume(broadcastPath);
|
||||
const track = broadcast.subscribe(trackParam, CATALOG_PRIORITY.audio);
|
||||
status(`subscribed broadcast=${broadcastName} track=${trackParam}`);
|
||||
|
||||
// The hang Container.Legacy.Consumer strips the Varint-encoded
|
||||
// timestamp prefix (per `kixelated/moq/js/hang/src/container/legacy.ts`)
|
||||
// and yields an Opus packet per `next()`. Mirrors the data path the
|
||||
// @moq/watch decoder uses internally for `container.kind = "legacy"`
|
||||
// catalogs (the kind Amethyst publishes via `MoqLiteHangCatalog.opus48k`).
|
||||
const consumer = new Container.Legacy.Consumer(track, {
|
||||
// Tight latency — the harness runs over loopback, no jitter.
|
||||
// Pass a literal Time.Milli (number); the consumer accepts it directly.
|
||||
latency: 100 as any,
|
||||
});
|
||||
|
||||
// -- WebCodecs AudioDecoder ----------------------------------------
|
||||
const sampleRate = 48_000;
|
||||
const numberOfChannels = 1; // overwritten by catalog if available; default mono
|
||||
let warmed = 0;
|
||||
|
||||
const decoder = new AudioDecoder({
|
||||
output: (data: AudioData) => {
|
||||
warmed++;
|
||||
if (warmed <= 3) {
|
||||
// Mirror @moq/watch's 3-frame WebCodecs warmup skip.
|
||||
data.close();
|
||||
return;
|
||||
}
|
||||
const channels = data.numberOfChannels;
|
||||
const frames = data.numberOfFrames;
|
||||
// Interleave channels into a single Float32 buffer (Float32 LE,
|
||||
// matching hang-listen's output format). Mono → just one plane.
|
||||
if (channels === 1) {
|
||||
const buf = new Float32Array(frames);
|
||||
data.copyTo(buf, { format: "f32-planar", planeIndex: 0 });
|
||||
sendPcm(buf);
|
||||
} else {
|
||||
const planes: Float32Array[] = [];
|
||||
for (let c = 0; c < channels; c++) {
|
||||
const p = new Float32Array(frames);
|
||||
data.copyTo(p, { format: "f32-planar", planeIndex: c });
|
||||
planes.push(p);
|
||||
}
|
||||
const interleaved = new Float32Array(frames * channels);
|
||||
for (let f = 0; f < frames; f++) {
|
||||
for (let c = 0; c < channels; c++) {
|
||||
interleaved[f * channels + c] = planes[c][f];
|
||||
}
|
||||
}
|
||||
sendPcm(interleaved);
|
||||
}
|
||||
data.close();
|
||||
},
|
||||
error: (err) => console.error("[listen] AudioDecoder", err),
|
||||
});
|
||||
|
||||
decoder.configure({
|
||||
codec: "opus",
|
||||
sampleRate,
|
||||
numberOfChannels,
|
||||
// No description for Opus per @moq/watch decoder.ts comment:
|
||||
// "Opus in CMAF uses raw packets; dOps is not a valid OGG header".
|
||||
});
|
||||
|
||||
// -- Frame pump -----------------------------------------------------
|
||||
const deadline = performance.now() + durationSec * 1000;
|
||||
let framesDecoded = 0;
|
||||
document.body.dataset.state = "playing";
|
||||
|
||||
while (performance.now() < deadline) {
|
||||
const next = await Promise.race([
|
||||
consumer.next(),
|
||||
new Promise<undefined>((r) =>
|
||||
setTimeout(() => r(undefined), Math.max(50, deadline - performance.now())),
|
||||
),
|
||||
]);
|
||||
if (!next) break;
|
||||
const { frame } = next;
|
||||
if (!frame) continue;
|
||||
|
||||
framesDecoded++;
|
||||
if (decoder.state === "closed") break;
|
||||
decoder.decode(
|
||||
new EncodedAudioChunk({
|
||||
type: frame.keyframe ? "key" : "delta",
|
||||
data: frame.data,
|
||||
timestamp: frame.timestamp,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
status(`done, frames=${framesDecoded}`);
|
||||
(window as any).__framesDecoded = framesDecoded;
|
||||
|
||||
// Flush any pending decoder output, then signal the WS server we're done.
|
||||
try {
|
||||
await decoder.flush();
|
||||
} catch (e) {
|
||||
console.warn("[listen] flush:", e);
|
||||
}
|
||||
if (decoder.state !== "closed") decoder.close();
|
||||
consumer.close();
|
||||
sendDone();
|
||||
|
||||
document.body.dataset.state = "done";
|
||||
status(`done. frames=${framesDecoded}`);
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error("[listen] fatal:", e);
|
||||
fail(String(e?.stack ?? e));
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>nests browser-interop publisher</title>
|
||||
<style>
|
||||
body { font: 14px monospace; padding: 8px; }
|
||||
body[data-state="error"] { background: #fdd; }
|
||||
body[data-state="done"] { background: #dfd; }
|
||||
body[data-state="publishing"] { background: #ffe; }
|
||||
</style>
|
||||
</head>
|
||||
<body data-state="init">
|
||||
<h1>nests-browser-interop / publish</h1>
|
||||
<div id="status">init</div>
|
||||
<script type="module" src="./publish.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,207 @@
|
||||
// Phase 4 (T16) browser-publisher harness.
|
||||
//
|
||||
// Inverse of `listen.ts`: drives a sine `OscillatorNode` through the
|
||||
// WebCodecs `AudioEncoder` (Opus mode) and pushes each encoded packet
|
||||
// onto a moq-lite track via `Container.Legacy.Producer`, prefixed with
|
||||
// a Varint-encoded timestamp the watcher (`Container.Legacy.Format`)
|
||||
// will strip on decode. Also publishes a `catalog.json` track that
|
||||
// matches `MoqLiteHangCatalog.opus48k` byte-for-byte so the Amethyst
|
||||
// listener (and `hang-listen` for cross-validation) can discover the
|
||||
// audio rendition.
|
||||
//
|
||||
// Status: Phase 4.A scaffold only — wire the Connection.connect +
|
||||
// catalog publish + first-frame send. Phase 4.C extends this for I4
|
||||
// reverse / I14 / I15 scenarios. Until then, the I1-forward smoke test
|
||||
// (Amethyst speaker → Chromium listener) is the path that lights this
|
||||
// harness up.
|
||||
|
||||
import * as Moq from "@moq/lite";
|
||||
import * as Container from "@moq/hang/container";
|
||||
|
||||
const params = new URLSearchParams(location.search);
|
||||
const relayParam = params.get("relay");
|
||||
const broadcastParam = params.get("broadcast");
|
||||
const trackParam = params.get("track") ?? "audio/data";
|
||||
const catalogTrack = params.get("catalogTrack") ?? "catalog.json";
|
||||
const freqHz = Number(params.get("freqHz") ?? "440");
|
||||
const channels = Number(params.get("channels") ?? "1");
|
||||
const durationSec = Number(params.get("duration") ?? "5");
|
||||
const wsPort = Number(params.get("wsPort") ?? "0");
|
||||
|
||||
function required(v: string | null, name: string): string {
|
||||
if (!v) throw new Error(`publish.html: missing ?${name}=`);
|
||||
return v;
|
||||
}
|
||||
const relayUrlString = required(relayParam, "relay");
|
||||
const broadcastName = required(broadcastParam, "broadcast");
|
||||
|
||||
const status = (msg: string) => {
|
||||
const el = document.getElementById("status");
|
||||
if (el) el.textContent = msg;
|
||||
console.log("[publish]", msg);
|
||||
};
|
||||
|
||||
async function main() {
|
||||
// Optional WS back-channel for the test driver to read out
|
||||
// status — currently only used by Phase 4.C scenarios that want
|
||||
// to assert the publisher reached `playing` before the listener
|
||||
// attaches.
|
||||
let ws: WebSocket | undefined;
|
||||
if (wsPort) {
|
||||
ws = new WebSocket(`ws://127.0.0.1:${wsPort}/pcm`);
|
||||
await new Promise<void>((resolve) => {
|
||||
ws!.addEventListener("open", () => resolve(), { once: true });
|
||||
ws!.addEventListener("error", () => resolve(), { once: true });
|
||||
});
|
||||
}
|
||||
const sendDone = () => {
|
||||
if (ws?.readyState === WebSocket.OPEN) ws.send("done");
|
||||
};
|
||||
|
||||
const relayUrl = new URL(relayUrlString);
|
||||
status(`connecting to ${relayUrl.toString()}`);
|
||||
const conn = await Moq.Connection.connect(relayUrl, {
|
||||
websocket: { enabled: false },
|
||||
});
|
||||
(window as any).__moqVersion = conn.version;
|
||||
status(`connected, alpn=${conn.version}`);
|
||||
|
||||
// Build a publishable Broadcast — the relay opens SUBSCRIBE bidis
|
||||
// back to us per track and we serve them via `broadcast.subscribe`
|
||||
// (despite the name, on the publish side `subscribe` is what the
|
||||
// relay calls to *request* the track).
|
||||
const broadcast = new Moq.Broadcast();
|
||||
conn.publish(Moq.Path.from(broadcastName), broadcast);
|
||||
status(`announced ${broadcastName}`);
|
||||
|
||||
// Catalog: match `MoqLiteHangCatalog.opus48k(audioTrackName, channels)`
|
||||
// byte-for-byte. Field order matters less than the content because
|
||||
// hang.js uses zod parsing, but we keep the shape canonical.
|
||||
const catalogJson = JSON.stringify({
|
||||
audio: {
|
||||
renditions: {
|
||||
[trackParam]: {
|
||||
codec: "opus",
|
||||
container: { kind: "legacy" },
|
||||
sampleRate: 48000,
|
||||
numberOfChannels: channels,
|
||||
jitter: 20,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const catalogBytes = new TextEncoder().encode(catalogJson);
|
||||
|
||||
// -- Audio encoder pump --------------------------------------------
|
||||
// Build oscillator → MediaStreamAudioDestinationNode → MediaStreamTrack
|
||||
// pipeline; then loop pulling AudioData out of an MSTrack reader
|
||||
// via `MediaStreamTrackProcessor` and feed each frame into the
|
||||
// WebCodecs AudioEncoder. Encoded outputs land in `Producer.encode`.
|
||||
const ctx = new AudioContext({ sampleRate: 48_000, latencyHint: "interactive" });
|
||||
await ctx.resume();
|
||||
const osc = ctx.createOscillator();
|
||||
osc.frequency.value = freqHz;
|
||||
osc.type = "sine";
|
||||
const dst = ctx.createMediaStreamDestination();
|
||||
osc.connect(dst);
|
||||
osc.start();
|
||||
|
||||
const audioTrack = dst.stream.getAudioTracks()[0];
|
||||
// @ts-expect-error MediaStreamTrackProcessor is Chrome-only
|
||||
const processor = new MediaStreamTrackProcessor({ track: audioTrack });
|
||||
const reader = (processor.readable as ReadableStream<AudioData>).getReader();
|
||||
|
||||
// Serve catalog + audio tracks as they're requested by the relay.
|
||||
const handleRequests = async () => {
|
||||
for (;;) {
|
||||
const req = await broadcast.requested();
|
||||
if (!req) return;
|
||||
if (req.track.name === catalogTrack) {
|
||||
// One-shot emit-on-subscribe, like Amethyst speaker's
|
||||
// `catalogPublisher.setOnNewSubscriber`.
|
||||
const group = req.track.appendGroup();
|
||||
group.writeFrame(catalogBytes);
|
||||
group.close();
|
||||
} else if (req.track.name === trackParam) {
|
||||
// The audio track is fed by the encoder pump below;
|
||||
// nothing to do here other than accept the request
|
||||
// (the Producer below writes into `req.track`).
|
||||
(window as any).__audioTrack = req.track;
|
||||
}
|
||||
}
|
||||
};
|
||||
handleRequests().catch((e) => console.error("[publish] requests:", e));
|
||||
|
||||
// Wait until the relay subscribes to the audio track, then start the
|
||||
// encoder pump. The test driver is responsible for spawning the
|
||||
// listener AFTER the publisher reports `data-state="publishing"`.
|
||||
const audioMoqTrack: Moq.Track = await new Promise((resolve) => {
|
||||
const probe = setInterval(() => {
|
||||
const t = (window as any).__audioTrack as Moq.Track | undefined;
|
||||
if (t) {
|
||||
clearInterval(probe);
|
||||
resolve(t);
|
||||
}
|
||||
}, 20);
|
||||
});
|
||||
const producer = new Container.Legacy.Producer(audioMoqTrack);
|
||||
|
||||
const encoder = new AudioEncoder({
|
||||
output: (chunk, _meta) => {
|
||||
const data = new Uint8Array(chunk.byteLength);
|
||||
chunk.copyTo(data);
|
||||
// Force a new group at the start so the first frame is a
|
||||
// keyframe — the moq-lite Container.Legacy.Producer requires
|
||||
// it for the first packet.
|
||||
const isKey = (window as any).__producerStarted !== true;
|
||||
(window as any).__producerStarted = true;
|
||||
producer.encode(data, chunk.timestamp as any, isKey);
|
||||
},
|
||||
error: (e) => console.error("[publish] AudioEncoder", e),
|
||||
});
|
||||
encoder.configure({
|
||||
codec: "opus",
|
||||
sampleRate: 48_000,
|
||||
numberOfChannels: channels,
|
||||
bitrate: 32_000,
|
||||
});
|
||||
|
||||
document.body.dataset.state = "publishing";
|
||||
status("publishing");
|
||||
|
||||
const deadline = performance.now() + durationSec * 1000;
|
||||
let framesIn = 0;
|
||||
while (performance.now() < deadline) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done || !value) break;
|
||||
try {
|
||||
encoder.encode(value);
|
||||
framesIn++;
|
||||
} finally {
|
||||
value.close();
|
||||
}
|
||||
}
|
||||
status(`flushing, framesIn=${framesIn}`);
|
||||
|
||||
try {
|
||||
await encoder.flush();
|
||||
} catch (e) {
|
||||
console.warn("[publish] flush:", e);
|
||||
}
|
||||
encoder.close();
|
||||
osc.stop();
|
||||
audioTrack.stop();
|
||||
producer.close();
|
||||
broadcast.close();
|
||||
conn.close();
|
||||
sendDone();
|
||||
|
||||
document.body.dataset.state = "done";
|
||||
status(`done. framesIn=${framesIn}`);
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error("[publish] fatal:", e);
|
||||
document.body.dataset.state = "error";
|
||||
status(`ERROR: ${e?.stack ?? e}`);
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
// Phase 4 (T16) bun static + WebSocket back-channel server.
|
||||
//
|
||||
// One process per Kotlin test, bound to a random port; the PlaywrightDriver
|
||||
// passes the port back to the harness pages as `?wsPort=…`. PCM frames sent
|
||||
// over the WS as binary messages get appended to `--out-pcm`. A textual
|
||||
// `done` message flips the server's `done` flag so the test driver can
|
||||
// poll it via the `/state` endpoint and tear down cleanly.
|
||||
//
|
||||
// Argv:
|
||||
// --port <int> listen port; 0 picks a random one (logged on stdout)
|
||||
// --root <dir> directory to serve static files from (= dist/)
|
||||
// --out-pcm <path> file to append received PCM frames to
|
||||
//
|
||||
// Stdout (machine-readable, single line then blank line):
|
||||
// port=<int>
|
||||
// ready
|
||||
//
|
||||
// Errors go to stderr; non-zero exit code on fatal startup failure.
|
||||
|
||||
import { type ServerWebSocket } from "bun";
|
||||
import { mkdirSync, openSync, closeSync, writeSync, existsSync } from "node:fs";
|
||||
import { dirname, resolve, join } from "node:path";
|
||||
|
||||
interface Args {
|
||||
port: number;
|
||||
root: string;
|
||||
outPcm: string;
|
||||
}
|
||||
|
||||
function parseArgs(): Args {
|
||||
const args = process.argv.slice(2);
|
||||
let port = 0;
|
||||
let root = "";
|
||||
let outPcm = "";
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const a = args[i];
|
||||
if (a === "--port") port = Number(args[++i]);
|
||||
else if (a === "--root") root = args[++i];
|
||||
else if (a === "--out-pcm") outPcm = args[++i];
|
||||
else throw new Error(`unknown arg: ${a}`);
|
||||
}
|
||||
if (!root) throw new Error("--root is required");
|
||||
if (!outPcm) throw new Error("--out-pcm is required");
|
||||
return { port, root, outPcm: resolve(outPcm) };
|
||||
}
|
||||
|
||||
const args = parseArgs();
|
||||
mkdirSync(dirname(args.outPcm), { recursive: true });
|
||||
// Truncate any prior file: the harness page reopens each run.
|
||||
const fd = openSync(args.outPcm, "w");
|
||||
|
||||
let done = false;
|
||||
const wsClients = new Set<ServerWebSocket<unknown>>();
|
||||
|
||||
const contentType = (path: string): string => {
|
||||
if (path.endsWith(".html")) return "text/html; charset=utf-8";
|
||||
if (path.endsWith(".js")) return "application/javascript; charset=utf-8";
|
||||
if (path.endsWith(".mjs")) return "application/javascript; charset=utf-8";
|
||||
if (path.endsWith(".json")) return "application/json; charset=utf-8";
|
||||
if (path.endsWith(".css")) return "text/css; charset=utf-8";
|
||||
if (path.endsWith(".wasm")) return "application/wasm";
|
||||
return "application/octet-stream";
|
||||
};
|
||||
|
||||
const server = Bun.serve({
|
||||
port: args.port,
|
||||
hostname: "127.0.0.1",
|
||||
fetch(req, srv) {
|
||||
const url = new URL(req.url);
|
||||
if (url.pathname === "/pcm") {
|
||||
// WebSocket upgrade for the PCM back-channel.
|
||||
if (srv.upgrade(req)) return;
|
||||
return new Response("expected websocket upgrade", { status: 400 });
|
||||
}
|
||||
if (url.pathname === "/state") {
|
||||
return new Response(JSON.stringify({ done }), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
// Static file serve out of root/.
|
||||
let path = url.pathname === "/" ? "/listen.html" : url.pathname;
|
||||
const filePath = join(args.root, path.replace(/^\/+/, ""));
|
||||
// Reject path traversal attempts.
|
||||
if (!filePath.startsWith(resolve(args.root))) {
|
||||
return new Response("forbidden", { status: 403 });
|
||||
}
|
||||
if (!existsSync(filePath)) {
|
||||
return new Response("not found: " + path, { status: 404 });
|
||||
}
|
||||
const file = Bun.file(filePath);
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
"content-type": contentType(filePath),
|
||||
// No-cache so a `bun build` rebuild between Playwright runs
|
||||
// is picked up immediately.
|
||||
"cache-control": "no-store",
|
||||
},
|
||||
});
|
||||
},
|
||||
websocket: {
|
||||
message(ws, message) {
|
||||
wsClients.add(ws);
|
||||
if (typeof message === "string") {
|
||||
if (message === "done") {
|
||||
done = true;
|
||||
console.log("[server] received `done`");
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Binary PCM frame — append raw bytes to the out file.
|
||||
const buf = message instanceof ArrayBuffer ? new Uint8Array(message) : new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
|
||||
writeSync(fd, buf);
|
||||
},
|
||||
open(ws) {
|
||||
wsClients.add(ws);
|
||||
},
|
||||
close(ws) {
|
||||
wsClients.delete(ws);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Machine-readable handshake for PlaywrightDriver.
|
||||
process.stdout.write(`port=${server.port}\n`);
|
||||
process.stdout.write("ready\n");
|
||||
|
||||
// Clean up the fd on Ctrl-C / parent kill so we don't leak it.
|
||||
const shutdown = () => {
|
||||
try {
|
||||
closeSync(fd);
|
||||
} catch { /* ignore */ }
|
||||
server.stop(true);
|
||||
process.exit(0);
|
||||
};
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
||||
@@ -0,0 +1,68 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
|
||||
// Driver test that the Kotlin `PlaywrightDriver` invokes once per
|
||||
// scenario via `npx playwright test`. Every parameter is passed via
|
||||
// environment variables (NPM_BROWSER_HARNESS_*) so the same single test
|
||||
// can serve every BrowserInteropTest scenario without us writing one
|
||||
// playwright spec per scenario.
|
||||
//
|
||||
// Required env:
|
||||
// NESTS_HARNESS_URL — http://127.0.0.1:<bunPort>/listen.html (or publish.html)
|
||||
// NESTS_TIMEOUT_MS — overall page timeout (default 60_000)
|
||||
//
|
||||
// The test:
|
||||
// 1. opens the URL,
|
||||
// 2. waits for `body[data-state="done"]` (or "error", which fails),
|
||||
// 3. dumps the status text + console logs back as the test failure message
|
||||
// so `--reporter list` surfaces them in stdout the Kotlin caller reads.
|
||||
|
||||
const harnessUrl = process.env.NESTS_HARNESS_URL;
|
||||
const timeoutMs = Number(process.env.NESTS_TIMEOUT_MS ?? "60000");
|
||||
|
||||
test.describe("nests-browser-interop", () => {
|
||||
test.skip(!harnessUrl, "NESTS_HARNESS_URL not set");
|
||||
|
||||
test("harness runs to completion", async ({ page }) => {
|
||||
const consoleLines: string[] = [];
|
||||
page.on("console", (msg) => {
|
||||
consoleLines.push(`[${msg.type()}] ${msg.text()}`);
|
||||
});
|
||||
page.on("pageerror", (err) => {
|
||||
consoleLines.push(`[pageerror] ${err.message}\n${err.stack ?? ""}`);
|
||||
});
|
||||
|
||||
await page.goto(harnessUrl!, { waitUntil: "domcontentloaded" });
|
||||
// Wait for the harness page to flip to either "done" (success)
|
||||
// or "error" (page-side fatal). Don't rely on `waitForFunction`'s
|
||||
// own polling cadence because Chromium on a busy CI runner can
|
||||
// miss a transient status; spin in 100 ms ticks ourselves.
|
||||
const finalState = await page.waitForFunction(
|
||||
() => {
|
||||
const s = (document.body as HTMLBodyElement).dataset.state;
|
||||
return s === "done" || s === "error" ? s : null;
|
||||
},
|
||||
null,
|
||||
{ timeout: timeoutMs, polling: 100 },
|
||||
);
|
||||
const state = await finalState.evaluate((v) => v as string);
|
||||
const status = await page.locator("#status").textContent();
|
||||
const meta = await page.evaluate(() => ({
|
||||
framesDecoded: (window as any).__framesDecoded,
|
||||
moqVersion: (window as any).__moqVersion,
|
||||
}));
|
||||
// Always print a summary line — Kotlin parses this for follow-up
|
||||
// assertions (e.g. moq-lite-03 ALPN echo for I15).
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
state,
|
||||
status,
|
||||
meta,
|
||||
logs: consoleLines.slice(-50),
|
||||
}),
|
||||
);
|
||||
if (state === "error") {
|
||||
throw new Error(`harness reached error state: ${status}\n\nlogs:\n${consoleLines.join("\n")}`);
|
||||
}
|
||||
expect(state).toBe("done");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable", "WebWorker"],
|
||||
"types": ["@types/bun"],
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
import java.io.File
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
@@ -227,3 +228,108 @@ tasks.withType<Test>().configureEach {
|
||||
systemProperty("nestsHangInteropSidecarsDir", sidecarRelease.absolutePath)
|
||||
systemProperty("nestsHangInteropCargoBinDir", cargoBin.absolutePath)
|
||||
}
|
||||
|
||||
// ---- Cross-stack interop: BROWSER (Phase 4 of T16) --------------------------
|
||||
//
|
||||
// Adds the bun + Playwright + headless Chromium harness at
|
||||
// `nestsClient-browser-interop/`. Mirrors the hang-interop wiring above
|
||||
// but with bun/npx subprocesses instead of cargo. Opt-in via
|
||||
// `-DnestsBrowserInterop=true`. See:
|
||||
// nestsClient/plans/2026-05-06-phase4-browser-harness.md
|
||||
//
|
||||
// Two tasks:
|
||||
// - interopBuildBrowserHarness — `bun install` + `bun build` of
|
||||
// listen.ts/publish.ts → dist/, plus copying static .html files.
|
||||
// - interopInstallPlaywrightChromium — `npx playwright install
|
||||
// --with-deps chromium`. Skipped if a Chromium build already lives
|
||||
// in `~/.cache/ms-playwright/`.
|
||||
//
|
||||
// We also forward the `bun` and `npx` binaries to be configurable via
|
||||
// env so CI can override them; defaults pick up the standard install
|
||||
// paths the agents/host runner ship with.
|
||||
|
||||
val browserInteropDir =
|
||||
rootProject.layout.projectDirectory.dir("nestsClient-browser-interop")
|
||||
|
||||
// `bun` lives at `/root/.bun/bin/bun` on the agent runner. CI may put it
|
||||
// elsewhere; allow override via env / system property. Falls back to
|
||||
// `bun` on PATH if the well-known path isn't executable.
|
||||
fun resolveBunBinary(): String {
|
||||
val explicit = System.getenv("BUN_BIN") ?: System.getProperty("bunBin")
|
||||
if (explicit != null) return explicit
|
||||
val agentPath = "/root/.bun/bin/bun"
|
||||
return if (File(agentPath).canExecute()) agentPath else "bun"
|
||||
}
|
||||
|
||||
fun resolveNpxBinary(): String =
|
||||
System.getenv("NPX_BIN") ?: System.getProperty("npxBin") ?: "npx"
|
||||
|
||||
val interopBuildBrowserHarness by tasks.registering(Exec::class) {
|
||||
description = "bun install && bun build for the browser interop harness"
|
||||
group = "interop"
|
||||
workingDir = browserInteropDir.asFile
|
||||
val bun = resolveBunBinary()
|
||||
// Single bash invocation so `&&` short-circuits on a failed install.
|
||||
// The trailing `cp` step copies the static HTML pages into dist/
|
||||
// alongside the bundled JS — bun's bundler doesn't carry .html.
|
||||
commandLine(
|
||||
"bash", "-c",
|
||||
"$bun install && $bun build src/listen.ts src/publish.ts --outdir dist --target browser && cp src/listen.html src/publish.html dist/",
|
||||
)
|
||||
inputs.files(
|
||||
fileTree(browserInteropDir.asFile) {
|
||||
include("package.json", "tsconfig.json", "playwright.config.ts", "src/**/*")
|
||||
},
|
||||
)
|
||||
outputs.dir(browserInteropDir.dir("dist"))
|
||||
}
|
||||
|
||||
val interopInstallPlaywrightChromium by tasks.registering(Exec::class) {
|
||||
description = "Install Playwright Chromium + dependencies for the browser interop harness"
|
||||
group = "interop"
|
||||
workingDir = browserInteropDir.asFile
|
||||
val npx = resolveNpxBinary()
|
||||
// `--with-deps` needs sudo on a fresh runner; on the agent host
|
||||
// Chromium is already pre-installed via apt so the system-package
|
||||
// step is a no-op. Use the plain `install` form when --with-deps
|
||||
// would error (e.g. unprivileged container) — fall back at runtime.
|
||||
commandLine("bash", "-c", "$npx playwright install chromium")
|
||||
onlyIf {
|
||||
// Skip if a Chromium build is already present in the Playwright
|
||||
// cache. The cache path is normally ~/.cache/ms-playwright/, but
|
||||
// the agent runner sets PLAYWRIGHT_BROWSERS_PATH=/opt/pw-browsers
|
||||
// and ships chromium pre-installed there. Honour the env var so
|
||||
// we don't redundantly download.
|
||||
val explicit = System.getenv("PLAYWRIGHT_BROWSERS_PATH")
|
||||
val candidates =
|
||||
if (explicit != null) {
|
||||
listOf(File(explicit))
|
||||
} else {
|
||||
val home = System.getProperty("user.home") ?: return@onlyIf true
|
||||
listOf(File(home, ".cache/ms-playwright"))
|
||||
}
|
||||
val hasChromium =
|
||||
candidates.any { dir ->
|
||||
dir.exists() &&
|
||||
dir.listFiles()?.any { it.name.startsWith("chromium-") || it.name == "chromium" } == true
|
||||
}
|
||||
!hasChromium
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType<Test>().configureEach {
|
||||
val isBrowserInterop = System.getProperty("nestsBrowserInterop") == "true"
|
||||
if (isBrowserInterop) {
|
||||
dependsOn(interopBuildBrowserHarness, interopInstallPlaywrightChromium)
|
||||
// Browser scenarios reuse the moq-relay subprocess that
|
||||
// hang-interop boots, so the Rust sidecars must be built too.
|
||||
dependsOn(interopBuildHangSidecars)
|
||||
}
|
||||
systemProperty(
|
||||
"nestsBrowserInteropHarnessDir",
|
||||
browserInteropDir.asFile.absolutePath,
|
||||
)
|
||||
System.getProperty("nestsBrowserInterop")?.let {
|
||||
systemProperty("nestsBrowserInterop", it)
|
||||
}
|
||||
}
|
||||
|
||||
+337
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.nestsclient.interop.native
|
||||
|
||||
import com.vitorpamplona.nestsclient.AudioBroadcastConfig
|
||||
import com.vitorpamplona.nestsclient.NestsClient
|
||||
import com.vitorpamplona.nestsclient.NestsRoomConfig
|
||||
import com.vitorpamplona.nestsclient.audio.AudioFormat
|
||||
import com.vitorpamplona.nestsclient.audio.JvmOpusEncoder
|
||||
import com.vitorpamplona.nestsclient.audio.PcmAssertions
|
||||
import com.vitorpamplona.nestsclient.audio.SineWaveAudioCapture
|
||||
import com.vitorpamplona.nestsclient.connectNestsSpeaker
|
||||
import com.vitorpamplona.nestsclient.transport.QuicWebTransportFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.UUID
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Phase 4 (T16) — browser-side cross-stack interop scenarios.
|
||||
*
|
||||
* Drives a headless Chromium subprocess (via [PlaywrightDriver])
|
||||
* through the same [NativeMoqRelayHarness] moq-relay that
|
||||
* [HangInteropTest] uses. The browser harness page connects via
|
||||
* Chromium's WebTransport stack (separate implementation from quinn /
|
||||
* `:quic`), decodes Opus via WebCodecs `AudioDecoder`, and posts
|
||||
* Float32 LE PCM frames back to a bun WebSocket back-channel that
|
||||
* appends them to a file the test reads.
|
||||
*
|
||||
* Phase 4.B P0 scenario:
|
||||
* - **I1 browser** — sine-wave round-trip Amethyst Kotlin speaker →
|
||||
* Chromium @moq/lite + @moq/hang listener; assert FFT peak at
|
||||
* 440 Hz on the captured PCM.
|
||||
*
|
||||
* Speaker pinned at `framesPerGroup = 5` to stay under the
|
||||
* `moq-relay 0.10.x` per-subscriber forward cliff (same as the
|
||||
* hang-tier scenarios).
|
||||
*
|
||||
* Gate: `-DnestsBrowserInterop=true` (also implies
|
||||
* `-DnestsHangInterop=true` indirectly because we boot the same
|
||||
* `NativeMoqRelayHarness`).
|
||||
*/
|
||||
class BrowserInteropTest {
|
||||
@BeforeTest
|
||||
fun gate() {
|
||||
PlaywrightDriver.assumeBrowserInterop()
|
||||
// The browser harness reuses the moq-relay subprocess that the
|
||||
// hang-tier scenarios bring up. Without `-DnestsHangInterop=true`
|
||||
// the harness would refuse to boot — flip it on automatically
|
||||
// for the browser gate so the user only has to set one flag.
|
||||
if (!NativeMoqRelayHarness.isEnabled()) {
|
||||
System.setProperty(NativeMoqRelayHarness.ENABLE_PROPERTY, "true")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* **I1 forward (browser)** — Amethyst Kotlin speaker → Chromium
|
||||
* `@moq/lite` listener with `@moq/hang` `Container.Legacy.Consumer`.
|
||||
* Asserts the captured PCM has the expected sample count and the
|
||||
* 440 Hz tone survives end-to-end.
|
||||
*
|
||||
* What this catches that the Rust hang-listen path doesn't:
|
||||
* - Chromium's WebTransport ALPN negotiation (independent
|
||||
* implementation from quinn),
|
||||
* - WebCodecs `AudioDecoder` first-frame handling (different
|
||||
* warmup behaviour from libopus — drops the first 3 output
|
||||
* frames; we offset the warmup window accordingly),
|
||||
* - `OpusHead` codec-config wedging would still bypass the
|
||||
* decoder warmup window and produce a click at offset 0;
|
||||
* T8's filter is verified again here.
|
||||
*/
|
||||
@Test
|
||||
fun amethyst_speaker_to_chromium_listener_static_tone_440() =
|
||||
runBlocking {
|
||||
// Speaker runs for 10 s wallclock; we assert the page captured
|
||||
// ≥ 1 s of decoded PCM. The looser bound reflects how the page's
|
||||
// capture window opens *after* Chromium cold-launch + WebTransport
|
||||
// handshake (3–5 s on a fresh runner), and the Kotlin speaker
|
||||
// pins `framesPerGroup = 5` (= 100 ms groups) so a late-joining
|
||||
// subscriber gets only the tail per `moq-relay 0.10.x`'s
|
||||
// per-subscriber cache semantics. The load-bearing assertion is
|
||||
// the FFT peak at 440 Hz — that catches a wire-format regression
|
||||
// (downmix, channel swap, OpusHead-as-frame leak) regardless of
|
||||
// how many seconds of tail the page captured.
|
||||
val out = runSpeakerToBrowserListen(speakerSeconds = 10)
|
||||
val pcm = readFloat32Pcm(out.pcmFile)
|
||||
// Skip the warmup window before FFT: 40 ms Opus
|
||||
// look-ahead + 60 ms WebCodecs 3-frame warmup-skip = 100 ms.
|
||||
val warmupSamples = AudioFormat.SAMPLE_RATE_HZ / 10
|
||||
assertTrue(
|
||||
pcm.size > warmupSamples,
|
||||
"captured PCM (${pcm.size} samples) shorter than the WebCodecs warmup " +
|
||||
"window — page never received any audio.\nplaywright stdout:\n${out.stdout}",
|
||||
)
|
||||
val analysed = pcm.copyOfRange(warmupSamples, pcm.size)
|
||||
assertTrue(
|
||||
analysed.size >= AudioFormat.SAMPLE_RATE_HZ,
|
||||
"after warmup window only ${analysed.size} samples remain; " +
|
||||
"expected ≥ 1 s of decoded audio.\nplaywright stdout:\n${out.stdout}",
|
||||
)
|
||||
PcmAssertions.assertFftPeak(
|
||||
analysed,
|
||||
expectedHz = 440.0,
|
||||
halfWindowHz = 5.0,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output bundle from one [runSpeakerToBrowserListen] invocation.
|
||||
*/
|
||||
private class BrowserListenOutput(
|
||||
val pcmFile: File,
|
||||
val stdout: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* Run the Kotlin speaker for [speakerSeconds] seconds, drive the
|
||||
* Playwright + Chromium harness page to capture decoded PCM via the
|
||||
* bun WS back-channel, and return the captured bundle.
|
||||
*
|
||||
* Mirror of [HangInteropTest]'s `runSpeakerToHangListen`, but the
|
||||
* listener subprocess is `npx playwright test` instead of
|
||||
* `hang-listen`. The relay endpoint is identical — both consumers
|
||||
* connect via WebTransport to the same `NativeMoqRelayHarness`
|
||||
* instance.
|
||||
*/
|
||||
private suspend fun runSpeakerToBrowserListen(
|
||||
speakerSeconds: Int,
|
||||
listenerLateJoinDelayMs: Long = 150L,
|
||||
channelCount: Int = 1,
|
||||
freqHzPerChannel: IntArray? = null,
|
||||
): BrowserListenOutput {
|
||||
val harness = NativeMoqRelayHarness.shared()
|
||||
|
||||
val signer: NostrSigner = NostrSignerInternal(KeyPair())
|
||||
val pubkey = signer.pubKey
|
||||
|
||||
val (relayHost, relayPort) = harness.loopbackHostPort()
|
||||
val speakerEndpoint = "https://$relayHost:$relayPort"
|
||||
|
||||
val room =
|
||||
NestsRoomConfig(
|
||||
authBaseUrl = "<unused-public-relay>",
|
||||
endpoint = speakerEndpoint,
|
||||
hostPubkey = pubkey,
|
||||
roomId = "rt-${UUID.randomUUID()}",
|
||||
)
|
||||
val moqNamespace = room.moqNamespace()
|
||||
// Build the same connect target the Kotlin speaker uses; the
|
||||
// Chromium page consumes it directly via `new URL(relay)`.
|
||||
// `NestsConnect.kt` uses `?jwt=<token>` query for auth and the
|
||||
// moq-rs relay we boot has `--auth-public ""` so the token is
|
||||
// empty — Chromium's WebTransport accepts an empty query value.
|
||||
val pageRelayUrl = "$speakerEndpoint/$moqNamespace?jwt="
|
||||
|
||||
val pumpScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
// Use a cert-capturing validator so we can pin the relay's
|
||||
// self-signed cert into Chromium's WebTransport via
|
||||
// `serverCertificateHashes`. The validator is just a wrapper —
|
||||
// it accepts every chain (delegating to PermissiveCertificateValidator
|
||||
// semantics) but stashes the leaf DER on the first handshake.
|
||||
val certCapture = CertCapturingValidator()
|
||||
val transport =
|
||||
QuicWebTransportFactory(
|
||||
parentScope = pumpScope,
|
||||
certificateValidator = certCapture,
|
||||
)
|
||||
|
||||
val captureFactory: () -> SineWaveAudioCapture = {
|
||||
SineWaveAudioCapture(
|
||||
freqHz = 440,
|
||||
channelCount = channelCount,
|
||||
freqHzPerChannel = freqHzPerChannel,
|
||||
)
|
||||
}
|
||||
val encoderFactory: () -> JvmOpusEncoder = {
|
||||
JvmOpusEncoder(channelCount = channelCount)
|
||||
}
|
||||
val broadcastConfig = AudioBroadcastConfig(channelCount = channelCount)
|
||||
|
||||
val speaker =
|
||||
connectNestsSpeaker(
|
||||
httpClient = StaticTokenNestsClientForBrowser,
|
||||
transport = transport,
|
||||
scope = pumpScope,
|
||||
room = room,
|
||||
signer = signer,
|
||||
speakerPubkeyHex = pubkey,
|
||||
captureFactory = captureFactory,
|
||||
encoderFactory = encoderFactory,
|
||||
broadcastConfig = broadcastConfig,
|
||||
framesPerGroup = 5,
|
||||
)
|
||||
val handle = speaker.startBroadcasting()
|
||||
delay(listenerLateJoinDelayMs)
|
||||
|
||||
// The speaker's connect path completes a QUIC handshake before
|
||||
// returning, so the cert validator has captured the leaf cert by
|
||||
// now. Compute the SHA-256 the WebTransport spec wants — `value`
|
||||
// in `serverCertificateHashes` is the SHA-256 of the entire
|
||||
// DER-encoded X.509 certificate, NOT of the SPKI.
|
||||
val derSha256 =
|
||||
certCapture.derSha256()
|
||||
?: error("cert capture failed — speaker handshake did not invoke validator")
|
||||
val derSha256B64 =
|
||||
java.util.Base64
|
||||
.getEncoder()
|
||||
.encodeToString(derSha256)
|
||||
|
||||
// Run Playwright on a side thread; keep the Kotlin speaker alive
|
||||
// until Playwright signals done. Chromium cold-launch + Playwright
|
||||
// setup eats 3–5 s before the page starts its `durationSec`
|
||||
// capture window, so closing the speaker on a fixed wall-clock
|
||||
// delay is fundamentally racy. Instead, the speaker stays
|
||||
// broadcasting until the side thread reports completion (which
|
||||
// happens after the page reaches `body[data-state="done"]` and
|
||||
// the bun server's WS shutdown).
|
||||
val pwResultRef =
|
||||
java.util.concurrent.atomic
|
||||
.AtomicReference<PlaywrightDriver.HarnessRun>()
|
||||
val pwErrorRef =
|
||||
java.util.concurrent.atomic
|
||||
.AtomicReference<Throwable>()
|
||||
val pwLatch = java.util.concurrent.CountDownLatch(1)
|
||||
val pwThread =
|
||||
Thread({
|
||||
try {
|
||||
pwResultRef.set(
|
||||
PlaywrightDriver.openListenPage(
|
||||
relayUrlFull = pageRelayUrl,
|
||||
broadcastPath = pubkey,
|
||||
durationSec = speakerSeconds,
|
||||
// Bigger overall timeout: Chromium cold-launch +
|
||||
// Playwright runner setup eats 3–10 s on a busy
|
||||
// CI runner before the page starts capturing.
|
||||
overallTimeoutSec = speakerSeconds + 90,
|
||||
serverCertHashB64 = derSha256B64,
|
||||
),
|
||||
)
|
||||
} catch (t: Throwable) {
|
||||
pwErrorRef.set(t)
|
||||
} finally {
|
||||
pwLatch.countDown()
|
||||
}
|
||||
}, "browser-interop-playwright").apply {
|
||||
isDaemon = true
|
||||
start()
|
||||
}
|
||||
|
||||
// Wait (off the main coroutine) for Playwright to finish. The
|
||||
// speaker keeps broadcasting in the background of `pumpScope`
|
||||
// until we close it below.
|
||||
val pwOverallTimeoutSec = speakerSeconds + 120
|
||||
val ok =
|
||||
kotlinx.coroutines.withContext(Dispatchers.IO) {
|
||||
pwLatch.await(pwOverallTimeoutSec.toLong(), java.util.concurrent.TimeUnit.SECONDS)
|
||||
}
|
||||
runCatching { handle.close() }
|
||||
runCatching { speaker.close() }
|
||||
val out =
|
||||
try {
|
||||
if (!ok) error("Playwright did not complete within ${pwOverallTimeoutSec}s")
|
||||
pwErrorRef.get()?.let { throw it }
|
||||
pwResultRef.get() ?: error("Playwright thread did not produce a result")
|
||||
} finally {
|
||||
pumpScope.coroutineContext[Job]?.cancel()
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
out.exitCode == 0,
|
||||
"Playwright exited with code ${out.exitCode}.\n--- stdout ---\n${out.playwrightStdout}",
|
||||
)
|
||||
return BrowserListenOutput(pcmFile = out.pcmFile, stdout = out.playwrightStdout)
|
||||
}
|
||||
|
||||
/**
|
||||
* Stub NestsClient for the browser interop scenarios. The harness's
|
||||
* `--auth-public ""` flag grants any path without a JWT, so we mint
|
||||
* an empty token. Mirrors [HangInteropTest]'s
|
||||
* `StaticTokenNestsClient`.
|
||||
*/
|
||||
private object StaticTokenNestsClientForBrowser : NestsClient {
|
||||
override suspend fun mintToken(
|
||||
room: NestsRoomConfig,
|
||||
publish: Boolean,
|
||||
signer: NostrSigner,
|
||||
): String = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file of native-endian Float32 LE PCM into a [FloatArray].
|
||||
* Matches the format the bun WS server appends per binary frame —
|
||||
* which itself matches `hang-listen`'s output format so the existing
|
||||
* [PcmAssertions] helpers slot in unchanged.
|
||||
*/
|
||||
private fun readFloat32Pcm(file: File): FloatArray {
|
||||
val bytes = file.readBytes()
|
||||
require(bytes.size % 4 == 0) {
|
||||
"PCM file size ${bytes.size} is not a multiple of 4 (Float32)"
|
||||
}
|
||||
val n = bytes.size / 4
|
||||
val out = FloatArray(n)
|
||||
val buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)
|
||||
for (i in 0 until n) out[i] = buf.float
|
||||
return out
|
||||
}
|
||||
+404
@@ -0,0 +1,404 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.nestsclient.interop.native
|
||||
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* Phase 4 (T16) Kotlin-side shim that drives a headless Chromium
|
||||
* harness via Playwright. Mirrors the role `hang-listen` plays for
|
||||
* the Phase 2 Rust-listener scenarios, but the listener is a
|
||||
* Chromium tab loading [openListenPage] / [openPublishPage] from
|
||||
* a bun static server.
|
||||
*
|
||||
* Two subprocesses per scenario:
|
||||
* 1. **bun static + WebSocket back-channel** (`server.ts`): serves
|
||||
* the bundled `listen.html` / `publish.html` and writes any PCM
|
||||
* frames the page posts over WS to the file the test reads.
|
||||
* 2. **`npx playwright test`** (or `bun x playwright test`): one-off
|
||||
* Chromium spawn that opens the harness page and waits for
|
||||
* `body[data-state="done"]`.
|
||||
*
|
||||
* Both are spawned per-scenario for isolation — sharing the bun
|
||||
* server across scenarios would race the PCM-output file across
|
||||
* runs, and sharing a Chromium across runs invites stale
|
||||
* `AudioContext` / `WebTransport` state.
|
||||
*
|
||||
* Gate: `-DnestsBrowserInterop=true`. The Gradle `Test` task hooks
|
||||
* `interopBuildBrowserHarness` + `interopInstallPlaywrightChromium`
|
||||
* dependencies onto this gate (see `nestsClient/build.gradle.kts`).
|
||||
*/
|
||||
internal object PlaywrightDriver {
|
||||
/** Gate property — mirrors [NativeMoqRelayHarness.ENABLE_PROPERTY]. */
|
||||
const val ENABLE_PROPERTY = "nestsBrowserInterop"
|
||||
|
||||
/**
|
||||
* Forwarded by Gradle: absolute path to `nestsClient-browser-interop/`.
|
||||
*/
|
||||
const val HARNESS_DIR_PROPERTY = "nestsBrowserInteropHarnessDir"
|
||||
|
||||
fun isEnabled(): Boolean = System.getProperty(ENABLE_PROPERTY) == "true"
|
||||
|
||||
/**
|
||||
* JUnit "skipped" if the gate isn't on. Mirrors
|
||||
* [NativeMoqRelayHarness.assumeHangInterop].
|
||||
*/
|
||||
fun assumeBrowserInterop() {
|
||||
if (isEnabled()) return
|
||||
val msg =
|
||||
"Skipping browser interop test — set -D$ENABLE_PROPERTY=true to enable. " +
|
||||
"See nestsClient/plans/2026-05-06-phase4-browser-harness.md."
|
||||
try {
|
||||
val assume = Class.forName("org.junit.Assume")
|
||||
val assumeTrue =
|
||||
assume.getMethod("assumeTrue", String::class.java, Boolean::class.javaPrimitiveType)
|
||||
assumeTrue.invoke(null, msg, false)
|
||||
} catch (e: java.lang.reflect.InvocationTargetException) {
|
||||
throw e.targetException ?: e
|
||||
} catch (_: ClassNotFoundException) {
|
||||
throw IllegalStateException(msg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Outcome handed back to the test once the Chromium harness has
|
||||
* finished. [pcmFile] holds the Float32 LE PCM bytes the listener
|
||||
* page wrote via the WS back-channel; [playwrightStdout] is the
|
||||
* combined stdout/stderr of the `npx playwright test` invocation
|
||||
* (Kotlin parses the trailing JSON line for diagnostic metadata).
|
||||
*/
|
||||
data class HarnessRun(
|
||||
val pcmFile: File,
|
||||
val playwrightStdout: String,
|
||||
val exitCode: Int,
|
||||
)
|
||||
|
||||
/**
|
||||
* Spawn the listener harness:
|
||||
* 1. Pick an ephemeral port (via `ServerSocket(0)`),
|
||||
* 2. Start `bun run server.ts --port <p> --root dist --out-pcm <pcm>`,
|
||||
* 3. Wait for the server's `ready` line,
|
||||
* 4. Spawn `npx playwright test` with NESTS_HARNESS_URL =
|
||||
* `http://127.0.0.1:<p>/listen.html?relay=<…>&broadcast=<pubkey>&wsPort=<p>&duration=<sec>`,
|
||||
* 5. Block until Playwright exits or [overallTimeoutSec] elapses,
|
||||
* 6. Tear down both subprocesses.
|
||||
*
|
||||
* The relay URL passed to the page is the *full* connect target
|
||||
* (path + `?jwt=` query), built by [buildHarnessRelayUrl] —
|
||||
* Chromium's WebTransport driver consumes it directly.
|
||||
*/
|
||||
fun openListenPage(
|
||||
relayUrlFull: String,
|
||||
broadcastPath: String,
|
||||
durationSec: Int,
|
||||
overallTimeoutSec: Int = durationSec + 30,
|
||||
track: String = "audio/data",
|
||||
serverCertHashB64: String? = null,
|
||||
): HarnessRun {
|
||||
val extraQuery =
|
||||
if (serverCertHashB64 != null) {
|
||||
"&certSha256=" + java.net.URLEncoder.encode(serverCertHashB64, Charsets.UTF_8)
|
||||
} else {
|
||||
""
|
||||
}
|
||||
return run(
|
||||
"listen.html",
|
||||
relayUrlFull,
|
||||
broadcastPath,
|
||||
durationSec,
|
||||
overallTimeoutSec,
|
||||
track,
|
||||
extraQuery,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn the publisher harness. Symmetric to [openListenPage] but
|
||||
* loads `publish.html` and passes the oscillator parameters.
|
||||
* Phase 4.C scenarios — the I1-forward smoke test does NOT use this.
|
||||
*/
|
||||
@Suppress("LongParameterList")
|
||||
fun openPublishPage(
|
||||
relayUrlFull: String,
|
||||
broadcastPath: String,
|
||||
freqHz: Int,
|
||||
channels: Int,
|
||||
durationSec: Int,
|
||||
overallTimeoutSec: Int = durationSec + 30,
|
||||
track: String = "audio/data",
|
||||
): HarnessRun {
|
||||
val extraQuery = "&freqHz=$freqHz&channels=$channels"
|
||||
return run(
|
||||
"publish.html",
|
||||
relayUrlFull,
|
||||
broadcastPath,
|
||||
durationSec,
|
||||
overallTimeoutSec,
|
||||
track,
|
||||
extraQuery,
|
||||
)
|
||||
}
|
||||
|
||||
private fun run(
|
||||
page: String,
|
||||
relayUrlFull: String,
|
||||
broadcastPath: String,
|
||||
durationSec: Int,
|
||||
overallTimeoutSec: Int,
|
||||
track: String,
|
||||
extraQuery: String = "",
|
||||
): HarnessRun {
|
||||
check(isEnabled()) {
|
||||
"PlaywrightDriver.run called without -D$ENABLE_PROPERTY=true."
|
||||
}
|
||||
val harnessDir = requireHarnessDir()
|
||||
val distDir =
|
||||
File(harnessDir, "dist").apply {
|
||||
check(isDirectory) {
|
||||
"browser harness dist/ missing at $absolutePath — did " +
|
||||
"`./gradlew :nestsClient:interopBuildBrowserHarness` run?"
|
||||
}
|
||||
}
|
||||
|
||||
// 1) Reserve a port for the bun server (it binds to 127.0.0.1
|
||||
// on the same number; a tiny race window but loopback in CI
|
||||
// is uncontested, same pattern as NativeMoqRelayHarness).
|
||||
val bunPort = java.net.ServerSocket(0).use { it.localPort }
|
||||
val pcmFile = File.createTempFile("browser-pcm", ".bin").also { it.deleteOnExit() }
|
||||
|
||||
val bun = resolveBunBinary()
|
||||
val bunProc =
|
||||
ProcessBuilder(
|
||||
bun,
|
||||
"run",
|
||||
File(harnessDir, "src/server.ts").absolutePath,
|
||||
"--port",
|
||||
bunPort.toString(),
|
||||
"--root",
|
||||
distDir.absolutePath,
|
||||
"--out-pcm",
|
||||
pcmFile.absolutePath,
|
||||
).directory(harnessDir)
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
val bunDrainer = PlaywrightProcessDrainer(bunProc, "bun-server").also { it.start() }
|
||||
|
||||
try {
|
||||
bunDrainer.waitForLine("ready", BUN_READY_TIMEOUT_MS)
|
||||
|
||||
// 2) Compose the harness page URL. The relay URL is already
|
||||
// a `https://host:port/path?jwt=...` string from
|
||||
// `buildRelayConnectTarget` — URL-encode it once for the
|
||||
// `?relay=` slot so the inner `?jwt=` doesn't truncate.
|
||||
val encodedRelay =
|
||||
java.net.URLEncoder.encode(relayUrlFull, Charsets.UTF_8)
|
||||
val pageUrl =
|
||||
"http://127.0.0.1:$bunPort/$page" +
|
||||
"?relay=$encodedRelay" +
|
||||
"&broadcast=$broadcastPath" +
|
||||
"&track=$track" +
|
||||
"&wsPort=$bunPort" +
|
||||
"&duration=$durationSec" +
|
||||
extraQuery
|
||||
|
||||
// 3) Spawn Playwright. Use bun's `bun x` if available so we
|
||||
// don't need a separate node install; falls back to npx.
|
||||
val pwCmd = mutableListOf<String>()
|
||||
if (File(bun).canExecute()) {
|
||||
pwCmd += listOf(bun, "x", "playwright", "test", "--config=playwright.config.ts")
|
||||
} else {
|
||||
pwCmd += listOf("npx", "playwright", "test", "--config=playwright.config.ts")
|
||||
}
|
||||
val pwProc =
|
||||
ProcessBuilder(pwCmd)
|
||||
.directory(harnessDir)
|
||||
.redirectErrorStream(true)
|
||||
.also { pb ->
|
||||
pb.environment()["NESTS_HARNESS_URL"] = pageUrl
|
||||
pb.environment()["NESTS_TIMEOUT_MS"] =
|
||||
(overallTimeoutSec * 1_000).toString()
|
||||
// Inherit PLAYWRIGHT_BROWSERS_PATH if the host
|
||||
// has it (the agent runner ships it pointing at
|
||||
// /opt/pw-browsers); otherwise Playwright falls
|
||||
// back to ~/.cache/ms-playwright.
|
||||
// No-op when env is already inherited.
|
||||
}.start()
|
||||
val pwDrainer = PlaywrightProcessDrainer(pwProc, "playwright").also { it.start() }
|
||||
|
||||
val exited = pwProc.waitFor(overallTimeoutSec.toLong(), TimeUnit.SECONDS)
|
||||
if (!exited) {
|
||||
runCatching { pwProc.destroyForcibly() }
|
||||
val tail = pwDrainer.tail()
|
||||
throw IllegalStateException(
|
||||
"Playwright did not exit within ${overallTimeoutSec}s.\n" +
|
||||
"--- playwright tail ---\n$tail",
|
||||
)
|
||||
}
|
||||
// Allow the bun server a brief moment to flush the WS frames
|
||||
// it's still writing to disk before we read the PCM.
|
||||
Thread.sleep(200)
|
||||
return HarnessRun(
|
||||
pcmFile = pcmFile,
|
||||
playwrightStdout = pwDrainer.tail(),
|
||||
exitCode = pwProc.exitValue(),
|
||||
)
|
||||
} finally {
|
||||
runCatching { bunProc.destroy() }
|
||||
if (!bunProc.waitFor(3, TimeUnit.SECONDS)) {
|
||||
runCatching { bunProc.destroyForcibly() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requireHarnessDir(): File {
|
||||
val raw = System.getProperty(HARNESS_DIR_PROPERTY)
|
||||
check(!raw.isNullOrBlank()) {
|
||||
"system property '$HARNESS_DIR_PROPERTY' not set — did the Gradle test task forward it?"
|
||||
}
|
||||
val dir = File(raw)
|
||||
check(dir.isDirectory) {
|
||||
"$HARNESS_DIR_PROPERTY = '$raw' is not a directory"
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
private fun resolveBunBinary(): String {
|
||||
System.getenv("BUN_BIN")?.let { return it }
|
||||
System.getProperty("bunBin")?.let { return it }
|
||||
val agentPath = "/root/.bun/bin/bun"
|
||||
if (File(agentPath).canExecute()) return agentPath
|
||||
return "bun"
|
||||
}
|
||||
|
||||
private const val BUN_READY_TIMEOUT_MS = 30_000L
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures the relay's leaf certificate during a QUIC TLS handshake
|
||||
* so the test driver can pin it via Chromium's
|
||||
* `WebTransport({ serverCertificateHashes: [...] })` option.
|
||||
*
|
||||
* Why we need this: Chromium's `--ignore-certificate-errors` flag does
|
||||
* NOT apply to QUIC — see crbug.com/1190655 — so we can't simply skip
|
||||
* certificate validation the way the Kotlin clients do.
|
||||
* `serverCertificateHashes` is the supported alternative for
|
||||
* test-only WebTransport pinning, accepting a SHA-256 of the entire
|
||||
* DER-encoded X.509 certificate as long as the cert is ECDSA P-256
|
||||
* and valid for ≤ 14 days. moq-relay's `--tls-generate` produces
|
||||
* exactly that (rcgen default = ECDSA P-256, validity = 14 days; see
|
||||
* `kixelated/moq/rs/moq-native/src/tls.rs:140`), so we can pin it.
|
||||
*/
|
||||
internal class CertCapturingValidator : com.vitorpamplona.quic.tls.CertificateValidator {
|
||||
@Volatile private var captured: ByteArray? = null
|
||||
|
||||
override fun validateChain(
|
||||
chain: List<ByteArray>,
|
||||
expectedHost: String,
|
||||
) {
|
||||
if (captured == null && chain.isNotEmpty()) {
|
||||
captured = chain.first().copyOf()
|
||||
}
|
||||
}
|
||||
|
||||
override fun verifySignature(
|
||||
signatureAlgorithm: Int,
|
||||
signature: ByteArray,
|
||||
transcriptHash: ByteArray,
|
||||
) {
|
||||
// No-op; we're just here for the cert.
|
||||
}
|
||||
|
||||
/** SHA-256 of the captured DER cert, base64-encoded. Null until handshake completes. */
|
||||
fun derSha256(): ByteArray? {
|
||||
val der = captured ?: return null
|
||||
return java.security.MessageDigest
|
||||
.getInstance("SHA-256")
|
||||
.digest(der)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal stdout drainer for the bun + Playwright subprocesses.
|
||||
* Mirrors the private one in `NativeMoqRelayHarness.kt` — kept
|
||||
* separate so the two test entry points don't share file-private
|
||||
* symbols.
|
||||
*/
|
||||
private class PlaywrightProcessDrainer(
|
||||
private val process: Process,
|
||||
private val name: String,
|
||||
) {
|
||||
private val ring = java.util.concurrent.ConcurrentLinkedQueue<String>()
|
||||
private val maxLines = 256
|
||||
private val lock =
|
||||
java.util.concurrent.locks
|
||||
.ReentrantLock()
|
||||
private val newLineCond = lock.newCondition()
|
||||
|
||||
fun start() {
|
||||
Thread({
|
||||
process.inputStream.bufferedReader().useLines { lines ->
|
||||
for (line in lines) {
|
||||
ring.add(line)
|
||||
while (ring.size > maxLines) ring.poll()
|
||||
lock.lock()
|
||||
try {
|
||||
newLineCond.signalAll()
|
||||
} finally {
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}, "PlaywrightDriver-$name").apply {
|
||||
isDaemon = true
|
||||
start()
|
||||
}
|
||||
}
|
||||
|
||||
fun tail(): String = ring.joinToString("\n")
|
||||
|
||||
fun waitForLine(
|
||||
needle: String,
|
||||
timeoutMs: Long,
|
||||
) {
|
||||
val deadlineNanos =
|
||||
System.nanoTime() +
|
||||
java.util.concurrent.TimeUnit.MILLISECONDS
|
||||
.toNanos(timeoutMs)
|
||||
if (ring.any { it.contains(needle) }) return
|
||||
lock.lock()
|
||||
try {
|
||||
while (true) {
|
||||
if (ring.any { it.contains(needle) }) return
|
||||
val remaining = deadlineNanos - System.nanoTime()
|
||||
if (remaining <= 0) {
|
||||
throw IllegalStateException(
|
||||
"did not observe '$needle' in $name output within ${timeoutMs}ms.\n" +
|
||||
"--- $name tail ---\n${tail()}",
|
||||
)
|
||||
}
|
||||
newLineCond.awaitNanos(remaining)
|
||||
}
|
||||
} finally {
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user