lifecycle-chrome.e2e.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Per the
  10. // lane's scope ruling there is no theme/layout golden (aria is color-blind);
  11. // the hero's waiting state gets the one golden here.
  12. import { readFile } from 'node:fs/promises'
  13. import { fileURLToPath } from 'node:url'
  14. import { join } from 'node:path'
  15. import type { Browser, Page } from 'playwright'
  16. import { chromium } from 'playwright'
  17. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  18. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  19. import {
  20. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  21. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  22. } from './scaffold.ts'
  23. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  24. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/lifecycle-chrome', import.meta.url))
  25. const FIXTURE = join(SNAPSHOT_DIR, 'session.jsonl')
  26. const HERO_EXPECTED = join(SNAPSHOT_DIR, 'hero.expected.md')
  27. const COMMAND_MENU_EXPECTED = join(SNAPSHOT_DIR, 'command-menu.expected.md')
  28. const PLAN_ACTIVE_EXPECTED = join(SNAPSHOT_DIR, 'plan-active.expected.md')
  29. // Post-reload golden: the same settled conversation rebuilt purely from
  30. // persistence + history — byte-equal rendering is exactly the recovery claim.
  31. const RELOADED_EXPECTED = join(SNAPSHOT_DIR, 'reloaded.expected.md')
  32. const MODE = webSnapshotMode()
  33. const PROMPT = 'Reply with the single word LIGHTHOUSE and stop.'
  34. describe('web e2e: lifecycle & chrome (workspace flow / reload / dark mode)', () => {
  35. let scaffold: WebScaffold
  36. let browser: Browser
  37. let page: Page
  38. let tripwire: ReturnType<typeof watchConsole>
  39. const sessionEvents: SessionEvent[] = []
  40. beforeAll(async () => {
  41. scaffold = await launchWebScaffold(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 })
  42. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  43. browser = await chromium.launch()
  44. page = await newEnglishPage(browser)
  45. tripwire = watchConsole(page)
  46. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  47. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  48. // Fresh world: connect a Workspace so the composer scenarios start live.
  49. await connectFreshWorkspace(page)
  50. }, 120_000)
  51. afterAll(async () => {
  52. await browser?.close()
  53. await scaffold?.close()
  54. })
  55. it.skipIf(MODE === 'record')('opens the shared slash menu from plus with only Command candidates', async () => {
  56. onTestFailed(() => saveFailureShot(page, 'web-e2e-command-menu-launcher'))
  57. const launcher = page.getByRole('button', { name: 'Commands' })
  58. await launcher.click()
  59. const menu = page.getByRole('listbox', { name: 'Trigger suggestions' })
  60. await menu.waitFor({ timeout: 10_000 })
  61. const snapshot = await captureStableAria(page, '[role="listbox"]', scaffold.workspaceCwd)
  62. await compareOrRefreshGolden(COMMAND_MENU_EXPECTED, snapshot, MODE)
  63. expect(snapshot).toContain('text: Commands')
  64. expect(snapshot).not.toContain('text: Skills')
  65. expect(snapshot).not.toContain('text: Subagents')
  66. const launchedBox = await menu.boundingBox()
  67. await page.locator('textarea').first().press('Escape')
  68. await expect.poll(() => menu.count()).toBe(0)
  69. const input = page.locator('textarea').first()
  70. await input.fill('/')
  71. await menu.waitFor({ timeout: 10_000 })
  72. const typedBox = await menu.boundingBox()
  73. expect(launchedBox).not.toBeNull()
  74. expect(typedBox).not.toBeNull()
  75. expect(Math.abs(launchedBox!.x - typedBox!.x)).toBeLessThan(1)
  76. expect(Math.abs(
  77. launchedBox!.y + launchedBox!.height - typedBox!.y - typedBox!.height,
  78. )).toBeLessThan(1)
  79. await input.fill('')
  80. await expect.poll(() => menu.count()).toBe(0)
  81. })
  82. it.skipIf(MODE === 'record')('shows active Plan as the warn-state status action', async () => {
  83. const activeScaffold = await launchWebScaffold()
  84. const activePage = await newEnglishPage(browser)
  85. const activeTripwire = watchConsole(activePage)
  86. try {
  87. await activePage.goto(activeScaffold.baseUrl, { waitUntil: 'load' })
  88. await activePage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  89. await connectFreshWorkspace(activePage)
  90. const input = activePage.locator('textarea').first()
  91. await activePage.getByRole('button', { name: 'Commands' }).click()
  92. const menu = activePage.getByRole('listbox', { name: 'Trigger suggestions' })
  93. await menu.waitFor({ timeout: 10_000 })
  94. await menu.getByRole('option', { name: 'plan Enter or leave plan mode' }).click()
  95. await expect.poll(() => input.inputValue()).toBe('/plan ')
  96. await input.press('Enter')
  97. const planButton = activePage.getByRole('button', { name: 'Plan mode on, press to turn off' })
  98. await planButton.waitFor({ timeout: 10_000 })
  99. const planSnapshot = await captureStableAria(activePage, '[class*="frame"]', activeScaffold.workspaceCwd)
  100. await compareOrRefreshGolden(PLAN_ACTIVE_EXPECTED, planSnapshot, MODE)
  101. const planStyle = await planButton.evaluate((element) => {
  102. const probe = document.createElement('span')
  103. probe.style.color = 'var(--dsw-alias-state-warn-label)'
  104. probe.style.backgroundColor = 'var(--dsw-alias-state-warn-tertiary)'
  105. document.body.append(probe)
  106. const actual = getComputedStyle(element)
  107. const reference = getComputedStyle(probe)
  108. const result = {
  109. color: actual.color,
  110. backgroundColor: actual.backgroundColor,
  111. borderRadius: actual.borderRadius,
  112. fontSize: actual.fontSize,
  113. referenceColor: reference.color,
  114. referenceBackgroundColor: reference.backgroundColor,
  115. }
  116. probe.remove()
  117. return result
  118. })
  119. expect(planStyle.color).toBe(planStyle.referenceColor)
  120. expect(planStyle.backgroundColor).toBe(planStyle.referenceBackgroundColor)
  121. expect(planStyle.borderRadius).toBe('999px')
  122. expect(planStyle.fontSize).toBe('13px')
  123. await planButton.click()
  124. await expect.poll(() => planButton.count()).toBe(0)
  125. expect(activeTripwire.pageErrors).toEqual([])
  126. expect(activeTripwire.warnings).toEqual([])
  127. } catch (error) {
  128. await saveFailureShot(activePage, 'web-e2e-plan-active').catch(() => undefined)
  129. throw error
  130. } finally {
  131. await activePage.close()
  132. await activeScaffold.close()
  133. }
  134. })
  135. it('sends the first prompt from the empty-state hero (all modes)', async () => {
  136. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-send'))
  137. if (MODE !== 'record') {
  138. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  139. }
  140. // The blank frame renders the hero, not the resident composer: the
  141. // headline plus the guidance placeholder are the empty state's anchors.
  142. await expect.poll(() => page.getByText("Let's start building", { exact: false }).count(), { timeout: 15_000 }).toBe(1)
  143. const input = page.locator('textarea').first()
  144. await input.waitFor({ timeout: 10_000 })
  145. if (MODE !== 'record') {
  146. // Golden of the hero's stable waiting state (captured before any send;
  147. // the conversation-region goldens belong to the other scenarios).
  148. const snapshot = await captureStableAria(page, '[class*="frame"]', scaffold.workspaceCwd)
  149. await compareOrRefreshGolden(HERO_EXPECTED, snapshot, MODE)
  150. }
  151. const settled = scaffold.whenTurnSettled()
  152. await input.fill(PROMPT)
  153. await input.press('Enter')
  154. const sessionId = await settled
  155. if (MODE === 'record') {
  156. await recordFixture(scaffold, sessionId, FIXTURE)
  157. }
  158. }, 200_000)
  159. it.skipIf(MODE === 'record')('materialized a real Workspace and Session over the wire', async () => {
  160. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-materialize'))
  161. // Browser: the sidebar tree now carries the auto-created workspace group
  162. // with its one session, and the opened session is the selected row.
  163. await expect.poll(() => page.getByText('1 session', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  164. await expect.poll(() => page.locator('[role="treeitem"][aria-selected="true"]').count(), { timeout: 10_000 }).toBe(1)
  165. await expect.poll(() => page.getByText('LIGHTHOUSE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  166. // Host: the session's durable header cwd is the workspace flow's
  167. // create-by-name target (<workspaceRoot>/workspace, the composer's
  168. // default draft name) — the proof the send went through workspace
  169. // materialization rather than a bare default-cwd session.
  170. const cwds = scaffold.ctx.sessions.list().map(session => session.header.cwd)
  171. expect(cwds).toEqual([join(scaffold.workspaceCwd, 'workspace')])
  172. const turnEnds = sessionEvents.filter(e => e.type === 'turn/end')
  173. expect(turnEnds).toHaveLength(1)
  174. expect((turnEnds[0] as SessionEvent & { data: { reason: { kind: string } } }).data.reason.kind).toBe('completed')
  175. }, 60_000)
  176. it.skipIf(MODE === 'record')('recovers the whole surface across a reload from the log alone', async () => {
  177. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-reload'))
  178. const warningStart = tripwire.warnings.length
  179. await page.reload({ waitUntil: 'load' })
  180. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  181. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  182. // Selection persisted (dsh.sessions.current) and history replayed: the
  183. // recorded turn re-renders from session.history with zero model calls —
  184. // the replay cursor was fully consumed before the reload, so any stray
  185. // request would fail the scenario loudly at close().
  186. await expect.poll(() => page.getByText('LIGHTHOUSE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  187. await expect.poll(() => page.locator('[role="treeitem"][aria-selected="true"]').count(), { timeout: 10_000 }).toBe(1)
  188. // Golden of the recovered conversation region: rebuilt from the log, it
  189. // must render the same settled transcript the live turn produced.
  190. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  191. await compareOrRefreshGolden(RELOADED_EXPECTED, snapshot, MODE)
  192. expect(tripwire.pageErrors).toEqual([])
  193. }, 90_000)
  194. it.skipIf(MODE === 'record')('cascades the dark theme from the body attribute to painted surfaces', async () => {
  195. onTestFailed(() => saveFailureShot(page, 'web-e2e-lifecycle-dark'))
  196. // This scenario pins the ThemeService's DOM contract seam directly (the
  197. // body[data-ds-dark-theme] attribute -> stylesheet cascade); the REAL
  198. // user gesture above it (Settings -> Appearance cubes) is owned by
  199. // settings-chrome.e2e.ts. Driving the attribute here keeps the cascade
  200. // pinned independently of the settings surface's own lifecycle.
  201. const sample = async (): Promise<{ token: string; sidebarBg: string; bodyBg: string }> =>
  202. await page.evaluate(() => {
  203. const sidebar = document.querySelector('[class*="sidebar"], [class*="rail"]') ?? document.body
  204. return {
  205. token: getComputedStyle(document.body).getPropertyValue('--dsw-alias-bg-base').trim(),
  206. sidebarBg: getComputedStyle(sidebar).backgroundColor,
  207. bodyBg: getComputedStyle(document.body).backgroundColor,
  208. }
  209. })
  210. const light = await sample()
  211. await page.evaluate(() => { document.body.setAttribute('data-ds-dark-theme', '') })
  212. const dark = await sample()
  213. // The alias token itself must flip — the cascade's root fact.
  214. expect(dark.token).not.toBe(light.token)
  215. // And a real painted surface must consume it (not just variables in a
  216. // void): at least one of the sampled backgrounds repaints.
  217. expect(dark.sidebarBg !== light.sidebarBg || dark.bodyBg !== light.bodyBg).toBe(true)
  218. // Removing the attribute restores the light values exactly (the palettes
  219. // live in one stylesheet; activation is attribute-only by design).
  220. await page.evaluate(() => { document.body.removeAttribute('data-ds-dark-theme') })
  221. const restored = await sample()
  222. expect(restored).toEqual(light)
  223. expect(tripwire.pageErrors).toEqual([])
  224. }, 60_000)
  225. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  226. expect(tripwire.warnings).toEqual([])
  227. await assertFixtureInventory(SNAPSHOT_DIR, [
  228. 'session.jsonl', 'command-menu.expected.md', 'hero.expected.md', 'plan-active.expected.md', 'reloaded.expected.md',
  229. ])
  230. })
  231. })