|
| 1 | +import { type Page } from "@playwright/test"; |
| 2 | +import { type E2ELocaleCode, translateForE2E } from "./i18n.js"; |
| 3 | + |
| 4 | +type SettingsSection = "general" | "appearance" | "providers" | "shortcuts"; |
| 5 | +type ProviderSettingLabel = |
| 6 | + | "base" |
| 7 | + | "config_file" |
| 8 | + | "open_config_file_editor" |
| 9 | + | "back_to_base" |
| 10 | + | "startup_args"; |
| 11 | +type SettingsGroupLabel = "notifications" | "theme" | "language"; |
| 12 | +type ConfigFileLabel = "claude" | "codex"; |
| 13 | + |
| 14 | +const SETTINGS_SECTION_KEYS: Record<SettingsSection, Parameters<typeof translateForE2E>[0]> = { |
| 15 | + general: "settings.general", |
| 16 | + appearance: "settings.appearance", |
| 17 | + providers: "settings.providers", |
| 18 | + shortcuts: "settings.shortcuts.title", |
| 19 | +}; |
| 20 | + |
| 21 | +const PROVIDER_SETTING_KEYS: Record<ProviderSettingLabel, Parameters<typeof translateForE2E>[0]> = { |
| 22 | + base: "settings.provider.base", |
| 23 | + config_file: "settings.provider.config_file", |
| 24 | + open_config_file_editor: "settings.provider.open_config_file_editor", |
| 25 | + back_to_base: "settings.provider.back_to_base", |
| 26 | + startup_args: "settings.provider.startup_args", |
| 27 | +}; |
| 28 | + |
| 29 | +const SETTINGS_GROUP_KEYS: Record<SettingsGroupLabel, Parameters<typeof translateForE2E>[0]> = { |
| 30 | + notifications: "settings.notifications", |
| 31 | + theme: "settings.theme.title", |
| 32 | + language: "settings.language.title", |
| 33 | +}; |
| 34 | + |
| 35 | +const CONFIG_FILE_KEYS: Record<ConfigFileLabel, Parameters<typeof translateForE2E>[0]> = { |
| 36 | + claude: "settings.config_files.claude_config", |
| 37 | + codex: "settings.config_files.codex_config", |
| 38 | +}; |
| 39 | + |
| 40 | +export const AUTH_PREVIEW_URL = new URL("../../packages/web/auth-preview.html", import.meta.url) |
| 41 | + .href; |
| 42 | + |
| 43 | +function escapeRegExp(value: string): string { |
| 44 | + return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 45 | +} |
| 46 | + |
| 47 | +function localizedPattern( |
| 48 | + key: Parameters<typeof translateForE2E>[0], |
| 49 | + params?: Record<string, string | number> |
| 50 | +): RegExp { |
| 51 | + const en = translateForE2E(key, "en", params); |
| 52 | + const zh = translateForE2E(key, "zh", params); |
| 53 | + const values = [...new Set([en, zh])].map(escapeRegExp); |
| 54 | + return new RegExp(`^(?:${values.join("|")})$`); |
| 55 | +} |
| 56 | + |
| 57 | +export function settingsSectionLabel( |
| 58 | + section: SettingsSection, |
| 59 | + locale: E2ELocaleCode = "zh" |
| 60 | +): string { |
| 61 | + return translateForE2E(SETTINGS_SECTION_KEYS[section], locale); |
| 62 | +} |
| 63 | + |
| 64 | +export function settingsSectionPattern(section: SettingsSection): RegExp { |
| 65 | + return localizedPattern(SETTINGS_SECTION_KEYS[section]); |
| 66 | +} |
| 67 | + |
| 68 | +export async function openSettingsSection( |
| 69 | + page: Page, |
| 70 | + section: SettingsSection, |
| 71 | + locale?: E2ELocaleCode |
| 72 | +): Promise<void> { |
| 73 | + await page |
| 74 | + .getByRole("button", { |
| 75 | + name: locale ? settingsSectionLabel(section, locale) : settingsSectionPattern(section), |
| 76 | + }) |
| 77 | + .click(); |
| 78 | +} |
| 79 | + |
| 80 | +export function providerSettingLabel( |
| 81 | + label: ProviderSettingLabel, |
| 82 | + locale: E2ELocaleCode = "zh" |
| 83 | +): string { |
| 84 | + return translateForE2E(PROVIDER_SETTING_KEYS[label], locale); |
| 85 | +} |
| 86 | + |
| 87 | +export function providerSettingPattern(label: ProviderSettingLabel): RegExp { |
| 88 | + return localizedPattern(PROVIDER_SETTING_KEYS[label]); |
| 89 | +} |
| 90 | + |
| 91 | +export function settingsGroupLabel( |
| 92 | + label: SettingsGroupLabel, |
| 93 | + locale: E2ELocaleCode = "zh" |
| 94 | +): string { |
| 95 | + return translateForE2E(SETTINGS_GROUP_KEYS[label], locale); |
| 96 | +} |
| 97 | + |
| 98 | +export function settingsGroupPattern(label: SettingsGroupLabel): RegExp { |
| 99 | + return localizedPattern(SETTINGS_GROUP_KEYS[label]); |
| 100 | +} |
| 101 | + |
| 102 | +export function configFileLabel(label: ConfigFileLabel, locale: E2ELocaleCode = "zh"): string { |
| 103 | + return translateForE2E(CONFIG_FILE_KEYS[label], locale); |
| 104 | +} |
| 105 | + |
| 106 | +export function configFilePattern(label: ConfigFileLabel): RegExp { |
| 107 | + return localizedPattern(CONFIG_FILE_KEYS[label]); |
| 108 | +} |
0 commit comments