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 e3fa699e3..e8033a629 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/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