settings-chrome.e2e.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 -> localStorage dsh.theme
  4. // -> theme/change -> ui-layout's presenter -> body attribute -> alias token)
  5. // and the Language row (settings-scoped localization + persisted dsh.locale),
  6. // plus Permission as the persisted default for subsequently created sessions.
  7. // Zero model calls: everything is pure client + persistence state on a blank
  8. // frame, so there is no fixture and a stray stream would fail loud on the
  9. // open llm seam.
  10. import { readFile } from 'node:fs/promises'
  11. import { fileURLToPath } from 'node:url'
  12. import type { Browser, Page } from 'playwright'
  13. import { chromium } from 'playwright'
  14. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  15. import { join } from 'node:path'
  16. import { SessionId } from '@deepseek-ai/dsh-session'
  17. import {
  18. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  19. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  20. } from './scaffold.ts'
  21. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/settings-chrome', import.meta.url))
  23. const DIALOG_EXPECTED = join(SNAPSHOT_DIR, 'dialog.expected.md')
  24. const MODE = webSnapshotMode()
  25. describe('web e2e: settings modal and General preferences', () => {
  26. let scaffold: WebScaffold
  27. let browser: Browser
  28. let page: Page
  29. let tripwire: ReturnType<typeof watchConsole>
  30. beforeAll(async () => {
  31. scaffold = await launchWebScaffold({})
  32. browser = await chromium.launch()
  33. // Chinese browser: the shared page asserts the localized settings surface
  34. // the client derives from it (the English default has its own spec below).
  35. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  36. tripwire = watchConsole(page)
  37. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  38. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  39. }, 120_000)
  40. afterAll(async () => {
  41. await browser?.close()
  42. await scaffold?.close()
  43. })
  44. it('opens the settings dialog, switches sections, and closes by every path', async () => {
  45. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-shell'))
  46. const trigger = page.getByRole('button', { name: '设置', exact: true })
  47. expect(await trigger.getAttribute('aria-haspopup')).toBe('dialog')
  48. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  49. await trigger.click()
  50. const dialog = page.getByRole('dialog', { name: '设置' })
  51. await dialog.waitFor({ timeout: 10_000 })
  52. expect(await trigger.getAttribute('aria-expanded')).toBe('true')
  53. // General is active by default; Permission, Language and Appearance are functional.
  54. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBe('true')
  55. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  56. await expect.poll(() => dialog.getByText('语言', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  57. await expect.poll(() => dialog.getByText('外观', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  58. // Golden of the freshly opened dialog (default zh, General active).
  59. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  60. await compareOrRefreshGolden(DIALOG_EXPECTED, snapshot, MODE)
  61. // Section switch: aria-current moves (the Models page itself has its own scenario file).
  62. await dialog.getByRole('button', { name: '模型' }).click()
  63. await expect.poll(() => dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current'), { timeout: 5_000 }).toBe('true')
  64. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBeNull()
  65. // Close path 1: Escape.
  66. await page.keyboard.press('Escape')
  67. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  68. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  69. // Close path 2: the header close button (focus lands there on open).
  70. await trigger.click()
  71. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '关闭' }).click()
  72. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  73. expect(tripwire.pageErrors).toEqual([])
  74. }, 60_000)
  75. it('stores Permission as the default for future sessions without changing an existing session', async () => {
  76. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-permission'))
  77. const existing = scaffold.ctx.sessions.create(SessionId('settings-permission-before'))
  78. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  79. .toEqual({ preset: 'danger-full-access' })
  80. await page.getByRole('button', { name: '设置', exact: true }).click()
  81. const dialog = page.getByRole('dialog', { name: '设置' })
  82. await dialog.waitFor({ timeout: 10_000 })
  83. const selector = dialog.getByRole('button', { name: 'Full access' })
  84. await selector.waitFor({ timeout: 10_000 })
  85. await expect.poll(() => selector.isEnabled(), { timeout: 5_000 }).toBe(true)
  86. await selector.click()
  87. await page.getByRole('menuitem', { name: 'Read Only' }).click()
  88. await dialog.getByRole('button', { name: 'Read Only' }).waitFor({ timeout: 10_000 })
  89. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  90. expect(document).toContain('permission:')
  91. expect(document).toContain('defaultPreset: read-only')
  92. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  93. .toEqual({ preset: 'danger-full-access' })
  94. const created = scaffold.ctx.sessions.create(SessionId('settings-permission-after'))
  95. expect(created.events.map(event => [event.type, event.data])).toEqual([
  96. ['permission/preset', { preset: 'read-only' }],
  97. ['sandbox/mode', { mode: 'read-only' }],
  98. ['approval/policy', { policy: 'ask' }],
  99. ])
  100. await dialog.getByRole('button', { name: 'Read Only' }).click()
  101. await page.getByRole('menuitem', { name: 'Full access' }).click()
  102. const confirmation = page.getByRole('dialog', { name: '确认启用 Full access?' })
  103. const enable = confirmation.getByRole('button', { name: '启用 Full access' })
  104. expect(await enable.isDisabled()).toBe(true)
  105. await confirmation.getByRole('checkbox').click()
  106. await enable.click()
  107. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  108. const confirmedDocument = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  109. expect(confirmedDocument).toContain('defaultPreset: danger-full-access')
  110. const confirmed = scaffold.ctx.sessions.create(SessionId('settings-permission-confirmed'))
  111. expect(confirmed.events.map(event => [event.type, event.data])).toEqual([
  112. ['permission/preset', { preset: 'danger-full-access' }],
  113. ['sandbox/mode', { mode: 'danger-full-access' }],
  114. ['approval/policy', { policy: 'never' }],
  115. ])
  116. await page.keyboard.press('Escape')
  117. expect(tripwire.pageErrors).toEqual([])
  118. }, 60_000)
  119. it('flips the theme through the Appearance cubes and persists across reload', async () => {
  120. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-appearance'))
  121. const readState = async (): Promise<{ attr: boolean; token: string; stored: string | null }> =>
  122. await page.evaluate(() => ({
  123. attr: document.body.hasAttribute('data-ds-dark-theme'),
  124. token: getComputedStyle(document.body).getPropertyValue('--dsw-alias-bg-base').trim(),
  125. stored: localStorage.getItem('dsh.theme'),
  126. }))
  127. // Pin the OS scheme to light so the default `system` preference resolves
  128. // light and the dark flip below is unambiguously the gesture's doing.
  129. await page.emulateMedia({ colorScheme: 'light' })
  130. const light = await readState()
  131. expect(light.attr).toBe(false)
  132. await page.getByRole('button', { name: '设置', exact: true }).click()
  133. const dialog = page.getByRole('dialog', { name: '设置' })
  134. await dialog.waitFor({ timeout: 10_000 })
  135. const darkCube = dialog.getByRole('button', { name: '深色' })
  136. expect(await darkCube.getAttribute('aria-pressed')).toBe('false')
  137. await darkCube.click()
  138. // The full cascade: pressed state, persisted preference, body attribute,
  139. // alias token flip — all from one real user gesture.
  140. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  141. const dark = await readState()
  142. expect(dark.attr).toBe(true)
  143. expect(dark.stored).toBe('dark')
  144. expect(dark.token).not.toBe(light.token)
  145. await page.keyboard.press('Escape')
  146. // Reload: the preference survives boot (restore + presenter initial apply).
  147. const warningStart = tripwire.warnings.length
  148. await page.reload({ waitUntil: 'load' })
  149. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  150. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  151. await page.emulateMedia({ colorScheme: 'light' })
  152. const reloaded = await readState()
  153. expect(reloaded.attr).toBe(true)
  154. expect(reloaded.stored).toBe('dark')
  155. // `system` follows the emulated OS scheme (dark stays dark, light clears).
  156. await page.getByRole('button', { name: '设置', exact: true }).click()
  157. const systemCube = page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '跟随系统' })
  158. await systemCube.click()
  159. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  160. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  161. await page.emulateMedia({ colorScheme: 'dark' })
  162. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  163. // Restore for the specs that follow: light preference beats the emulated
  164. // dark OS scheme, leaving the shared page in the light default.
  165. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '浅色' }).click()
  166. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  167. await page.keyboard.press('Escape')
  168. expect(tripwire.pageErrors).toEqual([])
  169. }, 90_000)
  170. it('switches the settings surface language and persists dsh.locale', async () => {
  171. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-language'))
  172. await page.getByRole('button', { name: '设置', exact: true }).click()
  173. const zhDialog = page.getByRole('dialog', { name: '设置' })
  174. await zhDialog.waitFor({ timeout: 10_000 })
  175. // The Language selector pill shows the active locale's own name.
  176. const selector = zhDialog.getByRole('button', { name: '中文' })
  177. expect(await selector.getAttribute('aria-haspopup')).toBe('menu')
  178. await selector.click()
  179. await page.getByRole('menuitem', { name: 'English' }).click()
  180. // The settings-owned copy re-registers localized: dialog title, nav,
  181. // Appearance labels. (Only the settings namespaces are localized today —
  182. // the rest of the app's copy is intentionally out of this row's scope.)
  183. const enDialog = page.getByRole('dialog', { name: 'Settings' })
  184. await enDialog.waitFor({ timeout: 10_000 })
  185. expect(await enDialog.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  186. await expect.poll(() => enDialog.getByText('Appearance', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  187. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBe('en')
  188. // Reload keeps English; then restore zh so shared page state (and the
  189. // other specs' 设置-anchored selectors + goldens) see the default again.
  190. const warningStart = tripwire.warnings.length
  191. await page.reload({ waitUntil: 'load' })
  192. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  193. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  194. const enTrigger = page.getByRole('button', { name: 'Settings' })
  195. await enTrigger.waitFor({ timeout: 10_000 })
  196. await enTrigger.click()
  197. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  198. await page.getByRole('menuitem', { name: '中文' }).click()
  199. await page.getByRole('dialog', { name: '设置' }).waitFor({ timeout: 10_000 })
  200. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBe('zh')
  201. await page.keyboard.press('Escape')
  202. expect(tripwire.pageErrors).toEqual([])
  203. }, 90_000)
  204. it('opens an English browser in English without any stored preference', async () => {
  205. // A second page under a different browser language: nothing is persisted
  206. // for it, so the settings surface must follow the browser rather than the
  207. // product fallback the shared zh page shows.
  208. const enPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: 'en-US' })
  209. const enTripwire = watchConsole(enPage)
  210. onTestFailed(() => saveFailureShot(enPage, 'web-e2e-settings-browser-language'))
  211. try {
  212. await enPage.goto(scaffold.baseUrl, { waitUntil: 'load' })
  213. await enPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  214. expect(await enPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  215. await enPage.getByRole('button', { name: 'Settings', exact: true }).click()
  216. const dialog = enPage.getByRole('dialog', { name: 'Settings' })
  217. await dialog.waitFor({ timeout: 10_000 })
  218. await dialog.getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  219. // This page has no closing inventory spec to sweep its console, so the
  220. // scenario clears both tripwire channels itself.
  221. expect(enTripwire.pageErrors).toEqual([])
  222. expect(enTripwire.warnings).toEqual([])
  223. } finally {
  224. await enPage.close()
  225. }
  226. }, 90_000)
  227. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  228. expect(tripwire.warnings).toEqual([])
  229. await assertFixtureInventory(SNAPSHOT_DIR, ['dialog.expected.md'])
  230. })
  231. })