conversation-column-overflow.e2e.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Web e2e scenario: the conversation column scrolls on one axis only, as the
  2. // browser actually lays it out. The hazard: a horizontal scrollbar appears
  3. // under the whole center column once the window (or the sidebar drag) narrows
  4. // it — the hero's decorative backdrop ellipse bleeds past the column and
  5. // becomes user-scrollable.
  6. //
  7. // The bleed is by construction and stays: `.heroGlow` is sized 1051/776 of the
  8. // hero box (ConversationRoot.module.css) so the blur scales with the input
  9. // card. The scroll container is where the bar comes from:
  10. // `[data-conversation-scroll]` scrolls vertically, and a one-axis scroller
  11. // computes the other axis's initial `visible` to `auto`, so the bleed becomes
  12. // a bar; `overflow-x: hidden` on the scroller prevents it.
  13. //
  14. // Only a real engine reports that pair — the bleed and the resulting scroll
  15. // range — so the scenario sweeps viewport widths that bracket the glow's
  16. // width and asserts both at each stop. Asserting no horizontal scroll alone
  17. // would go vacuous the moment the glow stopped bleeding for an unrelated
  18. // reason, which is why each stop also records whether it bleeds; the wide stop
  19. // is the control where it does not.
  20. //
  21. // Zero model calls: the hero is the boot state, so nothing is seeded and no
  22. // replay row mounts. A stray stream would fail loud with NO_ADAPTER.
  23. import { fileURLToPath } from 'node:url'
  24. import { join } from 'node:path'
  25. import type { Browser, Page } from 'playwright'
  26. import { chromium } from 'playwright'
  27. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  28. import {
  29. assertFixtureInventory, compareOrRefreshGolden, launchWebScaffold, watchConsole, webSnapshotMode,
  30. type WebScaffold,
  31. } from './scaffold.ts'
  32. import { newEnglishPage, saveFailureShot } from './support.ts'
  33. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/conversation-column-overflow', import.meta.url))
  34. /**
  35. * Committed golden of the one-axis relation at every stop. It records
  36. * relations and booleans, never absolute coordinates: the column width follows
  37. * the viewport and the sidebar, and a golden carrying pixels would document the
  38. * platform instead of the behavior.
  39. */
  40. const GEOMETRY_EXPECTED = join(SNAPSHOT_DIR, 'geometry.expected.md')
  41. const MODE = webSnapshotMode()
  42. /** Narrow sweep stop where the mutation control retains overflow across scrollbar implementations. */
  43. const CONTROL_VIEWPORT = 600
  44. /**
  45. * Viewport widths bracketing the glow: the narrow stops retain the reported
  46. * bleed while the widest stop proves the relation can also be false.
  47. */
  48. const WIDTHS = [1680, 1200, 1000, 800, CONTROL_VIEWPORT]
  49. /** Element id of the mutation control's injected sheet, so the test can take it back out. */
  50. const CONTROL_STYLE_ID = 'dsh-column-overflow-control'
  51. /** Horizontal wheel delta per gesture; must exceed the widest bleed the sweep can produce. */
  52. const WHEEL_DELTA = 300
  53. /** One viewport stop: whether the glow bleeds past the column, and whether that bleed scrolls. */
  54. interface ColumnMetrics {
  55. /** Viewport width the stop was measured at. */
  56. width: number
  57. /** The column's content width. Not committed to the golden — it is what settles after a resize, and what the sweep waits on. */
  58. columnWidth: number
  59. /** Resolved `overflow-x` on the conversation scroll container. */
  60. overflowX: string
  61. /**
  62. * True when the glow's box reaches past the column's content edge — the
  63. * condition the `overflow-x: hidden` declaration has to survive.
  64. */
  65. glowBleeds: boolean
  66. /**
  67. * `scrollWidth - clientWidth`. Deliberately NOT the assertion: `hidden` and
  68. * `auto` both report the same value, because `hidden` clips the bleed rather
  69. * than reflowing it away. Recorded because it is the vacuity guard in
  70. * numbers — it must stay positive at the narrow stops, or the scenario has
  71. * stopped reproducing the situation `overflow-x: hidden` exists for.
  72. */
  73. bleedRange: number
  74. /** True when the column still scrolls vertically — the axis `overflow-x: hidden` must not take away. */
  75. scrollsVertically: boolean
  76. }
  77. /**
  78. * Measure the conversation column at the page's current viewport.
  79. * @param page - the page under test.
  80. * @param width - the viewport width already applied, recorded with the reading.
  81. * @returns the stop's overflow relations.
  82. */
  83. function measureColumn(page: Page, width: number): Promise<ColumnMetrics> {
  84. return page.evaluate((viewportWidth) => {
  85. const scroller = document.querySelector<HTMLElement>('[data-conversation-scroll]')
  86. if (scroller === null) throw new Error('conversation scroll container not in the DOM')
  87. const glow = scroller.querySelector<SVGElement>('[class*="heroGlow"]')
  88. if (glow === null) throw new Error('hero glow not in the DOM — the boot state is not the hero')
  89. const box = scroller.getBoundingClientRect()
  90. const glowBox = glow.getBoundingClientRect()
  91. return {
  92. width: viewportWidth,
  93. columnWidth: scroller.clientWidth,
  94. overflowX: getComputedStyle(scroller).overflowX,
  95. // `clientWidth` is the content edge, which is what the scrollable
  96. // overflow region is measured against; either side counts as a bleed,
  97. // though only the right one can produce a bar in this writing mode.
  98. glowBleeds: glowBox.right > box.left + scroller.clientWidth + 0.5 || glowBox.left < box.left - 0.5,
  99. bleedRange: scroller.scrollWidth - scroller.clientWidth,
  100. scrollsVertically: getComputedStyle(scroller).overflowY === 'auto',
  101. }
  102. }, width)
  103. }
  104. /**
  105. * Scroll the column sideways the way a user would and report where it landed.
  106. *
  107. * This is the one signal that separates the two states, and it is why the
  108. * scenario needs a real engine: `overflow-x: hidden` leaves the box
  109. * programmatically scrollable and leaves `scrollWidth` untouched, so every
  110. * property reading agrees across the two overflow modes. Only refusing an
  111. * actual input event differs — measured at the 1200px stop, the shipped
  112. * column stays at 0 while the same page with `overflow-x: auto` forced on
  113. * lands at its scroll boundary.
  114. * @param page - the page under test.
  115. * @returns `scrollLeft` after one horizontal wheel over the column.
  116. */
  117. async function wheelHorizontally(page: Page): Promise<number> {
  118. const origin = await page.evaluate(() => {
  119. const scroller = document.querySelector<HTMLElement>('[data-conversation-scroll]')
  120. if (scroller === null) throw new Error('conversation scroll container not in the DOM')
  121. // Start from the origin so the reading is this gesture's own effect.
  122. scroller.scrollLeft = 0
  123. const box = scroller.getBoundingClientRect()
  124. // Near the top of the column, clear of the centered hero card: the wheel
  125. // must reach the column, not a nested scroller the composer owns.
  126. return { x: box.left + box.width / 2, y: box.top + 60 }
  127. })
  128. await page.mouse.move(origin.x, origin.y)
  129. await page.mouse.wheel(WHEEL_DELTA, 0)
  130. // A fixed settle, then two frames. Polling for a settled value cannot be
  131. // used here — the value under test is 0, which a poll starting at 0 accepts
  132. // before the gesture has had any chance to move it — so the wait is
  133. // generous enough to cover a smooth-scroll animation on any engine the lane
  134. // runs on. The timing is identical on both sides of the mutation control
  135. // below, which is what makes a 0 reading evidence rather than a race won.
  136. await page.waitForTimeout(400)
  137. return page.evaluate(() => new Promise<number>((resolve) => {
  138. requestAnimationFrame(() => {
  139. requestAnimationFrame(() => {
  140. resolve(document.querySelector<HTMLElement>('[data-conversation-scroll]')?.scrollLeft ?? -1)
  141. })
  142. })
  143. }))
  144. }
  145. /**
  146. * Measure the positive horizontal scroll boundary without changing the
  147. * shipped overflow mode. This is distinct from `scrollWidth - clientWidth`
  148. * when a stable scrollbar gutter leaves part of the overflow on the negative
  149. * side of the scroll origin.
  150. * @param page - the page under test.
  151. * @returns the greatest positive `scrollLeft` reachable by the control gesture.
  152. */
  153. async function horizontalScrollLimit(page: Page): Promise<number> {
  154. return page.evaluate((delta) => {
  155. const scroller = document.querySelector<HTMLElement>('[data-conversation-scroll]')
  156. if (scroller === null) throw new Error('conversation scroll container not in the DOM')
  157. const previousScrollBehavior = scroller.style.scrollBehavior
  158. scroller.style.scrollBehavior = 'auto'
  159. scroller.scrollLeft = delta
  160. const limit = scroller.scrollLeft
  161. scroller.scrollLeft = 0
  162. scroller.style.scrollBehavior = previousScrollBehavior
  163. return limit
  164. }, WHEEL_DELTA)
  165. }
  166. /** A stop's readings plus where a horizontal wheel over it landed. */
  167. type ColumnStop = ColumnMetrics & {
  168. /** `scrollLeft` after one horizontal wheel: the user-facing claim, 0 at every stop. */
  169. scrollLeftAfterWheel: number
  170. }
  171. /**
  172. * Render the golden body: one line per stop, relations only.
  173. *
  174. * Absolute pixels are deliberately absent apart from `scrollLeftAfterWheel`,
  175. * which the shipped overflow mode pins to 0 by construction. The bleed is
  176. * recorded as a boolean rather than its width, so the golden survives any
  177. * platform whose column lands a pixel off — a fixture that has to be
  178. * re-recorded per platform documents the platform, not the behavior.
  179. * @param stops - the measured stops, in sweep order.
  180. * @returns the golden body, without a trailing newline.
  181. */
  182. function renderGeometry(stops: ColumnStop[]): string {
  183. return [
  184. '# Conversation column horizontal overflow',
  185. '',
  186. '| viewport | overflow-x | glow bleeds past the column | scrollLeft after a horizontal wheel | scrolls vertically |',
  187. '| --- | --- | --- | --- | --- |',
  188. ...stops.map(stop => `| ${String(stop.width)}px | ${stop.overflowX} | ${String(stop.glowBleeds)} `
  189. + `| ${String(stop.scrollLeftAfterWheel)}px | ${String(stop.scrollsVertically)} |`),
  190. ].join('\n')
  191. }
  192. describe('web e2e: the conversation column scrolls on one axis', () => {
  193. let scaffold: WebScaffold
  194. let browser: Browser
  195. let page: Page
  196. let tripwire: ReturnType<typeof watchConsole>
  197. beforeAll(async () => {
  198. scaffold = await launchWebScaffold({})
  199. browser = await chromium.launch()
  200. page = await newEnglishPage(browser, 900)
  201. tripwire = watchConsole(page)
  202. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  203. await page.waitForSelector('[data-conversation-scroll] [class*="heroGlow"]', { timeout: 30_000 })
  204. }, 180_000)
  205. afterAll(async () => {
  206. await browser?.close()
  207. await scaffold?.close()
  208. })
  209. /**
  210. * Resize to a viewport and read the column once its width stops moving.
  211. *
  212. * The glow rides the hero box, which rides the column, and the frame eases
  213. * its column tracks over `--ds-transition-duration-slow`: reading straight
  214. * after a resize can report the previous viewport's relation, or a width
  215. * caught mid-transition.
  216. * @param width - viewport width to settle at.
  217. * @returns the column's readings at that width.
  218. */
  219. const settleAt = async (width: number): Promise<ColumnMetrics> => {
  220. await page.setViewportSize({ width, height: 900 })
  221. let previous = -1
  222. await expect.poll(async () => {
  223. const current = (await measureColumn(page, width)).columnWidth
  224. const settled = current === previous
  225. previous = current
  226. return settled
  227. }, { timeout: 10_000 }).toBe(true)
  228. return measureColumn(page, width)
  229. }
  230. /**
  231. * Sweep the stops once per run and hand the SAME readings to every assertion
  232. * below, so the golden and the assertions describe one measurement instead of
  233. * two runs that could disagree. Memoized rather than re-run per test: the
  234. * gestures below move the viewport, and a second sweep would be a second
  235. * chance for a resize to settle differently.
  236. * @returns the stops in {@link WIDTHS} order.
  237. */
  238. let swept: Promise<ColumnStop[]> | undefined
  239. const sweep = (): Promise<ColumnStop[]> => {
  240. swept ??= (async () => {
  241. const stops: ColumnStop[] = []
  242. for (const width of WIDTHS) {
  243. stops.push({ ...await settleAt(width), scrollLeftAfterWheel: await wheelHorizontally(page) })
  244. }
  245. return stops
  246. })()
  247. return swept
  248. }
  249. it('never scrolls horizontally, at any width the glow bleeds past', async () => {
  250. onTestFailed(() => saveFailureShot(page, 'web-e2e-conversation-column-overflow'))
  251. const stops = await sweep()
  252. // The vacuity guard, in two halves: the glow has to reach past the column
  253. // at the narrow stops, and that reach has to still register as scrollable
  254. // overflow. Without both, the claim below holds for free.
  255. expect(stops.filter(stop => stop.glowBleeds).map(stop => stop.width)).toEqual([
  256. 1200, 1000, 800, CONTROL_VIEWPORT,
  257. ])
  258. for (const stop of stops.filter(stop => stop.glowBleeds)) {
  259. expect(stop.bleedRange, `viewport ${String(stop.width)}`).toBeGreaterThan(0)
  260. }
  261. for (const stop of stops) {
  262. expect(stop.overflowX, `viewport ${String(stop.width)}`).toBe('hidden')
  263. // The reported symptom, stated directly: a horizontal wheel over the
  264. // column moves nothing, at every stop.
  265. expect(stop.scrollLeftAfterWheel, `viewport ${String(stop.width)}`).toBe(0)
  266. // The axis the column is a scroller for must survive `overflow-x: hidden`.
  267. expect(stop.scrollsVertically, `viewport ${String(stop.width)}`).toBe(true)
  268. }
  269. expect(tripwire.pageErrors).toEqual([])
  270. }, 120_000)
  271. it('scrolls horizontally again once the axis is opened back up (control)', async () => {
  272. onTestFailed(() => saveFailureShot(page, 'web-e2e-conversation-column-overflow-control'))
  273. // The mutation control, run in the page rather than against a second
  274. // build: it lifts exactly the `overflow-x: hidden` declaration, so the
  275. // initial `visible` that a one-axis scroller computes to `auto` takes
  276. // over, and shows the same gesture, at the same timing, carrying the
  277. // column to its positive scroll boundary.
  278. // Without it a `scrollLeft` of 0 could equally mean the wheel never arrived.
  279. // Injected with an id rather than through `addStyleTag`, so the teardown
  280. // below can take the sheet out again by selector: it must not outlive this
  281. // test, or the golden ends up reading the control.
  282. await page.evaluate((id: string) => {
  283. const sheet = document.createElement('style')
  284. sheet.id = id
  285. sheet.textContent = '[data-conversation-scroll] { overflow-x: auto !important; }'
  286. document.head.append(sheet)
  287. }, CONTROL_STYLE_ID)
  288. try {
  289. // Resolve the mutated layout at the narrowest sweep stop. At wider stops,
  290. // a classic scrollbar can change the available box enough to remove the
  291. // overflow that the control is meant to expose.
  292. const before = await settleAt(CONTROL_VIEWPORT)
  293. expect(before.overflowX).toBe('auto')
  294. expect(before.bleedRange).toBeGreaterThan(0)
  295. const scrollLimit = await horizontalScrollLimit(page)
  296. // The control has a reachable horizontal range, and the gesture exceeds
  297. // it so the equality below proves that the wheel reached the far edge.
  298. expect(scrollLimit).toBeGreaterThan(0)
  299. expect(scrollLimit).toBeLessThan(WHEEL_DELTA)
  300. // Rounded: `scrollLeft` is fractional under a fractional layout while
  301. // the claim is that the column reached the positive boundary, not that
  302. // two engines agree on a sub-pixel.
  303. expect(Math.round(await wheelHorizontally(page))).toBe(Math.round(scrollLimit))
  304. } finally {
  305. await page.evaluate((id: string) => {
  306. document.getElementById(id)?.remove()
  307. }, CONTROL_STYLE_ID)
  308. }
  309. // The override is gone and the shipped state is back: the later goldens
  310. // read the product, not the control.
  311. expect((await settleAt(CONTROL_VIEWPORT)).overflowX).toBe('hidden')
  312. expect(tripwire.pageErrors).toEqual([])
  313. }, 120_000)
  314. it('matches the committed column-overflow golden', async () => {
  315. onTestFailed(() => saveFailureShot(page, 'web-e2e-conversation-column-overflow-golden'))
  316. await compareOrRefreshGolden(GEOMETRY_EXPECTED, renderGeometry(await sweep()), MODE)
  317. expect(tripwire.pageErrors).toEqual([])
  318. }, 120_000)
  319. it('commits exactly the fixtures it reads', async () => {
  320. // No model calls, so no replay log: the golden is the whole inventory.
  321. await assertFixtureInventory(SNAPSHOT_DIR, ['geometry.expected.md'])
  322. })
  323. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', () => {
  324. expect(tripwire.warnings).toEqual([])
  325. expect(tripwire.pageErrors).toEqual([])
  326. })
  327. })