Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 resolveThreadFeedLiveFollow,
5 resolveThreadFeedSubmissionAnchor,
6 resolveThreadWorkGroupInitialScroll,
7 shouldFollowThreadWorkGroupAppend,
8} from "./thread-feed-live-follow";
9
10describe("tool-group scroll restoration", () => {
11 const position = {
12 rowId: "read-output",
13 offsetWithinRow: 80,
14 scrollOffset: 600,
15 contentHeight: 1_000,
16 };
17
18 it("restores the visible row and its detail offset rather than stale absolute pixels", () => {
19 const before = [{ id: "first" }, { id: "read-output" }, { id: "last" }];
20 const after = [{ id: "older" }, ...before];
21 expect(resolveThreadWorkGroupInitialScroll(before, position)).toEqual({
22 index: 1,
23 viewOffset: -80,
24 });
25 expect(resolveThreadWorkGroupInitialScroll(after, position)).toEqual({
26 index: 2,
27 viewOffset: -80,
28 });
29 });
30});
31
32describe("tool-group append following", () => {
33 const previousRows = Array.from({ length: 10 }, (_, index) => ({ id: `call-${index}` }));
34 const appendedRows = [...previousRows, { id: "new-call" }];
35 const atEnd = {
36 previousRows,
37 rows: appendedRows,
38 previousContentHeight: 289,
39 contentHeight: 318,
40 viewportHeight: 256,
41 scrollOffset: 33,
42 detailsChanged: false,
43 userScrolling: false,
44 };
45
46 it("follows a new call when the reader was at the end", () => {
47 expect(shouldFollowThreadWorkGroupAppend(atEnd)).toBe(true);
48 });
49
50 it("follows the first overflowing append as a short group reaches its height cap", () => {
51 expect(
52 shouldFollowThreadWorkGroupAppend({
53 ...atEnd,
54 previousRows: previousRows.slice(0, 8),
55 rows: previousRows.slice(0, 9),
56 previousContentHeight: 231,
57 contentHeight: 260,
58 viewportHeight: 231,
59 scrollOffset: 0,
60 }),
61 ).toBe(true);
62 });
63
64 it.each([
65 { name: "reading earlier calls", changes: { scrollOffset: 20 } },
66 { name: "streaming a result", changes: { rows: previousRows, contentHeight: 600 } },
67 {
68 name: "replacing a call while appending",
69 changes: { rows: [{ id: "replacement" }, ...appendedRows.slice(1)] },
70 },
71 ])("does not steal the reader's position when $name", ({ changes }) => {
72 expect(shouldFollowThreadWorkGroupAppend({ ...atEnd, ...changes })).toBe(false);
73 });
74});
75
76describe("resolveThreadFeedSubmissionAnchor", () => {
77 it("anchors the first user message in a thread", () => {
78 expect(
79 resolveThreadFeedSubmissionAnchor({
80 currentAnchorMessageId: null,
81 submittedMessageId: "first-message",
82 hasStartedTurn: false,
83 hasUserMessage: false,
84 queuedMessageCount: 0,
85 }),
86 ).toBe("first-message");
87 });
88
89 it("preserves the first-message anchor when another message is queued", () => {
90 expect(
91 resolveThreadFeedSubmissionAnchor({
92 currentAnchorMessageId: "first-message",
93 submittedMessageId: "second-message",
94 hasStartedTurn: false,
95 hasUserMessage: false,
96 queuedMessageCount: 1,
97 }),
98 ).toBe("first-message");
99 });
100
101 it("does not anchor a follow-up after a user message appears", () => {
102 expect(
103 resolveThreadFeedSubmissionAnchor({
104 currentAnchorMessageId: "first-message",
105 submittedMessageId: "second-message",
106 hasStartedTurn: false,
107 hasUserMessage: true,
108 queuedMessageCount: 0,
109 }),
110 ).toBeNull();
111 });
112
113 // regression: #7897 stop pushing follow-up messages to the top
114 it("does not anchor a thread that has already started a turn", () => {
115 expect(
116 resolveThreadFeedSubmissionAnchor({
117 currentAnchorMessageId: null,
118 submittedMessageId: "second-message",
119 hasStartedTurn: true,
120 hasUserMessage: false,
121 queuedMessageCount: 0,
122 }),
123 ).toBeNull();
124 });
125});
126
127describe("resolveThreadFeedLiveFollow", () => {
128 // regression: #5986 stabilize thread composer and interactions
129 it("pauses immediately when the user starts scrolling", () => {
130 expect(resolveThreadFeedLiveFollow(true, { type: "user-scroll-begin" })).toBe(false);
131 });
132
133 it("does not mistake programmatic layout compensation for a user scroll", () => {
134 expect(
135 resolveThreadFeedLiveFollow(true, {
136 type: "scroll",
137 isAtEnd: false,
138 userScrollSessionActive: false,
139 }),
140 ).toBe(true);
141 });
142
143 it("does not re-arm at the end while a user scroll session is active", () => {
144 expect(
145 resolveThreadFeedLiveFollow(false, {
146 type: "scroll",
147 isAtEnd: true,
148 userScrollSessionActive: true,
149 }),
150 ).toBe(false);
151 });
152
153 it.each([
154 { isAtEnd: false, userScrollSessionActive: false, expected: false },
155 { isAtEnd: true, userScrollSessionActive: false, expected: true },
156 { isAtEnd: false, userScrollSessionActive: true, expected: false },
157 { isAtEnd: true, userScrollSessionActive: true, expected: false },
158 ])("reconciles follow after a disclosure settles: %j", ({ expected, ...state }) => {
159 expect(resolveThreadFeedLiveFollow(!expected, { type: "disclosure-settled", ...state })).toBe(
160 expected,
161 );
162 });
163
164 it("re-arms at the actual end only after the user scroll session ends", () => {
165 expect(
166 resolveThreadFeedLiveFollow(false, {
167 type: "user-scroll-end",
168 isAtEnd: true,
169 userScrollSessionActive: true,
170 }),
171 ).toBe(true);
172 expect(
173 resolveThreadFeedLiveFollow(false, {
174 type: "user-scroll-end",
175 isAtEnd: false,
176 userScrollSessionActive: true,
177 }),
178 ).toBe(false);
179 });
180});