settings-chrome.e2e.ts 18 KB

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