composer-tab-geometry.e2e.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Web e2e scenario: the input card holds one horizontal position across the
  2. // Chat and Trajectory tabs.
  3. //
  4. // The composer seat is the same node in both tabs, but it measures itself
  5. // against a different edge in each (see
  6. // packages/client/ui-conversation/src/client/skeleton/ConversationRoot.module.css).
  7. // In Chat it is a sticky CHILD of the column's scroller, so it rides that
  8. // scroller's content box — the box a space-consuming scrollbar shortens. A view
  9. // that opts into a composer overlay (`data-conversation-composer-overlay`, which
  10. // Trajectory declares and which moves the column's own scrolling into the view)
  11. // gets an absolutely positioned seat instead, laid out against the padding box,
  12. // which the scrollbar never reduces.
  13. //
  14. // So the two tabs disagreed by exactly the bar's width for as long as the
  15. // transcript overflowed: the card jumped sideways on every tab switch, and
  16. // inside Chat alone at the moment a growing transcript started to scroll. The
  17. // column now reserves the gutter unconditionally (`scrollbar-gutter: stable`)
  18. // and states the overlay branch as a scroll container on the same axes, so both
  19. // edges are the same edge.
  20. //
  21. // Only a real engine can show this. The seat's geometry is layout: jsdom gives
  22. // every element a zero-sized box and reports no scrollbar at all, so a unit spec
  23. // can assert the declarations exist but not that the two states land in the same
  24. // place. What is asserted here is the user-visible fact — the card does not move
  25. // — measured as the distance between the two tabs' card rectangles.
  26. //
  27. // The browser is launched WITHOUT Playwright's default `--hide-scrollbars`,
  28. // which is load-bearing rather than incidental. Under that argument a scroll
  29. // container's bar consumes no layout width at all, so the two tabs agree before
  30. // this change as much as after it and every comparison below holds vacuously —
  31. // measured: the pre-fix cascade leaves both tabs' bands at 0 there, against 8
  32. // and 0 with the argument dropped. Dropping it is also the faithful
  33. // configuration: ui-theme's scrollbar.css gives `::-webkit-scrollbar` a width,
  34. // and a bar that occupies layout space is what the product actually draws.
  35. //
  36. // The scenario runs that pre-fix cascade in the page — `scrollbar-gutter: auto`
  37. // on the scroller, `overflow: hidden` on the overlay branch — and measures the
  38. // same two tabs through it, which is what keeps the equal rectangles above from
  39. // being explained by a tab switch that never reached the layout. It is the
  40. // reported symptom as a number: the card moves 4px, half the 8px band, on each
  41. // edge.
  42. //
  43. // Zero model calls: a seeded cold session renders from its log, and switching
  44. // tabs asks the host for nothing. A stray stream would fail loud with NO_ADAPTER.
  45. import { fileURLToPath } from 'node:url'
  46. import { join } from 'node:path'
  47. import type { Browser, Page } from 'playwright'
  48. import { chromium } from 'playwright'
  49. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  50. import { createChatScrollFixture } from './chat-scroll-fixture.ts'
  51. import {
  52. assertFixtureInventory, compareOrRefreshGolden, launchWebScaffold, seedSession, watchConsole,
  53. webSnapshotMode, type WebScaffold,
  54. } from './scaffold.ts'
  55. import { newEnglishPage, saveFailureShot } from './support.ts'
  56. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/composer-tab-geometry', import.meta.url))
  57. /**
  58. * Committed golden of where the input card sits in each tab, at a wide viewport
  59. * (card at its width cap) and a narrow one (card shrinking with the column).
  60. *
  61. * Absolute coordinates are deliberately absent: they depend on the sidebar's
  62. * laid-out width and on font metrics, so committing them would produce a fixture
  63. * that has to be re-recorded per platform. What is recorded is the distance
  64. * between the two tabs' rectangles, which is zero when the reservation holds and
  65. * the bar's width when it does not — including under the control, so the golden
  66. * carries the difference the fix removes rather than only its absence.
  67. */
  68. const GEOMETRY_EXPECTED = join(SNAPSHOT_DIR, 'geometry.expected.md')
  69. const MODE = webSnapshotMode()
  70. /** Long enough that the transcript overflows the lane's 1000px viewport; the scenario asserts the overflow rather than trusting it. */
  71. const FIXTURE = createChatScrollFixture({
  72. markerPrefix: 'TAB_GEOMETRY',
  73. title: 'COMPOSER_TAB_GEOMETRY long session',
  74. turns: 24,
  75. })
  76. const SEED_ID = 'composer-tab-geometry-web-e2e'
  77. /** Viewport widths the scenario measures at: the card capped, and the card shrinking with the column. */
  78. const WIDE_VIEWPORT = { width: 1680, height: 1000 }
  79. const NARROW_VIEWPORT = { width: 800, height: 1000 }
  80. /**
  81. * Resize to one measurement viewport after the responsive sidebar and center
  82. * column finish their track transition.
  83. * @param page - the page under test.
  84. * @param viewport - the viewport dimensions to apply.
  85. * @param sidebarCollapsed - the sidebar state expected at this width.
  86. */
  87. async function setMeasuredViewport(
  88. page: Page,
  89. viewport: { width: number; height: number },
  90. sidebarCollapsed: boolean,
  91. ): Promise<void> {
  92. await page.setViewportSize(viewport)
  93. await page.locator('[data-sidebar-collapsed="true"]').waitFor({
  94. state: sidebarCollapsed ? 'attached' : 'detached',
  95. timeout: 10_000,
  96. })
  97. await page.locator('[data-conversation-scroll]').evaluate(async (host) => {
  98. const deadline = performance.now() + 5_000
  99. let previous = host.getBoundingClientRect().width
  100. let stableFrames = 0
  101. while (performance.now() < deadline) {
  102. await new Promise<void>((resolve) => { requestAnimationFrame(() => { resolve() }) })
  103. const current = host.getBoundingClientRect().width
  104. stableFrames = Math.abs(current - previous) < 0.01 ? stableFrames + 1 : 0
  105. if (stableFrames >= 3) return
  106. previous = current
  107. }
  108. throw new Error('conversation width did not settle after the viewport changed')
  109. })
  110. }
  111. /**
  112. * The pre-fix cascade, injected into the page: the reservation dropped and the
  113. * overlay branch back to a hidden box. `!important` beats the module rules
  114. * without a rebuild, and the id lets the control be lifted again in the same
  115. * session.
  116. */
  117. const CONTROL_STYLE_ID = 'composer-tab-geometry-control'
  118. const CONTROL_CSS = `
  119. [data-conversation-scroll] { scrollbar-gutter: auto !important; }
  120. [data-conversation-scroll]:has([data-conversation-composer-overlay]) { overflow: hidden !important; }
  121. `
  122. /** The column scroller and the input card as the browser lays them out, in one tab. */
  123. interface TabMetrics {
  124. /** Resolved `scrollbar-gutter` on the column's scroller. */
  125. gutter: string
  126. /** Resolved `overflow-x`: `hidden` in both states, so neither grows a horizontal bar. */
  127. overflowX: string
  128. /** Resolved `overflow-y`: `auto` in both states, which is the form WebKit honours the gutter on. */
  129. overflowY: string
  130. /** Border-box width minus client width: the space the scrollbar takes out of the content area. */
  131. band: number
  132. /** True when the column's scroller actually scrolls — only Chat does. */
  133. scrolls: boolean
  134. /** Left edge of the input card in viewport coordinates. */
  135. cardLeft: number
  136. /** Right edge of the input card. */
  137. cardRight: number
  138. /** Width of the input card, capped at the composer card max width. */
  139. cardWidth: number
  140. }
  141. /** One tab's metrics beside the other's, plus the distances between them. */
  142. interface TabComparison {
  143. chat: TabMetrics
  144. trajectory: TabMetrics
  145. /** Distance between the two tabs' card left edges: 0 when the card holds its position. */
  146. leftShift: number
  147. /** Distance between the two tabs' card right edges. */
  148. rightShift: number
  149. /** Difference between the two tabs' card widths. */
  150. widthShift: number
  151. }
  152. /**
  153. * Measure the column scroller and the input card in the tab currently shown.
  154. * @param page - the page under test.
  155. * @returns the scroller's resolved overflow style and the card's rectangle.
  156. */
  157. function measureTab(page: Page): Promise<TabMetrics> {
  158. return page.evaluate(() => {
  159. const host = document.querySelector<HTMLElement>('[data-conversation-scroll]')
  160. if (host === null) throw new Error('conversation column scroller not in the DOM')
  161. const card = host.querySelector<HTMLElement>('[data-composer-seat] [data-composer-card]')
  162. if (card === null) throw new Error('no input card inside the composer seat')
  163. const style = getComputedStyle(host)
  164. const hostRect = host.getBoundingClientRect()
  165. const cardRect = card.getBoundingClientRect()
  166. return {
  167. gutter: style.scrollbarGutter,
  168. overflowX: style.overflowX,
  169. overflowY: style.overflowY,
  170. band: hostRect.width - host.clientWidth,
  171. scrolls: host.scrollHeight > host.clientHeight,
  172. cardLeft: cardRect.left,
  173. cardRight: cardRect.right,
  174. cardWidth: cardRect.width,
  175. }
  176. })
  177. }
  178. /**
  179. * Show one tab and wait for the view that owns it to be laid out.
  180. * @param page - the page under test.
  181. * @param tab - the tab to show.
  182. */
  183. async function showTab(page: Page, tab: 'Chat' | 'Trajectory'): Promise<void> {
  184. await page.getByRole('tab', { name: tab, exact: true }).click()
  185. if (tab === 'Trajectory') await page.getByLabel('Trajectory timeline').waitFor({ timeout: 30_000 })
  186. else await page.locator('[data-conversation-scroll] [data-chat-anchor-key]').first().waitFor({ timeout: 30_000 })
  187. // Both measurements are taken after a paint, so a rectangle read mid-transition
  188. // cannot be reported as a shift the cascade did not cause.
  189. await page.evaluate(() => new Promise<void>((settle) => {
  190. requestAnimationFrame(() => { requestAnimationFrame(() => { settle() }) })
  191. }))
  192. }
  193. /**
  194. * Measure both tabs and the distances between them, leaving Chat shown.
  195. * @param page - the page under test.
  196. * @returns each tab's metrics and the card's displacement between them.
  197. */
  198. async function compareTabs(page: Page): Promise<TabComparison> {
  199. await showTab(page, 'Chat')
  200. const chat = await measureTab(page)
  201. await showTab(page, 'Trajectory')
  202. const trajectory = await measureTab(page)
  203. await showTab(page, 'Chat')
  204. return {
  205. chat,
  206. trajectory,
  207. leftShift: Math.abs(trajectory.cardLeft - chat.cardLeft),
  208. rightShift: Math.abs(trajectory.cardRight - chat.cardRight),
  209. widthShift: Math.abs(trajectory.cardWidth - chat.cardWidth),
  210. }
  211. }
  212. /**
  213. * Run the pre-fix cascade in the page for one measurement, then lift it.
  214. * @param page - the page under test.
  215. * @returns the comparison as the column laid out before this change.
  216. */
  217. async function compareTabsWithoutReservation(page: Page): Promise<TabComparison> {
  218. await page.evaluate(({ id, css }) => {
  219. const style = document.createElement('style')
  220. style.id = id
  221. style.textContent = css
  222. document.head.append(style)
  223. }, { id: CONTROL_STYLE_ID, css: CONTROL_CSS })
  224. try {
  225. return await compareTabs(page)
  226. } finally {
  227. await page.evaluate((id) => { document.getElementById(id)?.remove() }, CONTROL_STYLE_ID)
  228. }
  229. }
  230. /**
  231. * Open the seeded session from the sidebar search.
  232. *
  233. * Cold summaries carry the temp workspace's basename, so the persisted first
  234. * message is the stable identity to search for, and the query itself drives the
  235. * lazy content-index reconciliation. Hand-rolled polling because `expect.poll`
  236. * is test-scoped and this runs in `beforeAll`.
  237. * @param page - the page under test.
  238. */
  239. async function openSeededSession(page: Page): Promise<void> {
  240. const search = page.getByRole('textbox', { name: 'Search name, keywords...', exact: true })
  241. await search.fill(FIXTURE.markers.user(1))
  242. const results = page.getByRole('tree', { name: 'Search results' }).getByRole('treeitem')
  243. const deadline = Date.now() + 60_000
  244. for (;;) {
  245. if (await results.count() === 1) break
  246. if (Date.now() > deadline) throw new Error('seeded session never appeared in the sidebar search results')
  247. await page.waitForTimeout(200)
  248. }
  249. await results.click()
  250. }
  251. /**
  252. * Render the golden body.
  253. * @param wide - comparison at the viewport where the card sits at its width cap.
  254. * @param narrow - comparison at the viewport where the card shrinks with the column.
  255. * @param control - comparison at the wide viewport with the reservation removed.
  256. * @returns the golden body, without a trailing newline.
  257. */
  258. function renderGeometry(wide: TabComparison, narrow: TabComparison, control: TabComparison): string {
  259. const section = (name: string, comparison: TabComparison): string[] => [
  260. `## ${name}`,
  261. '',
  262. `- Chat: scrollbar-gutter ${comparison.chat.gutter}, overflow ${comparison.chat.overflowX}/${comparison.chat.overflowY}`,
  263. `- Chat scroller scrolls: ${String(comparison.chat.scrolls)}`,
  264. `- Chat reserved band: ${String(comparison.chat.band)}px`,
  265. `- Trajectory: scrollbar-gutter ${comparison.trajectory.gutter}, overflow ${comparison.trajectory.overflowX}/${comparison.trajectory.overflowY}`,
  266. `- Trajectory scroller scrolls: ${String(comparison.trajectory.scrolls)}`,
  267. `- Trajectory reserved band: ${String(comparison.trajectory.band)}px`,
  268. `- input card left edge moves between tabs: ${String(comparison.leftShift)}px`,
  269. `- input card right edge moves between tabs: ${String(comparison.rightShift)}px`,
  270. `- input card width changes between tabs: ${String(comparison.widthShift)}px`,
  271. '',
  272. ]
  273. return [
  274. '# Input card position across the Chat and Trajectory tabs',
  275. '',
  276. ...section(`Wide viewport (${String(WIDE_VIEWPORT.width)}px, card at its cap)`, wide),
  277. ...section(`Narrow viewport (${String(NARROW_VIEWPORT.width)}px, card shrinking with the column)`, narrow),
  278. ...section('Wide viewport, reservation removed in the page (control)', control),
  279. ].join('\n').trimEnd()
  280. }
  281. describe('web e2e: input card position across view tabs', () => {
  282. let scaffold: WebScaffold
  283. let browser: Browser
  284. let page: Page
  285. let tripwire: ReturnType<typeof watchConsole>
  286. beforeAll(async () => {
  287. scaffold = await launchWebScaffold({})
  288. await seedSession(scaffold, FIXTURE.log, SEED_ID)
  289. // Scrollbars must take layout space here or the scenario proves nothing;
  290. // see the file header for the measurement behind dropping this argument.
  291. browser = await chromium.launch({ ignoreDefaultArgs: ['--hide-scrollbars'] })
  292. page = await newEnglishPage(browser, WIDE_VIEWPORT.height)
  293. tripwire = watchConsole(page)
  294. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  295. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  296. await openSeededSession(page)
  297. await page.getByRole('tab', { name: 'Chat', exact: true }).waitFor({ timeout: 30_000 })
  298. await page.getByText(FIXTURE.markers.assistant(FIXTURE.turns), { exact: false }).last()
  299. .waitFor({ timeout: 30_000 })
  300. }, 180_000)
  301. afterAll(async () => {
  302. await browser?.close()
  303. await scaffold?.close()
  304. })
  305. it('reserves the same gutter in both tabs while the transcript scrolls', async () => {
  306. onTestFailed(() => saveFailureShot(page, 'web-e2e-composer-tab-geometry-band'))
  307. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  308. // Vacuity guard, in two parts. A transcript that does not overflow gives
  309. // Chat no scrollbar, and a hidden or overlaid bar gives it no width; either
  310. // would make the tabs agree without the reservation doing anything.
  311. await expect.poll(async () => (await measureTab(page)).scrolls, { timeout: 10_000 }).toBe(true)
  312. const comparison = await compareTabs(page)
  313. expect(comparison.chat.band).toBeGreaterThan(0)
  314. // The reservation reaches both states, which is the whole change: the same
  315. // band, on a box that scrolls and on one that only holds a view.
  316. expect(comparison.chat.gutter).toBe('stable')
  317. expect(comparison.trajectory.gutter).toBe('stable')
  318. expect(comparison.trajectory.band).toBe(comparison.chat.band)
  319. // Declared as a scroll container on both axes rather than left to compute:
  320. // `overflow: hidden` would drop the reservation in WebKit, and a `visible`
  321. // horizontal axis computes to `auto` beside a scrolling one.
  322. expect(comparison.trajectory.overflowY).toBe('auto')
  323. expect(comparison.trajectory.overflowX).toBe('hidden')
  324. // Only Chat scrolls this box; the Trajectory view owns its own scrollers.
  325. expect(comparison.trajectory.scrolls).toBe(false)
  326. expect(tripwire.pageErrors).toEqual([])
  327. }, 60_000)
  328. it('holds the input card in place when the tab changes', async () => {
  329. onTestFailed(() => saveFailureShot(page, 'web-e2e-composer-tab-geometry-wide'))
  330. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  331. const comparison = await compareTabs(page)
  332. // The reported symptom as a number. At this viewport the card sits at its
  333. // width cap, so the pre-fix shift showed up as a centring difference — half
  334. // the band on each edge — rather than as a width change.
  335. expect(comparison.leftShift).toBe(0)
  336. expect(comparison.rightShift).toBe(0)
  337. expect(comparison.widthShift).toBe(0)
  338. expect(tripwire.pageErrors).toEqual([])
  339. }, 60_000)
  340. it('holds the input card in place at a viewport where it shrinks with the column', async () => {
  341. onTestFailed(() => saveFailureShot(page, 'web-e2e-composer-tab-geometry-narrow'))
  342. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  343. const capped = await measureTab(page)
  344. await setMeasuredViewport(page, NARROW_VIEWPORT, true)
  345. const comparison = await compareTabs(page)
  346. // The other geometry, and a different failure: below the cap the card takes
  347. // the column's width, so an unreserved gutter changed its WIDTH by the whole
  348. // band instead of shifting it by half. Asserted against the capped
  349. // measurement rather than against the cap's pixel value, which belongs to
  350. // the stylesheet.
  351. expect(comparison.chat.cardWidth).toBeLessThan(capped.cardWidth)
  352. expect(comparison.leftShift).toBe(0)
  353. expect(comparison.rightShift).toBe(0)
  354. expect(comparison.widthShift).toBe(0)
  355. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  356. expect(tripwire.pageErrors).toEqual([])
  357. }, 60_000)
  358. it('moves the card again once the reservation is removed in the page', async () => {
  359. onTestFailed(() => saveFailureShot(page, 'web-e2e-composer-tab-geometry-control'))
  360. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  361. // The control: without it, equal rectangles could also mean the tab switch
  362. // never reached the layout. Under the pre-fix cascade the Chat scroller keeps
  363. // its bar and the Trajectory branch goes back to a hidden box with none, and
  364. // the card moves by half the band on each edge.
  365. const comparison = await compareTabsWithoutReservation(page)
  366. expect(comparison.chat.gutter).toBe('auto')
  367. expect(comparison.chat.band).toBeGreaterThan(0)
  368. expect(comparison.trajectory.band).toBe(0)
  369. expect(comparison.leftShift).toBe(comparison.chat.band / 2)
  370. expect(comparison.rightShift).toBe(comparison.chat.band / 2)
  371. // Restoring the sheet restores the fix, so the control cannot leak into the
  372. // remaining measurements.
  373. const restored = await compareTabs(page)
  374. expect(restored.leftShift).toBe(0)
  375. expect(tripwire.pageErrors).toEqual([])
  376. }, 60_000)
  377. it('matches the committed tab geometry golden', async () => {
  378. onTestFailed(() => saveFailureShot(page, 'web-e2e-composer-tab-geometry-golden'))
  379. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  380. const wide = await compareTabs(page)
  381. await setMeasuredViewport(page, NARROW_VIEWPORT, true)
  382. const narrow = await compareTabs(page)
  383. await setMeasuredViewport(page, WIDE_VIEWPORT, false)
  384. const control = await compareTabsWithoutReservation(page)
  385. await compareOrRefreshGolden(GEOMETRY_EXPECTED, renderGeometry(wide, narrow, control), MODE)
  386. expect(tripwire.pageErrors).toEqual([])
  387. }, 60_000)
  388. it('commits exactly the fixtures it reads', async () => {
  389. // The seeded session is generated in-process, so the geometry golden is the
  390. // whole inventory.
  391. await assertFixtureInventory(SNAPSHOT_DIR, ['geometry.expected.md'])
  392. })
  393. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', () => {
  394. expect(tripwire.warnings).toEqual([])
  395. expect(tripwire.pageErrors).toEqual([])
  396. })
  397. })