Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(web): verify reaction growth during active backward scrolling (#1140)

Gustavo Yed9c56d06b6c2
diff --git a/src/web/src/test/e2e-ui/55-message-scroll-characterization.spec.ts b/src/web/src/test/e2e-ui/55-message-scroll-characterization.spec.ts
index ef70cf00b..89d9e5994 100644
--- a/src/web/src/test/e2e-ui/55-message-scroll-characterization.spec.ts
+++ b/src/web/src/test/e2e-ui/55-message-scroll-characterization.spec.ts
@@ -24,6 +24,7 @@ import {
type ScrollTraceResult,
} from "./_fixtures/scroll-trace"
import { tid } from "./_fixtures/testids"
+import { cancelControlledBackwardGrowth, controlledBackwardGrowth } from "./_fixtures/backward-growth"
import { WEB_URL } from "./_setup/paths"
import { expectUniqueSsrCommunityFrame, expectPublicCommunityDocument } from "./_fixtures/community-ssr-frame"
@@ -630,7 +631,7 @@ test.describe("message scroll characterization", () => {
await installScrollTraceInCurrentDocument(page)
await startScrollTrace(page, { scenario: "measured-reactions-backward-growth", identity: identity(channelId, { width: 390, height: 800 }) })
await beginScrollTraceAnalysis(page, "above-fold-repeated-growth", { commandDirection: "backward" })
- const samples: Array<{ at: number; rowHeight: number }> = []
+ const samples: Array<{ at: number; rowHeight: number; lastInputAt: number }> = []
let trace: ScrollTraceResult | undefined
try {
for (const [round, emojis] of batches.entries()) {
@@ -638,35 +639,28 @@ test.describe("message scroll characterization", () => {
await expect.poll(() => proxy.frames.flatMap(communityFrameEvents).filter(event =>
event.type === "community:reaction.add" && event.messageId === messageId).length).toBe((round + 1) * 5)
await markScrollTrace(page, `stimulus:reactions-${round}`, { detail: { input: "controlled-wheel-and-scroll", emojis } })
- const before = await scroller.evaluate((element, growingId) => {
- element.dispatchEvent(new WheelEvent("wheel", { deltaY: -1, bubbles: true }))
- element.scrollTop -= 1
- element.dispatchEvent(new Event("scroll"))
- const top = element.getBoundingClientRect().top
- const growing = element.querySelector<HTMLElement>(`[data-msg-id="${growingId}"]`)
- const reader = Array.from(element.querySelectorAll<HTMLElement>("[data-msg-id]"))
- .find(row => row.getBoundingClientRect().bottom > top + 1)
- const wrapper = growing?.closest<HTMLElement>("[data-index]")
- if (!growing || !reader || !wrapper) throw new Error("Mounted growth and reading rows are required")
- return { id: reader.dataset.msgId!, top: reader.getBoundingClientRect().top - top,
- growingBottom: wrapper.getBoundingClientRect().bottom - top, scrollTop: element.scrollTop,
- rowHeight: wrapper.getBoundingClientRect().height, at: performance.now() }
- }, messageId)
+ const before = await scroller.evaluate(controlledBackwardGrowth, { start: messageId })
expect(before.growingBottom).toBeLessThan(0)
- samples.push({ at: before.at, rowHeight: before.rowHeight })
expect(proxy.releaseHeld(reactionFrame)).toBeGreaterThan(0)
await expect.poll(() => page.getByTestId(tid.message(messageId)).evaluate(element => element.closest("[data-index]")!.getBoundingClientRect().height))
.toBeGreaterThan(before.rowHeight)
+ const input = await scroller.evaluate(controlledBackwardGrowth, { finish: true as const })
+ expect(input.measurement, "Native row growth must be measured during controlled input").not.toBeNull()
+ expect(input.measurement!.direction).toBe("backward")
+ expect(input.measurement!.at - input.measurement!.lastInputAt).toBeLessThan(150)
+ expect(input.inputDelta).toBeLessThan(0)
+ samples.push({ at: before.at, rowHeight: before.rowHeight, lastInputAt: input.measurement!.lastInputAt })
await advanceScrollTraceFrame(scroller)
const after = await page.getByTestId(tid.message(before.id)).evaluate(element => {
const root = element.closest<HTMLElement>("[data-message-scroller-boundary]")?.querySelector<HTMLElement>("[data-pagination-enabled]")
if (!root) throw new Error("The reading scroller is required")
return element.getBoundingClientRect().top - root.getBoundingClientRect().top
})
- await markScrollTrace(page, `observed:reactions-${round}`, { detail: { before, after } })
- expect.soft(Math.abs(after - before.top), `Reading drift after reaction batch ${round}`).toBeLessThanOrEqual(1)
+ await markScrollTrace(page, `observed:reactions-${round}`, { detail: { before, after, input } })
+ expect.soft(Math.abs(after - before.top + input.inputDelta), `Reading drift after reaction batch ${round}`).toBeLessThanOrEqual(1)
}
} finally {
+ await scroller.evaluate(cancelControlledBackwardGrowth)
await endScrollTraceAnalysis(page, "above-fold-repeated-growth")
trace = await finishAndAttach(page, testInfo)
}
@@ -674,7 +668,7 @@ test.describe("message scroll characterization", () => {
for (const sample of samples) {
const measurement = trace.measurements.find(row => row.id === messageId && row.timestamp >= sample.at && row.height > sample.rowHeight + 1)
expect(measurement, "A real growing-row measurement must precede the 150ms backward reset").toBeDefined()
- expect(measurement!.timestamp - sample.at).toBeLessThan(150)
+ expect(measurement!.timestamp - sample.lastInputAt).toBeLessThan(150)
}
})
diff --git a/src/web/src/test/e2e-ui/_fixtures/backward-growth.ts b/src/web/src/test/e2e-ui/_fixtures/backward-growth.ts
new file mode 100644
index 000000000..9e5e254ae
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_fixtures/backward-growth.ts
@@ -0,0 +1,79 @@
+import type { Virtualizer } from "@tanstack/react-virtual"
+
+type Measurement = { at: number; height: number; direction: string | null; lastInputAt: number; inputDelta: number }
+type Receipt = { id: string; top: number; growingBottom: number; rowHeight: number; at: number; inputDelta: number; measurement: Measurement | null }
+type Session = { receipt: Receipt; stop: () => void }
+type FixtureElement = HTMLElement & { __backwardGrowth?: Session }
+type Hook = { memoizedState?: unknown; next?: Hook }
+type Fiber = { memoizedState?: Hook; return?: Fiber }
+
+export function controlledBackwardGrowth(element: HTMLElement, command: { start: string } | { finish: true }): Receipt {
+ const root = element as FixtureElement
+ if ("finish" in command) {
+ const session = root.__backwardGrowth
+ if (!session) throw new Error("Backward growth session is required")
+ session.stop()
+ delete root.__backwardGrowth
+ return session.receipt
+ }
+ if (root.__backwardGrowth) throw new Error("Previous backward growth session is still active")
+ const fiberKey = Object.keys(root).find(key => key.startsWith("__reactFiber$"))
+ let fiber = fiberKey ? (root as unknown as Record<string, Fiber>)[fiberKey] : undefined
+ let native: Virtualizer<HTMLElement, Element> | undefined
+ for (; fiber && !native; fiber = fiber.return) {
+ for (let hook = fiber.memoizedState; hook; hook = hook.next) {
+ const value = hook.memoizedState as { current?: unknown } | undefined
+ for (const candidate of [value, value?.current]) {
+ const instance = candidate as Virtualizer<HTMLElement, Element> | undefined
+ if (instance?.scrollElement === root && typeof instance.resizeItem === "function") native = instance
+ }
+ }
+ }
+ if (!native) throw new Error("The mounted native virtualizer is required for direction evidence")
+ const growing = root.querySelector<HTMLElement>(`[data-msg-id="${command.start}"]`)
+ const wrapper = growing?.closest<HTMLElement>("[data-index]")
+ const top = root.getBoundingClientRect().top
+ const reader = Array.from(root.querySelectorAll<HTMLElement>("[data-msg-id]"))
+ .find(row => row.getBoundingClientRect().bottom > top + 1)
+ if (!wrapper || !reader) throw new Error("Mounted growing and reading rows are required")
+ const index = Number(wrapper.dataset.index)
+ const receipt: Receipt = { id: reader.dataset.msgId!, top: reader.getBoundingClientRect().top - top,
+ growingBottom: wrapper.getBoundingClientRect().bottom - top, rowHeight: wrapper.getBoundingClientRect().height,
+ at: performance.now(), inputDelta: 0, measurement: null }
+ const resizeItem = native.resizeItem
+ let frame = 0
+ let lastInputAt = receipt.at
+ let stopped = false
+ const stop = () => {
+ stopped = true
+ cancelAnimationFrame(frame)
+ native.resizeItem = resizeItem
+ }
+ native.resizeItem = (measuredIndex, height) => {
+ if (measuredIndex === index && height > receipt.rowHeight + 1 && !receipt.measurement) {
+ receipt.measurement = { at: performance.now(), height, direction: native.scrollDirection, lastInputAt, inputDelta: receipt.inputDelta }
+ stop()
+ }
+ resizeItem(measuredIndex, height)
+ }
+ const input = () => {
+ if (stopped) return
+ if (performance.now() - receipt.at > 10_000) { stop(); return }
+ root.dispatchEvent(new WheelEvent("wheel", { deltaY: -1, bubbles: true }))
+ const before = root.scrollTop
+ root.scrollTop -= 1
+ receipt.inputDelta += root.scrollTop - before
+ root.dispatchEvent(new Event("scroll"))
+ lastInputAt = performance.now()
+ frame = requestAnimationFrame(input)
+ }
+ root.__backwardGrowth = { receipt, stop }
+ input()
+ return { ...receipt }
+}
+
+export function cancelControlledBackwardGrowth(element: HTMLElement) {
+ const root = element as FixtureElement
+ root.__backwardGrowth?.stop()
+ delete root.__backwardGrowth
+}