chat-scroll-contract.e2e.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // Browser geometry contracts for a long Chat transcript. These scenarios are
  2. // deliberately virtualizer-neutral: they assert semantic-row position,
  3. // bottom ownership, interaction state, and the real outer scroll host rather
  4. // than DOM cardinality or implementation-specific spacer markup.
  5. import { access, mkdtemp, rm, writeFile } from 'node:fs/promises'
  6. import { tmpdir } from 'node:os'
  7. import { join } from 'node:path'
  8. import type { Browser, Page } from 'playwright'
  9. import { chromium } from 'playwright'
  10. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  11. import type { StreamChunk } from '@deepseek-ai/dsh-llm'
  12. import { ToolCallId } from '@deepseek-ai/dsh-llm'
  13. import type { ReplayEntry, ReplayOverrideDoc } from '@deepseek-ai/dsh-llm-replay'
  14. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  15. import { createChatScrollFixture, type ChatScrollFixture } from './chat-scroll-fixture.ts'
  16. import {
  17. launchWebScaffold,
  18. seedSession,
  19. watchConsole,
  20. webSnapshotMode,
  21. type WebScaffold,
  22. } from './scaffold.ts'
  23. import { expandOwningTurnProcess, newEnglishPage, saveFailureShot } from './support.ts'
  24. const MODE = webSnapshotMode()
  25. const HISTORY_SESSION_ID = 'chat-scroll-history-e2e'
  26. const TOOL_SESSION_ID = 'chat-scroll-tool-e2e'
  27. const RESTORE_SESSION_A_ID = 'chat-scroll-restore-a-e2e'
  28. const RESTORE_SESSION_B_ID = 'chat-scroll-restore-b-e2e'
  29. const REPLAY_CONTEXT_WINDOW = 10_000_000
  30. const STREAM_PACE_MS = 24
  31. const GEOMETRY_TOLERANCE = 2
  32. const RESPONSIVE_REFLOW_TOLERANCE = 32
  33. const LIVE_TEXT_PROMPT = 'CHAT_SCROLL_LIVE_USER Continue this long conversation while I inspect older history.'
  34. const LIVE_TEXT_FIRST = 'CHAT_SCROLL_LIVE_FIRST'
  35. const LIVE_TEXT_DONE = 'CHAT_SCROLL_LIVE_DONE'
  36. const LIVE_TOOL_PROMPT = 'CHAT_SCROLL_TOOL_USER Run the requested diagnostic and then summarize it.'
  37. const LIVE_TOOL_CALL_ID = ToolCallId('chat-scroll-live-tool-call')
  38. const LIVE_TOOL_RESULT = 'CHAT_SCROLL_LIVE_TOOL_RESULT'
  39. const LIVE_TOOL_FIRST = 'CHAT_SCROLL_TOOL_STREAM_FIRST'
  40. const LIVE_TOOL_DONE = 'CHAT_SCROLL_TOOL_STREAM_DONE'
  41. const TOOL_READY_FILE = '.chat-scroll-tool-ready'
  42. const TOOL_RELEASE_FILE = '.chat-scroll-tool-release'
  43. const INPUTS_SESSION_ID = 'chat-scroll-inputs-e2e'
  44. const FLING_SESSION_ID = 'chat-scroll-fling-e2e'
  45. const LIVE_FLING_PROMPT = 'CHAT_SCROLL_FLING_USER Keep streaming while I fling back through older output.'
  46. const LIVE_FLING_FIRST = 'CHAT_SCROLL_FLING_STREAM_FIRST'
  47. const LIVE_FLING_DONE = 'CHAT_SCROLL_FLING_STREAM_DONE'
  48. const HISTORY_FIXTURE = createChatScrollFixture({
  49. markerPrefix: 'HISTORY',
  50. title: 'CHAT_SCROLL_HISTORY long paging session',
  51. })
  52. const TOOL_FIXTURE = createChatScrollFixture({
  53. markerPrefix: 'TOOL',
  54. title: 'CHAT_SCROLL_TOOL live tool session',
  55. })
  56. const RESTORE_FIXTURE_A = createChatScrollFixture({
  57. markerPrefix: 'RESTORE_A',
  58. title: 'CHAT_SCROLL_RESTORE_A long session',
  59. })
  60. const RESTORE_FIXTURE_B = createChatScrollFixture({
  61. markerPrefix: 'RESTORE_B',
  62. title: 'CHAT_SCROLL_RESTORE_B comparison session',
  63. turns: 32,
  64. })
  65. const INPUTS_FIXTURE = createChatScrollFixture({
  66. markerPrefix: 'INPUTS',
  67. title: 'CHAT_SCROLL_INPUTS non-wheel reader input session',
  68. })
  69. interface ScrollGeometry {
  70. readonly distanceFromBottom: number
  71. readonly scrollTop: number
  72. }
  73. interface FlowAnchor {
  74. readonly key: string
  75. readonly top: number
  76. }
  77. interface ScrollWorld {
  78. readonly events: SessionEvent[]
  79. readonly page: Page
  80. readonly replayDir?: string
  81. readonly scaffold: WebScaffold
  82. readonly tripwire: ReturnType<typeof watchConsole>
  83. }
  84. interface ScrollWorldOptions {
  85. readonly failureShot: string
  86. readonly replay?: ReplayOverrideDoc
  87. readonly seeds: readonly { fixture: ChatScrollFixture; id: string }[]
  88. }
  89. function textStream(first: string, done: string, deltaCount: number): StreamChunk[] {
  90. const deltas = Array.from({ length: deltaCount }, (_, index) => {
  91. if (index === 0) return `${first} `
  92. if (index === deltaCount - 1) return `${done}.`
  93. return `stream-chunk-${String(index).padStart(3, '0')} ${'incremental response '.repeat(3)}`
  94. })
  95. const response = deltas.join('')
  96. return [
  97. { type: 'block-start', index: 0, blockType: 'text' },
  98. ...deltas.map(text => ({ type: 'text-delta' as const, index: 0, text })),
  99. { type: 'block-end', index: 0, block: { type: 'text', text: response } },
  100. {
  101. type: 'usage',
  102. usage: { inputTokens: 512, outputTokens: Math.ceil(response.length / 4) },
  103. },
  104. { type: 'finish', reason: { kind: 'stop' } },
  105. ]
  106. }
  107. function toolStream(): StreamChunk[] {
  108. const command = [
  109. `: > ${TOOL_READY_FILE}`,
  110. `while [ ! -f ${TOOL_RELEASE_FILE} ]; do sleep 0.02; done`,
  111. 'line=1',
  112. `while [ "$line" -le 64 ]; do printf '${LIVE_TOOL_RESULT} line %02d\\n' "$line"; line=$((line + 1)); done`,
  113. ].join('; ')
  114. const args = JSON.stringify({ command, description: LIVE_TOOL_RESULT })
  115. return [
  116. { type: 'block-start', index: 0, blockType: 'tool-call' },
  117. {
  118. type: 'tool-call-delta',
  119. index: 0,
  120. id: LIVE_TOOL_CALL_ID,
  121. name: 'bash',
  122. argumentsDelta: args,
  123. },
  124. {
  125. type: 'block-end',
  126. index: 0,
  127. block: { type: 'tool-call', id: LIVE_TOOL_CALL_ID, name: 'bash', arguments: args },
  128. },
  129. { type: 'usage', usage: { inputTokens: 256, outputTokens: 48 } },
  130. { type: 'finish', reason: { kind: 'tool-calls' } },
  131. ]
  132. }
  133. function replayEntry(chunks: StreamChunk[]): ReplayEntry {
  134. return { kind: 'chunks', chunks }
  135. }
  136. async function launchScrollWorld(options: ScrollWorldOptions): Promise<ScrollWorld> {
  137. let replayDir: string | undefined
  138. let scaffold: WebScaffold | undefined
  139. let page: Page | undefined
  140. try {
  141. if (options.replay !== undefined) {
  142. replayDir = await mkdtemp(join(tmpdir(), 'dsh-chat-scroll-replay-'))
  143. const replayOverride = join(replayDir, 'replay.override.json')
  144. await writeFile(replayOverride, JSON.stringify(options.replay))
  145. scaffold = await launchWebScaffold({
  146. replayFixture: join(replayDir, 'override-only.jsonl'),
  147. replayOverride,
  148. paceMs: STREAM_PACE_MS,
  149. replayContextWindow: REPLAY_CONTEXT_WINDOW,
  150. })
  151. } else {
  152. scaffold = await launchWebScaffold({})
  153. }
  154. for (const seed of options.seeds) await seedSession(scaffold, seed.fixture.log, seed.id)
  155. const events: SessionEvent[] = []
  156. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { events.push(event) })
  157. page = await newEnglishPage(browser, 900)
  158. const tripwire = watchConsole(page)
  159. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  160. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  161. // Session-list bootstrap can replace the controlled search state. Wait
  162. // for the seeded baseline before openSeed starts the lazy content query
  163. // (the compact layout dropped group session counts; the Ungrouped bucket
  164. // row is the barrier).
  165. await page.getByText('Ungrouped', { exact: true }).waitFor({ timeout: 30_000 })
  166. return {
  167. events,
  168. page,
  169. scaffold,
  170. tripwire,
  171. ...(replayDir === undefined ? {} : { replayDir }),
  172. }
  173. } catch (error) {
  174. const failures: unknown[] = [error]
  175. if (page !== undefined) await page.context().close().catch((cleanupError: unknown) => failures.push(cleanupError))
  176. if (scaffold !== undefined) await scaffold.close().catch((cleanupError: unknown) => failures.push(cleanupError))
  177. if (replayDir !== undefined) {
  178. await rm(replayDir, { recursive: true, force: true }).catch((cleanupError: unknown) => failures.push(cleanupError))
  179. }
  180. if (failures.length === 1) throw error
  181. throw new AggregateError(failures, 'chat-scroll browser world setup failed and cleanup was incomplete')
  182. }
  183. }
  184. async function closeScrollWorld(world: ScrollWorld): Promise<void> {
  185. const failures: unknown[] = []
  186. // newEnglishPage/browser.newPage owns an isolated context. Close the whole
  187. // context so its SSE connection and cache cannot leak into the next world
  188. // in this file's shared Chromium process.
  189. await world.page.context().close().catch((error: unknown) => failures.push(error))
  190. await world.scaffold.close().catch((error: unknown) => failures.push(error))
  191. if (world.replayDir !== undefined) {
  192. await rm(world.replayDir, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  193. }
  194. if (failures.length === 1) throw failures[0]
  195. if (failures.length > 1) throw new AggregateError(failures, 'chat-scroll browser world cleanup failed')
  196. }
  197. async function withScrollWorld(
  198. options: ScrollWorldOptions,
  199. run: (world: ScrollWorld) => Promise<void>,
  200. ): Promise<void> {
  201. const world = await launchScrollWorld(options)
  202. let runFailure: unknown
  203. try {
  204. await run(world)
  205. } catch (error) {
  206. runFailure = error
  207. try {
  208. await saveFailureShot(world.page, options.failureShot)
  209. } catch {
  210. // Best-effort evidence must never prevent cleanup of the owned world.
  211. }
  212. }
  213. let cleanupFailure: unknown
  214. try {
  215. await closeScrollWorld(world)
  216. } catch (error) {
  217. cleanupFailure = error
  218. }
  219. if (runFailure !== undefined && cleanupFailure !== undefined) {
  220. throw new AggregateError([runFailure, cleanupFailure], 'chat-scroll scenario and cleanup both failed')
  221. }
  222. if (runFailure !== undefined) throw runFailure
  223. if (cleanupFailure !== undefined) throw cleanupFailure
  224. }
  225. async function nextPaint(page: Page): Promise<void> {
  226. await page.evaluate(async () => {
  227. await document.fonts.ready
  228. await new Promise<void>(resolve => requestAnimationFrame(() => {
  229. requestAnimationFrame(() => { resolve() })
  230. }))
  231. })
  232. }
  233. function scrollGeometry(page: Page): Promise<ScrollGeometry> {
  234. return page.locator('[data-conversation-scroll]').evaluate(host => ({
  235. distanceFromBottom: host.scrollHeight - host.clientHeight - host.scrollTop,
  236. scrollTop: host.scrollTop,
  237. }))
  238. }
  239. /**
  240. * Rendered transcript rows in the loaded window. The stats strip cannot serve
  241. * as this probe: its turn/step counts ride the whole-log sessionStats
  242. * projection and stay fixed across paging by design, while the row count is
  243. * exactly what grows when an older page prepends or a live turn streams in.
  244. * @param page - the scenario page.
  245. * @returns the number of mounted chat flow rows.
  246. */
  247. async function loadedFlowRows(page: Page): Promise<number> {
  248. return page.locator('[data-chat-flow-key]').count()
  249. }
  250. async function openSeed(page: Page, fixture: ChatScrollFixture, tailMarker?: string): Promise<void> {
  251. // Search collapsed into a header action; expand it before filling.
  252. const searchButton = page.getByRole('button', { name: 'Search sessions' })
  253. if (await searchButton.getAttribute('aria-expanded') !== 'true') await searchButton.click()
  254. const search = page.getByRole('textbox', { name: 'Search sessions...', exact: true })
  255. // Cold summaries initially show the temporary workspace basename, so the
  256. // persisted first-prompt marker is the stable user-facing identity. The
  257. // query itself triggers lazy content-index reconciliation; no transient
  258. // empty-state paint is used as a barrier.
  259. await search.fill(fixture.markers.user(1))
  260. const results = page.getByRole('tree', { name: 'Search results' }).getByRole('treeitem')
  261. await expect.poll(() => results.count(), { timeout: 60_000 }).toBe(1)
  262. await results.click()
  263. await page.getByRole('tab', { name: 'Chat', exact: true }).waitFor({ timeout: 30_000 })
  264. if (tailMarker !== undefined) {
  265. await page.getByText(tailMarker, { exact: false }).last().waitFor({ timeout: 30_000 })
  266. }
  267. await nextPaint(page)
  268. }
  269. async function wheelTranscript(page: Page, deltaY: number): Promise<void> {
  270. const box = await page.locator('[data-conversation-scroll]').boundingBox()
  271. if (box === null) throw new Error('conversation scrollport has no layout box')
  272. await page.mouse.move(box.x + box.width / 2, box.y + Math.min(140, box.height / 3))
  273. await page.mouse.wheel(0, deltaY)
  274. await nextPaint(page)
  275. }
  276. /**
  277. * Touch-style momentum fling over the transcript. Headless Chromium in the
  278. * test lane cannot synthesize device scrolling (Input.synthesizeScrollGesture
  279. * and Input.dispatchTouchEvent both deliver DOM events without moving any
  280. * scroller, and compositor scrollbars ignore synthetic mouse input), so the
  281. * fling replays the signature a real pan leaves on the scrollport: per-frame
  282. * decaying displacements the component never authored, carrying no wheel
  283. * events. Wheel-sign semantics: positive deltaY reads downward.
  284. */
  285. async function flingTranscript(page: Page, deltaY: number): Promise<void> {
  286. await page.locator('[data-conversation-scroll]').evaluate(async (host, delta) => {
  287. const direction = Math.sign(delta)
  288. let remaining = Math.abs(delta)
  289. // Fast launch decaying toward a floor speed, like a released finger. The
  290. // floor stays above the follow threshold so contended frames (streaming
  291. // writes racing the fling) still deviate far enough to read as input.
  292. let velocity = Math.max(120, remaining / 8)
  293. while (remaining > 0) {
  294. const step = Math.min(velocity, remaining)
  295. host.scrollTop += direction * step
  296. remaining -= step
  297. velocity = Math.max(48, velocity * 0.9)
  298. await new Promise<void>(resolve => requestAnimationFrame(() => { resolve() }))
  299. }
  300. }, deltaY)
  301. await nextPaint(page)
  302. }
  303. async function wheelToHistoryStart(page: Page): Promise<void> {
  304. for (let attempt = 0; attempt < 12; attempt += 1) {
  305. if ((await scrollGeometry(page)).scrollTop <= 1) break
  306. await wheelTranscript(page, -2_400)
  307. }
  308. await expect.poll(async () => (await scrollGeometry(page)).scrollTop, { timeout: 10_000 })
  309. .toBeLessThanOrEqual(1)
  310. }
  311. async function wheelUntilMounted(page: Page, selector: string, deltaY: number): Promise<void> {
  312. for (let attempt = 0; attempt < 16; attempt += 1) {
  313. if (await page.locator(selector).count() > 0) return
  314. await wheelTranscript(page, deltaY)
  315. }
  316. throw new Error(`selector did not mount during transcript wheel: ${selector}`)
  317. }
  318. async function wheelUntilVisible(page: Page, selector: string, deltaY: number): Promise<void> {
  319. const target = page.locator(selector)
  320. for (let attempt = 0; attempt < 32; attempt += 1) {
  321. if (await target.count() > 0 && await target.evaluate((row) => {
  322. const host = row.closest<HTMLElement>('[data-conversation-scroll]')
  323. if (host === null) return false
  324. const viewport = host.getBoundingClientRect()
  325. const composer = host.querySelector<HTMLElement>('[data-composer-seat]')
  326. const visibleBottom = composer?.getBoundingClientRect().top ?? viewport.bottom
  327. const rect = row.getBoundingClientRect()
  328. return rect.bottom > viewport.top && rect.top < visibleBottom
  329. })) return
  330. await wheelTranscript(page, deltaY)
  331. }
  332. throw new Error(`selector did not become visible during transcript wheel: ${selector}`)
  333. }
  334. function visibleFlowAnchor(page: Page): Promise<FlowAnchor> {
  335. return page.locator('[data-conversation-scroll]').evaluate((host) => {
  336. const rows = [...host.querySelectorAll<HTMLElement>('[data-chat-anchor-key]:not([hidden])')]
  337. const viewport = host.getBoundingClientRect()
  338. const composer = host.querySelector<HTMLElement>('[data-composer-seat]')
  339. const visibleBottom = composer?.getBoundingClientRect().top ?? viewport.bottom
  340. const visible = rows.filter((candidate) => {
  341. const rect = candidate.getBoundingClientRect()
  342. return rect.bottom > viewport.top && rect.top < visibleBottom
  343. })
  344. const row = visible[0]
  345. if (row?.dataset.chatAnchorKey === undefined) {
  346. throw new Error(`no visible settled Chat row: ${JSON.stringify({
  347. composerTop: visibleBottom,
  348. host: { bottom: viewport.bottom, top: viewport.top },
  349. rows: rows.slice(0, 4).map(candidate => ({
  350. callId: candidate.dataset.chatCallId,
  351. key: candidate.dataset.chatAnchorKey,
  352. rect: {
  353. bottom: candidate.getBoundingClientRect().bottom,
  354. top: candidate.getBoundingClientRect().top,
  355. },
  356. })),
  357. totalRows: rows.length,
  358. })}`)
  359. }
  360. return {
  361. key: row.dataset.chatAnchorKey,
  362. top: row.getBoundingClientRect().top - viewport.top,
  363. }
  364. })
  365. }
  366. function flowTop(page: Page, key: string): Promise<number> {
  367. return page.locator('[data-chat-anchor-key]').evaluateAll((rows, anchorKey) => {
  368. const row = rows.find(candidate => (candidate as HTMLElement).dataset.chatAnchorKey === anchorKey)
  369. if (!(row instanceof HTMLElement)) throw new Error(`stable Chat anchor ${anchorKey} is not mounted`)
  370. const host = row.closest('[data-conversation-scroll]')
  371. if (!(host instanceof HTMLElement)) throw new Error('flow row has no conversation scrollport')
  372. return row.getBoundingClientRect().top - host.getBoundingClientRect().top
  373. }, key)
  374. }
  375. async function expectSameFlowTop(
  376. page: Page,
  377. anchor: FlowAnchor,
  378. tolerance = GEOMETRY_TOLERANCE,
  379. ): Promise<void> {
  380. await expect.poll(async () => Math.abs((await flowTop(page, anchor.key)) - anchor.top), {
  381. timeout: 10_000,
  382. message: `flow row ${anchor.key} moved relative to the transcript viewport`,
  383. }).toBeLessThanOrEqual(tolerance)
  384. }
  385. async function expectBottom(page: Page): Promise<void> {
  386. await expect.poll(async () => Math.abs((await scrollGeometry(page)).distanceFromBottom), {
  387. timeout: 10_000,
  388. }).toBeLessThanOrEqual(1)
  389. }
  390. async function expectMarkerAboveComposer(page: Page, marker: string): Promise<void> {
  391. const geometry = await page.getByText(marker, { exact: false }).last().evaluate((node) => {
  392. const row = node.closest('[data-chat-flow-key], [data-streaming]')
  393. const composer = node.closest('[data-conversation-scroll]')?.querySelector('[data-composer-seat]')
  394. if (!(row instanceof HTMLElement) || !(composer instanceof HTMLElement)) {
  395. throw new Error('latest marker or composer geometry is unavailable')
  396. }
  397. return {
  398. composerTop: composer.getBoundingClientRect().top,
  399. rowBottom: row.getBoundingClientRect().bottom,
  400. }
  401. })
  402. expect(geometry.rowBottom).toBeLessThanOrEqual(geometry.composerTop + GEOMETRY_TOLERANCE)
  403. }
  404. async function loadEarlierWithAnchor(page: Page): Promise<void> {
  405. await wheelToHistoryStart(page)
  406. const older = page.getByRole('button', { name: 'Load earlier', exact: true })
  407. const loading = page.getByRole('button', { name: 'Loading…', exact: true })
  408. await older.waitFor({ timeout: 10_000 })
  409. const anchor = await visibleFlowAnchor(page)
  410. const before = await loadedFlowRows(page)
  411. await older.click()
  412. await expect.poll(async () => (
  413. await loadedFlowRows(page) > before && await loading.count() === 0
  414. ), { timeout: 30_000 }).toBe(true)
  415. await nextPaint(page)
  416. if (await page.getByRole('button', { name: 'Load earlier', exact: true }).count() === 0) {
  417. expect(await page.locator('[data-turn-process][aria-expanded="false"]').count()).toBeGreaterThan(0)
  418. return
  419. }
  420. await expectSameFlowTop(page, anchor)
  421. }
  422. async function fileExists(path: string): Promise<boolean> {
  423. try {
  424. await access(path)
  425. return true
  426. } catch {
  427. return false
  428. }
  429. }
  430. function eventCarries(event: SessionEvent, marker: string): boolean {
  431. return JSON.stringify(event).includes(marker)
  432. }
  433. function assertClean(world: ScrollWorld): void {
  434. expect(world.tripwire.pageErrors).toEqual([])
  435. expect(world.tripwire.warnings).toEqual([])
  436. }
  437. let browser: Browser
  438. describe('web e2e: long Chat scroll contract', () => {
  439. beforeAll(async () => {
  440. browser = await chromium.launch()
  441. })
  442. afterAll(async () => {
  443. await browser?.close()
  444. })
  445. it.skipIf(MODE === 'record')('preserves the reader anchor when history and streaming arrive concurrently', async () => {
  446. await withScrollWorld({
  447. failureShot: 'web-e2e-chat-scroll-history-stream',
  448. replay: [replayEntry(textStream(LIVE_TEXT_FIRST, LIVE_TEXT_DONE, 120))],
  449. seeds: [{ fixture: HISTORY_FIXTURE, id: HISTORY_SESSION_ID }],
  450. }, async (world) => {
  451. await openSeed(
  452. world.page,
  453. HISTORY_FIXTURE,
  454. HISTORY_FIXTURE.markers.assistant(HISTORY_FIXTURE.turns),
  455. )
  456. await expectBottom(world.page)
  457. let releaseHistory = (): void => {}
  458. let held = false
  459. let releaseGate: (() => void) | undefined
  460. const gate = new Promise<void>((resolve) => { releaseGate = resolve })
  461. releaseHistory = () => { releaseGate?.() }
  462. await world.page.route('**/api/session/page', async (route) => {
  463. const request = route.request().postDataJSON() as {
  464. method?: string
  465. payload?: { args?: { request?: { beforeSeq?: number } } }
  466. }
  467. if (!held && request.method === 'session/page'
  468. && request.payload?.args?.request?.beforeSeq !== undefined) {
  469. held = true
  470. await gate
  471. }
  472. await route.continue()
  473. })
  474. const settled = world.scaffold.whenTurnSettled(60_000)
  475. try {
  476. const composer = world.page.locator('[data-composer-input][contenteditable="true"]').last()
  477. await composer.fill(LIVE_TEXT_PROMPT)
  478. await world.page.getByRole('button', { name: 'Send message', exact: true }).click()
  479. await world.page.getByText(LIVE_TEXT_FIRST, { exact: false }).last().waitFor({ timeout: 15_000 })
  480. await wheelToHistoryStart(world.page)
  481. const beforeRows = await loadedFlowRows(world.page)
  482. await world.page.getByRole('button', { name: 'Load earlier', exact: true }).click()
  483. await expect.poll(() => held, { timeout: 10_000 }).toBe(true)
  484. await wheelTranscript(world.page, 420)
  485. const readerAnchor = await visibleFlowAnchor(world.page)
  486. const chunksAfterAnchor = world.events.filter(event => event.type === 'assistant/chunk').length
  487. await expect.poll(
  488. () => world.events.filter(event => event.type === 'assistant/chunk').length,
  489. { timeout: 10_000 },
  490. ).toBeGreaterThan(chunksAfterAnchor + 5)
  491. releaseHistory()
  492. await expect.poll(() => loadedFlowRows(world.page), { timeout: 30_000 }).toBeGreaterThan(beforeRows)
  493. await nextPaint(world.page)
  494. await expectSameFlowTop(world.page, readerAnchor)
  495. } finally {
  496. releaseHistory()
  497. }
  498. await settled
  499. await expect.poll(() => world.page.locator('[data-streaming="true"]').count(), { timeout: 15_000 }).toBe(0)
  500. await world.page.getByText(LIVE_TEXT_DONE, { exact: false }).last().waitFor({ timeout: 15_000 })
  501. await world.page.unroute('**/api/session/page')
  502. let additionalPages = 0
  503. while (additionalPages < 8) {
  504. await wheelToHistoryStart(world.page)
  505. if (await world.page.getByRole('button', { name: 'Load earlier', exact: true }).count() === 0) break
  506. await loadEarlierWithAnchor(world.page)
  507. additionalPages += 1
  508. }
  509. expect(additionalPages).toBeGreaterThan(0)
  510. // The whole log is loaded: turn 1's unique marker renders in the
  511. // transcript (scoped: the sidebar search row also carries it) and no
  512. // page remains.
  513. expect(await world.page.locator('[data-conversation-scroll]')
  514. .getByText(HISTORY_FIXTURE.markers.user(1), { exact: false }).count()).toBe(1)
  515. expect(await world.page.getByRole('button', { name: 'Load earlier', exact: true }).count()).toBe(0)
  516. assertClean(world)
  517. })
  518. }, 180_000)
  519. it.skipIf(MODE === 'record')('keeps streaming ownership and tool disclosure state across a long scroll-away cycle', async () => {
  520. await withScrollWorld({
  521. failureShot: 'web-e2e-chat-scroll-live-tool',
  522. replay: [
  523. replayEntry(toolStream()),
  524. replayEntry(textStream(LIVE_TOOL_FIRST, LIVE_TOOL_DONE, 84)),
  525. ],
  526. seeds: [{ fixture: TOOL_FIXTURE, id: TOOL_SESSION_ID }],
  527. }, async (world) => {
  528. const readyPath = join(world.scaffold.workspaceCwd, TOOL_READY_FILE)
  529. const releasePath = join(world.scaffold.workspaceCwd, TOOL_RELEASE_FILE)
  530. await openSeed(world.page, TOOL_FIXTURE, TOOL_FIXTURE.markers.assistant(TOOL_FIXTURE.turns))
  531. const settled = world.scaffold.whenTurnSettled(60_000)
  532. let released = false
  533. try {
  534. const composer = world.page.locator('[data-composer-input][contenteditable="true"]').last()
  535. await composer.fill(LIVE_TOOL_PROMPT)
  536. await world.page.getByRole('button', { name: 'Send message', exact: true }).click()
  537. await expect.poll(() => fileExists(readyPath), { timeout: 15_000 }).toBe(true)
  538. const liveRow = world.page.locator(`[data-chat-call-id="${LIVE_TOOL_CALL_ID}"] [data-sample="bash"]`)
  539. await liveRow.waitFor({ timeout: 15_000 })
  540. expect(await liveRow.getAttribute('data-state')).toBe('running')
  541. await expectBottom(world.page)
  542. await wheelTranscript(world.page, -1_200)
  543. await world.page.getByRole('button', { name: 'Back to bottom', exact: true }).waitFor({ timeout: 10_000 })
  544. const awayAnchor = await visibleFlowAnchor(world.page)
  545. const chunksBeforeRelease = world.events.filter(event => event.type === 'assistant/chunk').length
  546. await writeFile(releasePath, 'release\n')
  547. released = true
  548. await expect.poll(
  549. () => world.events.some(event => event.type === 'tool/result'),
  550. { timeout: 15_000 },
  551. ).toBe(true)
  552. await expect.poll(
  553. () => world.events.some(event => eventCarries(event, LIVE_TOOL_FIRST)),
  554. { timeout: 15_000 },
  555. ).toBe(true)
  556. await expect.poll(
  557. () => world.events.filter(event => event.type === 'assistant/chunk').length,
  558. { timeout: 15_000 },
  559. ).toBeGreaterThan(chunksBeforeRelease + 5)
  560. await expectSameFlowTop(world.page, awayAnchor)
  561. const chunksAtRepin = world.events.filter(event => event.type === 'assistant/chunk').length
  562. await world.page.getByRole('button', { name: 'Back to bottom', exact: true }).click()
  563. await expectBottom(world.page)
  564. await expect.poll(
  565. () => world.events.filter(event => event.type === 'assistant/chunk').length,
  566. { timeout: 15_000 },
  567. ).toBeGreaterThan(chunksAtRepin + 5)
  568. await expectBottom(world.page)
  569. } finally {
  570. if (!released) await writeFile(releasePath, 'release\n').catch(() => {})
  571. }
  572. await settled
  573. await expect.poll(() => world.page.locator('[data-streaming="true"]').count(), { timeout: 15_000 }).toBe(0)
  574. await world.page.getByText(LIVE_TOOL_DONE, { exact: false }).last().waitFor({ timeout: 15_000 })
  575. await expectBottom(world.page)
  576. await expectMarkerAboveComposer(world.page, LIVE_TOOL_DONE)
  577. const liveRowSelector = `[data-chat-call-id="${LIVE_TOOL_CALL_ID}"] [data-sample="bash"]`
  578. const liveRow = world.page.locator(liveRowSelector)
  579. await expandOwningTurnProcess(world.page, liveRow)
  580. await wheelUntilVisible(world.page, liveRowSelector, -300)
  581. const toolAnchor = await liveRow.evaluate((row) => {
  582. const flow = row.closest<HTMLElement>('[data-chat-anchor-key]')
  583. const host = row.closest<HTMLElement>('[data-conversation-scroll]')
  584. if (flow?.dataset.chatAnchorKey === undefined || host === null) {
  585. throw new Error('live tool row has no settled flow identity')
  586. }
  587. return {
  588. key: flow.dataset.chatAnchorKey,
  589. top: flow.getBoundingClientRect().top - host.getBoundingClientRect().top,
  590. }
  591. })
  592. await liveRow.click()
  593. await expect.poll(() => liveRow.getAttribute('aria-expanded'), { timeout: 10_000 }).toBe('true')
  594. await expectSameFlowTop(world.page, toolAnchor)
  595. await wheelToHistoryStart(world.page)
  596. await world.page.getByRole('button', { name: 'Back to bottom', exact: true }).click()
  597. await expectBottom(world.page)
  598. await wheelUntilMounted(world.page, liveRowSelector, -1_100)
  599. const restoredRow = world.page.locator(liveRowSelector)
  600. await restoredRow.waitFor({ timeout: 10_000 })
  601. expect(await restoredRow.getAttribute('aria-expanded')).toBe('true')
  602. expect(await world.page.getByText(LIVE_TOOL_RESULT, { exact: false }).count()).toBeGreaterThan(0)
  603. assertClean(world)
  604. })
  605. }, 180_000)
  606. it.skipIf(MODE === 'record')('restores tab/session position and keeps composer resizing on the correct scroll owner', async () => {
  607. await withScrollWorld({
  608. failureShot: 'web-e2e-chat-scroll-restore-composer',
  609. seeds: [
  610. { fixture: RESTORE_FIXTURE_A, id: RESTORE_SESSION_A_ID },
  611. { fixture: RESTORE_FIXTURE_B, id: RESTORE_SESSION_B_ID },
  612. ],
  613. }, async (world) => {
  614. await openSeed(
  615. world.page,
  616. RESTORE_FIXTURE_A,
  617. RESTORE_FIXTURE_A.markers.assistant(RESTORE_FIXTURE_A.turns),
  618. )
  619. await loadEarlierWithAnchor(world.page)
  620. await loadEarlierWithAnchor(world.page)
  621. await wheelToHistoryStart(world.page)
  622. await wheelTranscript(world.page, 1_300)
  623. const sessionAnchor = await visibleFlowAnchor(world.page)
  624. await world.page.getByRole('tab', { name: 'Trajectory', exact: true }).click()
  625. await world.page.getByLabel('Trajectory timeline').waitFor({ timeout: 30_000 })
  626. await world.page.setViewportSize({ width: 700, height: 900 })
  627. // The narrow breakpoint auto-collapses the sidebar. Re-open it because
  628. // this scenario switches sessions while pinning the narrow Chat scroll owner.
  629. await world.page.getByRole('button', { name: 'Open sidebar', exact: true }).click()
  630. await world.page.getByRole('tab', { name: 'Chat', exact: true }).click()
  631. await nextPaint(world.page)
  632. await expectSameFlowTop(world.page, sessionAnchor, RESPONSIVE_REFLOW_TOLERANCE)
  633. const narrowSessionAnchor = await visibleFlowAnchor(world.page)
  634. await openSeed(
  635. world.page,
  636. RESTORE_FIXTURE_B,
  637. RESTORE_FIXTURE_B.markers.assistant(RESTORE_FIXTURE_B.turns),
  638. )
  639. await openSeed(
  640. world.page,
  641. RESTORE_FIXTURE_A,
  642. )
  643. await expectSameFlowTop(world.page, narrowSessionAnchor)
  644. const backToBottom = world.page.getByRole('button', { name: 'Back to bottom', exact: true })
  645. await backToBottom.evaluate((button) => {
  646. if (!(button instanceof HTMLElement)) throw new Error('Back-to-bottom control is not an HTML element')
  647. button.click()
  648. const trajectory = [...document.querySelectorAll<HTMLElement>('[role="tab"]')]
  649. .find(tab => tab.textContent?.trim() === 'Trajectory')
  650. if (!(trajectory instanceof HTMLElement)) {
  651. throw new Error('Trajectory tab is unavailable during pinned remount')
  652. }
  653. trajectory.click()
  654. })
  655. await world.page.getByLabel('Trajectory timeline').waitFor({ timeout: 30_000 })
  656. await world.page.getByRole('tab', { name: 'Chat', exact: true }).click()
  657. await expectBottom(world.page)
  658. await openSeed(
  659. world.page,
  660. RESTORE_FIXTURE_B,
  661. RESTORE_FIXTURE_B.markers.assistant(RESTORE_FIXTURE_B.turns),
  662. )
  663. await openSeed(
  664. world.page,
  665. RESTORE_FIXTURE_A,
  666. RESTORE_FIXTURE_A.markers.assistant(RESTORE_FIXTURE_A.turns),
  667. )
  668. await expectBottom(world.page)
  669. const composer = world.page.locator('[data-composer-input][contenteditable="true"]').last()
  670. const longDraft = Array.from(
  671. { length: 18 },
  672. (_, index) => `composer resize line ${String(index + 1).padStart(2, '0')}`,
  673. ).join('\n')
  674. await composer.fill(longDraft)
  675. await nextPaint(world.page)
  676. await expectBottom(world.page)
  677. await expectMarkerAboveComposer(
  678. world.page,
  679. RESTORE_FIXTURE_A.markers.assistant(RESTORE_FIXTURE_A.turns),
  680. )
  681. await composer.fill('short draft')
  682. await nextPaint(world.page)
  683. await wheelTranscript(world.page, -900)
  684. const resizeAnchor = await visibleFlowAnchor(world.page)
  685. await composer.fill(longDraft)
  686. await nextPaint(world.page)
  687. await expectSameFlowTop(world.page, resizeAnchor)
  688. await composer.fill('short draft')
  689. await nextPaint(world.page)
  690. await expectSameFlowTop(world.page, resizeAnchor)
  691. const beforeChain = await scrollGeometry(world.page)
  692. await composer.hover()
  693. await world.page.mouse.wheel(0, -320)
  694. await expect.poll(async () => (await scrollGeometry(world.page)).scrollTop, { timeout: 10_000 })
  695. .toBeLessThan(beforeChain.scrollTop)
  696. assertClean(world)
  697. })
  698. }, 180_000)
  699. // Keyboard is the only non-wheel device this lane's Chromium can drive for
  700. // real (see flingTranscript for the probe results on touch and scrollbars),
  701. // so it stands in for the whole hardware input pipeline here.
  702. it.skipIf(MODE === 'record')('keyboard paging owns bottom-follow without wheel input', async () => {
  703. await withScrollWorld({
  704. failureShot: 'web-e2e-chat-scroll-keyboard',
  705. seeds: [{ fixture: INPUTS_FIXTURE, id: INPUTS_SESSION_ID }],
  706. }, async (world) => {
  707. await openSeed(
  708. world.page,
  709. INPUTS_FIXTURE,
  710. INPUTS_FIXTURE.markers.assistant(INPUTS_FIXTURE.turns),
  711. )
  712. await expectBottom(world.page)
  713. const backToBottom = world.page.getByRole('button', { name: 'Back to bottom', exact: true })
  714. // Focus rides the last seeded tool row (a tabbable button whose keydown
  715. // handler passes scrolling keys through). End first normalizes the
  716. // focus-driven scrollIntoView back to the floor.
  717. const lastToolRow = world.page.locator(
  718. `[data-chat-call-id="chat-scroll-${String(INPUTS_FIXTURE.turns).padStart(3, '0')}-1"] [data-sample="bash"]`,
  719. )
  720. await expandOwningTurnProcess(world.page, lastToolRow)
  721. await lastToolRow.focus()
  722. await world.page.keyboard.press('End')
  723. await expectBottom(world.page)
  724. await expect.poll(() => backToBottom.count(), { timeout: 10_000 }).toBe(0)
  725. for (let press = 0; press < 3; press += 1) {
  726. await world.page.keyboard.press('PageUp')
  727. await nextPaint(world.page)
  728. }
  729. await backToBottom.waitFor({ timeout: 10_000 })
  730. await expect.poll(async () => (await scrollGeometry(world.page)).distanceFromBottom, { timeout: 10_000 })
  731. .toBeGreaterThan(100)
  732. await world.page.keyboard.press('End')
  733. await expectBottom(world.page)
  734. await expect.poll(() => backToBottom.count(), { timeout: 10_000 }).toBe(0)
  735. assertClean(world)
  736. })
  737. }, 180_000)
  738. it.skipIf(MODE === 'record')('touch-style fling scrolling owns streaming bottom-follow without wheel input', async () => {
  739. await withScrollWorld({
  740. failureShot: 'web-e2e-chat-scroll-fling-stream',
  741. replay: [
  742. replayEntry(toolStream()),
  743. replayEntry(textStream(LIVE_FLING_FIRST, LIVE_FLING_DONE, 240)),
  744. ],
  745. seeds: [{ fixture: INPUTS_FIXTURE, id: FLING_SESSION_ID }],
  746. }, async (world) => {
  747. const readyPath = join(world.scaffold.workspaceCwd, TOOL_READY_FILE)
  748. const releasePath = join(world.scaffold.workspaceCwd, TOOL_RELEASE_FILE)
  749. await openSeed(world.page, INPUTS_FIXTURE, INPUTS_FIXTURE.markers.assistant(INPUTS_FIXTURE.turns))
  750. const backToBottom = world.page.getByRole('button', { name: 'Back to bottom', exact: true })
  751. const settled = world.scaffold.whenTurnSettled(60_000)
  752. let released = false
  753. try {
  754. const composer = world.page.locator('[data-composer-input][contenteditable="true"]').last()
  755. await composer.fill(LIVE_FLING_PROMPT)
  756. await world.page.getByRole('button', { name: 'Send message', exact: true }).click()
  757. await expect.poll(() => fileExists(readyPath), { timeout: 15_000 }).toBe(true)
  758. await expectBottom(world.page)
  759. // Fling away while the turn is mid-flight: the scroll burst alone must
  760. // release bottom ownership, exactly like a wheel scroll would, even
  761. // while streaming keeps re-asserting the floor between frames.
  762. await flingTranscript(world.page, -900)
  763. await backToBottom.waitFor({ timeout: 10_000 })
  764. const awayAnchor = await visibleFlowAnchor(world.page)
  765. const chunksBeforeRelease = world.events.filter(event => event.type === 'assistant/chunk').length
  766. await writeFile(releasePath, 'release\n')
  767. released = true
  768. await expect.poll(
  769. () => world.events.some(event => event.type === 'tool/result'),
  770. { timeout: 15_000 },
  771. ).toBe(true)
  772. await expect.poll(
  773. () => world.events.filter(event => event.type === 'assistant/chunk').length,
  774. { timeout: 15_000 },
  775. ).toBeGreaterThan(chunksBeforeRelease + 5)
  776. await expectSameFlowTop(world.page, awayAnchor)
  777. // Fling back to the floor: re-pin must come from the reader's scroll
  778. // itself, and follow must then own the still-streaming tail. The
  779. // retry loop chases the floor that streaming keeps pushing down.
  780. for (let attempt = 0; attempt < 8; attempt += 1) {
  781. if ((await scrollGeometry(world.page)).distanceFromBottom <= 1) break
  782. await flingTranscript(world.page, 1_600)
  783. }
  784. await expectBottom(world.page)
  785. await expect.poll(() => backToBottom.count(), { timeout: 10_000 }).toBe(0)
  786. const chunksAtRepin = world.events.filter(event => event.type === 'assistant/chunk').length
  787. await expect.poll(
  788. () => world.events.filter(event => event.type === 'assistant/chunk').length,
  789. { timeout: 15_000 },
  790. ).toBeGreaterThan(chunksAtRepin + 5)
  791. await expectBottom(world.page)
  792. } finally {
  793. if (!released) await writeFile(releasePath, 'release\n').catch(() => {})
  794. }
  795. await settled
  796. await expect.poll(() => world.page.locator('[data-streaming="true"]').count(), { timeout: 15_000 }).toBe(0)
  797. await world.page.getByText(LIVE_FLING_DONE, { exact: false }).last().waitFor({ timeout: 15_000 })
  798. await expectBottom(world.page)
  799. assertClean(world)
  800. })
  801. }, 180_000)
  802. })