settings-chrome.e2e.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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.authenticatedUrl, { 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.authenticatedUrl, { 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('steps the content font size, applies it to body, and persists across reload', async () => {
  330. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-font-size'))
  331. const readFontSize = async (target: Page = page): Promise<string> => await target.evaluate(
  332. () => document.body.style.getPropertyValue('--dsh-content-font-size'),
  333. )
  334. expect(await readFontSize()).toBe('14px')
  335. await page.getByRole('button', { name: '设置', exact: true }).click()
  336. const dialog = page.getByRole('dialog', { name: '设置' })
  337. await dialog.waitFor({ timeout: 10_000 })
  338. // The stepper reveals its arrows on hover; the up arrow steps 14 → 15 → 16.
  339. await dialog.getByText('14', { exact: true }).hover()
  340. const increase = dialog.getByRole('button', { name: '增大字号' })
  341. await increase.click()
  342. await dialog.getByText('15', { exact: true }).waitFor({ timeout: 5_000 })
  343. await increase.click()
  344. await dialog.getByText('16', { exact: true }).waitFor({ timeout: 5_000 })
  345. await expect.poll(readFontSize, { timeout: 5_000 }).toBe('16px')
  346. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  347. .toMatch(/ui-theme:\n(?:\s+\w+: .*\n)*?\s+fontSize: 16/)
  348. await page.keyboard.press('Escape')
  349. // Reload: the boot script embeds the durable size and ThemeRuntime seeds
  350. // its initial snapshot from the boot-written body variable, so activation
  351. // never flashes the default while the settings read is in flight.
  352. const warningStart = tripwire.warnings.length
  353. await page.reload({ waitUntil: 'load' })
  354. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  355. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  356. await expect.poll(readFontSize, { timeout: 5_000 }).toBe('16px')
  357. // Restore the default for the specs that follow (and the dialog golden).
  358. await page.getByRole('button', { name: '设置', exact: true }).click()
  359. const restored = page.getByRole('dialog', { name: '设置' })
  360. await restored.waitFor({ timeout: 10_000 })
  361. await restored.getByText('16', { exact: true }).hover()
  362. const decrease = restored.getByRole('button', { name: '减小字号' })
  363. await decrease.click()
  364. await restored.getByText('15', { exact: true }).waitFor({ timeout: 5_000 })
  365. await decrease.click()
  366. await restored.getByText('14', { exact: true }).waitFor({ timeout: 5_000 })
  367. await expect.poll(readFontSize, { timeout: 5_000 }).toBe('14px')
  368. await page.keyboard.press('Escape')
  369. expect(tripwire.pageErrors).toEqual([])
  370. }, 90_000)
  371. it('persists the busy-state Enter behavior across reload and a distinct port', async () => {
  372. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-enter-behavior'))
  373. await page.getByRole('button', { name: '设置', exact: true }).click()
  374. const dialog = page.getByRole('dialog', { name: '设置' })
  375. await dialog.waitFor({ timeout: 10_000 })
  376. await dialog.getByRole('button', { name: '排队发送' }).click()
  377. await page.getByRole('menuitem', { name: '插话发送' }).click()
  378. await dialog.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  379. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  380. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  381. .toMatch(/ui-conversation:\n\s+busyEnter: steer/)
  382. await page.keyboard.press('Escape')
  383. const warningStart = tripwire.warnings.length
  384. await page.reload({ waitUntil: 'load' })
  385. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  386. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  387. await page.getByRole('button', { name: '设置', exact: true }).click()
  388. const reloaded = page.getByRole('dialog', { name: '设置' })
  389. await reloaded.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  390. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  391. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  392. const secondTripwire = watchConsole(secondPage)
  393. try {
  394. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  395. await secondPage.goto(second.authenticatedUrl, { waitUntil: 'load' })
  396. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  397. await secondPage.getByRole('button', { name: '设置', exact: true }).click()
  398. await secondPage.getByRole('dialog', { name: '设置' })
  399. .getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  400. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  401. expect(secondTripwire.pageErrors).toEqual([])
  402. expect(secondTripwire.warnings).toEqual([])
  403. } finally {
  404. await secondPage.close()
  405. await second.close()
  406. }
  407. await reloaded.getByRole('button', { name: '插话发送' }).click()
  408. await page.getByRole('menuitem', { name: '排队发送' }).click()
  409. await reloaded.getByRole('button', { name: '排队发送' }).waitFor({ timeout: 10_000 })
  410. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  411. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  412. .toMatch(/ui-conversation:\n\s+busyEnter: queue/)
  413. await page.keyboard.press('Escape')
  414. expect(tripwire.pageErrors).toEqual([])
  415. }, 90_000)
  416. it('persists the settings language across reload and a distinct port', async () => {
  417. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-language'))
  418. await page.getByRole('button', { name: '设置', exact: true }).click()
  419. const zhDialog = page.getByRole('dialog', { name: '设置' })
  420. await zhDialog.waitFor({ timeout: 10_000 })
  421. // The document language follows the active locale in the assembled app, not
  422. // only on a directly-mounted plugin. This is a zh browser, so the served
  423. // markup's `en` must already have been replaced — asserting it here (rather
  424. // than only in an English scenario) is what makes the check discriminating.
  425. expect(await page.evaluate(() => document.documentElement.lang)).toBe('zh-CN')
  426. // The Language selector pill shows the active locale's own name.
  427. const selector = zhDialog.getByRole('button', { name: '中文' })
  428. expect(await selector.getAttribute('aria-haspopup')).toBe('menu')
  429. await selector.click()
  430. await page.getByRole('menuitem', { name: 'English' }).click()
  431. // The settings-owned copy re-registers localized: dialog title, nav,
  432. // Appearance labels. (Only the settings namespaces are localized —
  433. // the rest of the app's copy is intentionally out of this row's scope.)
  434. const enDialog = page.getByRole('dialog', { name: 'Settings' })
  435. await enDialog.waitFor({ timeout: 10_000 })
  436. // ...and the attribute follows that switch, in the assembled app.
  437. await expect.poll(() => page.evaluate(() => document.documentElement.lang), { timeout: 5_000 }).toBe('en')
  438. expect(await enDialog.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  439. await expect.poll(() => enDialog.getByText('Appearance', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  440. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  441. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  442. .toMatch(/locale:\n\s+preference: en/)
  443. // Reload keeps English; then restore zh so shared page state (and the
  444. // other specs' 设置-anchored selectors + goldens) see the default again.
  445. const warningStart = tripwire.warnings.length
  446. await page.reload({ waitUntil: 'load' })
  447. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  448. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  449. const enTrigger = page.getByRole('button', { name: 'Settings' })
  450. await enTrigger.waitFor({ timeout: 10_000 })
  451. // A Chinese browser on another port still receives the explicit English
  452. // preference from the shared Host settings document.
  453. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  454. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  455. const secondTripwire = watchConsole(secondPage)
  456. try {
  457. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  458. await secondPage.goto(second.authenticatedUrl, { waitUntil: 'load' })
  459. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  460. await secondPage.getByRole('button', { name: 'Settings', exact: true }).click()
  461. await secondPage.getByRole('dialog', { name: 'Settings' })
  462. .getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  463. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  464. expect(secondTripwire.pageErrors).toEqual([])
  465. expect(secondTripwire.warnings).toEqual([])
  466. } finally {
  467. await secondPage.close()
  468. await second.close()
  469. }
  470. await enTrigger.click()
  471. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  472. await page.getByRole('menuitem', { name: '中文' }).click()
  473. await page.getByRole('dialog', { name: '设置' }).waitFor({ timeout: 10_000 })
  474. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  475. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  476. .toMatch(/locale:\n\s+preference: zh/)
  477. await page.keyboard.press('Escape')
  478. expect(tripwire.pageErrors).toEqual([])
  479. }, 90_000)
  480. it('opens an English browser in English without any stored preference', async () => {
  481. // A fresh Host home has no locale preference, so its surface follows the
  482. // browser. English is also FALLBACK_LOCALE, so this scenario alone cannot
  483. // distinguish detection from the default — the zh scenarios above supply
  484. // the discriminating half (a Chinese browser must NOT land on the default).
  485. const fresh = await launchWebScaffold({})
  486. const enPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: 'en-US' })
  487. const enTripwire = watchConsole(enPage)
  488. onTestFailed(() => saveFailureShot(enPage, 'web-e2e-settings-browser-language'))
  489. try {
  490. await enPage.goto(fresh.authenticatedUrl, { waitUntil: 'load' })
  491. await enPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  492. expect(await enPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  493. await enPage.getByRole('button', { name: 'Settings', exact: true }).click()
  494. const dialog = enPage.getByRole('dialog', { name: 'Settings' })
  495. await dialog.waitFor({ timeout: 10_000 })
  496. await dialog.getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  497. // This page has no closing inventory spec to sweep its console, so the
  498. // scenario clears both tripwire channels itself.
  499. expect(enTripwire.pageErrors).toEqual([])
  500. expect(enTripwire.warnings).toEqual([])
  501. } finally {
  502. await enPage.close()
  503. await fresh.close()
  504. }
  505. }, 90_000)
  506. it('opens a browser asking for no shipped language in English', async () => {
  507. // The product default for "no usable signal": a French browser ships
  508. // neither zh nor en, so resolution falls to FALLBACK_LOCALE (en) rather
  509. // than to Chinese.
  510. const fresh = await launchWebScaffold({})
  511. const frPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: 'fr-FR' })
  512. const frTripwire = watchConsole(frPage)
  513. onTestFailed(() => saveFailureShot(frPage, 'web-e2e-settings-unshipped-language'))
  514. try {
  515. await frPage.goto(fresh.authenticatedUrl, { waitUntil: 'load' })
  516. await frPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  517. expect(await frPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  518. await frPage.getByRole('button', { name: 'Settings', exact: true }).click()
  519. const dialog = frPage.getByRole('dialog', { name: 'Settings' })
  520. await dialog.waitFor({ timeout: 10_000 })
  521. await dialog.getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  522. const preset = dialog.getByRole('button', { name: 'Standard mode' })
  523. await expect.poll(() => preset.isEnabled(), { timeout: 10_000 }).toBe(true)
  524. // The markup already ships `en`, so this alone cannot prove the sync ran
  525. // — the zh scenario above is the discriminating half. Asserted here too
  526. // so a future change that resolves en but writes the wrong tag is caught.
  527. expect(await frPage.evaluate(() => document.documentElement.lang)).toBe('en')
  528. // Golden of the English fallback dialog — the visible output this change
  529. // produces. The zh golden above covers the detected-locale surface, so
  530. // the pair pins both directions of the resolution.
  531. const snapshot = await captureStableAria(frPage, '[role="dialog"]', fresh.workspaceCwd)
  532. await compareOrRefreshGolden(DIALOG_EN_EXPECTED, snapshot, MODE)
  533. expect(frTripwire.pageErrors).toEqual([])
  534. expect(frTripwire.warnings).toEqual([])
  535. } finally {
  536. await frPage.close()
  537. await fresh.close()
  538. }
  539. }, 90_000)
  540. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  541. expect(tripwire.warnings).toEqual([])
  542. await assertFixtureInventory(SNAPSHOT_DIR, ['dialog-en.expected.md', 'dialog.expected.md', 'plugins.expected.md'])
  543. })
  544. })