settings-chrome.e2e.ts 17 KB

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