Open History to see the last commit
1import { MessageId, RunId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 observeTimelineRun,
5 getAnchoredTurnMetrics,
6 getRowBottom,
7 readTimelinePosition,
8 rememberTimelinePosition,
9 timelineContentOverflowsViewport,
10} from "./timelineScrollAnchoring";
11
12function buildState({
13 positions,
14 sizes,
15 scroll = 0,
16 scrollLength = 700,
17}: {
18 readonly positions: readonly number[];
19 readonly sizes: readonly number[];
20 readonly scroll?: number;
21 readonly scrollLength?: number;
22}) {
23 return {
24 data: positions.map((_, index) => index),
25 scroll,
26 scrollLength,
27 positionAtIndex: (index: number) => positions[index],
28 sizeAtIndex: (index: number) => sizes[index],
29 };
30}
31
32describe("timelineContentOverflowsViewport", () => {
33 const inset = { composerInset: 100, anchorOffset: 24 };
34
35 it("reports overflow from the last row, not the inset spacer", () => {
36 const fits = buildState({ positions: [0, 200], sizes: [200, 300], scrollLength: 700 });
37 expect(timelineContentOverflowsViewport(fits, inset)).toBe(false);
38
39 const overflows = buildState({ positions: [0, 200], sizes: [200, 400], scrollLength: 700 });
40 expect(timelineContentOverflowsViewport(overflows, inset)).toBe(true);
41 });
42
43 it("treats an empty or unmeasured list as fitting", () => {
44 expect(timelineContentOverflowsViewport(undefined, inset)).toBe(false);
45 expect(
46 timelineContentOverflowsViewport(
47 buildState({ positions: [0, 200], sizes: [200, 400], scrollLength: 0 }),
48 inset,
49 ),
50 ).toBe(false);
51 expect(timelineContentOverflowsViewport(buildState({ positions: [], sizes: [] }), inset)).toBe(
52 false,
53 );
54 expect(
55 timelineContentOverflowsViewport(
56 buildState({ positions: [0, 200], sizes: [200, Number.NaN] }),
57 inset,
58 ),
59 ).toBe(false);
60 expect(
61 timelineContentOverflowsViewport(
62 buildState({ positions: [0, Number.NaN], sizes: [200] }),
63 inset,
64 ),
65 ).toBe(false);
66 });
67
68 it.each([
69 { measurement: "missing", sizes: [200] },
70 { measurement: "NaN", sizes: [200, Number.NaN] },
71 ])(
72 "reports overflow with a $measurement tail size without inventing anchor metrics",
73 ({ sizes }) => {
74 const state = buildState({ positions: [0, 900], sizes });
75 expect(timelineContentOverflowsViewport(state, inset)).toBe(true);
76 expect(getRowBottom(state, 1)).toBeNull();
77 expect(
78 getAnchoredTurnMetrics({
79 state,
80 anchorIndex: 0,
81 composerOverlayHeight: 100,
82 anchorOffset: 24,
83 }),
84 ).toBeNull();
85 },
86 );
87});
88
89describe("timeline scroll anchoring", () => {
90 it("measures row bottoms from LegendList row position and size", () => {
91 const state = buildState({
92 positions: [0, 120],
93 sizes: [80, 40],
94 });
95
96 expect(getRowBottom(state, 1)).toBe(160);
97 });
98
99 it("treats the active turn as fitting when it fits above the composer", () => {
100 const state = buildState({
101 positions: [0, 300, 460],
102 sizes: [240, 80, 140],
103 scrollLength: 760,
104 });
105
106 const metrics = getAnchoredTurnMetrics({
107 state,
108 anchorIndex: 1,
109 composerOverlayHeight: 180,
110 anchorOffset: 16,
111 });
112
113 expect(metrics?.turnHeight).toBe(300);
114 expect(metrics?.usableViewportHeight).toBe(564);
115 expect(metrics?.overflowsUsableViewport).toBe(false);
116 expect(metrics?.targetScrollToRevealEnd).toBe(36);
117 expect(metrics?.scrollDeltaToRevealEnd).toBe(36);
118 });
119
120 it("targets the real row end instead of any temporary reserved tail", () => {
121 const state = buildState({
122 positions: [0, 1720, 1880],
123 sizes: [1600, 80, 120],
124 scroll: 1900,
125 scrollLength: 760,
126 });
127
128 const metrics = getAnchoredTurnMetrics({
129 state,
130 anchorIndex: 1,
131 composerOverlayHeight: 180,
132 anchorOffset: 16,
133 });
134
135 expect(metrics?.lastBottom).toBe(2000);
136 expect(metrics?.targetScrollToRevealEnd).toBe(1436);
137 expect(metrics?.scrollDeltaToRevealEnd).toBe(0);
138 });
139
140 it("reports overflow only for the current anchored turn", () => {
141 const state = buildState({
142 positions: [0, 900, 1180],
143 sizes: [800, 220, 300],
144 scroll: 900,
145 scrollLength: 760,
146 });
147
148 const metrics = getAnchoredTurnMetrics({
149 state,
150 anchorIndex: 1,
151 composerOverlayHeight: 180,
152 anchorOffset: 16,
153 });
154
155 expect(metrics?.turnHeight).toBe(580);
156 expect(metrics?.usableViewportHeight).toBe(564);
157 expect(metrics?.overflowsUsableViewport).toBe(true);
158 });
159
160 it("returns the minimal positive scroll delta needed to reveal the turn end", () => {
161 const state = buildState({
162 positions: [0, 900, 1180],
163 sizes: [800, 220, 360],
164 scroll: 900,
165 scrollLength: 760,
166 });
167
168 const metrics = getAnchoredTurnMetrics({
169 state,
170 anchorIndex: 1,
171 composerOverlayHeight: 180,
172 anchorOffset: 16,
173 });
174
175 expect(metrics?.lastBottom).toBe(1540);
176 expect(metrics?.visibleUsableBottom).toBe(1464);
177 expect(metrics?.scrollDeltaToRevealEnd).toBe(76);
178 });
179
180 it("subtracts composer height from usable viewport height", () => {
181 const state = buildState({
182 positions: [0, 300],
183 sizes: [120, 470],
184 scrollLength: 700,
185 });
186
187 const withoutComposer = getAnchoredTurnMetrics({
188 state,
189 anchorIndex: 1,
190 composerOverlayHeight: 0,
191 anchorOffset: 16,
192 });
193 const withComposer = getAnchoredTurnMetrics({
194 state,
195 anchorIndex: 1,
196 composerOverlayHeight: 220,
197 anchorOffset: 16,
198 });
199
200 expect(withoutComposer?.overflowsUsableViewport).toBe(false);
201 expect(withComposer?.overflowsUsableViewport).toBe(true);
202 });
203});
204
205describe("observeTimelineRun", () => {
206 const existing = {
207 threadKey: "environment:thread",
208 hydrated: true,
209 runId: RunId.make("existing-run"),
210 queued: false,
211 messageId: MessageId.make("existing-message"),
212 };
213 const next = {
214 ...existing,
215 runId: RunId.make("new-run"),
216 messageId: MessageId.make("new-message"),
217 };
218
219 it("opens an already hydrated thread at the end instead of framing its existing run", () => {
220 const opened = observeTimelineRun(null, existing);
221 expect(opened.anchorMessageId).toBeNull();
222 expect(observeTimelineRun(opened.observation, existing).anchorMessageId).toBeNull();
223 expect(observeTimelineRun(opened.observation, next).anchorMessageId).toBe(next.messageId);
224 });
225
226 it("does not mistake delayed hydration for a newly started turn", () => {
227 const loading = { ...existing, hydrated: false, runId: null, messageId: null };
228 const opened = observeTimelineRun(null, loading);
229 const stillLoading = observeTimelineRun(opened.observation, loading);
230 const hydrated = observeTimelineRun(stillLoading.observation, existing);
231 expect(hydrated.anchorMessageId).toBeNull();
232 expect(observeTimelineRun(hydrated.observation, next).anchorMessageId).toBe(next.messageId);
233 });
234
235 it("keeps the end position while a stale cached run is replaced during synchronization", () => {
236 const cached = observeTimelineRun(null, { ...existing, hydrated: false });
237 const synchronizing = observeTimelineRun(cached.observation, { ...next, hydrated: false });
238 expect(synchronizing.anchorMessageId).toBeNull();
239 const live = observeTimelineRun(synchronizing.observation, next);
240 expect(live.anchorMessageId).toBeNull();
241 expect(
242 observeTimelineRun(live.observation, {
243 ...next,
244 runId: RunId.make("later-run"),
245 messageId: MessageId.make("later-message"),
246 }).anchorMessageId,
247 ).toBe("later-message");
248 });
249
250 it("does not anchor when an existing run's user message arrives after its run", () => {
251 const opened = observeTimelineRun(null, { ...existing, messageId: null });
252 expect(observeTimelineRun(opened.observation, existing).anchorMessageId).toBeNull();
253 });
254
255 it("waits for a new run's user message and frames it only once", () => {
256 const opened = observeTimelineRun(null, existing);
257 const waiting = observeTimelineRun(opened.observation, { ...next, messageId: null });
258 expect(waiting.anchorMessageId).toBeNull();
259 const ready = observeTimelineRun(waiting.observation, next);
260 expect(ready.anchorMessageId).toBe(next.messageId);
261 expect(observeTimelineRun(ready.observation, next).anchorMessageId).toBeNull();
262 });
263
264 it("establishes a separate baseline when the same thread ID belongs to another environment", () => {
265 const opened = observeTimelineRun(null, existing);
266 expect(
267 observeTimelineRun(opened.observation, { ...next, threadKey: "other:thread" })
268 .anchorMessageId,
269 ).toBeNull();
270 });
271
272 it("does not reset the baseline during a temporary loss of projection data", () => {
273 const opened = observeTimelineRun(null, existing);
274 const reconnecting = observeTimelineRun(opened.observation, {
275 ...existing,
276 hydrated: false,
277 runId: null,
278 });
279 expect(observeTimelineRun(reconnecting.observation, existing).anchorMessageId).toBeNull();
280 });
281
282 it("frames the first new run in an initially empty thread", () => {
283 const opened = observeTimelineRun(null, { ...existing, runId: null, messageId: null });
284 expect(observeTimelineRun(opened.observation, next).anchorMessageId).toBe(next.messageId);
285 });
286
287 it("waits for a queued run to start before framing it", () => {
288 const opened = observeTimelineRun(null, { ...existing, queued: true });
289 expect(opened.anchorMessageId).toBeNull();
290 expect(observeTimelineRun(opened.observation, existing).anchorMessageId).toBe(
291 existing.messageId,
292 );
293 });
294});
295
296describe("remembered timeline positions", () => {
297 it("keeps reading positions and end-follow independent across threads and environments", () => {
298 const reading = { rowId: "message-4", offsetWithinRow: 32, scrollOffset: 932, atEnd: false };
299 const following = { rowId: "message-9", offsetWithinRow: 10, scrollOffset: 2010, atEnd: true };
300 rememberTimelinePosition("scroll-test-a:thread-1", reading, false);
301 rememberTimelinePosition("scroll-test-a:thread-2", following, true);
302 rememberTimelinePosition("scroll-test-b:thread-1", following, true);
303 expect(readTimelinePosition("scroll-test-a:thread-1")).toEqual(reading);
304 expect(readTimelinePosition("scroll-test-a:thread-2")).toEqual(following);
305 expect(readTimelinePosition("scroll-test-b:thread-1")).toEqual(following);
306 expect(readTimelinePosition("scroll-test-a:unvisited")).toBeUndefined();
307 rememberTimelinePosition("scroll-test-a:thread-1", following, true);
308 expect(readTimelinePosition("scroll-test-a:thread-1")).toEqual(following);
309 });
310});