startup-auto-selection.e2e.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Web e2e scenario: startup auto-selection keeps the hero on screen.
  2. //
  3. // A page load with a workspace already registered runs
  4. // `WorkspacesService.startInitialSelection`: it connects the most recent
  5. // workspace and opens its blank session. `openState` flips to `loading` the
  6. // moment `open()` lands, which used to drive `data-phase=settling` on the
  7. // conversation root — `visibility:hidden` over the composer seat and the
  8. // header for the whole `session.history` round-trip, so the center column went
  9. // blank and repainted, reading as a full-page refresh on every launch.
  10. //
  11. // The unit spec pins the phase condition over hand-built stores. What only the
  12. // assembled application can show is that the path a user actually takes
  13. // reaches it: the real selection service, the real client session opening over
  14. // the real /api transport, and a real browser deciding what is painted.
  15. //
  16. // The round-trip against a loopback host is far too fast to observe, so this
  17. // scenario HOLDS the `session.history` response open at the browser's network
  18. // boundary and asserts the visible frame while it is in flight. That gate is
  19. // what makes the assertions non-vacuous: with the exemption reverted the held
  20. // window is exactly when `settling` is painted and the composer is hidden.
  21. //
  22. // Zero model calls: registering a workspace and opening its blank session are
  23. // host RPCs with no model involvement. A stray stream would fail loud with
  24. // NO_ADAPTER.
  25. import type { Browser, Page } from 'playwright'
  26. import { chromium } from 'playwright'
  27. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  28. import { acknowledgeReloadConnectionLoss, launchWebScaffold, watchConsole, type WebScaffold } from './scaffold.ts'
  29. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  30. /** Wire path of the history round-trip the conversation root waits out (POST /api/session.history). */
  31. const HISTORY_ROUTE = '**/api/session.history'
  32. /**
  33. * The conversation root's own phase attribute. `div` disambiguates it from the
  34. * composer textarea, which carries an unrelated `data-phase` of its own.
  35. */
  36. const ROOT_PHASE = 'div[data-phase]'
  37. /** Every distinct `data-phase` the conversation root shows, in order, across one page load. */
  38. function recordedPhases(page: Page): Promise<string[]> {
  39. return page.evaluate(() => (window as unknown as { __conversationPhases: string[] }).__conversationPhases)
  40. }
  41. describe('web e2e: startup auto-selection', () => {
  42. let scaffold: WebScaffold
  43. let browser: Browser
  44. let page: Page
  45. let tripwire: ReturnType<typeof watchConsole>
  46. beforeAll(async () => {
  47. scaffold = await launchWebScaffold({})
  48. browser = await chromium.launch()
  49. page = await newEnglishPage(browser)
  50. tripwire = watchConsole(page)
  51. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  52. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  53. // A registered workspace is the precondition for auto-selection: the first
  54. // load has nothing to select, so the reload below is the path under test.
  55. await connectFreshWorkspace(page, scaffold.workspaceCwd, 'startup-auto-selection')
  56. }, 180_000)
  57. afterAll(async () => {
  58. await browser?.close()
  59. await scaffold?.close()
  60. })
  61. it('keeps the hero and the composer on screen while the auto-selected blank session opens', async () => {
  62. onTestFailed(() => saveFailureShot(page, 'web-e2e-startup-auto-selection'))
  63. // Runs before any page script on the reload below, so the first phase the
  64. // root ever renders is recorded, not just the ones after a listener attaches.
  65. await page.addInitScript(() => {
  66. const phases: string[] = []
  67. ;(window as unknown as { __conversationPhases: string[] }).__conversationPhases = phases
  68. setInterval(() => {
  69. const phase = document.querySelector('div[data-phase]')?.getAttribute('data-phase')
  70. if (phase === null || phase === undefined) return
  71. if (phases[phases.length - 1] !== phase) phases.push(phase)
  72. }, 8)
  73. })
  74. let releaseHistory = (): void => {}
  75. const historyHeld = new Promise<void>((resolve) => { releaseHistory = resolve })
  76. let historyRequested = (): void => {}
  77. const historyInFlight = new Promise<void>((resolve) => { historyRequested = resolve })
  78. let gated = false
  79. await page.route(HISTORY_ROUTE, async (route) => {
  80. // Only the auto-selection's own round-trip is held; later pages must not
  81. // deadlock behind a gate this test has already released.
  82. if (gated) { await route.continue(); return }
  83. gated = true
  84. historyRequested()
  85. await historyHeld
  86. await route.continue()
  87. })
  88. const warningsBefore = tripwire.warnings.length
  89. await page.reload({ waitUntil: 'commit' })
  90. await historyInFlight
  91. // The frame a user sees while the session is still opening: hero phase, the
  92. // hero title, and a composer that is actually painted (`settling` hides the
  93. // seat with `visibility:hidden`, which Playwright reports as not visible).
  94. await page.waitForSelector(ROOT_PHASE, { timeout: 15_000 })
  95. expect(await page.locator(ROOT_PHASE).first().getAttribute('data-phase')).toBe('hero')
  96. expect(await page.getByText("Let's start building").isVisible()).toBe(true)
  97. expect(await page.locator('textarea').first().isVisible()).toBe(true)
  98. releaseHistory()
  99. await page.locator('textarea:enabled[placeholder="Describe what you want to build"]')
  100. .waitFor({ timeout: 15_000 })
  101. acknowledgeReloadConnectionLoss(tripwire, warningsBefore)
  102. // Settling is not merely absent from the frame sampled above: the root
  103. // never entered it at any point of the load.
  104. expect(await recordedPhases(page)).toEqual(['hero'])
  105. expect(tripwire.pageErrors).toEqual([])
  106. }, 120_000)
  107. })