From 229303c669c27510ea6e2399cf66049a2e469c20 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sat, 24 Oct 2026 13:56:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=BF=20feat:=20Branch=20Tool=20Fold=20R?= =?UTF-8?q?ails=20Into=20Each=20Row=20(#17407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Branch Tool Fold Rails Into Each Row Draw each open fold's rail as a trunk with a curved branch into every row, measured from the rows themselves. Hovering a row lights the route along the tree: out of the previous branch, along the trunk and into the new one, at a steady pace. Layout changes under a still pointer (folding, streaming, scrolling) re-hit-test instead of clearing the path. * fix: Retrace the Fold Path When Its Row Moves Mid-Trip A row that moved while the lit path was still traveling to it kept the trip measured to its old position, so the path ran the old distance and jumped the rest. Start a fresh trip from the path's current position whenever the destination row moves. * test: Cover the Tool Fold Tree With Scenario Specs One tagged Playwright scenario per contract behavior: branches into each row, the hover route from the header, nested folds, travel along the tree, a click that unfolds the hovered row, reduced motion, right-to-left mirroring and touch. --- .../Parts/__tests__/SubagentCall.test.tsx | 8 +- .../ContentParts.integration.test.tsx | 11 +- .../Messages/Content/__tests__/rail.test.tsx | 406 ++++++++++----- .../components/Chat/Messages/Content/rail.tsx | 462 +++++++++++++++--- e2e/specs/mock/activity-fold.spec.ts | 65 ++- e2e/specs/mock/scenarios/fold-tree.spec.ts | 381 +++++++++++++++ 6 files changed, 1108 insertions(+), 225 deletions(-) create mode 100644 e2e/specs/mock/scenarios/fold-tree.spec.ts diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/SubagentCall.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/SubagentCall.test.tsx index 5f749d5824..d385e2895d 100644 --- a/client/src/components/Chat/Messages/Content/Parts/__tests__/SubagentCall.test.tsx +++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/SubagentCall.test.tsx @@ -217,7 +217,7 @@ const event = ( }); describe('SubagentCall', () => { - it('registers its existing header glyph for both containing fold rails', () => { + it('registers its existing header glyph as a branch of its fold', () => { renderWithState({ toolCallId: 'fold-glyph', initialProgress: 1 }); const row = screen.getByRole('button', { name: 'Ran agent' }).cloneNode(true) as HTMLElement; const glyph = row.querySelector(FOLD_GLYPH_SELECTOR); @@ -240,10 +240,8 @@ describe('SubagentCall', () => { rails.push(rail); } parent.append(row); - expect(litFoldPath(root, row, 155)).toEqual([ - { rail: rails[1], length: 54, end: true }, - { rail: rails[0], length: 118, end: false }, - ]); + /** The outer fold holds no row of its own above this one, so it has no branch to light. */ + expect(litFoldPath(root, row, 155)).toEqual([{ rail: rails[1], branch: 0 }]); }); it('keeps the configured name and avatar after live progress is cleared', () => { diff --git a/client/src/components/Chat/Messages/Content/__tests__/ContentParts.integration.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/ContentParts.integration.test.tsx index 2ad264861d..98694e9033 100644 --- a/client/src/components/Chat/Messages/Content/__tests__/ContentParts.integration.test.tsx +++ b/client/src/components/Chat/Messages/Content/__tests__/ContentParts.integration.test.tsx @@ -2538,7 +2538,7 @@ describe('ContentParts — live activity fold', () => { }); describe('fold paths in parallel columns', () => { - it('marks real lane boundaries and selects the header owning each output', () => { + it('marks real lane boundaries and keeps lane rows off the fold trunk', () => { const { container } = render(
@@ -2576,12 +2576,9 @@ describe('fold paths in parallel columns', () => { ({ top: 300, height: 20 }) as DOMRect; screen.getByTestId('lane-glyph-1').getBoundingClientRect = () => ({ top: 50, height: 20 }) as DOMRect; + /** Lane rows are not branches of the fold, which keeps its trunk dark over them. */ const cache = new WeakMap(); - expect(litFoldPath(root, screen.getByTestId('lane-output-0'), 350, cache)).toEqual([ - { rail, length: 268, end: true }, - ]); - expect(litFoldPath(root, screen.getByTestId('lane-output-1'), 350, cache)).toEqual([ - { rail, length: 18, end: true }, - ]); + expect(litFoldPath(root, screen.getByTestId('lane-output-0'), 350, cache)).toEqual([]); + expect(litFoldPath(root, screen.getByTestId('lane-output-1'), 350, cache)).toEqual([]); }); }); diff --git a/client/src/components/Chat/Messages/Content/__tests__/rail.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/rail.test.tsx index 0b10ff77b2..a892725b2e 100644 --- a/client/src/components/Chat/Messages/Content/__tests__/rail.test.tsx +++ b/client/src/components/Chat/Messages/Content/__tests__/rail.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { render, renderHook, screen, fireEvent } from '@testing-library/react'; +import { act, render, renderHook, screen, fireEvent } from '@testing-library/react'; import { FoldRail, RailGlyph, @@ -194,38 +194,32 @@ describe('litFoldPath', () => { document.body.innerHTML = ''; }); - it('lights every fold that holds the row, innermost first, down to its center', () => { + it('lights every fold that holds the row, innermost first, each into its own branch', () => { const { root, a, b, rows } = foldFixture(); expect(litFoldPath(root, rows.b2, 150)).toEqual([ - { rail: b.rail, length: 156 - 104 - 2, end: true }, - { rail: a.rail, length: 156 - 40 - 2, end: false }, + { rail: b.rail, branch: 1 }, + { rail: a.rail, branch: 1 }, ]); }); it('ends a nested header at the fold that holds it', () => { const { root, a, rows } = foldFixture(); - expect(litFoldPath(root, rows.header, 90)).toEqual([ - { rail: a.rail, length: 92 - 40 - 2, end: true }, - ]); + expect(litFoldPath(root, rows.header, 90)).toEqual([{ rail: a.rail, branch: 1 }]); }); it('counts the margin above a glyph as that row', () => { const { root, b, rows } = foldFixture(); - expect(litFoldPath(root, rows.b2, 141)[0]).toEqual({ rail: b.rail, length: 50, end: true }); + expect(litFoldPath(root, rows.b2, 141)[0]).toEqual({ rail: b.rail, branch: 1 }); }); it('points at the row above when the pointer is inside an open body', () => { const { root, b } = foldFixture(); const body = document.createElement('pre'); b.el.append(body); - expect(litFoldPath(root, body, 205)[0]).toEqual({ - rail: b.rail, - length: 188 - 104 - 2, - end: true, - }); + expect(litFoldPath(root, body, 205)[0]).toEqual({ rail: b.rail, branch: 2 }); }); - it('searches only the hovered column when document order is not vertical order', () => { + it('leaves rows in parallel columns without a branch to light', () => { const { root, a, rows } = foldFixture(); const left = document.createElement('div'); const right = document.createElement('div'); @@ -237,15 +231,13 @@ describe('litFoldPath', () => { const body = document.createElement('pre'); left.append(body); const cache = new WeakMap(); - expect(litFoldPath(root, rows.a2, 225, cache)).toEqual([ - { rail: a.rail, length: 188, end: true }, - ]); - expect(litFoldPath(root, body, 260, cache)[0].length).toBe(188); - expect(litFoldPath(root, rows.header, 90, cache)[0].length).toBe(50); + expect(litFoldPath(root, rows.a2, 225, cache)).toEqual([]); + expect(litFoldPath(root, body, 260, cache)).toEqual([]); + expect(litFoldPath(root, rows.header, 90, cache)).toEqual([]); expect(litFoldPath(root, a.el, 225, cache)).toEqual([]); }); - it('excludes nested columns but keeps a nested panel in its surrounding column', () => { + it('still lights a nested panel that sits in a column of its parent', () => { const { root, a, b, rows } = foldFixture(); const column = document.createElement('div'); const nested = document.createElement('div'); @@ -254,12 +246,9 @@ describe('litFoldPath', () => { nested.append(rows.b1); column.append(rows.a1, rows.a2, nested, b.el); a.el.replaceChildren(a.rail, column); - expect(litFoldPath(root, rows.a2, 225)[0].length).toBe(188); - expect(litFoldPath(root, rows.b1, 120)[0].length).toBe(82); - expect(litFoldPath(root, rows.b2, 150)).toEqual([ - { rail: b.rail, length: 50, end: true }, - { rail: a.rail, length: 114, end: false }, - ]); + expect(litFoldPath(root, rows.a2, 225)).toEqual([]); + expect(litFoldPath(root, rows.b1, 120)).toEqual([]); + expect(litFoldPath(root, rows.b2, 150)).toEqual([{ rail: b.rail, branch: 0 }]); }); it('leaves the rails and the outermost header alone', () => { @@ -272,6 +261,8 @@ describe('litFoldPath', () => { describe('useFoldPath', () => { let frames: FrameRequestCallback[] = []; let resize: ResizeObserverCallback; + /** What sits under the pointer when a layout change has the hook hit-test again. */ + const hit = jest.fn(() => null); const observe = jest.fn(); const disconnect = jest.fn(); const flushFrames = () => { @@ -308,34 +299,148 @@ describe('useFoldPath', () => { jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => { frames = []; }); + hit.mockReset().mockReturnValue(null); + Object.defineProperty(document, 'elementFromPoint', { configurable: true, value: hit }); }); afterEach(() => { jest.restoreAllMocks(); + Reflect.deleteProperty(document, 'elementFromPoint'); + window.matchMedia = allMotion; document.body.innerHTML = ''; }); + /** Gives each fixture rail the lit path and the row centers `FoldRail` renders. */ + const traced = (rail: HTMLElement, centers: number[]) => { + rail.dataset.centers = centers.join(','); + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + path.setAttribute('data-fold-trace', ''); + rail.append(path); + return path; + }; + /** The setup's `matchMedia` is already a mock, so swap it rather than spy on it. */ + const allMotion = window.matchMedia; + const reduceMotion = () => { + window.matchMedia = ((query: string) => + ({ + matches: query.includes('reduce'), + media: query, + }) as MediaQueryList) as typeof window.matchMedia; + }; + it('paints the path onto the rails and clears it when the pointer leaves', () => { + reduceMotion(); const { root, a, b, rows } = foldFixture(); + /** Centers relative to each rail's top. */ + const bPath = traced(b.rail, [20, 52, 84]); + const aPath = traced(a.rail, [20, 52, 190]); const { unmount } = renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b2, 150); - expect(b.rail.dataset.foldLit).toBe('end'); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe('50px'); - expect(a.rail.dataset.foldLit).toBe('through'); - expect(a.rail.style.getPropertyValue('--fold-lit')).toBe('114px'); + expect(b.rail.dataset.foldLit).toBe('1'); + expect(a.rail.dataset.foldLit).toBe('1'); + expect(bPath).toHaveAttribute('data-lit'); + expect(bPath).toHaveAttribute('d', 'M 11.5 2 V 44 A 8 8 0 0 0 19.5 52 H 22'); + expect(aPath).toHaveAttribute('d', 'M 11.5 2 V 44 A 8 8 0 0 0 19.5 52 H 22'); pointer('pointermove', rows.a2, 225); expect(b.rail.dataset.foldLit).toBeUndefined(); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe(''); - expect(a.rail.dataset.foldLit).toBe('end'); + expect(bPath).not.toHaveAttribute('data-lit'); + expect(a.rail.dataset.foldLit).toBe('2'); + expect(aPath).toHaveAttribute('d', 'M 11.5 2 V 182 A 8 8 0 0 0 19.5 190 H 22'); pointer('pointerleave', root, 0); expect(a.rail.dataset.foldLit).toBeUndefined(); + expect(aPath).not.toHaveAttribute('data-lit'); unmount(); }); - it('follows no touch, and drops the path on a press that may move the rows', () => { + it('travels the tree from one row to the next: out of the branch, along the trunk, in', () => { + const { root, b, rows } = foldFixture(); + const path = traced(b.rail, [20, 52, 84]); + renderHook(() => useFoldPath({ current: root }, true)); + const shape = () => { + const d = path.getAttribute('d') ?? ''; + return { d, tip: Number(/V ([\d.]+)/.exec(d)?.[1]), branch: d.includes(' A ') }; + }; + const frameAt = (now: number) => { + const pending = frames; + frames = []; + pending.forEach((callback) => callback(now)); + }; + + pointer('pointermove', rows.b3, 180, 'mouse', false); + frameAt(0); + frameAt(0); + frameAt(1000); + expect(path).toHaveAttribute('d', 'M 11.5 2 V 76 A 8 8 0 0 0 19.5 84 H 22'); + expect(frames).toHaveLength(0); + + /** From the third row to the second, sampled every 10ms. */ + pointer('pointermove', rows.b2, 150, 'mouse', false); + frameAt(2000); + const seen: Array> = []; + for (let now = 2000; frames.length > 0; now += 10) { + frameAt(now); + seen.push(shape()); + } + expect(seen[seen.length - 1].d).toBe('M 11.5 2 V 44 A 8 8 0 0 0 19.5 52 H 22'); + for (let i = 0; i < seen.length; i++) { + const { tip, branch } = seen[i]; + /** Never above the row it is heading for, never back via the top. */ + expect(tip).toBeGreaterThanOrEqual(44); + expect(tip).toBeLessThanOrEqual(76); + if (i > 0) { + expect(tip).toBeLessThanOrEqual(seen[i - 1].tip); + } + /** A branch is only ever drawn from a branch point, so nothing leaves the tree. */ + if (branch) { + expect([44, 76]).toContain(tip); + } + } + expect(seen.some(({ tip, branch }) => !branch && tip > 44 && tip < 76)).toBe(true); + }); + + it('keeps traveling, without a jump, when the row it is heading for moves mid-trip', async () => { + const { root, b, rows } = foldFixture(); + const path = traced(b.rail, [20, 52, 84]); + renderHook(() => useFoldPath({ current: root }, true)); + const tipOf = () => Number(/V ([\d.]+)/.exec(path.getAttribute('d') ?? '')?.[1]); + const frameAt = (now: number) => { + const pending = frames; + frames = []; + pending.forEach((callback) => callback(now)); + }; + + pointer('pointermove', rows.b1, 120, 'mouse', false); + frameAt(0); + frameAt(0); + frameAt(1000); + expect(tipOf()).toBe(12); + + pointer('pointermove', rows.b3, 180, 'mouse', false); + frameAt(2000); + const tips = [tipOf()]; + for (let now = 2000; now <= 2040; now += 5) { + frameAt(now); + tips.push(tipOf()); + } + /** A fold above the third row opens: the row is 16px further down now. */ + hit.mockReturnValue(rows.b3); + b.rail.dataset.centers = '20,52,100'; + await Promise.resolve(); + for (let now = 2045; frames.length > 0; now += 5) { + frameAt(now); + tips.push(tipOf()); + } + expect(path).toHaveAttribute('d', 'M 11.5 2 V 92 A 8 8 0 0 0 19.5 100 H 22'); + for (let i = 1; i < tips.length; i++) { + expect(tips[i]).toBeGreaterThanOrEqual(tips[i - 1]); + expect(tips[i] - tips[i - 1]).toBeLessThan(10); + } + }); + + it('follows no touch, and clears on a touch press', () => { const { root, a, rows } = foldFixture(); renderHook(() => useFoldPath({ current: root }, true)); @@ -343,17 +448,34 @@ describe('useFoldPath', () => { expect(a.rail.dataset.foldLit).toBeUndefined(); pointer('pointermove', rows.a1, 55); - expect(a.rail.dataset.foldLit).toBe('end'); - pointer('pointerdown', rows.a1, 55); + expect(a.rail.dataset.foldLit).toBe('0'); + pointer('pointerdown', rows.a1, 55, 'touch'); expect(a.rail.dataset.foldLit).toBeUndefined(); }); - it('cancels a queued paint when the pointer presses before the frame', () => { + it('keeps the path through a click that folds the rows, re-hit under the pointer', async () => { + const { root, a, b, rows } = foldFixture(); + renderHook(() => useFoldPath({ current: root }, true)); + pointer('pointermove', rows.b2, 150); + expect(b.rail.dataset.foldLit).toBe('1'); + + /** Folding the group unmounts its panel; the group's header is now under the pointer. */ + hit.mockReturnValue(rows.header); + pointer('pointerdown', rows.b2, 150); + b.el.remove(); + await Promise.resolve(); + flushFrames(); + expect(hit).toHaveBeenCalledWith(100, 150); + expect(b.rail.dataset.foldLit).toBeUndefined(); + expect(a.rail.dataset.foldLit).toBe('1'); + }); + + it('cancels a queued paint when a touch presses before the frame', () => { const { root, a, b, rows } = foldFixture(); renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b2, 150, 'mouse', false); expect(frames).toHaveLength(1); - pointer('pointerdown', rows.b2, 150); + pointer('pointerdown', rows.b2, 150, 'touch'); expect(window.cancelAnimationFrame).toHaveBeenCalledWith(1); expect(frames).toHaveLength(0); expect(a.rail.dataset.foldLit).toBeUndefined(); @@ -366,74 +488,98 @@ describe('useFoldPath', () => { renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b1, 120); await Promise.resolve(); - expect(b.rail.dataset.foldLit).toBe('end'); + expect(b.rail.dataset.foldLit).toBe('0'); pointer('pointermove', rows.b2, 150); pointer('pointermove', rows.b3, 180); expect(query).toHaveBeenCalledTimes(1); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe('82px'); + expect(b.rail.dataset.foldLit).toBe('2'); }); - it('clears on streamed text changes without relighting a queued frame', async () => { + it('relights under the pointer when streamed text moves the rows', async () => { const { root, b, rows } = foldFixture(); rows.b1.append(document.createTextNode('output')); renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b2, 150); - await Promise.resolve(); - pointer('pointermove', rows.b2, 150, 'mouse', false); + expect(b.rail.dataset.foldLit).toBe('1'); + + /** The first row's output grows and pushes the rows below it out from under the pointer. */ + rows.b2.getBoundingClientRect = () => ({ top: 178, height: 20 }) as DOMRect; + rows.b3.getBoundingClientRect = () => ({ top: 210, height: 20 }) as DOMRect; + hit.mockReturnValue(rows.b1); rows.b1.firstChild!.textContent = 'growing output'; await Promise.resolve(); flushFrames(); - expect(b.rail.dataset.foldLit).toBeUndefined(); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe(''); + expect(b.rail.dataset.foldLit).toBe('0'); + }); + + it('uses a fresh move over a pending re-hit in the same frame', async () => { + const { root, b, rows } = foldFixture(); + renderHook(() => useFoldPath({ current: root }, true)); + pointer('pointermove', rows.b1, 120); + rows.b1.setAttribute('class', 'fold-glyph changed'); + await Promise.resolve(); + pointer('pointermove', rows.b3, 180); + expect(hit).not.toHaveBeenCalled(); + expect(b.rail.dataset.foldLit).toBe('2'); }); it('rebuilds cached glyphs after rows are inserted or removed', async () => { const { root, b, rows } = foldFixture(); const query = jest.spyOn(b.el, 'querySelectorAll'); renderHook(() => useFoldPath({ current: root }, true)); + hit.mockReturnValue(rows.b1); pointer('pointermove', rows.b1, 120); const added = rows.b2.cloneNode() as HTMLElement; added.getBoundingClientRect = () => ({ top: 210, height: 20 }) as DOMRect; b.el.append(added); await Promise.resolve(); - expect(b.rail.dataset.foldLit).toBeUndefined(); + flushFrames(); + expect(b.rail.dataset.foldLit).toBe('0'); pointer('pointermove', added, 215); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe('114px'); + expect(b.rail.dataset.foldLit).toBe('3'); added.remove(); await Promise.resolve(); pointer('pointermove', rows.b3, 180); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe('82px'); + expect(b.rail.dataset.foldLit).toBe('2'); expect(query).toHaveBeenCalledTimes(3); }); - it.each(['class', 'style', 'hidden', 'open'])( - 'clears on a disclosure %s change', + it.each(['class', 'style', 'hidden', 'open', 'data-centers'])( + 're-hits on a disclosure %s change', async (attribute) => { const { root, b, rows } = foldFixture(); renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b2, 150); rows.b1.setAttribute(attribute, 'changed'); await Promise.resolve(); + flushFrames(); + expect(hit).toHaveBeenCalledWith(100, 150); expect(b.rail.dataset.foldLit).toBeUndefined(); }, ); - it('clears when resizing or scrolling moves the hovered row', () => { + it('re-hits when resizing or scrolling moves the rows under the pointer', () => { const { root, b, rows } = foldFixture(); renderHook(() => useFoldPath({ current: root }, true)); + hit.mockReturnValue(rows.b2); pointer('pointermove', rows.b2, 150); resize([], {} as ResizeObserver); - expect(b.rail.dataset.foldLit).toBeUndefined(); - pointer('pointermove', rows.b2, 150); + flushFrames(); + expect(b.rail.dataset.foldLit).toBe('1'); root.dispatchEvent(new Event('scroll')); - expect(b.rail.dataset.foldLit).toBeUndefined(); - pointer('pointermove', rows.b2, 150); + flushFrames(); + expect(b.rail.dataset.foldLit).toBe('1'); + expect(hit).toHaveBeenCalledTimes(2); + + /** Scrolled out from under the pointer: nothing of this fold is there any more. */ + hit.mockReturnValue(document.body); window.dispatchEvent(new Event('resize')); + flushFrames(); expect(b.rail.dataset.foldLit).toBeUndefined(); }); it.each([false, true])( - 'clears after a preceding message shifts the fold, with a queued paint=%s', + 'relights after a preceding message shifts the fold, with a queued paint=%s', (queued) => { const { root, a, b, rows } = foldFixture(); const transcript = document.createElement('div'); @@ -453,24 +599,18 @@ describe('useFoldPath', () => { const query = jest.spyOn(b.el, 'querySelectorAll'); const { unmount } = renderHook(() => useFoldPath({ current: root }, true)); pointer('pointermove', rows.b2, 150); - expect(b.rail.dataset.foldLit).toBe('end'); + expect(b.rail.dataset.foldLit).toBe('1'); if (queued) { pointer('pointermove', rows.b2, 150, 'mouse', false); } + /** 32px down, the row above the one that was hovered is now under the pointer. */ offset = 32; - if (observe.mock.calls.some(([element]) => element === transcript)) { - resize([], {} as ResizeObserver); - } + hit.mockReturnValue(rows.b1); + expect(observe.mock.calls.some(([element]) => element === transcript)).toBe(true); + resize([], {} as ResizeObserver); flushFrames(); - expect(root.getBoundingClientRect()).toMatchObject({ top: 32, height: 250 }); - expect(message.getBoundingClientRect()).toMatchObject({ top: 32, height: 250 }); - expect(a.rail.dataset.foldLit).toBeUndefined(); - expect(b.rail.dataset.foldLit).toBeUndefined(); - expect(a.rail.style.getPropertyValue('--fold-lit')).toBe(''); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe(''); - pointer('pointermove', rows.b1, 150); - expect(b.rail.dataset.foldLit).toBe('end'); - expect(b.rail.style.getPropertyValue('--fold-lit')).toBe('18px'); + expect(b.rail.dataset.foldLit).toBe('0'); + expect(a.rail.dataset.foldLit).toBe('1'); expect(query).toHaveBeenCalledTimes(1); unmount(); expect(disconnect).toHaveBeenCalledTimes(1); @@ -480,45 +620,32 @@ describe('useFoldPath', () => { it('re-hit-tests one queued paint after scrolling instead of discarding a fresh move', () => { const { root, a, b, rows } = foldFixture(); rows.a2.getBoundingClientRect = () => ({ top: 140, height: 20 }) as DOMRect; - const hit = jest.fn(() => rows.a2); - Object.defineProperty(document, 'elementFromPoint', { configurable: true, value: hit }); - try { - renderHook(() => useFoldPath({ current: root }, true)); - pointer('pointermove', rows.b2, 150, 'mouse', false); - root.dispatchEvent(new Event('scroll')); - root.dispatchEvent(new Event('scroll')); - expect(frames).toHaveLength(1); - expect(hit).not.toHaveBeenCalled(); - flushFrames(); - expect(hit).toHaveBeenCalledTimes(1); - expect(hit).toHaveBeenCalledWith(100, 150); - expect(b.rail.dataset.foldLit).toBeUndefined(); - expect(a.rail.dataset.foldLit).toBe('end'); - expect(a.rail.style.getPropertyValue('--fold-lit')).toBe('108px'); - } finally { - Reflect.deleteProperty(document, 'elementFromPoint'); - } + hit.mockReturnValue(rows.a2); + renderHook(() => useFoldPath({ current: root }, true)); + pointer('pointermove', rows.b2, 150, 'mouse', false); + root.dispatchEvent(new Event('scroll')); + root.dispatchEvent(new Event('scroll')); + expect(frames).toHaveLength(1); + expect(hit).not.toHaveBeenCalled(); + flushFrames(); + expect(hit).toHaveBeenCalledTimes(1); + expect(hit).toHaveBeenCalledWith(100, 150); + expect(b.rail.dataset.foldLit).toBeUndefined(); + expect(a.rail.dataset.foldLit).toBe('2'); }); it.each([null, document.body])( 'clears a queued path when scrolling moves the pointer outside the fold (%p)', (target) => { const { root, a, b, rows } = foldFixture(); - Object.defineProperty(document, 'elementFromPoint', { - configurable: true, - value: jest.fn(() => target), - }); - try { - renderHook(() => useFoldPath({ current: root }, true)); - pointer('pointermove', rows.b2, 150); - pointer('pointermove', rows.b3, 180, 'mouse', false); - root.dispatchEvent(new Event('scroll')); - flushFrames(); - expect(a.rail.dataset.foldLit).toBeUndefined(); - expect(b.rail.dataset.foldLit).toBeUndefined(); - } finally { - Reflect.deleteProperty(document, 'elementFromPoint'); - } + hit.mockReturnValue(target); + renderHook(() => useFoldPath({ current: root }, true)); + pointer('pointermove', rows.b2, 150); + pointer('pointermove', rows.b3, 180, 'mouse', false); + root.dispatchEvent(new Event('scroll')); + flushFrames(); + expect(a.rail.dataset.foldLit).toBeUndefined(); + expect(b.rail.dataset.foldLit).toBeUndefined(); }, ); @@ -558,20 +685,77 @@ describe('useFoldPath', () => { rerender({ hasBody: true }); pointer('pointermove', rows.b2, 150); - expect(b.rail.dataset.foldLit).toBe('end'); + expect(b.rail.dataset.foldLit).toBe('1'); }); }); -describe('FoldRail path segment', () => { - it('draws the lit path inside the rail, hidden from assistive tech', () => { - function Rail() { - const hover = useRailHover(); - return ; - } - render(); +describe('FoldRail branches', () => { + let measure: ResizeObserverCallback; + + beforeEach(() => { + /** The rail measures in a layout effect, before its later siblings' refs attach. */ + jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function ( + this: HTMLElement, + ) { + return { top: Number(this.dataset.top ?? 0), height: 20 } as DOMRect; + }); + jest.spyOn(window, 'ResizeObserver').mockImplementation((callback) => { + measure = callback; + return { observe: jest.fn(), unobserve: jest.fn(), disconnect: jest.fn() }; + }); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + function Panel({ rows, expanded = true }: { rows: number[]; expanded?: boolean }) { + const hover = useRailHover(); + return ( +
+ + {rows.map((top) => ( + + ))} +
+ +
+
+ ); + } + + it('branches the trunk into each of its own rows, hidden from assistive tech', () => { + render(); const rail = screen.getByTestId('fold-rail'); - expect(rail).toHaveAttribute('data-fold-rail'); expect(rail).toHaveAttribute('aria-hidden', 'true'); - expect(rail).toContainElement(screen.getByTestId('fold-rail-path')); + expect(rail).toHaveAttribute('data-centers', '16,48'); + const drawn = Array.from(screen.getByTestId('fold-rail-tree').querySelectorAll('g > path')); + expect(drawn.map((path) => path.getAttribute('d'))).toEqual([ + 'M 11.5 2 V 40', + 'M 11.5 8 A 8 8 0 0 0 19.5 16 H 22', + 'M 11.5 40 A 8 8 0 0 0 19.5 48 H 22', + ]); + /** The lit path has no shape until `useFoldPath` traces it to a row. */ + expect(screen.getByTestId('fold-rail-path')).not.toHaveAttribute('d'); + }); + + it('adds a branch when a streamed row resizes the panel', () => { + const { rerender } = render(); + expect(screen.getByTestId('fold-rail')).toHaveAttribute('data-centers', '16'); + rerender(); + act(() => measure([], {} as ResizeObserver)); + expect(screen.getByTestId('fold-rail')).toHaveAttribute('data-centers', '16,48'); + }); + + it('keeps a plain hairline for a panel without rows of its own', () => { + render(); + expect(screen.queryByTestId('fold-rail-tree')).toBeNull(); + }); + + it('fades the branches out as the fold collapses', () => { + const { rerender } = render(); + expect(screen.getByTestId('fold-rail-tree')).toHaveClass('opacity-100'); + rerender(); + expect(screen.getByTestId('fold-rail-tree')).toHaveClass('opacity-0'); }); }); diff --git a/client/src/components/Chat/Messages/Content/rail.tsx b/client/src/components/Chat/Messages/Content/rail.tsx index 62cfb48a0a..35eae36628 100644 --- a/client/src/components/Chat/Messages/Content/rail.tsx +++ b/client/src/components/Chat/Messages/Content/rail.tsx @@ -1,4 +1,11 @@ -import { createContext, useEffect, useState, useSyncExternalStore } from 'react'; +import { + useRef, + useState, + useEffect, + createContext, + useLayoutEffect, + useSyncExternalStore, +} from 'react'; import { CircleMinus } from 'lucide-react'; import type { ReactNode, RefObject } from 'react'; import { ROW_GLYPH_SLOT, FOLD_GLYPH_SELECTOR } from './rows'; @@ -65,12 +72,103 @@ export function RailGlyph({ hover, children }: { hover: RailHover; children: Rea ); } +const FOLD_PANEL = '[data-fold-panel]'; +const FOLD_RAIL = '[data-fold-rail]'; +const FOLD_ROOT = '[data-fold-root]'; +const FOLD_COLUMN = '[data-fold-column]'; +/** A row's glyph is 20px tall inside a 32px line, so the pointer is on that row from + * the margin above the glyph down to the next row's margin. */ +const ROW_REACH = 6; + +/** The row glyphs of one panel or column, in document order, which within that scope + * is vertical order. Rows of nested folds belong to those folds. */ +function glyphsIn(panel: Element, scope: Element): Element[] { + const glyphs: Element[] = []; + for (const glyph of scope.querySelectorAll(FOLD_GLYPH_SELECTOR)) { + if (glyph.closest(FOLD_PANEL) === panel && scopeOf(panel, glyph) === scope) { + glyphs.push(glyph); + } + } + return glyphs; +} + +/** The column of `panel` that holds `node`, or the panel itself. */ +function scopeOf(panel: Element, node: Element): Element { + const column = node.closest(FOLD_COLUMN); + return column != null && panel.contains(column) ? column : panel; +} + +/** Branch geometry in the rail's 24px inset: the trunk under the header glyph's + * center, an elbow of `BRANCH_RADIUS` and a stub that stops short of the row's glyph. */ +const TRUNK_X = 11.5; +const TRUNK_TOP = 2; +const BRANCH_RADIUS = 8; +const BRANCH_END = 22; + +/** Where a row's branch leaves the trunk: one radius above the row's center. */ +function branchStart(center: number) { + return center - Math.max(Math.min(BRANCH_RADIUS, center - TRUNK_TOP), 0); +} + +/** The curve from the trunk into the row, continuing a path already at `branchStart`. */ +function elbow(center: number) { + const r = center - branchStart(center); + return `A ${r} ${r} 0 0 0 ${TRUNK_X + r} ${center} H ${BRANCH_END}`; +} + +function sameCenters(a: number[], b: number[]) { + return a.length === b.length && a.every((value, i) => value === b[i]); +} + +/** Vertical centers of the panel's own row glyphs, relative to the rail's top. */ +function useBranchCenters(railRef: RefObject) { + const [centers, setCenters] = useState([]); + useLayoutEffect(() => { + const rail = railRef.current; + const panel = rail?.parentElement; + if (rail == null || panel == null || !panel.matches(FOLD_PANEL)) { + return; + } + const measure = () => { + const top = rail.getBoundingClientRect().top; + const next = glyphsIn(panel, panel).map((glyph) => { + const rect = glyph.getBoundingClientRect(); + return Math.round(rect.top + rect.height / 2 - top); + }); + setCenters((previous) => (sameCenters(previous, next) ? previous : next)); + }; + measure(); + /** Rows arriving or a nested fold opening resize the panel or the wrapper that holds + * the rows; that is when branches move. A panel's own height can be pinned while it + * animates, so its children are watched too, including ones mounted later. */ + const resize = new ResizeObserver(measure); + const observeChildren = () => { + for (const child of panel.children) { + resize.observe(child); + } + }; + resize.observe(panel); + observeChildren(); + const children = new MutationObserver(() => { + observeChildren(); + measure(); + }); + children.observe(panel, { childList: true }); + return () => { + resize.disconnect(); + children.disconnect(); + }; + }, [railRef]); + return centers; +} + /** - * The hairline that hangs from an open header's glyph, drawn inside a hit area - * the width of the panel's inset so the pointer only has to come near it. A - * pointer shortcut only: the header button stays the one control keyboard and - * screen-reader users reach, so the rail is hidden from both and never takes - * focus. Must sit in a `FOLD_RAIL_CLASSES` wrapper, whose inset it fills. + * The trunk that hangs from an open header's glyph and branches into each of its + * rows, drawn inside a hit area the width of the panel's inset so the pointer only + * has to come near it. A panel without rows of its own (parallel columns) keeps a + * plain hairline. A pointer shortcut only: the header button stays the one control + * keyboard and screen-reader users reach, so the rail is hidden from both and never + * takes focus. Must sit in a `FOLD_RAIL_CLASSES` wrapper, whose inset it fills. */ export function FoldRail({ hover, @@ -81,6 +179,8 @@ export function FoldRail({ expanded: boolean; onCollapse: () => void; }) { + const railRef = useRef(null); + const centers = useBranchCenters(railRef); /** Collapsed approval bodies stay mounted without a pointer leave. */ useEffect(() => { if (!expanded) { @@ -88,8 +188,10 @@ export function FoldRail({ } return () => hover.set(false); }, [hover, expanded]); + const last = centers[centers.length - 1]; return ( ); } -const FOLD_PANEL = '[data-fold-panel]'; -const FOLD_RAIL = '[data-fold-rail]'; -const FOLD_ROOT = '[data-fold-root]'; -const FOLD_COLUMN = '[data-fold-column]'; -/** A row's glyph is 20px tall inside a 32px line, so the pointer is on that row from - * the margin above the glyph down to the next row's margin. */ -const ROW_REACH = 6; +/** A fold to light: the rail and the index of its own row the path ends in. */ +export type LitRail = { rail: HTMLElement; branch: number }; -export type LitRail = { rail: HTMLElement; length: number; end: boolean }; - -/** The glyph at `y` in one panel or column, or the nearest above an open body. - * Within that scope, document order is vertical order. */ +/** The glyph at `y` in one panel or column, or the nearest above an open body. */ function glyphAt( panel: Element, scope: Element, y: number, glyphsByPanel: WeakMap, -): Element | null { +): { glyph: Element; index: number } | null { let glyphs = glyphsByPanel.get(scope); if (glyphs == null) { - glyphs = []; - for (const glyph of scope.querySelectorAll(FOLD_GLYPH_SELECTOR)) { - const column = glyph.closest(FOLD_COLUMN); - const glyphScope = column != null && panel.contains(column) ? column : panel; - if (glyph.closest(FOLD_PANEL) === panel && glyphScope === scope) { - glyphs.push(glyph); - } - } + glyphs = glyphsIn(panel, scope); glyphsByPanel.set(scope, glyphs); } - let found: Element | null = null; + let found = -1; let low = 0; let high = glyphs.length - 1; while (low <= high) { const mid = (low + high) >> 1; if (glyphs[mid].getBoundingClientRect().top - ROW_REACH <= y) { - found = glyphs[mid]; + found = mid; low = mid + 1; } else { high = mid - 1; } } - return found; + return found < 0 ? null : { glyph: glyphs[found], index: found }; } /** * The rails to light for a pointer at `y` over `target`: one per fold that holds the - * row under it, innermost first, each `length` px from its top to that row's center. - * Empty over a rail itself, whose own hover shows what it collapses, and over any - * header that no fold of `root` holds. + * row under it, innermost first, each ending in the branch of its own row that leads + * there (for an outer fold, the header of the nested one). Rows inside a parallel + * column have no branch, so their fold stays dark. Empty over a rail itself, whose own + * hover shows what it collapses, and over any header that no fold of `root` holds. */ export function litFoldPath( root: Element, @@ -180,44 +291,224 @@ export function litFoldPath( if (innermost == null || !root.contains(innermost)) { return []; } - const column = target.closest(FOLD_COLUMN); - const scope = column != null && innermost.contains(column) ? column : innermost; - const glyph = glyphAt(innermost, scope, y, glyphsByPanel); - if (glyph == null) { - return []; - } - const { top, height } = glyph.getBoundingClientRect(); - const center = top + height / 2; const path: LitRail[] = []; + let node: Element = target; let panel: Element | null = innermost; while (panel != null && root.contains(panel)) { + const scope = scopeOf(panel, node); const rail = panel.querySelector(`:scope > ${FOLD_RAIL}`); - if (rail != null) { - /** The path starts where the rail's own line does, 2px below the panel. */ - const length = Math.round(center - rail.getBoundingClientRect().top) - 2; - path.push({ rail, length: Math.max(length, 0), end: path.length === 0 }); + const hit = glyphAt(panel, scope, y, glyphsByPanel); + if (rail != null && hit != null && scope === panel) { + path.push({ rail, branch: hit.index }); } + node = panel; panel = panel.parentElement?.closest(FOLD_PANEL) ?? null; } return path; } +/** + * A rail's lit path while it is shown. It only ever lies on the drawn tree: the trunk + * from the top down to `tip`, then, once the tip rests on a branch point, `elbow` px + * of that row's branch (centered at `center`). + */ +type Trace = { + path: SVGPathElement; + branch: number; + center: number; + tip: number; + elbow: number; + move: TraceMove | null; + frame: number; + fade: number; +}; + +/** One trip along the tree: back out of the current branch, along the trunk, into the next. */ +type TraceMove = { + from: { center: number; tip: number; elbow: number }; + branch: number; + center: number; + retract: number; + travel: number; + /** How much of the new row's branch is already drawn when the trip starts. */ + extendFrom: number; + total: number; + duration: number; + start: number | null; +}; + +const traces = new WeakMap(); +/** The pace along the tree, bounded so a hop to the next row and a long run both read. */ +const TRACE_MS_PER_PX = 2.2; +const TRACE_MIN_MS = 140; +const TRACE_MAX_MS = 320; +/** Long enough for the path's fade-out, after which it next enters from the top. */ +const TRACE_FADE_MS = 160; +const FOLD_TRACE = '[data-fold-trace]'; + +const easeOut = (t: number) => 1 - (1 - t) ** 3; + +function prefersReducedMotion() { + return window.matchMedia?.('(prefers-reduced-motion: reduce)').matches === true; +} + +/** Length of a row's branch: the quarter turn off the trunk, then the stub to the row. */ +function branchLength(center: number) { + const r = center - branchStart(center); + return (Math.PI * r) / 2 + (BRANCH_END - TRUNK_X - r); +} + +/** The first `length` px of a row's branch, continuing a path that is at its start. */ +function partialBranch(center: number, length: number) { + const r = center - branchStart(center); + const arc = (Math.PI * r) / 2; + if (length >= arc) { + const end = Math.min(BRANCH_END, Math.round((TRUNK_X + r + length - arc) * 100) / 100); + return `A ${r} ${r} 0 0 0 ${TRUNK_X + r} ${center} H ${end}`; + } + const angle = r === 0 ? 0 : length / r; + const x = TRUNK_X + r - r * Math.cos(angle); + const y = center - r + r * Math.sin(angle); + return `A ${r} ${r} 0 0 0 ${x} ${y}`; +} + +function drawTrace(trace: Trace) { + const branch = trace.elbow > 0 ? ` ${partialBranch(trace.center, trace.elbow)}` : ''; + trace.path.setAttribute('d', `M ${TRUNK_X} ${TRUNK_TOP} V ${trace.tip}${branch}`); +} + +/** Where a move has the path after `distance` px of it. */ +function stepMove(trace: Trace, move: TraceMove, distance: number) { + const { from } = move; + if (distance < move.retract) { + trace.center = from.center; + trace.tip = from.tip; + trace.elbow = from.elbow - distance; + return; + } + const along = distance - move.retract; + const target = branchStart(move.center); + if (along < move.travel) { + trace.elbow = 0; + trace.tip = from.tip + Math.sign(target - from.tip) * along; + return; + } + trace.center = move.center; + trace.tip = target; + trace.elbow = Math.min(move.extendFrom + along - move.travel, branchLength(move.center)); +} + +/** + * Lights a rail's path into the row `to` names, or fades it out for `null`. The path + * travels the tree's own route: it backs out of the branch it is in, runs along the + * trunk to the new row's branch point and draws into that branch, as one motion at a + * steady pace. Retargeting mid-trip starts the new trip from wherever it is. When the + * same row moves (a fold above it opened), the path follows it at once. + */ +function traceTo(rail: HTMLElement, to: { branch: number; center: number } | null) { + const path = rail.querySelector(FOLD_TRACE); + let trace = traces.get(rail); + if (to == null || path == null) { + if (trace != null) { + cancelAnimationFrame(trace.frame); + trace.frame = 0; + trace.move = null; + /** A path left mid-trip finishes its trip if the pointer comes straight back. */ + trace.branch = -1; + trace.path.removeAttribute('data-lit'); + window.clearTimeout(trace.fade); + trace.fade = window.setTimeout(() => traces.delete(rail), TRACE_FADE_MS); + } + return; + } + if (trace == null || trace.path !== path) { + /** Enters from the top, never from where a faded-out path last ended. */ + trace = { + path, + branch: -1, + center: TRUNK_TOP, + tip: TRUNK_TOP, + elbow: 0, + move: null, + frame: 0, + fade: 0, + }; + traces.set(rail, trace); + drawTrace(trace); + } + window.clearTimeout(trace.fade); + path.setAttribute('data-lit', ''); + + const settled = trace.move == null && trace.branch === to.branch; + if (settled && trace.center === to.center) { + return; + } + if (settled || prefersReducedMotion()) { + cancelAnimationFrame(trace.frame); + trace.frame = 0; + trace.move = null; + trace.branch = to.branch; + trace.center = to.center; + trace.tip = branchStart(to.center); + trace.elbow = branchLength(to.center); + drawTrace(trace); + return; + } + /** A row that moved mid-trip gets a fresh trip from where the path is now, so the + * remaining distance is measured to where the row actually is. */ + if (trace.move != null && trace.move.branch === to.branch && trace.move.center === to.center) { + return; + } + + cancelAnimationFrame(trace.frame); + const target = branchStart(to.center); + /** Turning back into the branch it is still partly in: keep what is drawn. */ + const resume = trace.elbow > 0 && trace.center === to.center && trace.tip === target; + const retract = resume ? 0 : trace.elbow; + const extendFrom = resume ? trace.elbow : 0; + const travel = Math.abs(target - trace.tip); + const total = retract + travel + branchLength(to.center) - extendFrom; + const move: TraceMove = { + from: { center: trace.center, tip: trace.tip, elbow: trace.elbow }, + branch: to.branch, + center: to.center, + retract, + travel, + extendFrom, + total, + duration: Math.min(TRACE_MAX_MS, Math.max(TRACE_MIN_MS, total * TRACE_MS_PER_PX)), + start: null, + }; + trace.move = move; + trace.branch = to.branch; + const current = trace; + const step = (now: number) => { + move.start ??= now; + const t = Math.min(1, (now - move.start) / move.duration); + stepMove(current, move, move.total * easeOut(t)); + drawTrace(current); + if (t < 1) { + current.frame = requestAnimationFrame(step); + return; + } + current.frame = 0; + current.move = null; + }; + current.frame = requestAnimationFrame(step); +} + function paintFoldPath(previous: LitRail[], next: LitRail[]) { for (const { rail } of previous) { if (!next.some((lit) => lit.rail === rail)) { delete rail.dataset.foldLit; - rail.style.removeProperty('--fold-lit'); + traceTo(rail, null); } } - for (const { rail, length, end } of next) { - const kind = end ? 'end' : 'through'; - if (rail.dataset.foldLit !== kind) { - rail.dataset.foldLit = kind; - } - const value = `${length}px`; - if (rail.style.getPropertyValue('--fold-lit') !== value) { - rail.style.setProperty('--fold-lit', value); - } + for (const { rail, branch } of next) { + rail.dataset.foldLit = String(branch); + /** Re-read on every paint: a re-hit after a layout change can move the branch. */ + const center = Number(rail.dataset.centers?.split(',')[branch]); + traceTo(rail, rail.dataset.centers && Number.isFinite(center) ? { branch, center } : null); } } @@ -268,17 +559,30 @@ export function useFoldPath(rootRef: RefObject, hasBody: boolean) { return; } pointer = { target: event.target, x: event.clientX, y: event.clientY }; + /** The move's own target is current; a layout change after it sets this again. */ + rehit = false; if (frame === 0) { frame = requestAnimationFrame(paint); } }; - const onScroll = () => { - /** Scroll events can arrive after a fresh move but before its queued paint. */ - if (frame !== 0) { - rehit = true; + /** The rows moved under a pointer that did not: hit-test its last position again + * on the next frame rather than going dark until the pointer next moves. Clicking + * a header to fold or unfold it, streaming, scrolling and resizing all land here. */ + const relight = () => { + if (pointer == null) { return; } - clear(); + rehit = true; + if (frame === 0) { + frame = requestAnimationFrame(paint); + } + }; + const onDown = (event: PointerEvent) => { + if (event.pointerType === 'touch') { + clear(); + return; + } + relight(); }; const mutations = new MutationObserver((records) => { let changed = false; @@ -300,7 +604,7 @@ export function useFoldPath(rootRef: RefObject, hasBody: boolean) { glyphsByPanel = new WeakMap(); } if (changed) { - clear(); + relight(); } }); mutations.observe(root, { @@ -308,9 +612,17 @@ export function useFoldPath(rootRef: RefObject, hasBody: boolean) { childList: true, characterData: true, attributes: true, - attributeFilter: ['class', 'style', 'hidden', 'open', 'data-fold-panel', 'data-fold-column'], + attributeFilter: [ + 'class', + 'style', + 'hidden', + 'open', + 'data-centers', + 'data-fold-panel', + 'data-fold-column', + ], }); - const resize = new ResizeObserver(clear); + const resize = new ResizeObserver(relight); /** Earlier messages can move this fold without resizing it. */ for ( let ancestor: HTMLElement | null = root; @@ -321,15 +633,15 @@ export function useFoldPath(rootRef: RefObject, hasBody: boolean) { } root.addEventListener('pointermove', onMove); root.addEventListener('pointerleave', clear); - root.addEventListener('pointerdown', clear); - window.addEventListener('scroll', onScroll, true); - window.addEventListener('resize', clear); + root.addEventListener('pointerdown', onDown); + window.addEventListener('scroll', relight, true); + window.addEventListener('resize', relight); return () => { root.removeEventListener('pointermove', onMove); root.removeEventListener('pointerleave', clear); - root.removeEventListener('pointerdown', clear); - window.removeEventListener('scroll', onScroll, true); - window.removeEventListener('resize', clear); + root.removeEventListener('pointerdown', onDown); + window.removeEventListener('scroll', relight, true); + window.removeEventListener('resize', relight); mutations.disconnect(); resize.disconnect(); clear(); diff --git a/e2e/specs/mock/activity-fold.spec.ts b/e2e/specs/mock/activity-fold.spec.ts index 8101ffc74d..1e4e71bca1 100644 --- a/e2e/specs/mock/activity-fold.spec.ts +++ b/e2e/specs/mock/activity-fold.spec.ts @@ -152,30 +152,39 @@ test.describe('activity fold', () => { const groupHeader = groupPanel.locator('xpath=preceding-sibling::div[1]').getByRole('button'); const lineColor = (railLocator: typeof groupRail) => railLocator - .locator('span') + .locator('g > path') .first() - .evaluate((line) => getComputedStyle(line).backgroundColor); + .evaluate((line) => getComputedStyle(line).stroke); const restingLine = await lineColor(groupRail); - /** A row two folds deep lights both rails down to it: the group's turns into - * the row's glyph, the phase's runs on past the group's header. */ + /** A row two folds deep lights both rails, each into its own branch: the + * group's into the row's glyph, the phase's into the group's header. */ const failedCall = groupPanel.getByTestId('tool-call').filter({ hasText: /^Failed:/ }); - const litLength = (railLocator: typeof groupRail) => - railLocator.evaluate((rail) => parseFloat(rail.style.getPropertyValue('--fold-lit'))); + const litBranch = (railLocator: typeof groupRail) => + railLocator.locator('[data-fold-trace][data-lit]'); + const endsAt = async (path: ReturnType, row: typeof failedCall) => { + await expect(path).toHaveCSS('opacity', '1'); + /** The foot slides into place, and branches are re-measured after rows move. */ + await expect + .poll(async () => { + const [elbow, glyph] = await Promise.all([ + path.boundingBox(), + row.locator('.fold-glyph').first().boundingBox(), + ]); + return elbow == null || glyph == null + ? Infinity + : Math.abs(elbow.y + elbow.height - (glyph.y + glyph.height / 2)); + }) + .toBeLessThan(2); + }; await failedCall.scrollIntoViewIfNeeded(); await failedCall.hover(); - await expect(groupRail).toHaveAttribute('data-fold-lit', 'end'); - await expect(phaseRail).toHaveAttribute('data-fold-lit', 'through'); - expect(await litLength(phaseRail)).toBeGreaterThan(await litLength(groupRail)); - const groupPath = groupRail.getByTestId('fold-rail-path'); - await expect(groupPath).toHaveCSS('opacity', '1'); - const [elbow, glyph] = await Promise.all([ - groupPath.boundingBox(), - failedCall.locator('.fold-glyph').first().boundingBox(), - ]); - expect(elbow).not.toBeNull(); - expect(glyph).not.toBeNull(); - expect(Math.abs(elbow!.y + elbow!.height - (glyph!.y + glyph!.height / 2))).toBeLessThan(2); + await expect(groupRail).toHaveAttribute('data-fold-lit'); + await expect(phaseRail).toHaveAttribute('data-fold-lit'); + await expect(litBranch(groupRail)).toHaveCount(1); + await expect(litBranch(phaseRail)).toHaveCount(1); + await endsAt(litBranch(groupRail), failedCall); + await endsAt(litBranch(phaseRail), groupHeader); await shot(page, 'lit-path'); await failedCall.evaluate((row) => { @@ -192,16 +201,16 @@ test.describe('activity fold', () => { row.dispatchEvent(new PointerEvent('pointerdown', { ...pointer, buttons: 1 })); row.dispatchEvent(new PointerEvent('pointerup', pointer)); }); - await expect(groupRail).not.toHaveAttribute('data-fold-lit'); - await expect(phaseRail).not.toHaveAttribute('data-fold-lit'); - await failedCall.scrollIntoViewIfNeeded(); - await failedCall.hover(); - await expect(groupRail).toHaveAttribute('data-fold-lit', 'end'); + /** A press keeps the path, hit-tested again under the pointer that did not move. */ + await expect(groupRail).toHaveAttribute('data-fold-lit'); + await expect(phaseRail).toHaveAttribute('data-fold-lit'); + await endsAt(litBranch(groupRail), failedCall); + /** Rows moving under a still pointer relight from the new geometry. */ await groupPanel.locator('> div > div').evaluate((rows) => { (rows as HTMLElement).style.paddingTop = '12px'; }); - await expect(groupRail).not.toHaveAttribute('data-fold-lit'); - await expect(phaseRail).not.toHaveAttribute('data-fold-lit'); + await expect(groupRail).toHaveAttribute('data-fold-lit'); + await endsAt(litBranch(groupRail), failedCall); await groupPanel.locator('> div > div').evaluate((rows) => { (rows as HTMLElement).style.removeProperty('padding-top'); }); @@ -223,7 +232,7 @@ test.describe('activity fold', () => { }); await failedCall.scrollIntoViewIfNeeded(); await failedCall.hover(); - await expect(groupRail).toHaveAttribute('data-fold-lit', 'end'); + await expect(groupRail).toHaveAttribute('data-fold-lit'); const shift = await failedCall.evaluate((row) => { const fold = row.closest('[data-testid="activity-phase-card"]')!; const message = row.closest('.message-render')!; @@ -242,8 +251,10 @@ test.describe('activity fold', () => { expect(shift.after.foldHeight).toBe(shift.before.foldHeight); expect(shift.after.messageHeight).toBe(shift.before.messageHeight); expect(shift.after.scrollTop).toBe(shift.before.scrollTop); + /** 32px further down, the pointer is over the group's header: the path is hit-tested + * again and now ends in that header's branch of the phase. */ await expect(groupRail).not.toHaveAttribute('data-fold-lit'); - await expect(phaseRail).not.toHaveAttribute('data-fold-lit'); + await endsAt(litBranch(phaseRail), groupHeader); await transcript.evaluate((content, anchor) => { content.querySelector('#fold-layout-predecessor')!.remove(); content.closest('.scrollbar-gutter-stable')!.style.overflowAnchor = anchor; diff --git a/e2e/specs/mock/scenarios/fold-tree.spec.ts b/e2e/specs/mock/scenarios/fold-tree.spec.ts new file mode 100644 index 0000000000..45a04e3c6e --- /dev/null +++ b/e2e/specs/mock/scenarios/fold-tree.spec.ts @@ -0,0 +1,381 @@ +import { expect, test } from '@playwright/test'; +import type { Locator, Page, Route } from '@playwright/test'; +import { messagesView } from '../helpers'; + +/** + * An open tool fold draws its rail as a tree: a trunk under the header's glyph with a + * curved branch into each of its rows. Hovering a row lights the route along that tree, + * from the header into the row, and the route only ever travels the drawn branches. + * Conversations are routed fixtures, so every run sees the same rows. + */ +test.describe.configure({ timeout: 120_000 }); + +const NO_PARENT = '00000000-0000-0000-0000-000000000000'; +const DESKTOP = { viewport: { width: 1280, height: 900 }, isMobile: false, hasTouch: false }; +const unique = (prefix: string) => `${prefix}-${Date.now()}-${Math.floor(Math.random() * 1e6)}`; +const escapeRe = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + +type Part = Record; + +const call = (id: string, name: string, args: Record, output: string): Part => ({ + type: 'tool_call', + tool_call: { id, name, args: JSON.stringify(args), output, progress: 1, type: 'tool_call' }, +}); + +/** Four finished calls in one batch: a single tool group with four rows. */ +const GROUP_CONTENT: Part[] = [ + call('g1', 'edit_file_mcp_filesystem', { path: 'src/config.ts' }, 'Edited src/config.ts'), + call('g2', 'run_command_mcp_shell', { command: 'npm test -- config' }, 'PASS config.test.ts'), + call('g3', 'run_command_mcp_shell', { command: 'npm test' }, 'Tests: 148 passed'), + call('g4', 'run_command_mcp_shell', { command: 'git diff --stat' }, '1 file changed'), + { type: 'text', text: 'Fold tree fixture done.' }, +]; + +/** A phase over two labeled batches: tool groups nested inside an activity phase. */ +const PHASE_CONTENT: Part[] = [ + call('p1', 'read_file_mcp_filesystem', { path: 'ci.yml' }, 'name: CI'), + call('p2', 'read_file_mcp_filesystem', { path: 'package.json' }, '{ "name": "app" }'), + call('p3', 'list_directory_mcp_filesystem', { path: 'src' }, 'index.ts'), + { + type: 'activity_label', + activity_label: 'Read the build config', + tool_call_ids: ['p1', 'p2', 'p3'], + status: 'ok', + }, + call('p4', 'run_command_mcp_shell', { command: 'npm test' }, 'PASS'), + call('p5', 'read_file_mcp_filesystem', { path: 'src/config.ts' }, 'export const PORT = 3080;'), + { + type: 'activity_label', + activity_label: 'Ran the test suite', + tool_call_ids: ['p4', 'p5'], + status: 'ok', + }, + { + type: 'activity_label', + activity_label: 'Investigated the build', + activity_label_type: 'phase', + activity_start_index: 0, + activity_end_index: 7, + activity_count: 5, + status: 'ok', + pending: false, + }, + { type: 'text', text: 'Fold tree fixture done.' }, +]; + +async function routeConversation(page: Page, content: Part[]): Promise { + const conversationId = unique('e2e-fold-tree'); + const now = new Date(0).toISOString(); + const message = { + messageId: `${conversationId}-msg`, + conversationId, + parentMessageId: NO_PARENT, + isCreatedByUser: false, + sender: 'Assistant', + endpoint: 'Mock Provider A', + model: 'mock-model-a', + text: '', + content, + createdAt: now, + updatedAt: now, + }; + const conversation = { + conversationId, + title: 'Fold tree', + endpoint: 'Mock Provider A', + endpointType: 'custom', + model: 'mock-model-a', + createdAt: now, + updatedAt: now, + }; + const convoIdRe = escapeRe(conversationId); + await page.route(new RegExp(`/api/convos/${convoIdRe}(?:\\?.*)?$`), (route: Route) => + route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(conversation), + }), + ); + await page.route(new RegExp(`/api/messages/${convoIdRe}(?:\\?.*)?$`), (route: Route) => + route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify([message]), + }), + ); + return conversationId; +} + +async function openConversation(page: Page, content: Part[]) { + const conversationId = await routeConversation(page, content); + await page.goto(`/c/${conversationId}`, { timeout: 30000 }); + await expect(messagesView(page).getByText('Fold tree fixture done.')).toBeVisible({ + timeout: 20000, + }); +} + +/** The panel of the tool group whose header reads `name`, opened if it is folded. */ +async function openGroup(page: Page, name: RegExp): Promise { + const header = messagesView(page).getByRole('button', { name }).first(); + await expect(header).toBeVisible(); + if ((await header.getAttribute('aria-expanded')) !== 'true') { + await header.click(); + } + await expect(header).toHaveAttribute('aria-expanded', 'true'); + return messagesView(page) + .getByTestId('tool-call-group-panel') + .filter({ has: page.getByRole('button', { name: /^Ran / }) }) + .last(); +} + +const railOf = (panel: Locator) => panel.locator('> div > [data-testid="fold-rail"]'); +const traceOf = (rail: Locator) => rail.locator('[data-fold-trace]'); +const branchesOf = (rail: Locator) => rail.locator('[data-testid="fold-rail-tree"] > g > path'); +const rowsOf = (panel: Locator) => panel.getByRole('button', { name: /^Ran / }); + +async function center(locator: Locator) { + const box = await locator.boundingBox(); + expect(box).not.toBeNull(); + return { x: box!.x + box!.width / 2, y: box!.y + box!.height / 2, box: box! }; +} + +/** The lit route ends in `row`: the bottom of its bounding box sits on the row glyph's center. */ +async function expectRouteEndsAt(rail: Locator, row: Locator) { + const trace = traceOf(rail); + await expect(trace).toHaveAttribute('data-lit', ''); + await expect + .poll(async () => { + const [path, glyph] = await Promise.all([ + trace.boundingBox(), + row.locator('.fold-glyph').first().boundingBox(), + ]); + return path == null || glyph == null + ? Infinity + : Math.abs(path.y + path.height - (glyph.y + glyph.height / 2)); + }) + .toBeLessThan(2); +} + +/** + * Records the lit path's shape on every animation frame while `act` runs, in the rail's + * own coordinates, alongside the branch points the rail drew (one radius above each + * row center). + */ +async function sampleRoute(rail: Locator, act: () => Promise, ms = 900) { + await rail.evaluate((element, duration) => { + const path = element.querySelector('[data-fold-trace]')!; + const samples: string[] = []; + (window as unknown as { __foldSamples: string[] }).__foldSamples = samples; + const until = performance.now() + duration; + const step = () => { + samples.push(path.getAttribute('d') ?? ''); + if (performance.now() < until) { + requestAnimationFrame(step); + } + }; + requestAnimationFrame(step); + }, ms); + await act(); + await rail.page().waitForTimeout(ms + 100); + return rail.evaluate((element) => ({ + samples: (window as unknown as { __foldSamples: string[] }).__foldSamples, + centers: (element.getAttribute('data-centers') ?? '').split(',').map(Number), + })); +} + +const tipOf = (d: string) => Number(/V ([\d.]+)/.exec(d)?.[1]); +const hasBranch = (d: string) => d.includes(' A '); + +test.describe('tool fold tree (pointer)', () => { + test.use(DESKTOP); + + test('an open tool fold draws a branch into each of its rows @scenario:open-tool-fold-branches-into-each-row', async ({ + page, + }) => { + await openConversation(page, GROUP_CONTENT); + const panel = await openGroup(page, /^Ran 4 actions/); + const rail = railOf(panel); + const rows = rowsOf(panel); + await expect(rows).toHaveCount(4); + /** The trunk, then one branch per row. */ + await expect(branchesOf(rail)).toHaveCount(5); + for (let i = 0; i < 4; i++) { + const [branch, glyph] = await Promise.all([ + branchesOf(rail) + .nth(i + 1) + .boundingBox(), + rows.nth(i).locator('.fold-glyph').first().boundingBox(), + ]); + expect(Math.abs(branch!.y + branch!.height - (glyph!.y + glyph!.height / 2))).toBeLessThan(2); + /** The branch stops short of the row's icon rather than running under it. */ + expect(branch!.x + branch!.width).toBeLessThanOrEqual(glyph!.x + 2); + } + /** The trunk hangs from the header glyph's axis. */ + const header = messagesView(page) + .getByRole('button', { name: /^Ran 4 actions/ }) + .first(); + const trunk = await center(branchesOf(rail).first()); + const headerGlyph = await center(header.locator('.fold-glyph').first()); + expect(Math.abs(trunk.x - headerGlyph.x)).toBeLessThan(2); + await expect(rail).toHaveAttribute('aria-hidden', 'true'); + await expect(rail).toHaveAttribute('tabindex', '-1'); + }); + + test('hovering a row lights the route from the header into that row @scenario:hovered-tool-row-lights-route-from-header', async ({ + page, + }) => { + await openConversation(page, GROUP_CONTENT); + const panel = await openGroup(page, /^Ran 4 actions/); + const rail = railOf(panel); + const rows = rowsOf(panel); + await expect(traceOf(rail)).not.toHaveAttribute('data-lit', ''); + await rows.nth(2).hover(); + await expect(rail).toHaveAttribute('data-fold-lit', '2'); + await expectRouteEndsAt(rail, rows.nth(2)); + /** Leaving the fold fades the route out. */ + await page.mouse.move(5, 5); + await expect(rail).not.toHaveAttribute('data-fold-lit'); + await expect(traceOf(rail)).not.toHaveAttribute('data-lit', ''); + await expect(traceOf(rail)).toHaveCSS('opacity', '0'); + }); + + test('a row in a nested fold lights every level into its own branch @scenario:nested-fold-row-lights-each-level', async ({ + page, + }) => { + await openConversation(page, PHASE_CONTENT); + const phaseHeader = messagesView(page).getByRole('button', { name: 'Investigated the build' }); + await phaseHeader.click(); + await expect(phaseHeader).toHaveAttribute('aria-expanded', 'true'); + const phaseRail = messagesView(page) + .getByTestId('activity-phase-panel') + .locator('> div > [data-testid="fold-rail"]'); + const groupHeader = messagesView(page).getByRole('button', { name: /^Ran the test suite/ }); + const panel = await openGroup(page, /^Ran the test suite/); + const groupRail = railOf(panel); + const row = rowsOf(panel).first(); + await row.hover(); + await expectRouteEndsAt(groupRail, row); + /** The phase lights its branch into the group's header, which leads on into the row. */ + await expectRouteEndsAt(phaseRail, groupHeader); + }); + + test('moving between rows travels the tree, never cutting across it @scenario:lit-route-travels-tree-between-rows', async ({ + page, + }) => { + await openConversation(page, GROUP_CONTENT); + const panel = await openGroup(page, /^Ran 4 actions/); + const rail = railOf(panel); + const rows = rowsOf(panel); + await rows.nth(2).hover(); + await expectRouteEndsAt(rail, rows.nth(2)); + + const { samples, centers } = await sampleRoute(rail, () => rows.nth(1).hover()); + const from = centers[2] - 8; + const to = centers[1] - 8; + const moving = samples.filter((d) => d !== ''); + expect(moving.length).toBeGreaterThan(3); + for (let i = 0; i < moving.length; i++) { + const tip = tipOf(moving[i]); + /** Only the trunk between the two rows: never above the new row, never via the top. */ + expect(tip).toBeGreaterThanOrEqual(to - 0.5); + expect(tip).toBeLessThanOrEqual(from + 0.5); + if (i > 0) { + expect(tip).toBeLessThanOrEqual(tipOf(moving[i - 1]) + 0.5); + } + /** A branch is drawn only from a branch point, so nothing leaves the tree. */ + if (hasBranch(moving[i])) { + expect(Math.min(Math.abs(tip - from), Math.abs(tip - to))).toBeLessThan(0.5); + } + } + expect(moving.some((d) => !hasBranch(d) && tipOf(d) > to + 1 && tipOf(d) < from - 1)).toBe( + true, + ); + await expectRouteEndsAt(rail, rows.nth(1)); + }); + + test('the route stays lit through a click that unfolds the hovered row @scenario:lit-route-stays-after-click-folds-rows', async ({ + page, + }) => { + await openConversation(page, GROUP_CONTENT); + const panel = await openGroup(page, /^Ran 4 actions/); + const rail = railOf(panel); + const rows = rowsOf(panel); + await rows.nth(1).hover(); + await expectRouteEndsAt(rail, rows.nth(1)); + /** Click without moving the pointer: the row opens and the rows below it move. */ + await page.mouse.down(); + await page.mouse.up(); + await expect(rows.nth(1)).toHaveAttribute('aria-expanded', 'true'); + await expect(rail).toHaveAttribute('data-fold-lit', '1'); + await expectRouteEndsAt(rail, rows.nth(1)); + /** And again to fold it: still lit, still ending in the same row. */ + await page.mouse.down(); + await page.mouse.up(); + await expect(rows.nth(1)).toHaveAttribute('aria-expanded', 'false'); + await expectRouteEndsAt(rail, rows.nth(1)); + }); + + test('with reduced motion the route is placed at once @scenario:reduced-motion-places-lit-route-at-once', async ({ + page, + }) => { + await page.emulateMedia({ reducedMotion: 'reduce' }); + await openConversation(page, GROUP_CONTENT); + const panel = await openGroup(page, /^Ran 4 actions/); + const rail = railOf(panel); + const rows = rowsOf(panel); + await rows.nth(0).hover(); + await expectRouteEndsAt(rail, rows.nth(0)); + const { samples } = await sampleRoute(rail, () => rows.nth(3).hover(), 500); + /** The shape for the first row, then the shape for the last row, and nothing between. */ + expect(new Set(samples.filter((d) => d !== '')).size).toBeLessThanOrEqual(2); + await expectRouteEndsAt(rail, rows.nth(3)); + }); + + test('a right-to-left locale mirrors the tree toward its rows @scenario:rtl-tool-tree-mirrors-branches', async ({ + page, + }) => { + await page.addInitScript(() => localStorage.setItem('lang', JSON.stringify('ar'))); + await openConversation(page, GROUP_CONTENT); + await expect(page.locator('html')).toHaveAttribute('dir', 'rtl', { timeout: 30000 }); + const panel = messagesView(page).getByTestId('tool-call-group-panel').last(); + /** The copy is Arabic, so the header is found by its place right above the panel. */ + const header = panel.locator('xpath=preceding-sibling::div[1]').getByRole('button').first(); + await header.click(); + await expect(header).toHaveAttribute('aria-expanded', 'true'); + const rail = railOf(panel); + const row = panel.locator('.fold-glyph').nth(1); + const branch = branchesOf(rail).nth(2); + const [path, glyph, trunk] = await Promise.all([ + branch.boundingBox(), + row.boundingBox(), + branchesOf(rail).first().boundingBox(), + ]); + /** The trunk sits to the row's right, and the branch runs leftward from it to the row. */ + expect(trunk!.x).toBeGreaterThan(glyph!.x + glyph!.width); + expect(path!.x).toBeGreaterThanOrEqual(glyph!.x + glyph!.width - 2); + expect(Math.abs(path!.x + path!.width - (trunk!.x + trunk!.width / 2))).toBeLessThan(2); + expect(Math.abs(path!.y + path!.height - (glyph!.y + glyph!.height / 2))).toBeLessThan(2); + }); +}); + +test.describe('tool fold tree (touch)', () => { + test.use({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }); + + test('tapping a row leaves the tree dark @scenario:touch-tap-leaves-tool-tree-dark', async ({ + page, + }) => { + await openConversation(page, GROUP_CONTENT); + const header = messagesView(page) + .getByRole('button', { name: /^Ran 4 actions/ }) + .first(); + await header.tap(); + await expect(header).toHaveAttribute('aria-expanded', 'true'); + const panel = messagesView(page).getByTestId('tool-call-group-panel').last(); + const rail = railOf(panel); + await expect(branchesOf(rail)).toHaveCount(5); + await rowsOf(panel).nth(1).tap(); + await expect(rowsOf(panel).nth(1)).toHaveAttribute('aria-expanded', 'true'); + await expect(rail).not.toHaveAttribute('data-fold-lit'); + await expect(traceOf(rail)).not.toHaveAttribute('data-lit', ''); + }); +});