test(app): keep a returning reader's place in the reconnect case

The offset-only reconnect case expected a view that returns to be revealed at
scrollTop 0. That was the old behaviour, not the contract: the case is about
the reveal, which must not wait for a native scroll or resize. A reader who
scrolled up now returns to the place they left (200, neither the start nor the
end), and the reveal records that no native scroll had arrived.

A second case covers a view that is first put in the page after it stopped
following. It holds the offset it was built with, the furthest there is, and
must start at the start of its rows: only a view that returns gets its offset
written back.
This commit is contained in:
Luke Parker 2026-11-25 11:15:53 +10:00
parent 60b3bac309
commit 995a6734e4
3 changed files with 90 additions and 24 deletions

View file

@ -133,10 +133,11 @@ This section is the single source for what may move or restyle the session timel
- A timeline that is not shown is out of the document: behind another view on a narrow screen, in a window that narrows onto the view it last showed there, or while another session is selected. Its scroll element loses its offset there and comes back at 0. Out of the document it also measures as empty: `scrollTop`, `scrollHeight` and `clientHeight` all read 0.
- The virtualizer's offset is the reader's place while the view is away. TanStack moves that offset with the row under it when an older page lands above, so it names the same rows when the view returns. `restoreOffset` in `src/session/timeline/virtualizer.tsx` writes it back to the element once the element is back (`returning` in `src/session/timeline/reconnect-machine.ts`). A view that follows goes to its end instead. Before, the element's 0 was reported to the virtualizer, and a reader who had scrolled up came back to the start of the loaded history.
- Only a view that returns has a place to go back to. A view that is put in the page for the first time holds the offset it was built with: the furthest there is when it was built following, which no reader was at. That offset is never written to the element.
- Do not report the native offset of a returned element to the virtualizer before that write. Do not save a native offset to restore later either: the same number names other rows once history has loaded above.
- Check `isConnected` before code decides anything from scroll geometry. The history probes in `src/session/timeline/interaction.ts` read the empty element as room to fill and as a reader at the top, and loaded one older page after another for as long as the view was not shown.
- A session switch differs on purpose: the follow state starts over for each session (`src/session/timeline/follow.ts`), so a session opens at its latest message, or at the message its link names.
- Keepers: "returns a reader to the rows they left …" in `e2e/regression/timeline-cache.spec.ts`, from another narrow-screen view, under an older page that landed while another view showed, and when the window narrows onto another view and widens again; "asks for no older page while the conversation is behind another narrow-screen view" in `e2e/regression/timeline-history.spec.ts`; "tells a view's first attachment from its return" in `src/session/timeline/reconnect-machine.test.ts`.
- Keepers: "returns a reader to the rows they left …" in `e2e/regression/timeline-cache.spec.ts`, from another narrow-screen view, under an older page that landed while another view showed, and when the window narrows onto another view and widens again; "asks for no older page while the conversation is behind another narrow-screen view" in `e2e/regression/timeline-history.spec.ts`; "reveals ready measured rows after an offset-only reconnect" and "reveals a view that no longer follows at its start when it is first put in the page" in `component-tests/timeline-virtualizer.spec.ts`; "tells a view's first attachment from its return" in `src/session/timeline/reconnect-machine.test.ts`.
- To check by hand: at a width under 768 px, scroll up into history, open Changes and come back. Then widen the window with Changes open, scroll up, and narrow and widen it again. The same rows must be in the same place each time.
### Scroll writes

View file

