lifecycle-chrome.e2e.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Web e2e scenarios: lifecycle & chrome — the workspace-aware first-send
  2. // flow over the real wire, reload recovery, and the dark-mode token cascade.
  3. // One tiny recorded turn (text-only) drives the whole spec: the empty-state
  4. // hero materializes a real Workspace + Session on first send (the jsdom
  5. // workspace-flow suite pins the object-layer state machine over the fixture
  6. // client; THIS spec pins the same flow through HTTP RPC + SSE + the host
  7. // gateway), reload replays everything from the log (zero further model
  8. // calls), and the theme scenario proves the shipped dark palette actually
  9. // cascades: attribute -> alias token flip -> painted surface change. No
  10. // theme/layout golden: aria snapshots are color-blind (lane scope: the
  11. // browser-e2e-lane Agent Note); the hero's waiting state gets the one golden
  12. // here.
  13. import { readFile } from 'node:fs/promises'
  14. import { fileURLToPath } from 'node:url'
  15. import { join } from 'node:path'
  16. import type { Browser, Page } from 'playwright'
  17. import { chromium } from 'playwright'
  18. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  19. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  20. import {
  21. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureExpandedTurnProcessAria,
  22. captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  23. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  24. } from './scaffold.ts'
  25. import { connectFreshWorkspace, newEnglishPage, saveFailureShot, writeComposerDraft } from './support.ts'
  26. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/lifecycle-chrome', import.meta.url))
  27. const FIXTURE = join(SNAPSHOT_DIR, 'session.jsonl')
  28. const REPLAY_OVERRIDE = join(SNAPSHOT_DIR, 'replay.override.json')
  29. const HERO_EXPECTED = join(SNAPSHOT_DIR, 'hero.expected.md')
  30. const COMMAND_MENU_EXPECTED = join(SNAPSHOT_DIR, 'command-menu.expected.md')
  31. const FUZZY_COMMAND_MENU_EXPECTED = join(SNAPSHOT_DIR, 'command-menu-fuzzy.expected.md')
  32. const PLAN_ACTIVE_EXPECTED = join(SNAPSHOT_DIR, 'plan-active.expected.md')
  33. // Post-reload golden: the same settled conversation rebuilt purely from
  34. // persistence + history — byte-equal rendering is exactly the recovery claim.
  35. const RELOADED_EXPECTED = join(SNAPSHOT_DIR, 'reloaded.expected.md')
  36. const RELOADED_EXPANDED_EXPECTED = join(SNAPSHOT_DIR, 'reloaded-expanded.expected.md')
  37. const MODE = webSnapshotMode()
  38. const PROMPT = 'Reply with the single word LIGHTHOUSE and stop.'
  39. const REPLAY_PACE_MS = 100
  40. describe('web e2e: lifecycle & chrome (workspace flow / reload / dark mode)', () => {
  41. let scaffold: WebScaffold
  42. let browser: Browser
  43. let page: Page
  44. let tripwire: ReturnType<typeof watchConsole>
  45. const sessionEvents: SessionEvent[] = []
  46. beforeAll(async () => {
  47. scaffold = await launchWebScaffold(MODE === 'record'
  48. ? {}
  49. : { replayFixture: FIXTURE, replayOverride: REPLAY_OVERRIDE, paceMs: REPLAY_PACE_MS })
  50. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  51. browser = await chromium.launch()
  52. page = await newEnglishPage(browser)
  53. tripwire = watchConsole(page)
  54. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  55. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  56. // Fresh world: connect a Workspace so the composer scenarios start live.
  57. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  58. }, 120_000)
  59. afterAll(async () => {
  60. await browser?.close()
  61. await scaffold?.close()
  62. })
  63. it.skipIf(MODE === 'record')('opens the shared slash menu from plus with only Command candidates', async () => {
  64. onTestFailed(() => saveFailureShot(page, 'web-e2e-command-menu-launcher'))
  65. const launcher = page.getByRole('button', { name: 'Commands' })
  66. await launcher.click()
  67. const menu = page.getByRole('listbox', { name: 'Trigger suggestions' })
  68. await menu.waitFor({ timeout: 10_000 })
  69. const snapshot = await captureStableAria(page, '[role="listbox"]', scaffold.workspaceCwd)
  70. await compareOrRefreshGolden(COMMAND_MENU_EXPECTED, snapshot, MODE)
  71. expect(snapshot).toContain('text: Commands')
  72. expect(snapshot).not.toContain('text: Skills')
  73. expect(snapshot).not.toContain('text: Subagents')
  74. const launchedBox = await menu.boundingBox()
  75. await page.locator('[data-composer-input]').first().press('Escape')
  76. await expect.poll(() => menu.count()).toBe(0)
  77. const input = page.locator('[data-composer-input]').first()
  78. await writeComposerDraft(page, input, '/')
  79. await menu.waitFor({ timeout: 10_000 })
  80. const typedBox = await menu.boundingBox()
  81. expect(launchedBox).not.toBeNull()
  82. expect(typedBox).not.toBeNull()
  83. expect(Math.abs(launchedBox!.x - typedBox!.x)).toBeLessThan(1)
  84. expect(Math.abs(
  85. launchedBox!.y + launchedBox!.height - typedBox!.y - typedBox!.height,
  86. )).toBeLessThan(1)
  87. await writeComposerDraft(page, input, '/cpt')
  88. await expect.poll(() => menu.getByRole('option').allTextContents()).toEqual([
  89. 'compactCompact older conversation history',
  90. ])
  91. const fuzzySnapshot = await captureStableAria(page, '[role="listbox"]', scaffold.workspaceCwd)
  92. await compareOrRefreshGolden(FUZZY_COMMAND_MENU_EXPECTED, fuzzySnapshot, MODE)
  93. await writeComposerDraft(page, input, '')
  94. await expect.poll(() => menu.count()).toBe(0)
  95. })
  96. it.skipIf(MODE === 'record')('shows active Plan as the warn-state status action', async () => {
  97. const activeScaffold = await launchWebScaffold()
  98. const activePage = await newEnglishPage(browser)
  99. const activeTripwire = watchConsole(activePage)
  100. try {
  101. await activePage.goto(activeScaffold.authenticatedUrl, { waitUntil: 'load' })
  102. await activePage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  103. await connectFreshWorkspace(activePage, activeScaffold.workspaceCwd)
  104. const input = activePage.locator('[data-composer-input]').first()
  105. await activePage.getByRole('button', { name: 'Commands' }).click()
  106. const menu = activePage.getByRole('listbox', { name: 'Trigger suggestions' })
  107. await menu.waitFor({ timeout: 10_000 })
  108. await menu.getByRole('option', { name: 'plan Enter or leave plan mode' }).click()
  109. await expect.poll(() => input.textContent()).toBe('/plan ')
  110. await input.press('Enter')
  111. const planButton = activePage.getByRole('button', { name: 'Plan mode on, press to turn off' })
  112. await planButton.waitFor({ timeout: 10_000 })
  113. // The golden encodes an empty composer, and the button arriving does not
  114. // mean the submitted text is gone yet: under load the capture can catch
  115. // a textbox still holding `/plan`.
  116. await expect.poll(() => input.textContent(), { timeout: 10_000 }).toBe('')
  117. const planSnapshot = await captureStableAria(activePage, '[class*="frame"]', activeScaffold.workspaceCwd)
  118. await compareOrRefreshGolden(PLAN_ACTIVE_EXPECTED, planSnapshot, MODE)
  119. const planStyle = await planButton.evaluate((element) => {
  120. const probe = document.createElement('span')
  121. probe.style.color = 'var(--dsw-alias-state-warn-label)'
  122. probe.style.backgroundColor = 'var(--dsw-alias-state-warn-tertiary)'
  123. document.body.append(probe)
  124. const actual = getComputedStyle(element)
  125. const reference = getComputedStyle(probe)
  126. const result = {
  127. color: actual.color,
  128. backgroundColor: actual.backgroundColor,
  129. borderRadius: actual.borderRadius,
  130. fontSize: actual.fontSize,
  131. referenceColor: reference.color,
  132. referenceBackgroundColor: reference.backgroundColor,
  133. }
  134. probe.remove()
  135. return result
  136. })
  137. expect(planStyle.color).toBe(planStyle.referenceColor)
  138. expect(planStyle.backgroundColor).toBe(planStyle.referenceBackgroundColor)
  139. expect(planStyle.borderRadius).toBe('999px')
  140. expect(planStyle.fontSize).toBe('13px')
  141. await planButton.click()
  142. await expect.poll(() => planButton.count()).toBe(0)
  143. expect(activeTripwire.pageErrors).toEqual([])
  144. expect(activeTripwire.warnings).toEqual([])
  145. } catch (error) {
  146. await saveFailureShot(activePage, 'web-e2e-plan-active').catch(() => undefined)
  147. throw error
  148. } finally {
  149. await activePage.close()
  150. await activeScaffold.close()
  151. }
  152. })
  153. it('sends the first prompt from the empty-state hero (all modes)', async () => {
  154. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-send'))
  155. if (MODE !== 'record') {
  156. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  157. }
  158. // The blank frame renders the hero, not the resident composer: the
  159. // headline plus the guidance placeholder are the empty state's anchors.
  160. await expect.poll(() => page.getByText('Into the Unknown', { exact: false }).count(), { timeout: 15_000 }).toBe(1)
  161. const input = page.locator('[data-composer-input]').first()
  162. await input.waitFor({ timeout: 10_000 })
  163. if (MODE !== 'record') {
  164. await page.getByText('Into the Unknown', { exact: false }).hover()
  165. await expect.poll(() => page.getByRole('tooltip').count()).toBe(0)
  166. // Golden of the hero's stable waiting state (captured before any send;
  167. // the conversation-region goldens belong to the other scenarios).
  168. const snapshot = await captureStableAria(page, '[class*="frame"]', scaffold.workspaceCwd)
  169. await compareOrRefreshGolden(HERO_EXPECTED, snapshot, MODE)
  170. }
  171. const settled = scaffold.whenTurnSettled()
  172. await writeComposerDraft(page, input, PROMPT)
  173. const observeTurn = async () => {
  174. const originalViewport = page.viewportSize() ?? { width: 1680, height: 1000 }
  175. if (MODE !== 'record') await page.setViewportSize({ width: 480, height: 1000 })
  176. try {
  177. await input.press('Enter')
  178. if (MODE !== 'record') {
  179. const liveTail = page.locator('[data-variant="think"][data-state="running"] [data-follow-end]')
  180. await expect.poll(async () => await liveTail.evaluate(element => (
  181. element.scrollWidth > element.clientWidth
  182. && element.scrollLeft >= element.scrollWidth - element.clientWidth - 1
  183. )), { timeout: 10_000, interval: 10 }).toBe(true)
  184. }
  185. return await settled
  186. } finally {
  187. if (MODE !== 'record') await page.setViewportSize(originalViewport)
  188. }
  189. }
  190. const sessionId = await observeTurn()
  191. if (MODE === 'record') {
  192. await recordFixture(scaffold, sessionId, FIXTURE)
  193. }
  194. }, 200_000)
  195. it.skipIf(MODE === 'record')('materialized a real Workspace and Session over the wire', async () => {
  196. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-materialize'))
  197. // Browser: the sidebar tree now carries the auto-created workspace group
  198. // with its one session, and the opened session is the selected row. The
  199. // compact layout dropped group session counts, so the group row itself is
  200. // the barrier.
  201. await expect.poll(
  202. () => page.locator('[role="treeitem"][aria-expanded]').filter({ hasText: 'workspace' }).count(),
  203. { timeout: 15_000 },
  204. ).toBeGreaterThanOrEqual(1)
  205. await expect.poll(() => page.locator('[role="treeitem"][aria-selected="true"]').count(), { timeout: 10_000 }).toBe(1)
  206. await expect.poll(() => page.getByText('LIGHTHOUSE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  207. await expect.poll(() => page.getByText('Cache hit 99.5%', { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  208. // Host: the session's durable header cwd is the folder the workspace
  209. // flow created and adopted (<workspaceCwd>/workspace) — the proof the
  210. // send went through workspace materialization rather than a bare
  211. // default-cwd session.
  212. const cwds = scaffold.ctx.sessions.list().map(session => session.header.cwd)
  213. expect(cwds).toEqual([join(scaffold.workspaceCwd, 'workspace')])
  214. const turnEnds = sessionEvents.filter(e => e.type === 'turn/end')
  215. expect(turnEnds).toHaveLength(1)
  216. expect((turnEnds[0] as SessionEvent & { data: { reason: { kind: string } } }).data.reason.kind).toBe('completed')
  217. }, 60_000)
  218. it.skipIf(MODE === 'record')('recovers the whole surface across a reload from the log alone', async () => {
  219. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-reload'))
  220. const warningStart = tripwire.warnings.length
  221. await page.reload({ waitUntil: 'load' })
  222. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  223. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  224. // Selection persisted (dsh.sessions.current) and history replayed: the
  225. // recorded turn re-renders from a Session Controller page with zero model calls —
  226. // the replay cursor was fully consumed before the reload, so any stray
  227. // request would fail the scenario loudly at close().
  228. await expect.poll(() => page.getByText('LIGHTHOUSE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  229. await expect.poll(() => page.locator('[role="treeitem"][aria-selected="true"]').count(), { timeout: 10_000 }).toBe(1)
  230. // Golden of the recovered conversation region: rebuilt from the log, it
  231. // must render the same settled transcript the live turn produced.
  232. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  233. await compareOrRefreshGolden(RELOADED_EXPECTED, snapshot, MODE)
  234. const expanded = await captureExpandedTurnProcessAria(
  235. page,
  236. '[class*="centerCol"]',
  237. scaffold.workspaceCwd,
  238. )
  239. await compareOrRefreshGolden(RELOADED_EXPANDED_EXPECTED, expanded, MODE)
  240. expect(tripwire.pageErrors).toEqual([])
  241. }, 90_000)
  242. it.skipIf(MODE === 'record')('cascades the dark theme from the body attribute to painted surfaces', async () => {
  243. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-dark'))
  244. // This scenario pins the ThemeRuntime's DOM contract directly (the
  245. // body[data-ds-dark-theme] attribute -> stylesheet cascade); the REAL
  246. // user gesture above it (Settings -> Appearance cubes) is owned by
  247. // settings-chrome.e2e.ts. Driving the attribute here keeps the cascade
  248. // pinned independently of the settings surface's own lifecycle.
  249. const sample = async (): Promise<{ token: string; sidebarBg: string; bodyBg: string }> =>
  250. await page.evaluate(() => {
  251. const sidebar = document.querySelector('[class*="sidebar"], [class*="rail"]') ?? document.body
  252. return {
  253. token: getComputedStyle(document.body).getPropertyValue('--dsw-alias-bg-base').trim(),
  254. sidebarBg: getComputedStyle(sidebar).backgroundColor,
  255. bodyBg: getComputedStyle(document.body).backgroundColor,
  256. }
  257. })
  258. const light = await sample()
  259. await page.evaluate(() => { document.body.setAttribute('data-ds-dark-theme', '') })
  260. const dark = await sample()
  261. // The alias token itself must flip — the cascade's root fact.
  262. expect(dark.token).not.toBe(light.token)
  263. // And a real painted surface must consume it (not just variables in a
  264. // void): at least one of the sampled backgrounds repaints.
  265. expect(dark.sidebarBg !== light.sidebarBg || dark.bodyBg !== light.bodyBg).toBe(true)
  266. // Removing the attribute restores the light values exactly (the palettes
  267. // live in one stylesheet; activation is attribute-only by design).
  268. await page.evaluate(() => { document.body.removeAttribute('data-ds-dark-theme') })
  269. const restored = await sample()
  270. expect(restored).toEqual(light)
  271. expect(tripwire.pageErrors).toEqual([])
  272. }, 60_000)
  273. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  274. expect(tripwire.warnings).toEqual([])
  275. await assertFixtureInventory(SNAPSHOT_DIR, [
  276. 'session.jsonl', 'replay.override.json', 'command-menu.expected.md',
  277. 'command-menu-fuzzy.expected.md', 'hero.expected.md', 'plan-active.expected.md',
  278. 'reloaded.expected.md', 'reloaded-expanded.expected.md',
  279. ])
  280. })
  281. })