1import { expect, type Page } from "@playwright/test";
2import type { Api } from "./api";
3
4/** Open the settings modal from the user menu and switch to a tab. */
5export async function openSettings(page: Page, tab: string | RegExp) {
6 await page.getByRole("button", { name: /^Opendesk / }).click();
7 await page.getByRole("menuitem", { name: "Settings" }).click();
8 const dialog = page.getByRole("dialog");
9 await dialog.getByRole("button", { name: tab, exact: true }).click();
10 await expect(dialog.getByRole("heading", { level: 1 }).nth(1)).toBeVisible();
11 return dialog;
12}
13
14/** Snapshot OD Settings fields so a test can put them back afterwards. */
15export async function snapshotSettings(api: Api, fields: string[]) {
16 const settings = await api.get("OD Settings", "OD Settings");
17 // REST updates skip nulls, so an empty field is restored as "".
18 const saved = Object.fromEntries(fields.map((field) => [field, settings[field] ?? ""]));
19 return () => api.update("OD Settings", "OD Settings", saved);
20}
21
22/** Create an active agent nobody else uses, safe to disable or delete. */
23export async function createThrowawayAgent(api: Api, email: string, password?: string) {
24 await api.insert("User", {
25 email,
26 first_name: email.split("@")[0],
27 send_welcome_email: 0,
28 roles: [{ role: "Agent" }],
29 ...(password && { new_password: password }),
30 });
31 return api.insert("OD Agent", { user: email, agent_name: email.split("@")[0], is_active: 1 });
32}
33