settings-chrome.e2e.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 { 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. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  34. tripwire = watchConsole(page)
  35. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  36. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  37. }, 120_000)
  38. afterAll(async () => {
  39. await browser?.close()
  40. await scaffold?.close()
  41. })
  42. it('opens the settings dialog, switches sections, and closes by every path', async () => {
  43. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-shell'))
  44. const trigger = page.getByRole('button', { name: '设置', exact: true })
  45. expect(await trigger.getAttribute('aria-haspopup')).toBe('dialog')
  46. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  47. await trigger.click()
  48. const dialog = page.getByRole('dialog', { name: '设置' })
  49. await dialog.waitFor({ timeout: 10_000 })
  50. expect(await trigger.getAttribute('aria-expanded')).toBe('true')
  51. // General is active by default; Permission, Language and Appearance are functional.
  52. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBe('true')
  53. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  54. await expect.poll(() => dialog.getByText('语言', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  55. await expect.poll(() => dialog.getByText('外观', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  56. // Golden of the freshly opened dialog (default zh, General active).
  57. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  58. await compareOrRefreshGolden(DIALOG_EXPECTED, snapshot, MODE)
  59. // Section switch: aria-current moves (the Models page itself has its own scenario file).
  60. await dialog.getByRole('button', { name: '模型' }).click()
  61. await expect.poll(() => dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current'), { timeout: 5_000 }).toBe('true')
  62. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBeNull()
  63. // Close path 1: Escape.
  64. await page.keyboard.press('Escape')
  65. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  66. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  67. // Close path 2: the header close button (focus lands there on open).
  68. await trigger.click()
  69. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '关闭' }).click()
  70. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  71. expect(tripwire.pageErrors).toEqual([])
  72. }, 60_000)
  73. it('stores Permission as the default for future sessions without changing an existing session', async () => {
  74. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-permission'))
  75. const existing = scaffold.ctx.sessions.create(SessionId('settings-permission-before'))
  76. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  77. .toEqual({ preset: 'danger-full-access' })
  78. await page.getByRole('button', { name: '设置', exact: true }).click()
  79. const dialog = page.getByRole('dialog', { name: '设置' })
  80. await dialog.waitFor({ timeout: 10_000 })
  81. const selector = dialog.getByRole('button', { name: 'Full access' })
  82. await selector.waitFor({ timeout: 10_000 })
  83. await expect.poll(() => selector.isEnabled(), { timeout: 5_000 }).toBe(true)
  84. await selector.click()
  85. await page.getByRole('menuitem', { name: 'Read Only' }).click()
  86. await dialog.getByRole('button', { name: 'Read Only' }).waitFor({ timeout: 10_000 })
  87. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  88. expect(document).toContain('permission:')
  89. expect(document).toContain('defaultPreset: read-only')
  90. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  91. .toEqual({ preset: 'danger-full-access' })
  92. const created = scaffold.ctx.sessions.create(SessionId('settings-permission-after'))
  93. expect(created.events.map(event => [event.type, event.data])).toEqual([
  94. ['permission/preset', { preset: 'read-only' }],
  95. ['sandbox/mode', { mode: 'read-only' }],
  96. ['approval/policy', { policy: 'ask' }],
  97. ])
  98. await dialog.getByRole('button', { name: 'Read Only' }).click()
  99. await page.getByRole('menuitem', { name: 'Full access' }).click()
  100. const confirmation = page.getByRole('dialog', { name: '确认启用 Full access?' })
  101. const enable = confirmation.getByRole('button', { name: '启用 Full access' })
  102. expect(await enable.isDisabled()).toBe(true)
  103. await confirmation.getByRole('checkbox').click()
  104. await enable.click()
  105. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  106. const confirmedDocument = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  107. expect(confirmedDocument).toContain('defaultPreset: danger-full-access')
  108. const confirmed = scaffold.ctx.sessions.create(SessionId('settings-permission-confirmed'))
  109. expect(confirmed.events.map(event => [event.type, event.data])).toEqual([
  110. ['permission/preset', { preset: 'danger-full-access' }],
  111. ['sandbox/mode', { mode: 'danger-full-access' }],
  112. ['approval/policy', { policy: 'never' }],
  113. ])
  114. await page.keyboard.press('Escape')
  115. expect(tripwire.pageErrors).toEqual([])
  116. }, 60_000)
  117. it('flips the theme through the Appearance cubes and persists across reload', async () => {
  118. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-appearance'))
  119. const readState = async (): Promise<{ attr: boolean; token: string; stored: string | null }> =>
  120. await page.evaluate(() => ({
  121. attr: document.body.hasAttribute('data-ds-dark-theme'),
  122. token: getComputedStyle(document.body).getPropertyValue('--dsw-alias-bg-base').trim(),
  123. stored: localStorage.getItem('dsh.theme'),
  124. }))
  125. // Pin the OS scheme to light so the default `system` preference resolves
  126. // light and the dark flip below is unambiguously the gesture's doing.
  127. await page.emulateMedia({ colorScheme: 'light' })
  128. const light = await readState()
  129. expect(light.attr).toBe(false)
  130. await page.getByRole('button', { name: '设置', exact: true }).click()
  131. const dialog = page.getByRole('dialog', { name: '设置' })
  132. await dialog.waitFor({ timeout: 10_000 })
  133. const darkCube = dialog.getByRole('button', { name: '深色' })
  134. expect(await darkCube.getAttribute('aria-pressed')).toBe('false')
  135. await darkCube.click()
  136. // The full cascade: pressed state, persisted preference, body attribute,
  137. // alias token flip — all from one real user gesture.
  138. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  139. const dark = await readState()
  140. expect(dark.attr).toBe(true)
  141. expect(dark.stored).toBe('dark')
  142. expect(dark.token).not.toBe(light.token)
  143. await page.keyboard.press('Escape')
  144. // Reload: the preference survives boot (restore + presenter initial apply).
  145. const warningStart = tripwire.warnings.length
  146. await page.reload({ waitUntil: 'load' })
  147. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  148. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  149. await page.emulateMedia({ colorScheme: 'light' })
  150. const reloaded = await readState()
  151. expect(reloaded.attr).toBe(true)
  152. expect(reloaded.stored).toBe('dark')
  153. // `system` follows the emulated OS scheme (dark stays dark, light clears).
  154. await page.getByRole('button', { name: '设置', exact: true }).click()
  155. const systemCube = page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '跟随系统' })
  156. await systemCube.click()
  157. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  158. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  159. await page.emulateMedia({ colorScheme: 'dark' })
  160. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  161. // Restore for the specs that follow: light preference beats the emulated
  162. // dark OS scheme, leaving the shared page in the light default.
  163. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '浅色' }).click()
  164. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  165. await page.keyboard.press('Escape')
  166. expect(tripwire.pageErrors).toEqual([])
  167. }, 90_000)
  168. it('switches the settings surface language and persists dsh.locale', async () => {
  169. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-language'))
  170. await page.getByRole('button', { name: '设置', exact: true }).click()
  171. const zhDialog = page.getByRole('dialog', { name: '设置' })
  172. await zhDialog.waitFor({ timeout: 10_000 })
  173. // The Language selector pill shows the active locale's own name.
  174. const selector = zhDialog.getByRole('button', { name: '中文' })
  175. expect(await selector.getAttribute('aria-haspopup')).toBe('menu')
  176. await selector.click()
  177. await page.getByRole('menuitem', { name: 'English' }).click()
  178. // The settings-owned copy re-registers localized: dialog title, nav,
  179. // Appearance labels. (Only the settings namespaces are localized today —
  180. // the rest of the app's copy is intentionally out of this row's scope.)
  181. const enDialog = page.getByRole('dialog', { name: 'Settings' })
  182. await enDialog.waitFor({ timeout: 10_000 })
  183. expect(await enDialog.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  184. await expect.poll(() => enDialog.getByText('Appearance', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  185. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBe('en')
  186. // Reload keeps English; then restore zh so shared page state (and the
  187. // other specs' 设置-anchored selectors + goldens) see the default again.
  188. const warningStart = tripwire.warnings.length
  189. await page.reload({ waitUntil: 'load' })
  190. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  191. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  192. const enTrigger = page.getByRole('button', { name: 'Settings' })
  193. await enTrigger.waitFor({ timeout: 10_000 })
  194. await enTrigger.click()
  195. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  196. await page.getByRole('menuitem', { name: '中文' }).click()
  197. await page.getByRole('dialog', { name: '设置' }).waitFor({ timeout: 10_000 })
  198. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBe('zh')
  199. await page.keyboard.press('Escape')
  200. expect(tripwire.pageErrors).toEqual([])
  201. }, 90_000)
  202. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  203. expect(tripwire.warnings).toEqual([])
  204. await assertFixtureInventory(SNAPSHOT_DIR, ['dialog.expected.md'])
  205. })
  206. })