settings-chrome.e2e.ts 16 KB

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