@ -11,6 +11,8 @@ export function mountTimelineVirtualizer(input: {
rowHeight: number
immediate?: boolean
viewportHeight?: number
/** Builds the view, and lets the virtualizer watch it, before it is in the page. "Attach view" puts it there. */
staged?: boolean
}) {
const host = document.createElement("main")
host.dataset.testid = "timeline-virtualizer-fixture"
@ -55,7 +57,7 @@ export function mountTimelineVirtualizer(input: {
},
onContentResize: () => {
if (content) return
content = host.querySelector<HTMLDivElement>("[data-timeline-virtual-content]")!
content = container.querySelector<HTMLDivElement>("[data-timeline-virtual-content]")!
reveal.observe(content, { attributes: true, attributeFilter: ["style"] })
},
onScheduleScrollState: (element) => {
@ -85,6 +87,8 @@ export function mountTimelineVirtualizer(input: {
pendingMarkdown: content.querySelectorAll('[data-component="markdown"]:not([data-markdown-ready])').length,
viewportHeight: viewport.clientHeight,
scrollTop: viewport.scrollTop,
// The native scrolls delivered since the view was built, or since it last left the page.
scrolls: Number(host.dataset.scrolls),
clipped: mounted.flatMap((element) =>
element.firstElementChild!.getBoundingClientRect().height > element.offsetHeight + 1
? [element.dataset.timelineKey]
@ -98,6 +102,31 @@ export function mountTimelineVirtualizer(input: {
reveal.disconnect()
})
const view = (
<div ref={container} style={{ height: `${input.viewportHeight ?? 180}px`, width: "400px" }}>
<timeline.View
header={null}
deferred={() => false}
renderRow={(row) => (
<div
data-component="markdown"
data-markdown-ready={state.ready ? "" : undefined}
style={{
height: `${input.rowHeight + (state.grown && row().userMessageID === `message-${input.count - 1}` ? 40 : 0)}px`,
}}
data-timeline-part-ids={row().userMessageID === "message-0" ? "prt_first,prt_grouped" : undefined}
>
{row().userMessageID}
</div>
)}
/>
</div>
)
// A view that waits outside the page belongs to the page's document, as one that has been shown does. The
// virtualizer watches only such an element.
if (input.staged) document.createElement("div").append(container)
return (
<div data-testid="timeline-controls" data-pinned={follow.following()} data-anchored={!!follow.anchor()}>
<button type="button" onClick={() => setState("ready", true)}>
@ -121,6 +150,13 @@ export function mountTimelineVirtualizer(input: {
>
Reconnect ready rows
</button>
<button type="button" onClick={(event) => event.currentTarget.parentElement!.append(container)}>
Attach view
</button>
{/* What navigation to a message does before it scrolls there. */}
<button type="button" onClick={() => follow.dispatch({ type: "detach" })}>
Leave the end
</button>
<button type="button" onClick={() => scrollToEnd()}>
Scroll to end
</button>
@ -136,24 +172,7 @@ export function mountTimelineVirtualizer(input: {
>
Anchor first row
</button>
<div ref={container} style={{ height: `${input.viewportHeight ?? 180}px`, width: "400px" }}>
<timeline.View
header={null}
deferred={() => false}
renderRow={(row) => (
<div
data-component="markdown"
data-markdown-ready={state.ready ? "" : undefined}
style={{
height: `${input.rowHeight + (state.grown && row().userMessageID === `message-${input.count - 1}` ? 40 : 0)}px`,
}}
data-timeline-part-ids={row().userMessageID === "message-0" ? "prt_first,prt_grouped" : undefined}
>
{row().userMessageID}
</div>
)}
/>
</div>
{input.staged ? undefined : view}
</div>
)
}

View file

@ -168,8 +168,12 @@ story("holds a revealed tool inside a group of several through a later resize",
await expect(controls).toHaveAttribute("data-anchored", "true")
})
// A session's view leaves the page and comes back: while another session is selected, or behind another view on a
// narrow screen. Its scroll element comes back at offset 0, and nothing tells the timeline so. `offset-only`: the view
// comes back at the size it had, to a reader who had scrolled up. They find the place they left, and no native scroll
// or resize has to arrive first. The start of the list, where the element comes back, is not their place.
for (const input of [
{ name: "offset-only", count: 1, rowHeight: 600, reveal: { scrollTop: 0 } },
{ name: "offset-only", count: 1, rowHeight: 600, reveal: { scrollTop: 200, scrolls: 0 } },
{ name: "zero-height", count: 4, rowHeight: 60, reveal: {} },
]) {
story(`reveals ready measured rows after an ${input.name} reconnect`, async ({ page }) => {
@ -189,8 +193,18 @@ for (const input of [
if (input.name === "offset-only") {
// Pinned to the end: the 600px row and the virtualizer's 92px tail, less the 180px viewport.
await expect(root).toHaveAttribute("data-last-scroll-top", "512")
await root.locator("[data-scrollable]").dispatchEvent("wheel", { deltaY: -1 })
// The reader scrolls up to a place that is neither the start nor the end. The wheel and the scroll it causes
// arrive together.
await root.locator("[data-scrollable]").evaluate(
(element) =>
new Promise((resolve) => {
element.addEventListener("scroll", resolve, { once: true })
element.dispatchEvent(new WheelEvent("wheel", { deltaY: -1, bubbles: true }))
element.scrollTop = 200
}),
)
await expect(root.getByTestId("timeline-controls")).toHaveAttribute("data-pinned", "false")
await expect(root).toHaveAttribute("data-last-scroll-top", "200")
}
if (input.name === "zero-height") {
@ -214,13 +228,45 @@ for (const input of [
})
if (input.name === "offset-only") {
// This repair must not depend on another native scroll or resize delivery.
await expect(root).toHaveAttribute("data-scrolls", "0")
// The return must not depend on a native scroll or resize delivery. No scroll had arrived when the view was
// revealed (`scrolls` above), and the viewport reported no size.
await expect(root).toHaveAttribute("data-viewport-resizes", resizes!)
}
})
}
story("reveals a view that no longer follows at its start when it is first put in the page", async ({ page }) => {
// A view can be built, and watched by the virtualizer, before it is in the page. One built while it follows holds
// the furthest offset there is, so that its last rows mount first: an offset no reader was at. When the view no
// longer follows by the time it is put in the page, as after navigation to a message, it starts where its scroll
// element is, at the start of its rows. Only a view that returns gets the offset it holds written back. Written
// back here, that offset shows the end.
await page.evaluate(async (fixture) => {
const { mountTimelineVirtualizer } = await import(fixture)
mountTimelineVirtualizer({ count: 100, rowHeight: 60, staged: true })
}, fixture)
const root = page.getByTestId("timeline-virtualizer-fixture")
const controls = root.getByTestId("timeline-controls")
const content = root.locator("[data-timeline-virtual-content]")
await expect(controls).toHaveAttribute("data-pinned", "true")
await root.getByRole("button", { name: "Complete Markdown", exact: true }).click()
await root.getByRole("button", { name: "Leave the end", exact: true }).click()
await expect(controls).toHaveAttribute("data-pinned", "false")
await expect(content).toHaveCount(0)
await root.getByRole("button", { name: "Attach view", exact: true }).click()
await expect(content).toHaveCSS("visibility", "visible")
await expect(root).toHaveAttribute("data-first-reveal", /.+/)
expect(await root.evaluate((element) => JSON.parse(element.dataset.firstReveal!))).toMatchObject({
rows: [0, 1, 2, 3, 4],
pendingMarkdown: 0,
clipped: [],
viewportHeight: 180,
scrollTop: 0,
scrolls: 0,
})
})
// TanStack lists the rows it last read, and rows found their row by key or else by index. A listed key the projection
// had dropped found nothing and crashed rendering with "Match/exhaustive: absurd" (#63944). A row whose key had left
// read the row that moved to its index, and a renderer handed another kind of row throws (#62800). Two rows can share