Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(web): calibrate resize observer fixture notifications (#913)

Gener646476d04393
diff --git a/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts b/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts
index db7defac6..2ff2a7f46 100644
--- a/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts
+++ b/src/web/src/hooks/community/use-scroll-anchor.repin.dom.test.ts
@@ -6,6 +6,121 @@ beforeEach(installMessageScrollFixture)
afterEach(restoreMessageScrollFixture)
describe("locked native adapter and existing message scroll owner", () => {
+ it("delivers only the changed observed box and keeps content, client and border sizes separate", () => {
+ scrollFixture.scrollbarWidth = 11
+ const h = mount()
+ h.root.style.padding = "7px 10px"
+ const content = vi.fn<ResizeObserverCallback>()
+ const border = vi.fn<ResizeObserverCallback>()
+ const contentObserver = new ResizeObserver(content)
+ const borderObserver = new ResizeObserver(border)
+ contentObserver.observe(h.root)
+ borderObserver.observe(h.root, { box: "border-box" })
+ resize(0)
+ expect(content).toHaveBeenCalledOnce()
+ expect(border).toHaveBeenCalledOnce()
+ expect(content.mock.calls[0][1]).toBe(contentObserver)
+ expect(h.root.clientWidth).toBe(309)
+ expect(content.mock.calls[0][0][0].target).toBe(h.root)
+ expect(content.mock.calls[0][0][0]).toMatchObject({
+ borderBoxSize: [{ inlineSize: 320, blockSize: 500 }],
+ contentBoxSize: [{ inlineSize: 289, blockSize: 486 }],
+ contentRect: { x: 10, y: 7, width: 289, height: 486 },
+ })
+ resize(0)
+ expect(content).toHaveBeenCalledOnce()
+ expect(border).toHaveBeenCalledOnce()
+ scrollFixture.scrollbarWidth = 21
+ resize(0)
+ expect(content).toHaveBeenCalledTimes(2)
+ expect(border).toHaveBeenCalledOnce()
+ expect(content.mock.lastCall![0][0]).toMatchObject({
+ borderBoxSize: [{ inlineSize: 320, blockSize: 500 }],
+ contentBoxSize: [{ inlineSize: 279, blockSize: 486 }],
+ })
+ contentObserver.disconnect()
+ borderObserver.disconnect()
+ })
+ it("retires removed observations and starts a fresh delivery after reobserve", () => {
+ const h = mount()
+ const callback = vi.fn<ResizeObserverCallback>()
+ const observer = new ResizeObserver(callback)
+ observer.observe(h.root)
+ resize(0)
+ expect(callback).toHaveBeenCalledOnce()
+ observer.unobserve(h.root)
+ scrollFixture.width += 10
+ resize(0)
+ expect(callback).toHaveBeenCalledOnce()
+ observer.observe(h.root, { box: "border-box" })
+ resize(0)
+ expect(callback).toHaveBeenCalledTimes(2)
+ resize(0)
+ expect(callback).toHaveBeenCalledTimes(2)
+ observer.disconnect()
+ scrollFixture.height += 10
+ resize(0)
+ expect(callback).toHaveBeenCalledTimes(2)
+ h.view.unmount()
+ const before = scrollFixture.scrollCalls.length
+ resize()
+ expect(scrollFixture.scrollCalls).toHaveLength(before)
+ expect(scrollFixture.frames.size).toBe(0)
+ })
+ it.each(["present", "away"])("keeps the %s position when the border height changes without a range change", position => {
+ scrollFixture.bodyHeights.set("m0", 1800)
+ const h = mount({ items: [message("m0")], hasMoreOlder: false })
+ if (position === "away") h.move(300)
+ const before = bodyTop(h.root, "m0")
+ const native = scrollFixture.latest.virtualizer
+ const range = { ...native.range }
+ expect(native.isScrolling).toBe(false)
+ scrollFixture.height -= 100
+ resize()
+ expect(native.range).toMatchObject({ startIndex: range.startIndex, endIndex: range.endIndex })
+ expect(native.isScrolling).toBe(false)
+ if (position === "present") expect(h.root.scrollHeight - h.root.clientHeight - h.root.scrollTop).toBeLessThanOrEqual(1)
+ else expect(bodyTop(h.root, "m0")).toBe(before)
+ expect(scrollFixture.latest.readPositionReady).toBe(true)
+ })
+ it.each(["present", "away"])("keeps the %s position when only the client height changes at a stable border box", position => {
+ const h = mount()
+ if (position === "away") h.move(300)
+ const before = bodyTop(h.root, position === "away" ? "m2" : "m13")
+ const native = scrollFixture.latest.virtualizer
+ const rect = { ...native.scrollRect }
+ expect(scrollFixture.frames.size).toBe(0)
+ h.root.style.borderBottom = "100px solid transparent"
+ expect(h.root.clientHeight).toBe(400)
+ expect(h.root.offsetHeight).toBe(500)
+ resize()
+ expect(native.scrollRect).toEqual(rect)
+ if (position === "present") expect(h.root.scrollHeight - h.root.clientHeight - h.root.scrollTop).toBeLessThanOrEqual(1)
+ else expect(bodyTop(h.root, "m2")).toBe(before)
+ expect(scrollFixture.latest.readPositionReady).toBe(true)
+ })
+ it("keeps a settled target and page anchor through a content-only gutter change", () => {
+ const positioned = vi.fn()
+ const h = mount({ onScrollTargetPositioned: positioned })
+ h.update({ scrollToMessageId: "m4" })
+ expect(positioned).toHaveBeenCalledWith("m4")
+ const top = bodyTop(h.root, "m4")
+ const rect = { ...scrollFixture.latest.virtualizer.scrollRect }
+ scrollFixture.scrollbarWidth = 11
+ resize()
+ expect(scrollFixture.latest.virtualizer.scrollRect).toEqual(rect)
+ expect(bodyTop(h.root, "m4")).toBe(top)
+ expect(scrollFixture.latest.readPositionReady).toBe(true)
+ h.move(0)
+ act(() => scrollFixture.latest.captureOlderPageAnchor())
+ h.update({ isFetchingOlder: true })
+ const before = bodyTop(h.root, "m0")
+ scrollFixture.scrollbarWidth = 21
+ resize()
+ h.update({ items: [message("older"), ...h.input.items], isFetchingOlder: false })
+ expect(bodyTop(h.root, "m0")).toBeCloseTo(before, 0)
+ expect(scrollFixture.latest.isOlderPageAnchorSettling).toBe(false)
+ })
it("does not restore the old origin after a reader scroll with a rejected pending row position", () => {
const h = mount({ items: Array.from({ length: 28 }, (_, i) => message(`m${i}`)) })
h.move(0)
diff --git a/src/web/src/test/message-scroll-fixture.ts b/src/web/src/test/message-scroll-fixture.ts
index eba12a9e1..47523b293 100644
--- a/src/web/src/test/message-scroll-fixture.ts
+++ b/src/web/src/test/message-scroll-fixture.ts
@@ -16,7 +16,12 @@ let firstPrefix: number
let bodyHeights: Map<string, number>
let frames: Map<number, FrameRequestCallback>
let frameId: number
-let resizeObservers: Array<{ callback: ResizeObserverCallback; elements: Set<Element> }>
+let resizeObservers: Array<{
+ callback: ResizeObserverCallback
+ observer: ResizeObserver
+ elements: Map<Element, { box: "content-box" | "border-box"; lastSize: ResizeObserverSize }>
+}>
+let scrollbarWidth: number
let scrollCalls: number[]
let scrollDescriptor: PropertyDescriptor | undefined
@@ -108,10 +113,32 @@ export function runFrames(count = 26) {
export function resize(frameCount = 26) {
act(() => {
for (const observer of [...resizeObservers]) {
- const entries = [...observer.elements].filter(element => element.isConnected).map(target => ({
- target, borderBoxSize: [{ blockSize: (target as HTMLElement).offsetHeight, inlineSize: width }],
- } as unknown as ResizeObserverEntry))
- if (entries.length) observer.callback(entries, {} as ResizeObserver)
+ const entries: ResizeObserverEntry[] = []
+ for (const [target, observation] of observer.elements) {
+ const node = target as HTMLElement
+ const style = window.getComputedStyle(node)
+ const rendered = node.isConnected && style.display !== "none"
+ const padding = (value: string) => Number.parseFloat(value) || 0
+ const borderBoxSize = {
+ inlineSize: rendered ? node.offsetWidth : 0,
+ blockSize: rendered ? node.offsetHeight : 0,
+ }
+ const contentBoxSize = {
+ inlineSize: rendered ? Math.max(0, node.clientWidth - padding(style.paddingLeft) - padding(style.paddingRight)) : 0,
+ blockSize: rendered ? Math.max(0, node.clientHeight - padding(style.paddingTop) - padding(style.paddingBottom)) : 0,
+ }
+ const size = observation.box === "border-box" ? borderBoxSize : contentBoxSize
+ if (size.inlineSize === observation.lastSize.inlineSize && size.blockSize === observation.lastSize.blockSize) continue
+ observation.lastSize = size
+ entries.push({
+ target, borderBoxSize: [borderBoxSize], contentBoxSize: [contentBoxSize], devicePixelContentBoxSize: [],
+ contentRect: DOMRect.fromRect({
+ x: rendered ? padding(style.paddingLeft) : 0, y: rendered ? padding(style.paddingTop) : 0,
+ width: contentBoxSize.inlineSize, height: contentBoxSize.blockSize,
+ }),
+ })
+ }
+ if (entries.length) observer.callback(entries, observer.observer)
}
})
runFrames(frameCount)
@@ -141,24 +168,33 @@ export function installMessageScrollFixture() {
frames = new Map()
frameId = 0
resizeObservers = []
+ scrollbarWidth = 0
scrollCalls = []
vi.useFakeTimers()
vi.spyOn(window, "requestAnimationFrame").mockImplementation(callback => { frames.set(++frameId, callback); return frameId })
vi.spyOn(window, "cancelAnimationFrame").mockImplementation(id => { frames.delete(id) })
- vi.stubGlobal("ResizeObserver", class {
+ vi.stubGlobal("ResizeObserver", class implements ResizeObserver {
private record: typeof resizeObservers[number]
- constructor(callback: ResizeObserverCallback) { this.record = { callback, elements: new Set() }; resizeObservers.push(this.record) }
- observe(element: Element) { this.record.elements.add(element) }
+ constructor(callback: ResizeObserverCallback) { this.record = { callback, observer: this, elements: new Map() }; resizeObservers.push(this.record) }
+ observe(element: Element, options?: ResizeObserverOptions) {
+ const box = options?.box ?? "content-box"
+ if (box === "device-pixel-content-box") throw new Error("Message scroll fixture supports CSS content and border boxes")
+ this.record.elements.set(element, { box, lastSize: { inlineSize: 0, blockSize: 0 } })
+ }
unobserve(element: Element) { this.record.elements.delete(element) }
disconnect() { this.record.elements.clear() }
})
- vi.spyOn(HTMLElement.prototype, "clientHeight", "get").mockImplementation(function (this: HTMLElement) { return isScrollRoot(this) ? height : rowGeometry(this).height })
- vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockImplementation(function (this: HTMLElement) { return this.clientHeight })
+ vi.spyOn(HTMLElement.prototype, "clientHeight", "get").mockImplementation(function (this: HTMLElement) {
+ return isScrollRoot(this) ? Math.max(0, height - (Number.parseFloat(this.style.borderTopWidth) || 0) - (Number.parseFloat(this.style.borderBottomWidth) || 0)) : rowGeometry(this).height
+ })
+ vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockImplementation(function (this: HTMLElement) { return isScrollRoot(this) ? height : this.clientHeight })
vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(() => width)
- vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockImplementation(() => width)
+ vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockImplementation(function (this: HTMLElement) {
+ return isScrollRoot(this) ? Math.max(0, width - scrollbarWidth - (Number.parseFloat(this.style.borderLeftWidth) || 0) - (Number.parseFloat(this.style.borderRightWidth) || 0)) : width
+ })
vi.spyOn(HTMLElement.prototype, "scrollHeight", "get").mockImplementation(function (this: HTMLElement) {
return isScrollRoot(this)
- ? Math.max(height, Number.parseFloat(this.querySelector<HTMLElement>('[data-message-list-content] > div')?.style.height ?? "0") || 0)
+ ? Math.max(this.clientHeight, Number.parseFloat(this.querySelector<HTMLElement>('[data-message-list-content] > div')?.style.height ?? "0") || 0)
: rowGeometry(this).height
})
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
@@ -185,6 +221,7 @@ export const scrollFixture = {
set width(value: number) { width = value },
get height() { return height },
set height(value: number) { height = value },
+ set scrollbarWidth(value: number) { scrollbarWidth = value },
get firstPrefix() { return firstPrefix },
set firstPrefix(value: number) { firstPrefix = value },
get bodyHeights() { return bodyHeights },