details-session-lifecycle.e2e.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Keyless browser regression for the details column's default visibility and Session ownership.
  2. // The shipped composition starts closed after selection and reload, retains an explicitly opened width through
  3. // unselected states, and closes it only when a different Session takes ownership.
  4. import { readFile } from 'node:fs/promises'
  5. import { fileURLToPath } from 'node:url'
  6. import { join } from 'node:path'
  7. import type { Browser, Page } from 'playwright'
  8. import { chromium } from 'playwright'
  9. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  10. import {
  11. acknowledgeReloadConnectionLoss, assertFixtureInventory, compareOrRefreshGolden,
  12. fixtureUserPrompts, launchWebScaffold, seedSession, watchConsole, webSnapshotMode,
  13. type WebScaffold,
  14. } from './scaffold.ts'
  15. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  16. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/details-session-lifecycle', import.meta.url))
  17. const HANDLES_EXPECTED = join(SNAPSHOT_DIR, 'handles.expected.md')
  18. const FIXTURE = fileURLToPath(new URL('./snapshots/lifecycle-chrome/session.jsonl', import.meta.url))
  19. const SEED_FIXTURE = fileURLToPath(new URL('./snapshots/seeded-history/seed.jsonl', import.meta.url))
  20. const PROMPT = 'Reply with the single word LIGHTHOUSE and stop.'
  21. const MODE = webSnapshotMode()
  22. /** Last AppFrame grid track in CSS pixels. */
  23. async function detailsTrack(page: Page): Promise<number> {
  24. return await appFrame(page).evaluate((element) => {
  25. const tracks = getComputedStyle(element).gridTemplateColumns.split(' ')
  26. return Number.parseFloat(tracks.at(-1) ?? 'NaN')
  27. })
  28. }
  29. /** First AppFrame grid track in CSS pixels. */
  30. async function sidebarTrack(page: Page): Promise<number> {
  31. return await appFrame(page).evaluate((element) => {
  32. const tracks = getComputedStyle(element).gridTemplateColumns.split(' ')
  33. return Number.parseFloat(tracks[0] ?? 'NaN')
  34. })
  35. }
  36. /** AppFrame is the only product element with an inline grid track template. */
  37. function appFrame(page: Page) {
  38. return page.locator('[style*="grid-template-columns"]').first()
  39. }
  40. /** Render the two boundary affordances without platform-dependent coordinates. */
  41. async function handleSnapshot(page: Page): Promise<string> {
  42. const handles = await page.locator('[class*="handle"]').evaluateAll(elements =>
  43. elements.map(element => ({
  44. side: element.getAttribute('data-side'),
  45. cursor: getComputedStyle(element).cursor,
  46. pillGenerated: getComputedStyle(element, '::after').content !== 'none',
  47. })))
  48. return [
  49. '# AppFrame drag handles',
  50. '',
  51. ...handles.flatMap(handle => [
  52. `## ${handle.side}`,
  53. '',
  54. '- hit strip present: true',
  55. `- cursor: ${handle.cursor}`,
  56. `- pill generated: ${String(handle.pillGenerated)}`,
  57. '',
  58. ]),
  59. ].join('\n').trimEnd()
  60. }
  61. describe.skipIf(MODE === 'record')('web e2e: details panel follows the current Session lifecycle', () => {
  62. let scaffold: WebScaffold
  63. let browser: Browser
  64. let page: Page
  65. let tripwire: ReturnType<typeof watchConsole>
  66. beforeAll(async () => {
  67. const fixture = await readFile(FIXTURE, 'utf8')
  68. expect(fixtureUserPrompts(fixture)).toEqual([PROMPT])
  69. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, paceMs: 5 })
  70. await seedSession(scaffold, await readFile(SEED_FIXTURE, 'utf8'), 'details-session-lifecycle-seed')
  71. browser = await chromium.launch()
  72. page = await newEnglishPage(browser)
  73. tripwire = watchConsole(page)
  74. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  75. await appFrame(page).waitFor({ timeout: 30_000 })
  76. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  77. }, 120_000)
  78. afterAll(async () => {
  79. await browser?.close()
  80. await scaffold?.close()
  81. })
  82. it('starts and reloads closed, then stays closed across Session ownership changes', async () => {
  83. onTestFailed(() => saveFailureShot(page, 'web-e2e-details-session-lifecycle'))
  84. const settled = scaffold.whenTurnSettled()
  85. const input = page.locator('textarea').first()
  86. await input.fill(PROMPT)
  87. await input.press('Enter')
  88. await settled
  89. await page.getByText('LIGHTHOUSE', { exact: true }).waitFor({ timeout: 15_000 })
  90. await expect.poll(() => detailsTrack(page), { timeout: 5_000 }).toBe(0)
  91. expect(await page.getByText('Details', { exact: true }).isVisible()).toBe(false)
  92. await compareOrRefreshGolden(HANDLES_EXPECTED, await handleSnapshot(page), MODE)
  93. const sidebarBefore = await sidebarTrack(page)
  94. const sidebarHandle = page.locator('[data-side="sidebar"]')
  95. const sidebarBox = await sidebarHandle.boundingBox()
  96. expect(sidebarBox).not.toBeNull()
  97. const dragStartX = sidebarBox!.x + sidebarBox!.width / 2
  98. await page.mouse.move(dragStartX, sidebarBox!.y + 200)
  99. await page.mouse.down()
  100. await page.mouse.move(dragStartX + 70, sidebarBox!.y + 200, { steps: 6 })
  101. await page.mouse.up()
  102. await expect.poll(() => sidebarTrack(page), { timeout: 5_000 }).toBe(sidebarBefore + 70)
  103. const warningStart = tripwire.warnings.length
  104. await page.reload({ waitUntil: 'load' })
  105. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  106. await appFrame(page).waitFor({ timeout: 30_000 })
  107. await page.getByText('LIGHTHOUSE', { exact: true }).waitFor({ timeout: 15_000 })
  108. await expect.poll(() => detailsTrack(page), { timeout: 5_000 }).toBe(0)
  109. expect(await page.getByText('Details', { exact: true }).isVisible()).toBe(false)
  110. await page.getByRole('button', { name: /^(?:New session|新.*会话)$/ }).last().click()
  111. await page.getByText("Let's start building", { exact: false }).waitFor({ timeout: 15_000 })
  112. await expect.poll(() => detailsTrack(page), { timeout: 5_000 }).toBe(0)
  113. expect(await page.getByText('Details', { exact: true }).isVisible()).toBe(false)
  114. const original = page.locator('[role=treeitem]').filter({ hasText: 'Reply with the single word' }).first()
  115. await original.click()
  116. await page.getByText('LIGHTHOUSE', { exact: true }).waitFor({ timeout: 15_000 })
  117. await expect.poll(() => detailsTrack(page), { timeout: 5_000 }).toBe(0)
  118. expect(await page.getByText('Details', { exact: true }).isVisible()).toBe(false)
  119. const ungrouped = page.getByText('Ungrouped', { exact: true })
  120. const ungroupedRow = ungrouped.locator('..').locator('..')
  121. const ungroupedSection = ungroupedRow.locator('..')
  122. await expect.poll(async () => {
  123. if (await ungroupedRow.getAttribute('aria-expanded') !== 'true') {
  124. await ungrouped.click()
  125. await page.waitForTimeout(50)
  126. }
  127. return await ungroupedRow.getAttribute('aria-expanded')
  128. }, { timeout: 5_000 }).toBe('true')
  129. const seeded = ungroupedSection.locator('[role="treeitem"]').nth(1)
  130. await seeded.click()
  131. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 15_000 })
  132. await expect.poll(() => detailsTrack(page), { timeout: 5_000 }).toBe(0)
  133. expect(tripwire.pageErrors).toEqual([])
  134. expect(tripwire.warnings).toEqual([])
  135. await assertFixtureInventory(SNAPSHOT_DIR, ['handles.expected.md'])
  136. }, 90_000)
  137. })