Add playwright test for widget with sdk rtc data sending

This commit is contained in:
Timo K.
2026-11-13 17:45:58 +01:00
parent 0475f2b2b0
commit 84d596d2e4
13 changed files with 285 additions and 18 deletions
+1
View File
@@ -2,6 +2,7 @@ node_modules
.DS_Store
.env
dist
/dist-widget-rtc-sdk-example
dist-ssr
*.local
*.bkp
+1
View File
@@ -60,4 +60,5 @@ pnpm backend # Synapse + LiveKit, required for e2e
pnpm test:playwright # or :open
pnpm dev:component # component harness, port 3001
pnpm dev:sdk # SDK harness, port 3002
pnpm dev:widget-rtc-sdk # widget RTC SDK example widget, port 3003
```
+1
View File
@@ -15,6 +15,7 @@ export default {
"vite-sdk.config.ts",
"vite-sdk-dev.config.ts",
"vite-widget-rtc-sdk.config.ts",
"vite-widget-rtc-sdk-dev.config.ts",
"vite-component.config.ts",
"vite-component-dev.config.ts",
],
+1
View File
@@ -8,6 +8,7 @@
"dev:embedded": "vite --config vite-embedded.config.js",
"dev:component": "vite --config vite-component-dev.config.ts",
"dev:sdk": "vite --config vite-sdk-dev.config.ts",
"dev:widget-rtc-sdk": "vite --config vite-widget-rtc-sdk-dev.config.ts",
"build": "pnpm build:full",
"build:full": "node --max-old-space-size=16384 node_modules/vite/bin/vite.js build",
"build:full:production": "pnpm build:full",
+16
View File
@@ -13,6 +13,7 @@ import { fileURLToPath } from "node:url";
import { COMPONENT_HARNESS_URL } from "./playwright/component/harness.ts";
import { SDK_HARNESS_URL } from "./playwright/sdk/harness.ts";
import { WIDGET_RTC_SDK_URL } from "./playwright/widget/test-helpers.ts";
const baseURL = process.env.USE_DOCKER
? "http://localhost:8080"
@@ -183,5 +184,20 @@ export default defineConfig({
timeout: 500,
},
},
{
// The widget RTC SDK's example widget, which Element Web opens as a
// widget. Bundled rather than from the dev server: Firefox now and then
// stalls on the dev server's hundreds of module requests.
command:
"pnpm exec vite build --config vite-widget-rtc-sdk-dev.config.ts && " +
"pnpm exec vite preview --config vite-widget-rtc-sdk-dev.config.ts",
url: WIDGET_RTC_SDK_URL,
reuseExistingServer: !process.env.CI,
ignoreHTTPSErrors: true,
gracefulShutdown: {
signal: "SIGTERM",
timeout: 500,
},
},
],
});
+5 -3
View File
@@ -32,9 +32,11 @@ export interface MyFixtures {
export const widgetTest = test.extend<MyFixtures>({
// allow per-test override: `widgetTest.use({ callType: "dm" })`
callType: ["room", { option: true }],
asWidget: async ({ browser, callType }, pUse) => {
const brooksDisplayName = `brooks_${Date.now()}`;
const whistlerDisplayName = `whistler_${Date.now()}`;
asWidget: async ({ browser, callType }, pUse, testInfo) => {
// Tests in parallel workers can reach this in the same millisecond.
const uniqueSuffix = `${Date.now()}_${testInfo.parallelIndex}`;
const brooksDisplayName = `brooks_${uniqueSuffix}`;
const whistlerDisplayName = `whistler_${uniqueSuffix}`;
// Register users
const {
+6
View File
@@ -22,6 +22,12 @@ const PASSWORD = "foobarbaz1!";
export const HOST1 = "https://app.m.localhost/#/welcome";
export const HOST2 = "https://app.othersite.m.localhost/#/welcome";
/**
* Where the widget RTC SDK's example widget is served (`pnpm dev:widget-rtc-sdk`):
* `widget-rtc-sdk/index.html`, running the bundle from source.
*/
export const WIDGET_RTC_SDK_URL = "https://localhost:3003";
export type RtcMode = "compat" | "2_0";
export class TestHelpers {
+129
View File
@@ -0,0 +1,129 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, type FrameLocator, type Page } from "@playwright/test";
import { widgetTest } from "../fixtures/widget-user.ts";
import { WIDGET_RTC_SDK_URL } from "./test-helpers.ts";
/**
* The widget RTC SDK's example widget, `widget-rtc-sdk/index.html`, added to a
* room as a custom widget and opened in two Element Webs: the bundle's whole
* public API, from the widget API to the data channel.
*/
widgetTest.describe.configure({ timeout: 300_000 });
const WIDGET_ID = "widget-rtc-sdk-example";
const WIDGET_NAME = "Widget RTC SDK";
/** Accepts the prompts Element Web shows before it runs a widget, and finds it. */
async function openWidget(page: Page): Promise<FrameLocator> {
// Only for a widget someone else added
const loadPrompt = page.getByRole("button", { name: "Continue" });
const capabilitiesPrompt = page.getByRole("button", { name: "Approve" });
await expect(loadPrompt.or(capabilitiesPrompt)).toBeVisible({
timeout: 30_000,
});
if (await loadPrompt.isVisible()) await loadPrompt.click();
await capabilitiesPrompt.click({ timeout: 30_000 });
// The SDK authenticates with the media transport by an OpenID token, and
// asks for more than one, so the answer has to be remembered
const openIdPrompt = page.getByRole("dialog", {
name: "Allow this widget to verify your identity",
});
await openIdPrompt.getByText("Remember this").click({ timeout: 30_000 });
await openIdPrompt.getByRole("button", { name: "Continue" }).click();
return page.frameLocator(`iframe[title="${WIDGET_NAME}"]`);
}
widgetTest(
"data sent from one widget arrives in the other",
async ({ asWidget }) => {
const { brooks, whistler } = asWidget;
// Brooks adds the example widget to the room and pins it to the top, so
// that both Element Webs open it without anyone looking for it
await brooks.clientHandle.evaluate(
async (client, { widgetId, name, url }) => {
const roomId = client
.getRooms()
.find((room) => room.name === "Welcome Room")!.roomId;
await client.sendStateEvent(
roomId,
// @ts-expect-error not in the js-sdk's state event map
"im.vector.modular.widgets",
{
id: widgetId,
type: "m.custom",
name,
url,
data: {},
creatorUserId: client.getUserId(),
},
widgetId,
);
await client.sendStateEvent(
roomId,
// @ts-expect-error not in the js-sdk's state event map
"io.element.widgets.layout",
{
widgets: {
[widgetId]: {
container: "top",
index: 0,
width: 100,
height: 50,
},
},
},
"",
);
},
{
widgetId: WIDGET_ID,
name: WIDGET_NAME,
// Element Web fills the $ variables in and adds widgetId and parentUrl
url:
`${WIDGET_RTC_SDK_URL}/?widgetId=$matrix_widget_id&roomId=$matrix_room_id` +
"&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id" +
"&baseUrl=$org.matrix.msc4039.matrix_base_url",
},
);
const brooksWidget = await openWidget(brooks.page);
const whistlerWidget = await openWidget(whistler.page);
// The example joins on load; each sees the other as a member
for (const [widget, other] of [
[brooksWidget, whistler],
[whistlerWidget, brooks],
] as const) {
await expect(widget.locator("#connect_status")).toHaveText("Connected", {
timeout: 60_000,
});
await expect(widget.locator("#members")).toContainText(other.mxId, {
timeout: 60_000,
});
}
// Data is not queued: the receiver drops what arrives before it has matched
// the sender on the media transport, which the example does not show. So
// send until it arrives.
await expect(async () => {
await brooksWidget.getByRole("button", { name: "Send Text" }).click();
await expect(whistlerWidget.locator("#data")).toContainText(
"Hello, world!",
{ timeout: 2_000 },
);
}).toPass({ timeout: 60_000 });
// Our own data is not echoed back
await expect(brooksWidget.locator("#data")).not.toContainText(
"Hello, world!",
);
},
);
+74
View File
@@ -0,0 +1,74 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { defineConfig, searchForWorkspaceRoot } from "vite";
import { realpathSync } from "node:fs";
import * as fs from "node:fs";
import { vitePluginsConfig, withoutMatrixCrypto } from "./vite.config.ts";
// Serves the widget RTC SDK's example widget, `widget-rtc-sdk/index.html`, with
// its import of a deployed bundle pointed at the source instead, so that a host
// can open the example as a widget against the code in the tree. The shape of
// the SDK's development config.
//
// `vite build` and `vite preview` with this config serve the example bundled,
// which the e2e tests use: unbundled, the page is hundreds of module requests,
// on which Firefox now and then stalls.
export default defineConfig(({ mode }) => {
const allow = [searchForWorkspaceRoot(process.cwd())];
for (const path of [
"node_modules/matrix-js-sdk/node_modules/@matrix-org/matrix-sdk-crypto-wasm",
"node_modules/@matrix-org/matrix-sdk-crypto-wasm",
]) {
try {
allow.push(realpathSync(path));
} catch {}
}
const base = vitePluginsConfig({ mode, html: false });
return {
...base,
root: "widget-rtc-sdk",
publicDir: false,
plugins: [
...(base.plugins ?? []),
// As in the bundle itself (vite-widget-rtc-sdk.config.ts)
withoutMatrixCrypto(),
{
name: "widget-rtc-sdk-from-source",
transformIndexHtml: {
order: "pre" as const,
handler: (html: string): string =>
html.replace(
/https:\/\/pr\d+-widget-rtc-sdk--element-call\.netlify\.app\/widget-rtc-sdk\.js/,
"/main.ts",
),
},
},
],
server: {
host: true,
// After the app (3000), the component harness (3001) and the SDK harness
// (3002), so that all of them can run at once
port: 3003,
strictPort: true,
fs: { allow },
// The same certificate the app uses, which Element Web's pages trust
https: {
key: fs.readFileSync("./backend/dev_tls_m.localhost.key"),
cert: fs.readFileSync("./backend/dev_tls_m.localhost.crt"),
},
},
// The e2e tests' copy, out of the way of `pnpm build:widget-rtc-sdk`'s
build: { outDir: "../dist-widget-rtc-sdk-example", emptyOutDir: true },
preview: { port: 3003, strictPort: true },
worker: {
format: "es" as const,
},
};
});
+3 -1
View File
@@ -10,6 +10,8 @@ import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import nodePolyfills from "vite-plugin-node-stdlib-browser";
import { withoutMatrixCrypto } from "./vite.config.ts";
// Config for the widget RTC SDK: a single bundle that wraps the sdk/ package for
// a widget page. CI deploys it as the `pr<N>-widget-rtc-sdk` Netlify preview.
export default defineConfig(({ mode }) => ({
@@ -40,5 +42,5 @@ export default defineConfig(({ mode }) => ({
fileName: "widget-rtc-sdk",
},
},
plugins: [nodePolyfills()],
plugins: [nodePolyfills(), withoutMatrixCrypto()],
}));
+44
View File
@@ -195,6 +195,50 @@ export default ({
};
};
/**
* Leaves the Matrix SDK's own end-to-end encryption out of a build that only
* runs as a widget: the host's client encrypts and decrypts for it, so the Rust
* crypto chunk and its WASM (about 10 MB) would only be shipped, never loaded.
* The SDK imports it dynamically, from `initRustCrypto` and `preloadRustCrypto`;
* here those reject instead.
*
* With `standaloneClient`, the worker behind the standalone app's sync store
* goes too: a second copy of the SDK with the crypto, which only a client of
* Element Call's own uses.
*/
export function withoutMatrixCrypto({
standaloneClient = false,
}: { standaloneClient?: boolean } = {}): Plugin {
const crypto = "\0without-matrix-crypto";
const worker = "\0without-matrix-store-worker";
const unavailable =
"new Error('This build runs as a widget only: the host does the crypto')";
return {
name: "without-matrix-crypto",
enforce: "pre",
resolveId(source, importer): string | null {
if (
importer?.includes("matrix-js-sdk") &&
/(^|\/)rust-crypto\/index(\.js)?$/.test(source)
)
return crypto;
if (standaloneClient && source.endsWith("/IndexedDBWorker?worker"))
return worker;
return null;
},
load(id): string | null {
if (id === crypto)
return [
`export async function initRustCrypto() { throw ${unavailable}; }`,
`export async function preloadRustCrypto() { throw ${unavailable}; }`,
].join("\n");
if (id === worker)
return `export default class { constructor() { throw ${unavailable}; } }`;
return null;
},
};
}
/**
* Keeps a plugin's transform off MediaPipe's WASM loader. The loader is a
* classic script that sets `self.ModuleFactory`; transformed into a module it
+3 -2
View File
@@ -50,8 +50,9 @@ https://pr<N>-widget-rtc-sdk--element-call.netlify.app/widget-rtc-sdk.js
/addwidget http://localhost:8080?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
```
3. Open the widget and accept the capabilities it asks for:
the example joins on load. Open it from a second account to see members and
3. Open the widget, approve the capabilities it asks for and allow it to
verify your identity, ticking "Remember this": it asks for an OpenID token
more than once. The example joins on load. Open it from a second account to see members and
data arrive.
## Building it yourself
+1 -12
View File
@@ -46,7 +46,6 @@ import {
const TOPIC = "matrixRTC";
/** The widget actions of Element Web's that this bundle takes part in. */
const JOIN_ACTION = "io.element.join";
const HANGUP_ACTION = "im.vector.hangup";
const CLOSE_ACTION = "io.element.close";
@@ -241,14 +240,7 @@ export async function createMatrixRTCSdk(
),
);
const leave = (): void => {
const participation = slot.participation$.value;
if (participation === null) return;
participation.leave();
api.transport
.send(HANGUP_ACTION, {})
.catch(failed("Failed to notify the host that we hung up"));
};
const leave = (): void => slot.participation$.value?.leave();
// The host's hang-up button
fromEvent<CustomEvent>(api, `action:${HANGUP_ACTION}`)
@@ -267,9 +259,6 @@ export async function createMatrixRTCSdk(
.setAlwaysOnScreen(true)
.catch(failed("Failed to make the widget sticky"));
const participation = slot.join(participationOptions);
api.transport
.send(JOIN_ACTION, {})
.catch(failed("Failed to notify the host that we joined"));
// Once the hang-up has gone through, unstick and ask the host to close us
participation.state$
.pipe(