Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions web/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export function userMessageDock(messages: { id: string; role: string; text: stri
* Legacy `onScroll()` decision: streamed content pushes the viewport up without
* the user leaving, so an upward movement marks `away`; returning near the
* bottom resumes follow. A clamp that lands exactly at the bottom is the
* browser adjusting to shrunk content, not the user scrolling up; once away,
* only downward movement back into that window resumes follow.
* browser adjusting to shrunk content, not the user scrolling up; when that
* clamp was not deliberate input, it can also resume follow.
*/
export function chatScrollState(element: { scrollHeight: number; scrollTop: number; clientHeight: number } | null, previousTop: number, away: boolean, deliberateUp = false, threshold = CHAT_SCROLL_THRESHOLD) {
if (!element) return { away, showScrollButton: false };
Expand All @@ -105,7 +105,8 @@ export function chatScrollState(element: { scrollHeight: number; scrollTop: numb
}

/** Message updates follow the same state as scroll tracking: once a deliberate
* upward scroll marks the reader away, only returning near the bottom resumes follow. */
* upward scroll marks the reader away, follow resumes after returning near the bottom
* or after a browser clamp lands exactly at the bottom. */
export function chatShouldFollowMessages(away: boolean) {
return !away;
}
Expand Down
42 changes: 36 additions & 6 deletions web/tests/chat-scroll.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -374,34 +374,64 @@ test('sparse measured outliers do not inflate unmounted row estimates', async ()
test('virtual layout corrections preserve the overlapping anchor row', async () => {
const scroll = document.createElement('div');
Object.defineProperties(scroll, {
scrollTop: { configurable: true, writable: true, value: 100 },
scrollTop: { configurable: true, writable: true, value: 3200 },
clientHeight: { configurable: true, value: 600 },
});
scroll.getBoundingClientRect = () => ({ top: 0, height: 600, bottom: 600, left: 0, right: 800, width: 800, x: 0, y: 0, toJSON: () => ({}) });
const scrollRef = { current: scroll };
const corrections = [];
const view = await renderMessages(longSession(), { scrollRef, onCorrectScroll: delta => corrections.push(delta) });
let applyCorrections = false;
const correct = delta => {
corrections.push(delta);
if (applyCorrections) scroll.scrollTop += delta;
};
const view = await renderMessages(longSession(), { scrollRef, onCorrectScroll: correct });
try {
const positions = new Map();
for (const node of view.container.querySelectorAll('.virtual-message-item')) {
const id = node.querySelector('[data-message-id]').dataset.messageId;
const index = Number(id.slice(1));
positions.set(id, { top: index * 320, height: 320 });
}
patchVirtualLayout(view.container, positions);
const patchDynamicLayout = () => {
for (const node of view.container.querySelectorAll('.virtual-message-item')) {
const id = node.querySelector('[data-message-id]').dataset.messageId;
const position = positions.get(id) || { top: 0, height: 320 };
node.getBoundingClientRect = () => {
const top = position.top - scroll.scrollTop;
return { top, height: position.height, bottom: top + position.height, left: 0, right: 600, width: 600, x: 0, y: top, toJSON: () => ({}) };
};
}
};
patchDynamicLayout();
await measureVirtualMessages();
const originalAnchor = [...view.container.querySelectorAll('.virtual-message-item')]
.map(node => ({ id: node.dataset.virtualMessageId, rect: node.getBoundingClientRect() }))
.find(row => row.rect.bottom > 0);
assert.ok(originalAnchor, 'the initial viewport has an anchor row');
corrections.length = 0;
applyCorrections = true;

for (const [id, position] of positions) positions.set(id, { ...position, top: position.top + 500 });
patchVirtualLayout(view.container, positions);
patchDynamicLayout();
const rows = [...view.container.querySelectorAll('.virtual-message-item')];
const last = rows.at(-1);
const rect = last.getBoundingClientRect();
last.getBoundingClientRect = () => ({ ...rect, height: 321, bottom: rect.top + 321 });
const lastId = last.dataset.virtualMessageId;
positions.set(lastId, { ...positions.get(lastId), height: 321 });
patchDynamicLayout();
const resize = ResizeObserverStub.instances.at(-1);
await act(async () => resize.callback([{ target: last }]));

assert.deepEqual(corrections, [500]);
assert.equal(scroll.scrollTop, 3700, 'the correction is applied to the scroller');
const anchorNode = view.container.querySelector(`[data-virtual-message-id="${originalAnchor.id}"]`);
assert.ok(anchorNode, 'the original anchor row is still mounted');
const anchorRect = anchorNode.getBoundingClientRect();
assert.equal(anchorRect.top, originalAnchor.rect.top, 'the original anchor returns to its viewport position');
assert.ok(anchorRect.bottom > 0 && anchorRect.top < scroll.clientHeight, 'the original anchor remains visible after correction');
const correctionCount = corrections.length;
await act(async () => scroll.dispatchEvent(new dom.window.Event('scroll')));
assert.equal(corrections.length, correctionCount, 'the resulting real scroll event does not trigger another correction');
} finally { await view.close(); }
});

Expand Down
7 changes: 5 additions & 2 deletions web/tests/conversation-performance.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ const blockUrl = dataUrl(`
}
`);
const { renders } = await import(blockUrl);
const chatUrl = dataUrl(transpile(await readFile(new URL('../src/chat.ts', import.meta.url), 'utf8')));
const chatWindowOutput = transpile(await readFile(new URL('../src/chat-window.ts', import.meta.url), 'utf8'))
.replaceAll(`'./chat'`, JSON.stringify(chatUrl)).replaceAll(`"./chat"`, JSON.stringify(chatUrl));
const specifiers = {
react: reactUrl,
'react-i18next': reactI18nextStubUrl,
'react/jsx-runtime': import.meta.resolve('react/jsx-runtime'),
'lucide-react': import.meta.resolve('lucide-react'),
'../chat': dataUrl(transpile(await readFile(new URL('../src/chat.ts', import.meta.url), 'utf8'))),
'../chat-window': dataUrl(transpile(await readFile(new URL('../src/chat-window.ts', import.meta.url), 'utf8'))),
'../chat': chatUrl,
'../chat-window': dataUrl(chatWindowOutput),
'../file-stats': dataUrl(transpile(await readFile(new URL('../src/file-stats.ts', import.meta.url), 'utf8'))),
'../web-sources': dataUrl(transpile(await readFile(new URL('../src/web-sources.ts', import.meta.url), 'utf8'))),
'../lib': dataUrl('export const getSessionMessages = session => session.messages;'),
Expand Down
Loading