diff --git a/packages/element-web-playwright-common/src/index.ts b/packages/element-web-playwright-common/src/index.ts index 4f13e8be..cffc4466 100644 --- a/packages/element-web-playwright-common/src/index.ts +++ b/packages/element-web-playwright-common/src/index.ts @@ -9,6 +9,9 @@ Please see LICENSE files in the repository root for full details. import { type Config as BaseConfig } from "@element-hq/element-web-module-api"; import { test as base } from "./fixtures/index.js"; +import { routeConfigJson } from "./utils/config_json.js"; + +export * from "./utils/config_json.js"; // Enable experimental service worker support // See https://playwright.dev/docs/service-workers-experimental#how-to-enable @@ -68,32 +71,7 @@ export const test = base.extend({ labsFlags: async ({}, use) => use([]), disablePresence: async ({}, use) => use(false), page: async ({ homeserver, context, page, config, labsFlags, disablePresence }, use) => { - await context.route(`http://localhost:8080/config.json*`, async (route) => { - const json = { - ...CONFIG_JSON, - ...config, - default_server_config: { - "m.homeserver": { - base_url: homeserver.baseUrl, - }, - ...config.default_server_config, - }, - }; - json["features"] = { - ...json["features"], - // Enable the lab features - ...labsFlags.reduce>((obj, flag) => { - obj[flag] = true; - return obj; - }, {}), - }; - if (disablePresence) { - json["enable_presence_by_hs_url"] = { - [homeserver.baseUrl]: false, - }; - } - await route.fulfill({ json }); - }); + await routeConfigJson(context, homeserver.baseUrl, config, labsFlags, disablePresence); await use(page); }, }); diff --git a/packages/element-web-playwright-common/src/utils/config_json.ts b/packages/element-web-playwright-common/src/utils/config_json.ts new file mode 100644 index 00000000..f2a98f47 --- /dev/null +++ b/packages/element-web-playwright-common/src/utils/config_json.ts @@ -0,0 +1,71 @@ +/* +Copyright 2025 New Vector Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { BrowserContext, Page } from "@playwright/test"; + +import { Config, CONFIG_JSON } from "../index.js"; + +/** Construct a suitable config.json for the given homeserver + * + * @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to. + * @param additionalConfig - Additional config to add to the default config.json. + * @param labsFlags - Lab flags to enable in the client. + * @param disablePresence - Whether to disable presence for the given homeserver. + */ +export function buildConfigJson( + homeserverBaseUrl: string, + additionalConfig: Partial = {}, + labsFlags: string[] = [], + disablePresence: boolean = false, +): Partial { + const json = { + ...CONFIG_JSON, + ...additionalConfig, + default_server_config: { + "m.homeserver": { + base_url: homeserverBaseUrl, + }, + ...additionalConfig.default_server_config, + }, + }; + json["features"] = { + ...json["features"], + // Enable the lab features + ...labsFlags.reduce>((obj, flag) => { + obj[flag] = true; + return obj; + }, {}), + }; + if (disablePresence) { + json["enable_presence_by_hs_url"] = { + [homeserverBaseUrl]: false, + }; + } + return json; +} + +/** + * Add a route to the browser context/page which will serve a suitable config.json for the given homeserver. + * + * @param context - The browser context or page to route the config.json to. + * @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to. + * @param additionalConfig - Additional config to add to the default config.json. + * @param labsFlags - Lab flags to enable in the client. + * @param disablePresence - Whether to disable presence for the given homeserver. + */ +export async function routeConfigJson( + context: BrowserContext | Page, + homeserverBaseUrl: string, + additionalConfig: Partial = {}, + labsFlags: string[] = [], + disablePresence: boolean = false, +): Promise { + await context.route(`http://localhost:8080/config.json*`, async (route) => { + const json = buildConfigJson(homeserverBaseUrl, additionalConfig, labsFlags, disablePresence); + await route.fulfill({ json }); + }); +}