Skip to content

Commit 1215925

Browse files
authored
Merge pull request #81 from element-hq/rav/config_json_funcs
Playwright: factor out helpers for config.json
2 parents 35b6052 + 9bc44e7 commit 1215925

File tree

2 files changed

+75
-26
lines changed

2 files changed

+75
-26
lines changed

packages/element-web-playwright-common/src/index.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Please see LICENSE files in the repository root for full details.
99
import { type Config as BaseConfig } from "@element-hq/element-web-module-api";
1010

1111
import { test as base } from "./fixtures/index.js";
12+
import { routeConfigJson } from "./utils/config_json.js";
13+
14+
export * from "./utils/config_json.js";
1215

1316
export { populateLocalStorageWithCredentials } from "./fixtures/user.js";
1417

@@ -70,32 +73,7 @@ export const test = base.extend<TestFixtures>({
7073
labsFlags: async ({}, use) => use([]),
7174
disablePresence: async ({}, use) => use(false),
7275
page: async ({ homeserver, context, page, config, labsFlags, disablePresence }, use) => {
73-
await context.route(`http://localhost:8080/config.json*`, async (route) => {
74-
const json = {
75-
...CONFIG_JSON,
76-
...config,
77-
default_server_config: {
78-
"m.homeserver": {
79-
base_url: homeserver.baseUrl,
80-
},
81-
...config.default_server_config,
82-
},
83-
};
84-
json["features"] = {
85-
...json["features"],
86-
// Enable the lab features
87-
...labsFlags.reduce<NonNullable<(typeof CONFIG_JSON)["features"]>>((obj, flag) => {
88-
obj[flag] = true;
89-
return obj;
90-
}, {}),
91-
};
92-
if (disablePresence) {
93-
json["enable_presence_by_hs_url"] = {
94-
[homeserver.baseUrl]: false,
95-
};
96-
}
97-
await route.fulfill({ json });
98-
});
76+
await routeConfigJson(context, homeserver.baseUrl, config, labsFlags, disablePresence);
9977
await use(page);
10078
},
10179
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)