settings-chrome.e2e.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Web e2e scenarios: the settings surface — the modal shell (trigger, nav,
  2. // section switching, both close paths), the Appearance preference row (the
  3. // real theme gesture — click 深色 and the whole cascade runs: ThemeService preference -> Host settings
  4. // -> theme/change -> ui-layout's presenter -> body attribute -> alias token +
  5. // browser theme-color metadata)
  6. // the Language row and busy-state Enter preference (both Host-backed), plus
  7. // Permission as the persisted default for subsequently created sessions.
  8. // Zero model calls: everything is pure client + persistence state on a blank
  9. // frame, so there is no fixture and a stray stream would fail loud on the
  10. // open llm seam.
  11. import { readFile } from 'node:fs/promises'
  12. import { fileURLToPath } from 'node:url'
  13. import type { Browser, Page } from 'playwright'
  14. import { chromium } from 'playwright'
  15. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  16. import { join } from 'node:path'
  17. import { SessionId } from '@deepseek-ai/dsh-session'
  18. import {
  19. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  20. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  21. } from './scaffold.ts'
  22. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  23. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/settings-chrome', import.meta.url))
  24. const DIALOG_EXPECTED = join(SNAPSHOT_DIR, 'dialog.expected.md')
  25. const PLUGINS_EXPECTED = join(SNAPSHOT_DIR, 'plugins.expected.md')
  26. const PLUGIN_ROW_SELECTOR = '[data-plugin-entry$="ui-settings"]'
  27. const MODE = webSnapshotMode()
  28. describe('web e2e: settings modal and General preferences', () => {
  29. let scaffold: WebScaffold
  30. let browser: Browser
  31. let page: Page
  32. let tripwire: ReturnType<typeof watchConsole>
  33. beforeAll(async () => {
  34. scaffold = await launchWebScaffold({})
  35. browser = await chromium.launch()
  36. // Chinese browser: the shared page asserts the localized settings surface
  37. // the client derives from it (the English default has its own spec below).
  38. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  39. tripwire = watchConsole(page)
  40. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  41. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  42. }, 120_000)
  43. afterAll(async () => {
  44. await browser?.close()
  45. await scaffold?.close()
  46. })
  47. it('opens the settings dialog, switches sections, and closes by every path', async () => {
  48. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-shell'))
  49. const trigger = page.getByRole('button', { name: '设置', exact: true })
  50. expect(await trigger.getAttribute('aria-haspopup')).toBe('dialog')
  51. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  52. await trigger.click()
  53. const dialog = page.getByRole('dialog', { name: '设置' })
  54. await dialog.waitFor({ timeout: 10_000 })
  55. expect(await trigger.getAttribute('aria-expanded')).toBe('true')
  56. // General is active by default; Permission, Language and Appearance are functional.
  57. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBe('true')
  58. await dialog.getByRole('button', { name: 'Workspace Write' }).waitFor({ timeout: 10_000 })
  59. await expect.poll(() => dialog.getByText('语言', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  60. await expect.poll(() => dialog.getByText('外观', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  61. const openDocument = dialog.getByRole('button', { name: '打开配置文件' })
  62. await openDocument.waitFor({ timeout: 10_000 })
  63. let openRequests = 0
  64. await page.route('**/api/settings.openDocument', async (route) => {
  65. const envelope = route.request().postDataJSON() as {
  66. rpcId: string
  67. payload: Record<string, never>
  68. }
  69. expect(envelope.payload).toEqual({})
  70. openRequests += 1
  71. await route.fulfill({
  72. status: 200,
  73. contentType: 'application/json',
  74. body: JSON.stringify({
  75. type: 'server-response',
  76. rpcId: envelope.rpcId,
  77. result: { ok: true, value: { opened: true } },
  78. }),
  79. })
  80. })
  81. await openDocument.click()
  82. await expect.poll(() => openRequests, { timeout: 5_000 }).toBe(1)
  83. await expect.poll(() => openDocument.isEnabled(), { timeout: 5_000 }).toBe(true)
  84. await page.unroute('**/api/settings.openDocument')
  85. // Golden of the freshly opened dialog (default zh, General active).
  86. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  87. await compareOrRefreshGolden(DIALOG_EXPECTED, snapshot, MODE)
  88. // Section switch: aria-current moves (the Models page itself has its own scenario file).
  89. await dialog.getByRole('button', { name: '模型' }).click()
  90. await expect.poll(() => dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current'), { timeout: 5_000 }).toBe('true')
  91. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBeNull()
  92. // Plugins is a read-only projection of the same assembled Loader tree.
  93. // Capture one stable shipped row rather than the whole inventory so adding
  94. // an unrelated plugin does not rewrite this surface's golden.
  95. await dialog.getByRole('button', { name: '插件', exact: true }).click()
  96. await dialog.getByRole('heading', { name: '插件', exact: true }).waitFor({ timeout: 10_000 })
  97. const pluginRow = dialog.locator(PLUGIN_ROW_SELECTOR)
  98. await pluginRow.waitFor({ timeout: 10_000 })
  99. const expectedPluginCount = [...scaffold.ctx.loader.entries()]
  100. .filter(entry => !entry.options.group)
  101. .length
  102. expect(await dialog.getByRole('searchbox', { name: '搜索插件' }).count()).toBe(1)
  103. expect(await dialog.locator('[data-plugin-entry]').count()).toBe(expectedPluginCount)
  104. expect(await dialog.locator('[data-plugin-count]').getAttribute('data-plugin-count'))
  105. .toBe(String(expectedPluginCount))
  106. expect(await dialog.getByRole('button', { name: '插件', exact: true }).getAttribute('aria-current')).toBe('true')
  107. expect(await dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current')).toBeNull()
  108. const pluginsSnapshot = await captureStableAria(
  109. page,
  110. PLUGIN_ROW_SELECTOR,
  111. scaffold.workspaceCwd,
  112. )
  113. await compareOrRefreshGolden(PLUGINS_EXPECTED, pluginsSnapshot, MODE)
  114. // Close path 1: Escape.
  115. await page.keyboard.press('Escape')
  116. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  117. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  118. // Close path 2: the header close button (focus lands there on open).
  119. await trigger.click()
  120. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '关闭' }).click()
  121. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  122. expect(tripwire.pageErrors).toEqual([])
  123. }, 60_000)
  124. it('stores Permission as the default for future sessions without changing an existing session', async () => {
  125. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-permission'))
  126. const existing = scaffold.ctx.sessions.create(SessionId('settings-permission-before'))
  127. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  128. .toEqual({ preset: 'workspace-write' })
  129. await page.getByRole('button', { name: '设置', exact: true }).click()
  130. const dialog = page.getByRole('dialog', { name: '设置' })
  131. await dialog.waitFor({ timeout: 10_000 })
  132. const selector = dialog.getByRole('button', { name: 'Workspace Write' })
  133. await selector.waitFor({ timeout: 10_000 })
  134. await expect.poll(() => selector.isEnabled(), { timeout: 5_000 }).toBe(true)
  135. await selector.click()
  136. await page.getByRole('menuitem', { name: 'Read Only' }).click()
  137. await dialog.getByRole('button', { name: 'Read Only' }).waitFor({ timeout: 10_000 })
  138. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  139. expect(document).toContain('permission:')
  140. expect(document).toContain('defaultPreset: read-only')
  141. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  142. .toEqual({ preset: 'workspace-write' })
  143. const created = scaffold.ctx.sessions.create(SessionId('settings-permission-after'))
  144. expect(created.events.map(event => [event.type, event.data])).toEqual([
  145. ['permission/preset', { preset: 'read-only' }],
  146. ['sandbox/mode', { mode: 'read-only' }],
  147. ['approval/policy', { policy: 'ask' }],
  148. ])
  149. await dialog.getByRole('button', { name: 'Read Only' }).click()
  150. await page.getByRole('menuitem', { name: 'Full access' }).click()
  151. const confirmation = page.getByRole('dialog', { name: '确认启用 Full access?' })
  152. const enable = confirmation.getByRole('button', { name: '启用 Full access' })
  153. expect(await enable.isDisabled()).toBe(true)
  154. await confirmation.getByRole('checkbox').click()
  155. await enable.click()
  156. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  157. const confirmedDocument = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  158. expect(confirmedDocument).toContain('defaultPreset: danger-full-access')
  159. const confirmed = scaffold.ctx.sessions.create(SessionId('settings-permission-confirmed'))
  160. expect(confirmed.events.map(event => [event.type, event.data])).toEqual([
  161. ['permission/preset', { preset: 'danger-full-access' }],
  162. ['sandbox/mode', { mode: 'danger-full-access' }],
  163. ['approval/policy', { policy: 'never' }],
  164. ])
  165. await page.keyboard.press('Escape')
  166. expect(tripwire.pageErrors).toEqual([])
  167. }, 60_000)
  168. it('uses the persisted dark preference while plugins are still loading', async () => {
  169. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-boot-theme'))
  170. await page.emulateMedia({ colorScheme: 'light' })
  171. await page.getByRole('button', { name: '设置', exact: true }).click()
  172. const initialDialog = page.getByRole('dialog', { name: '设置' })
  173. const darkCube = initialDialog.getByRole('button', { name: '深色' })
  174. await darkCube.click()
  175. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  176. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  177. .toMatch(/ui-theme:\n\s+preference: dark/)
  178. await page.keyboard.press('Escape')
  179. // Hold real plugin bundles so the shell-owned loading page remains observable.
  180. const pluginPattern = '**/plugins/**'
  181. let releaseBundles = (): void => {}
  182. const bundlesReleased = new Promise<void>((resolve) => { releaseBundles = resolve })
  183. await page.route(pluginPattern, async (route) => {
  184. await bundlesReleased
  185. await route.continue()
  186. })
  187. const warningStart = tripwire.warnings.length
  188. let reload: ReturnType<Page['reload']> | undefined
  189. try {
  190. reload = page.reload({ waitUntil: 'domcontentloaded' })
  191. const loading = page.getByText('Loading plugins…', { exact: true })
  192. await loading.waitFor({ timeout: 10_000 })
  193. const state = await loading.evaluate((element) => {
  194. const boot = element.parentElement?.parentElement
  195. if (boot === undefined || boot === null) throw new Error('loading hint is detached from the boot page')
  196. return {
  197. attr: document.body.hasAttribute('data-ds-dark-theme'),
  198. background: getComputedStyle(boot).backgroundColor,
  199. colorScheme: document.documentElement.style.colorScheme,
  200. }
  201. })
  202. expect(state).toEqual({
  203. attr: true,
  204. background: 'rgb(21, 21, 23)',
  205. colorScheme: 'dark',
  206. })
  207. } finally {
  208. releaseBundles()
  209. await reload
  210. await page.unroute(pluginPattern)
  211. }
  212. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  213. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  214. await page.getByRole('button', { name: '设置', exact: true }).click()
  215. const restoredDialog = page.getByRole('dialog', { name: '设置' })
  216. const systemCube = restoredDialog.getByRole('button', { name: '跟随系统' })
  217. await systemCube.click()
  218. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  219. await expect.poll(() => page.evaluate(() => document.body.hasAttribute('data-ds-dark-theme')), {
  220. timeout: 5_000,
  221. }).toBe(false)
  222. await page.keyboard.press('Escape')
  223. expect(tripwire.pageErrors).toEqual([])
  224. }, 90_000)
  225. it('flips the theme through the Appearance cubes and persists across reload and a distinct port', async () => {
  226. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-appearance'))
  227. interface ThemeState {
  228. attr: boolean
  229. background: string
  230. /** Pre-migration localStorage key; the Host-backed world never writes it. */
  231. legacy: string | null
  232. themeColor: string | null
  233. themeColorCount: number
  234. token: string
  235. }
  236. const readState = async (target: Page = page): Promise<ThemeState> => await target.evaluate(() => {
  237. const metas = document.head.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]')
  238. const computed = getComputedStyle(document.body)
  239. return {
  240. attr: document.body.hasAttribute('data-ds-dark-theme'),
  241. background: computed.backgroundColor,
  242. legacy: localStorage.getItem('dsh.theme'),
  243. themeColor: metas[0]?.content ?? null,
  244. themeColorCount: metas.length,
  245. token: computed.getPropertyValue('--dsw-alias-bg-base').trim(),
  246. }
  247. })
  248. const expectThemeColorSynchronized = (state: ThemeState): void => {
  249. expect(state.themeColorCount).toBe(1)
  250. expect(state.background).not.toBe('rgba(0, 0, 0, 0)')
  251. expect(state.themeColor).toBe(state.background)
  252. }
  253. // Pin the OS scheme to light so the default `system` preference resolves
  254. // light and the dark flip below is unambiguously the gesture's doing.
  255. await page.emulateMedia({ colorScheme: 'light' })
  256. const light = await readState()
  257. expect(light.attr).toBe(false)
  258. expectThemeColorSynchronized(light)
  259. await page.getByRole('button', { name: '设置', exact: true }).click()
  260. const dialog = page.getByRole('dialog', { name: '设置' })
  261. await dialog.waitFor({ timeout: 10_000 })
  262. const darkCube = dialog.getByRole('button', { name: '深色' })
  263. expect(await darkCube.getAttribute('aria-pressed')).toBe('false')
  264. await darkCube.click()
  265. // The full cascade: pressed state, Host-backed preference, body attribute,
  266. // alias token flip — all from one real user gesture.
  267. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  268. const dark = await readState()
  269. expect(dark.attr).toBe(true)
  270. expect(dark.legacy).toBeNull()
  271. expect(dark.token).not.toBe(light.token)
  272. expectThemeColorSynchronized(dark)
  273. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  274. .toMatch(/ui-theme:\n\s+preference: dark/)
  275. await page.keyboard.press('Escape')
  276. // Reload: the preference survives the background Host read + presenter update.
  277. const warningStart = tripwire.warnings.length
  278. await page.reload({ waitUntil: 'load' })
  279. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  280. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  281. await page.emulateMedia({ colorScheme: 'light' })
  282. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  283. const reloaded = await readState()
  284. expect(reloaded.legacy).toBeNull()
  285. expectThemeColorSynchronized(reloaded)
  286. // A second live Host binds another ephemeral port but shares the same
  287. // user-settings home. Its fresh origin has no theme localStorage and still
  288. // converges to dark before the settings dialog opens.
  289. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  290. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  291. const secondTripwire = watchConsole(secondPage)
  292. try {
  293. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  294. await secondPage.emulateMedia({ colorScheme: 'light' })
  295. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  296. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  297. await expect.poll(async () => (await readState(secondPage)).attr, { timeout: 5_000 }).toBe(true)
  298. const secondState = await readState(secondPage)
  299. expect(secondState.legacy).toBeNull()
  300. expectThemeColorSynchronized(secondState)
  301. expect(secondTripwire.pageErrors).toEqual([])
  302. expect(secondTripwire.warnings).toEqual([])
  303. } finally {
  304. await secondPage.close()
  305. await second.close()
  306. }
  307. // `system` follows the emulated OS scheme (dark stays dark, light clears).
  308. await page.getByRole('button', { name: '设置', exact: true }).click()
  309. const systemCube = page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '跟随系统' })
  310. await systemCube.click()
  311. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  312. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  313. expectThemeColorSynchronized(await readState())
  314. await page.emulateMedia({ colorScheme: 'dark' })
  315. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  316. expectThemeColorSynchronized(await readState())
  317. // Restore for the specs that follow: light preference beats the emulated
  318. // dark OS scheme, leaving the shared page in the light default.
  319. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '浅色' }).click()
  320. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  321. expectThemeColorSynchronized(await readState())
  322. await page.keyboard.press('Escape')
  323. expect(tripwire.pageErrors).toEqual([])
  324. }, 90_000)
  325. it('persists the busy-state Enter behavior across reload and a distinct port', async () => {
  326. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-enter-behavior'))
  327. await page.getByRole('button', { name: '设置', exact: true }).click()
  328. const dialog = page.getByRole('dialog', { name: '设置' })
  329. await dialog.waitFor({ timeout: 10_000 })
  330. await dialog.getByRole('button', { name: '排队发送' }).click()
  331. await page.getByRole('menuitem', { name: '插话发送' }).click()
  332. await dialog.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  333. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  334. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  335. .toMatch(/ui-conversation:\n\s+busyEnter: steer/)
  336. await page.keyboard.press('Escape')
  337. const warningStart = tripwire.warnings.length
  338. await page.reload({ waitUntil: 'load' })
  339. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  340. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  341. await page.getByRole('button', { name: '设置', exact: true }).click()
  342. const reloaded = page.getByRole('dialog', { name: '设置' })
  343. await reloaded.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  344. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  345. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  346. const secondTripwire = watchConsole(secondPage)
  347. try {
  348. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  349. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  350. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  351. await secondPage.getByRole('button', { name: '设置', exact: true }).click()
  352. await secondPage.getByRole('dialog', { name: '设置' })
  353. .getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  354. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  355. expect(secondTripwire.pageErrors).toEqual([])
  356. expect(secondTripwire.warnings).toEqual([])
  357. } finally {
  358. await secondPage.close()
  359. await second.close()
  360. }
  361. await reloaded.getByRole('button', { name: '插话发送' }).click()
  362. await page.getByRole('menuitem', { name: '排队发送' }).click()
  363. await reloaded.getByRole('button', { name: '排队发送' }).waitFor({ timeout: 10_000 })
  364. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  365. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  366. .toMatch(/ui-conversation:\n\s+busyEnter: queue/)
  367. await page.keyboard.press('Escape')
  368. expect(tripwire.pageErrors).toEqual([])
  369. }, 90_000)
  370. it('persists the settings language across reload and a distinct port', async () => {
  371. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-language'))
  372. await page.getByRole('button', { name: '设置', exact: true }).click()
  373. const zhDialog = page.getByRole('dialog', { name: '设置' })
  374. await zhDialog.waitFor({ timeout: 10_000 })
  375. // The Language selector pill shows the active locale's own name.
  376. const selector = zhDialog.getByRole('button', { name: '中文' })
  377. expect(await selector.getAttribute('aria-haspopup')).toBe('menu')
  378. await selector.click()
  379. await page.getByRole('menuitem', { name: 'English' }).click()
  380. // The settings-owned copy re-registers localized: dialog title, nav,
  381. // Appearance labels. (Only the settings namespaces are localized —
  382. // the rest of the app's copy is intentionally out of this row's scope.)
  383. const enDialog = page.getByRole('dialog', { name: 'Settings' })
  384. await enDialog.waitFor({ timeout: 10_000 })
  385. expect(await enDialog.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  386. await expect.poll(() => enDialog.getByText('Appearance', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  387. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  388. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  389. .toMatch(/locale:\n\s+preference: en/)
  390. // Reload keeps English; then restore zh so shared page state (and the
  391. // other specs' 设置-anchored selectors + goldens) see the default again.
  392. const warningStart = tripwire.warnings.length
  393. await page.reload({ waitUntil: 'load' })
  394. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  395. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  396. const enTrigger = page.getByRole('button', { name: 'Settings' })
  397. await enTrigger.waitFor({ timeout: 10_000 })
  398. // A Chinese browser on another port still receives the explicit English
  399. // preference from the shared Host settings document.
  400. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  401. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  402. const secondTripwire = watchConsole(secondPage)
  403. try {
  404. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  405. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  406. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  407. await secondPage.getByRole('button', { name: 'Settings', exact: true }).click()
  408. await secondPage.getByRole('dialog', { name: 'Settings' })
  409. .getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  410. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  411. expect(secondTripwire.pageErrors).toEqual([])
  412. expect(secondTripwire.warnings).toEqual([])
  413. } finally {
  414. await secondPage.close()
  415. await second.close()
  416. }
  417. await enTrigger.click()
  418. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  419. await page.getByRole('menuitem', { name: '中文' }).click()
  420. await page.getByRole('dialog', { name: '设置' }).waitFor({ timeout: 10_000 })
  421. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  422. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  423. .toMatch(/locale:\n\s+preference: zh/)
  424. await page.keyboard.press('Escape')
  425. expect(tripwire.pageErrors).toEqual([])
  426. }, 90_000)
  427. it('opens an English browser in English without any stored preference', async () => {
  428. // A fresh Host home has no locale preference, so its surface follows the
  429. // browser rather than the product fallback.
  430. const fresh = await launchWebScaffold({})
  431. const enPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: 'en-US' })
  432. const enTripwire = watchConsole(enPage)
  433. onTestFailed(() => saveFailureShot(enPage, 'web-e2e-settings-browser-language'))
  434. try {
  435. await enPage.goto(fresh.baseUrl, { waitUntil: 'load' })
  436. await enPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  437. expect(await enPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  438. await enPage.getByRole('button', { name: 'Settings', exact: true }).click()
  439. const dialog = enPage.getByRole('dialog', { name: 'Settings' })
  440. await dialog.waitFor({ timeout: 10_000 })
  441. await dialog.getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  442. // This page has no closing inventory spec to sweep its console, so the
  443. // scenario clears both tripwire channels itself.
  444. expect(enTripwire.pageErrors).toEqual([])
  445. expect(enTripwire.warnings).toEqual([])
  446. } finally {
  447. await enPage.close()
  448. await fresh.close()
  449. }
  450. }, 90_000)
  451. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  452. expect(tripwire.warnings).toEqual([])
  453. await assertFixtureInventory(SNAPSHOT_DIR, ['dialog.expected.md', 'plugins.expected.md'])
  454. })
  455. })