settings-chrome.e2e.ts 29 KB

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