Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-feed-live-follow.test.ts

312 lines9.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 resolveThreadFeedLiveFollow,
5 resolveThreadFeedReflowAnchor,
6 resolveThreadFeedReflowOffset,
7 resolveThreadFeedReflowScroll,
8 resolveThreadFeedSubmissionAnchor,
9 resolveThreadWorkGroupInitialScroll,
10 shouldFollowThreadWorkGroupAppend,
11} from "./thread-feed-live-follow";
12​
13describe("tool-group scroll restoration", () => {
14 const position = {
15 rowId: "read-output",
16 offsetWithinRow: 80,
17 scrollOffset: 600,
18 contentHeight: 1_000,
19 };
20​
21 it("restores the visible row and its detail offset rather than stale absolute pixels", () => {
22 const before = [{ id: "first" }, { id: "read-output" }, { id: "last" }];
23 const after = [{ id: "older" }, ...before];
24 expect(resolveThreadWorkGroupInitialScroll(before, position)).toEqual({
25 index: 1,
26 viewOffset: -80,
27 });
28 expect(resolveThreadWorkGroupInitialScroll(after, position)).toEqual({
29 index: 2,
30 viewOffset: -80,
31 });
32 });
33});
34​
35describe("tool-group append following", () => {
36 const previousRows = Array.from({ length: 10 }, (_, index) => ({ id: `call-${index}` }));
37 const appendedRows = [...previousRows, { id: "new-call" }];
38 const atEnd = {
39 previousRows,
40 rows: appendedRows,
41 previousContentHeight: 289,
42 contentHeight: 318,
43 viewportHeight: 256,
44 scrollOffset: 33,
45 detailsChanged: false,
46 userScrolling: false,
47 };
48​
49 it("follows a new call when the reader was at the end", () => {
50 expect(shouldFollowThreadWorkGroupAppend(atEnd)).toBe(true);
51 });
52​
53 it("follows the first overflowing append as a short group reaches its height cap", () => {
54 expect(
55 shouldFollowThreadWorkGroupAppend({
56 ...atEnd,
57 previousRows: previousRows.slice(0, 8),
58 rows: previousRows.slice(0, 9),
59 previousContentHeight: 231,
60 contentHeight: 260,
61 viewportHeight: 231,
62 scrollOffset: 0,
63 }),
64 ).toBe(true);
65 });
66​
67 it.each([
68 { name: "reading earlier calls", changes: { scrollOffset: 20 } },
69 { name: "streaming a result", changes: { rows: previousRows, contentHeight: 600 } },
70 {
71 name: "replacing a call while appending",
72 changes: { rows: [{ id: "replacement" }, ...appendedRows.slice(1)] },
73 },
74 ])("does not steal the reader's position when $name", ({ changes }) => {
75 expect(shouldFollowThreadWorkGroupAppend({ ...atEnd, ...changes })).toBe(false);
76 });
77});
78​
79describe("resolveThreadFeedSubmissionAnchor", () => {
80 it("keeps an offline first message at the end", () => {
81 expect(
82 resolveThreadFeedSubmissionAnchor({
83 currentAnchorMessageId: null,
84 submittedMessageId: "offline-first",
85 hasStartedTurn: false,
86 hasUserMessage: false,
87 queuedMessageCount: 0,
88 deliveryDeferred: true,
89 }),
90 ).toBeNull();
91 });
92​
93 it("anchors the first user message in a thread", () => {
94 expect(
95 resolveThreadFeedSubmissionAnchor({
96 currentAnchorMessageId: null,
97 submittedMessageId: "first-message",
98 hasStartedTurn: false,
99 hasUserMessage: false,
100 deliveryDeferred: false,
101 queuedMessageCount: 0,
102 }),
103 ).toBe("first-message");
104 });
105​
106 it("preserves the first-message anchor when another message is queued", () => {
107 expect(
108 resolveThreadFeedSubmissionAnchor({
109 currentAnchorMessageId: "first-message",
110 submittedMessageId: "second-message",
111 hasStartedTurn: false,
112 hasUserMessage: false,
113 deliveryDeferred: false,
114 queuedMessageCount: 1,
115 }),
116 ).toBe("first-message");
117 });
118​
119 it("does not anchor a follow-up after a user message appears", () => {
120 expect(
121 resolveThreadFeedSubmissionAnchor({
122 currentAnchorMessageId: "first-message",
123 submittedMessageId: "second-message",
124 hasStartedTurn: false,
125 hasUserMessage: true,
126 deliveryDeferred: false,
127 queuedMessageCount: 0,
128 }),
129 ).toBeNull();
130 });
131​
132 // regression: #7897 stop pushing follow-up messages to the top
133 it("does not anchor a thread that has already started a turn", () => {
134 expect(
135 resolveThreadFeedSubmissionAnchor({
136 currentAnchorMessageId: null,
137 submittedMessageId: "second-message",
138 hasStartedTurn: true,
139 hasUserMessage: false,
140 deliveryDeferred: false,
141 queuedMessageCount: 0,
142 }),
143 ).toBeNull();
144 });
145});
146​
147describe("resolveThreadFeedLiveFollow", () => {
148 // regression: #5986 stabilize thread composer and interactions
149 it("pauses immediately when the user starts scrolling", () => {
150 expect(resolveThreadFeedLiveFollow(true, { type: "user-scroll-begin" })).toBe(false);
151 });
152​
153 it("does not mistake programmatic layout compensation for a user scroll", () => {
154 expect(
155 resolveThreadFeedLiveFollow(true, {
156 type: "scroll",
157 isAtEnd: false,
158 userScrollSessionActive: false,
159 }),
160 ).toBe(true);
161 });
162​
163 it("does not re-arm at the end while a user scroll session is active", () => {
164 expect(
165 resolveThreadFeedLiveFollow(false, {
166 type: "scroll",
167 isAtEnd: true,
168 userScrollSessionActive: true,
169 }),
170 ).toBe(false);
171 });
172​
173 it.each([
174 { isAtEnd: false, userScrollSessionActive: false, expected: false },
175 { isAtEnd: true, userScrollSessionActive: false, expected: true },
176 { isAtEnd: false, userScrollSessionActive: true, expected: false },
177 { isAtEnd: true, userScrollSessionActive: true, expected: false },
178 ])("reconciles follow after a disclosure settles: %j", ({ expected, ...state }) => {
179 expect(resolveThreadFeedLiveFollow(!expected, { type: "disclosure-settled", ...state })).toBe(
180 expected,
181 );
182 });
183​
184 it("re-arms at the actual end only after the user scroll session ends", () => {
185 expect(
186 resolveThreadFeedLiveFollow(false, {
187 type: "user-scroll-end",
188 isAtEnd: true,
189 userScrollSessionActive: true,
190 }),
191 ).toBe(true);
192 expect(
193 resolveThreadFeedLiveFollow(false, {
194 type: "user-scroll-end",
195 isAtEnd: false,
196 userScrollSessionActive: true,
197 }),
198 ).toBe(false);
199 });
200});
201​
202describe("width reflow anchors", () => {
203 const layout = { rowsTop: 50, headerInset: 100 };
204 const feedState = (
205 rows: ReadonlyArray<{ readonly id: string; readonly size: number }>,
206 scroll: number,
207 isWithinMaintainScrollAtEndThreshold = false,
208 ) => {
209 const positions = rows.map((_, index) =>
210 rows.slice(0, index).reduce((top, row) => top + row.size, 0),
211 );
212 return {
213 data: rows,
214 scroll,
215 isWithinMaintainScrollAtEndThreshold,
216 indexByKey: (key: string) => {
217 const index = rows.findIndex((row) => row.id === key);
218 return index < 0 ? undefined : index;
219 },
220 positionAtIndex: (index: number) => positions[index],
221 sizes: new Map(rows.map((row) => [row.id, row.size])),
222 };
223 };
224 const wide = [
225 { id: "question", size: 400 },
226 { id: "reply", size: 2_000 },
227 { id: "follow-up", size: 300 },
228 ];
229 // The line under the header sits 1,200px, 60%, into the reply.
230 const readingReply = feedState(wide, 1_550);
231​
232 it("keeps the end for a feed that is following it", () => {
233 expect(resolveThreadFeedReflowAnchor(feedState(wide, 1_550, true), layout, true)).toBe("end");
234 });
235​
236 it("keeps the reader's row after a status-bar scroll left follow on far from the end", () => {
237 expect(resolveThreadFeedReflowAnchor(readingReply, layout, true)).toEqual({
238 key: "reply",
239 offsetWithinRow: 1_200,
240 size: 2_000,
241 });
242 });
243​
244 it.each([
245 { change: "narrows", replySize: 4_000 },
246 { change: "widens", replySize: 1_000 },
247 ])("keeps the reader 60% into a reply when the feed $change", ({ replySize }) => {
248 const anchor = resolveThreadFeedReflowAnchor(readingReply, layout, false);
249 if (anchor === null || anchor === "end") throw new Error("expected a row anchor");
250 const reflowed = feedState(
251 [
252 { id: "question", size: 400 * (replySize / 2_000) },
253 { id: "reply", size: replySize },
254 { id: "follow-up", size: 300 },
255 ],
256 readingReply.scroll,
257 );
258 expect(resolveThreadFeedReflowScroll(anchor, reflowed, layout)).toEqual({
259 index: 1,
260 viewOffset: layout.headerInset - 0.6 * replySize,
261 });
262 });
263​
264 it.each([
265 { change: "narrows", replySize: 4_000 },
266 { change: "widens", replySize: 1_000 },
267 ])(
268 "holds the reader 60% into a reply on each re-measure as the feed $change",
269 ({ replySize }) => {
270 const anchor = resolveThreadFeedReflowAnchor(readingReply, layout, false);
271 if (anchor === null || anchor === "end") throw new Error("expected a row anchor");
272 const rows = [
273 { id: "question", size: 400 * (replySize / 2_000) },
274 { id: "reply", size: replySize },
275 { id: "follow-up", size: 300 },
276 ];
277 const offset = resolveThreadFeedReflowOffset(
278 anchor,
279 feedState(rows, readingReply.scroll),
280 layout,
281 );
282 if (offset === undefined) throw new Error("expected an offset");
283 expect(resolveThreadFeedReflowAnchor(feedState(rows, offset), layout, false)).toEqual({
284 key: "reply",
285 offsetWithinRow: 0.6 * replySize,
286 size: replySize,
287 });
288 },
289 );
290​
291 it("leaves a reader at the top of the thread where they are", () => {
292 expect(resolveThreadFeedReflowAnchor(feedState(wide, -layout.headerInset), layout, false)).toBe(
293 null,
294 );
295 });
296​
297 it("does not scroll when the row did not change size", () => {
298 const anchor = resolveThreadFeedReflowAnchor(readingReply, layout, false);
299 if (anchor === null || anchor === "end") throw new Error("expected a row anchor");
300 expect(resolveThreadFeedReflowScroll(anchor, readingReply, layout)).toBeNull();
301 });
302​
303 it("does not restore a row that left the feed", () => {
304 expect(
305 resolveThreadFeedReflowScroll(
306 { key: "reply", offsetWithinRow: 1_200, size: 2_000 },
307 feedState([{ id: "question", size: 400 }], 0),
308 layout,
309 ),
310 ).toBeNull();
311 });
312});