From 84d596d2e4a8bac23555c17a081563ecc802f0e0 Mon Sep 17 00:00:00 2001 From: "Timo K." Date: Fri, 13 Nov 2026 17:45:58 +0100 Subject: [PATCH] Add playwright test for widget with sdk rtc data sending --- .gitignore | 1 + docs/agents/testing.md | 1 + knip.ts | 1 + package.json | 1 + playwright.config.ts | 16 +++ playwright/fixtures/widget-user.ts | 8 +- playwright/widget/test-helpers.ts | 6 ++ playwright/widget/widget-rtc-sdk.spec.ts | 129 +++++++++++++++++++++++ vite-widget-rtc-sdk-dev.config.ts | 74 +++++++++++++ vite-widget-rtc-sdk.config.ts | 4 +- vite.config.ts | 44 ++++++++ widget-rtc-sdk/README.md | 5 +- widget-rtc-sdk/main.ts | 13 +-- 13 files changed, 285 insertions(+), 18 deletions(-) create mode 100644 playwright/widget/widget-rtc-sdk.spec.ts create mode 100644 vite-widget-rtc-sdk-dev.config.ts diff --git a/.gitignore b/.gitignore index 3d9e7f92b..bc994b826 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node_modules .DS_Store .env dist +/dist-widget-rtc-sdk-example dist-ssr *.local *.bkp diff --git a/docs/agents/testing.md b/docs/agents/testing.md index ba203fd11..d4f16ecf7 100644 --- a/docs/agents/testing.md +++ b/docs/agents/testing.md @@ -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 ``` diff --git a/knip.ts b/knip.ts index 1574a4a1a..bc9b6a705 100644 --- a/knip.ts +++ b/knip.ts @@ -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", ], diff --git a/package.json b/package.json index 875f958ca..a22261416 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/playwright.config.ts b/playwright.config.ts index ebd5feea7..b30bee83b 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -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, + }, + }, ], }); diff --git a/playwright/fixtures/widget-user.ts b/playwright/fixtures/widget-user.ts index f069db1a1..8a1361949 100644 --- a/playwright/fixtures/widget-user.ts +++ b/playwright/fixtures/widget-user.ts @@ -32,9 +32,11 @@ export interface MyFixtures { export const widgetTest = test.extend({ // 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 { diff --git a/playwright/widget/test-helpers.ts b/playwright/widget/test-helpers.ts index 38e61f49c..4622b12f4 100644 --- a/playwright/widget/test-helpers.ts +++ b/playwright/widget/test-helpers.ts @@ -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 { diff --git a/playwright/widget/widget-rtc-sdk.spec.ts b/playwright/widget/widget-rtc-sdk.spec.ts new file mode 100644 index 000000000..f4f9fb843 --- /dev/null +++ b/playwright/widget/widget-rtc-sdk.spec.ts @@ -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 { + // 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!", + ); + }, +); diff --git a/vite-widget-rtc-sdk-dev.config.ts b/vite-widget-rtc-sdk-dev.config.ts new file mode 100644 index 000000000..608e57db2 --- /dev/null +++ b/vite-widget-rtc-sdk-dev.config.ts @@ -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, + }, + }; +}); diff --git a/vite-widget-rtc-sdk.config.ts b/vite-widget-rtc-sdk.config.ts index 36edf2580..8bb84e806 100644 --- a/vite-widget-rtc-sdk.config.ts +++ b/vite-widget-rtc-sdk.config.ts @@ -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-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()], })); diff --git a/vite.config.ts b/vite.config.ts index 3e9f6aef1..9276fe93c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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 diff --git a/widget-rtc-sdk/README.md b/widget-rtc-sdk/README.md index 1f4c1f1c4..a62582e7b 100644 --- a/widget-rtc-sdk/README.md +++ b/widget-rtc-sdk/README.md @@ -50,8 +50,9 @@ https://pr-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 diff --git a/widget-rtc-sdk/main.ts b/widget-rtc-sdk/main.ts index fb29442ff..690ef033b 100644 --- a/widget-rtc-sdk/main.ts +++ b/widget-rtc-sdk/main.ts @@ -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(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(