Open History to see the last commit
1import { MessageId, RunId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { observeTimelineRun, getAnchoredTurnMetrics } from "./timelineScrollAnchoring";
4
5function buildState({
6 positions,
7 sizes,
8 scroll = 0,
9 scrollLength = 700,
10}: {
11 readonly positions: readonly number[];
12 readonly sizes: readonly number[];
13 readonly scroll?: number;
14 readonly scrollLength?: number;
15}) {
16 return {
17 data: positions.map((_, index) => index),
18 scroll,
19 scrollLength,
20 positionAtIndex: (index: number) => positions[index],
21 sizeAtIndex: (index: number) => sizes[index],
22 };
23}
24
25describe("timeline scroll anchoring", () => {
26 it("targets the real row end instead of any temporary reserved tail", () => {
27 const state = buildState({
28 positions: [0, 1720, 1880],
29 sizes: [1600, 80, 120],
30 scroll: 1900,
31 scrollLength: 760,
32 });
33
34 const metrics = getAnchoredTurnMetrics({
35 state,
36 anchorIndex: 1,
37 composerOverlayHeight: 180,
38 anchorOffset: 16,
39 });
40
41 expect(metrics?.lastBottom).toBe(2000);
42 expect(metrics?.targetScrollToRevealEnd).toBe(1436);
43 expect(metrics?.scrollDeltaToRevealEnd).toBe(0);
44 });
45});
46
47describe("observeTimelineRun", () => {
48 const existing = {
49 threadKey: "environment:thread",
50 hydrated: true,
51 runId: RunId.make("existing-run"),
52 queued: false,
53 messageId: MessageId.make("existing-message"),
54 };
55 const next = {
56 ...existing,
57 runId: RunId.make("new-run"),
58 messageId: MessageId.make("new-message"),
59 };
60
61 it("does not mistake delayed hydration for a newly started turn", () => {
62 const loading = { ...existing, hydrated: false, runId: null, messageId: null };
63 const opened = observeTimelineRun(null, loading);
64 const stillLoading = observeTimelineRun(opened.observation, loading);
65 const hydrated = observeTimelineRun(stillLoading.observation, existing);
66 expect(hydrated.anchorMessageId).toBeNull();
67 expect(observeTimelineRun(hydrated.observation, next).anchorMessageId).toBe(next.messageId);
68 });
69
70 it("keeps the end position while a stale cached run is replaced during synchronization", () => {
71 const cached = observeTimelineRun(null, { ...existing, hydrated: false });
72 const synchronizing = observeTimelineRun(cached.observation, { ...next, hydrated: false });
73 expect(synchronizing.anchorMessageId).toBeNull();
74 const live = observeTimelineRun(synchronizing.observation, next);
75 expect(live.anchorMessageId).toBeNull();
76 expect(
77 observeTimelineRun(live.observation, {
78 ...next,
79 runId: RunId.make("later-run"),
80 messageId: MessageId.make("later-message"),
81 }).anchorMessageId,
82 ).toBe("later-message");
83 });
84
85 it("waits for a new run's user message and frames it only once", () => {
86 const opened = observeTimelineRun(null, existing);
87 const waiting = observeTimelineRun(opened.observation, { ...next, messageId: null });
88 expect(waiting.anchorMessageId).toBeNull();
89 const ready = observeTimelineRun(waiting.observation, next);
90 expect(ready.anchorMessageId).toBe(next.messageId);
91 expect(observeTimelineRun(ready.observation, next).anchorMessageId).toBeNull();
92 });
93
94 it("does not reset the baseline during a temporary loss of projection data", () => {
95 const opened = observeTimelineRun(null, existing);
96 const reconnecting = observeTimelineRun(opened.observation, {
97 ...existing,
98 hydrated: false,
99 runId: null,
100 });
101 expect(observeTimelineRun(reconnecting.observation, existing).anchorMessageId).toBeNull();
102 });
103
104 it("waits for a queued run to start before framing it", () => {
105 const opened = observeTimelineRun(null, { ...existing, queued: true });
106 expect(opened.anchorMessageId).toBeNull();
107 expect(observeTimelineRun(opened.observation, existing).anchorMessageId).toBe(
108 existing.messageId,
109 );
110 });
111});