|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { BrowserContext, Page } from "@playwright/test"; |
| 9 | + |
| 10 | +import { Config, CONFIG_JSON } from "../index.js"; |
| 11 | + |
| 12 | +/** Construct a suitable config.json for the given homeserver |
| 13 | + * |
| 14 | + * @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to. |
| 15 | + * @param additionalConfig - Additional config to add to the default config.json. |
| 16 | + * @param labsFlags - Lab flags to enable in the client. |
| 17 | + * @param disablePresence - Whether to disable presence for the given homeserver. |
| 18 | + */ |
| 19 | +export function buildConfigJson( |
| 20 | + homeserverBaseUrl: string, |
| 21 | + additionalConfig: Partial<Config> = {}, |
| 22 | + labsFlags: string[] = [], |
| 23 | + disablePresence: boolean = false, |
| 24 | +): Partial<Config> { |
| 25 | + const json = { |
| 26 | + ...CONFIG_JSON, |
| 27 | + ...additionalConfig, |
| 28 | + default_server_config: { |
| 29 | + "m.homeserver": { |
| 30 | + base_url: homeserverBaseUrl, |
| 31 | + }, |
| 32 | + ...additionalConfig.default_server_config, |
| 33 | + }, |
| 34 | + }; |
| 35 | + json["features"] = { |
| 36 | + ...json["features"], |
| 37 | + // Enable the lab features |
| 38 | + ...labsFlags.reduce<NonNullable<(typeof CONFIG_JSON)["features"]>>((obj, flag) => { |
| 39 | + obj[flag] = true; |
| 40 | + return obj; |
| 41 | + }, {}), |
| 42 | + }; |
| 43 | + if (disablePresence) { |
| 44 | + json["enable_presence_by_hs_url"] = { |
| 45 | + [homeserverBaseUrl]: false, |
| 46 | + }; |
| 47 | + } |
| 48 | + return json; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Add a route to the browser context/page which will serve a suitable config.json for the given homeserver. |
| 53 | + * |
| 54 | + * @param context - The browser context or page to route the config.json to. |
| 55 | + * @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to. |
| 56 | + * @param additionalConfig - Additional config to add to the default config.json. |
| 57 | + * @param labsFlags - Lab flags to enable in the client. |
| 58 | + * @param disablePresence - Whether to disable presence for the given homeserver. |
| 59 | + */ |
| 60 | +export async function routeConfigJson( |
| 61 | + context: BrowserContext | Page, |
| 62 | + homeserverBaseUrl: string, |
| 63 | + additionalConfig: Partial<Config> = {}, |
| 64 | + labsFlags: string[] = [], |
| 65 | + disablePresence: boolean = false, |
| 66 | +): Promise<void> { |
| 67 | + await context.route(`http://localhost:8080/config.json*`, async (route) => { |
| 68 | + const json = buildConfigJson(homeserverBaseUrl, additionalConfig, labsFlags, disablePresence); |
| 69 | + await route.fulfill({ json }); |
| 70 | + }); |
| 71 | +} |
0 commit comments