Skip to content

Commit f714602

Browse files
test(web): cover bounded anchor wait in thread position restore
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent db0a3a6 commit f714602

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { create, type ReactTestRenderer } from "react-test-renderer";
1212
import { beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1313
import type { LegendListRef, MaintainScrollAtEndOptions } from "@legendapp/list/react";
1414
import { shouldUseRestingComposerLayout } from "../composerFooterLayout";
15+
import { rememberTimelinePosition } from "./timelineScrollAnchoring";
1516
import { useComposerFocusState } from "./useComposerFocusState";
1617

1718
vi.mock("@legendapp/list/react", async () => {
@@ -998,6 +999,102 @@ describe("MessagesTimeline", () => {
998999
}
9991000
});
10001001

1002+
it("waits for the saved anchor's rows during a position restore, then falls back at its deadline", async () => {
1003+
vi.useFakeTimers();
1004+
const frames = new Map<number, FrameRequestCallback>();
1005+
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
1006+
const frame = frames.size + 1;
1007+
frames.set(frame, callback);
1008+
return frame;
1009+
});
1010+
vi.stubGlobal("cancelAnimationFrame", (frame: number) => frames.delete(frame));
1011+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1012+
const createListRef = (calls: {
1013+
scrollToIndex: ReturnType<typeof vi.fn>;
1014+
scrollToOffset: ReturnType<typeof vi.fn>;
1015+
scrollToEnd: ReturnType<typeof vi.fn>;
1016+
}) => {
1017+
const ref = createRef<LegendListRef | null>();
1018+
ref.current = {
1019+
getState: () => ({ indexByKey: () => undefined }),
1020+
getScrollableNode: () => null,
1021+
scrollToIndex: calls.scrollToIndex,
1022+
scrollToOffset: calls.scrollToOffset,
1023+
scrollToEnd: calls.scrollToEnd,
1024+
} as unknown as LegendListRef;
1025+
return ref;
1026+
};
1027+
const savedPosition = {
1028+
rowId: "entry-anchor",
1029+
offsetWithinRow: 12,
1030+
scrollOffset: 420,
1031+
atEnd: false,
1032+
};
1033+
const staleEntries = [buildUserTimelineEntry("Another thread's rows")];
1034+
const anchoredEntries = [
1035+
buildUserTimelineEntry("Earlier message"),
1036+
{ ...buildUserTimelineEntry("Saved anchor"), id: "entry-anchor" },
1037+
];
1038+
let renderer: ReactTestRenderer | undefined;
1039+
try {
1040+
// Switching back paints rows before the saved anchor exists among them.
1041+
// The restore must not clamp a fallback scroll against those rows.
1042+
rememberTimelinePosition("environment-local:thread-restore-wait", savedPosition);
1043+
const waitCalls = {
1044+
scrollToIndex: vi.fn(() => Promise.resolve()),
1045+
scrollToOffset: vi.fn(() => Promise.resolve()),
1046+
scrollToEnd: vi.fn(() => Promise.resolve()),
1047+
};
1048+
const waitProps = {
1049+
...buildProps(),
1050+
routeThreadKey: "environment-local:thread-restore-wait",
1051+
listRef: createListRef(waitCalls),
1052+
};
1053+
act(() => {
1054+
renderer = create(<MessagesTimeline {...waitProps} timelineEntries={staleEntries} />);
1055+
});
1056+
expect(waitCalls.scrollToIndex).not.toHaveBeenCalled();
1057+
expect(waitCalls.scrollToOffset).not.toHaveBeenCalled();
1058+
expect(waitCalls.scrollToEnd).not.toHaveBeenCalled();
1059+
1060+
// Once rows carrying the anchor arrive, the restore targets the anchor.
1061+
act(() => {
1062+
renderer!.update(<MessagesTimeline {...waitProps} timelineEntries={anchoredEntries} />);
1063+
});
1064+
expect(waitCalls.scrollToIndex).toHaveBeenCalledWith(
1065+
expect.objectContaining({ index: 1, animated: false, viewPosition: 0, viewOffset: -12 }),
1066+
);
1067+
expect(waitCalls.scrollToOffset).not.toHaveBeenCalled();
1068+
act(() => renderer?.unmount());
1069+
renderer = undefined;
1070+
1071+
// If the anchor never appears, the bounded wait ends and the restore
1072+
// completes against the saved offset instead of hanging.
1073+
rememberTimelinePosition("environment-local:thread-restore-fallback", savedPosition);
1074+
const fallbackCalls = {
1075+
scrollToIndex: vi.fn(() => Promise.resolve()),
1076+
scrollToOffset: vi.fn(() => Promise.resolve()),
1077+
scrollToEnd: vi.fn(() => Promise.resolve()),
1078+
};
1079+
const fallbackProps = {
1080+
...buildProps(),
1081+
routeThreadKey: "environment-local:thread-restore-fallback",
1082+
listRef: createListRef(fallbackCalls),
1083+
};
1084+
act(() => {
1085+
renderer = create(<MessagesTimeline {...fallbackProps} timelineEntries={staleEntries} />);
1086+
});
1087+
expect(fallbackCalls.scrollToOffset).not.toHaveBeenCalled();
1088+
await act(() => vi.advanceTimersByTimeAsync(2_100));
1089+
expect(fallbackCalls.scrollToOffset).toHaveBeenCalledWith({ offset: 420, animated: false });
1090+
expect(fallbackCalls.scrollToIndex).not.toHaveBeenCalled();
1091+
} finally {
1092+
act(() => renderer?.unmount());
1093+
vi.useRealTimers();
1094+
vi.unstubAllGlobals();
1095+
}
1096+
});
1097+
10011098
it("keeps reserved end space when tool work starts while reading history", () => {
10021099
const turnId = TurnId.make("turn-with-active-tool");
10031100
const firstEntry = buildUserTimelineEntry("Run the command.");

0 commit comments

Comments
 (0)