Open History to see the last commit
1import {
2 MessageId,
3 RunId,
4 ThreadId,
5 TurnItemId,
6 type OrchestrationV2ProjectedTurnItem,
7 type OrchestrationV2TurnItem,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10import { describe, expect, it } from "vite-plus/test";
11
12import {
13 buildThreadFeed,
14 deriveThreadFeedPresentation,
15 threadFeedRunIsUnsettled,
16 type ThreadFeedActivity,
17 type ThreadFeedEntry,
18} from "./threadActivity";
19
20const threadId = ThreadId.make("thread-1");
21const sourceThreadId = ThreadId.make("thread-source");
22const runId = RunId.make("run-1");
23
24function base(id: string, updatedAt: string, ordinal: number) {
25 const timestamp = DateTime.makeUnsafe(updatedAt);
26 return {
27 id: TurnItemId.make(id),
28 threadId,
29 runId,
30 nodeId: null,
31 providerThreadId: null,
32 providerTurnId: null,
33 nativeItemRef: null,
34 parentItemId: null,
35 ordinal,
36 status: "completed" as const,
37 title: null,
38 startedAt: timestamp,
39 completedAt: timestamp,
40 updatedAt: timestamp,
41 };
42}
43
44function projected(
45 item: OrchestrationV2TurnItem,
46 position: number,
47 visibility: OrchestrationV2ProjectedTurnItem["visibility"] = "local",
48): OrchestrationV2ProjectedTurnItem {
49 return {
50 position,
51 visibility,
52 sourceThreadId: visibility === "local" ? threadId : sourceThreadId,
53 sourceItemId: item.id,
54 item,
55 };
56}
57
58function userMessage(updatedAt = "2026-06-20T00:00:01.000Z") {
59 return {
60 ...base("item-user", updatedAt, 0),
61 type: "user_message" as const,
62 messageId: MessageId.make("message-user"),
63 createdBy: "user" as const,
64 creationSource: "mobile" as const,
65 inputIntent: "turn_start" as const,
66 text: "Run checks",
67 attachments: [],
68 };
69}
70
71function command(updatedAt = "2026-06-20T00:00:02.000Z") {
72 return {
73 ...base("item-command", updatedAt, 1),
74 type: "command_execution" as const,
75 input: "vp check",
76 output: "ok",
77 exitCode: 0,
78 };
79}
80
81function assistantMessage(updatedAt = "2026-06-20T00:00:03.000Z") {
82 return {
83 ...base("item-assistant", updatedAt, 2),
84 type: "assistant_message" as const,
85 messageId: MessageId.make("message-assistant"),
86 text: "Done",
87 streaming: false,
88 };
89}
90
91describe("buildThreadFeed", () => {
92 it("does not treat a queued-only run as live feed activity", () => {
93 expect(
94 threadFeedRunIsUnsettled({
95 runId,
96 status: "queued",
97 startedAt: null,
98 completedAt: null,
99 }),
100 ).toBe(false);
101 expect(
102 threadFeedRunIsUnsettled({
103 runId,
104 status: "running",
105 startedAt: "2026-06-20T00:00:01.000Z",
106 completedAt: null,
107 }),
108 ).toBe(true);
109 });
110
111 it("adds queued input only after dispatch creates its turn item", () => {
112 const dispatchedRunId = RunId.make("run-dispatched-queued");
113 const dispatchedMessageId = MessageId.make("message-dispatched-queued");
114 expect(buildThreadFeed([])).toEqual([]);
115
116 const promotedEntries = buildThreadFeed([
117 projected(
118 {
119 ...userMessage(),
120 id: TurnItemId.make("item-dispatched-queued"),
121 runId: dispatchedRunId,
122 messageId: dispatchedMessageId,
123 inputIntent: "turn_start",
124 },
125 0,
126 ),
127 ]);
128 expect(promotedEntries.map((entry) => entry.id)).toEqual([dispatchedMessageId]);
129 expect(
130 promotedEntries[0]?.type === "message" ? promotedEntries[0].message.inputIntent : undefined,
131 ).toBe("turn_start");
132 });
133
134 it("preserves authoritative V2 order instead of sorting reconstructed collections", () => {
135 const rows = [
136 projected(userMessage("2026-06-20T00:00:03.000Z"), 0),
137 projected(command("2026-06-20T00:00:01.000Z"), 1),
138 projected(assistantMessage("2026-06-20T00:00:02.000Z"), 2),
139 ];
140
141 const feed = buildThreadFeed(rows);
142 expect(feed.map((entry) => entry.type)).toEqual(["message", "activity-group", "message"]);
143 expect(feed.map((entry) => entry.id)).toEqual([
144 "message-user",
145 "local:thread-1:item-command",
146 "message-assistant",
147 ]);
148 const activity = feed.find((entry) => entry.type === "activity-group")?.activities[0];
149 expect(activity?.projectedItem).toBe(rows[1]);
150 expect(activity?.getFullDetail()).toContain('"input": "vp check"');
151 });
152
153 it("retains inherited and synthetic rows with their original projected identity", () => {
154 const inherited = projected(command(), 0, "inherited");
155 const { providerThreadId: _providerThreadId, ...forkBase } = base(
156 "item-fork",
157 "2026-06-20T00:00:03.000Z",
158 2,
159 );
160 const synthetic = projected(
161 {
162 ...forkBase,
163 type: "fork",
164 source: { type: "run", threadId: sourceThreadId, runId },
165 targetThreadId: threadId,
166 },
167 1,
168 "synthetic",
169 );
170
171 const feed = buildThreadFeed([inherited, synthetic]);
172 const activities = feed.flatMap((entry) =>
173 entry.type === "activity-group" ? entry.activities : [],
174 );
175 expect(activities.map((activity) => activity.projectedItem)).toEqual([inherited, synthetic]);
176 expect(activities.map((activity) => activity.projectedItem.visibility)).toEqual([
177 "inherited",
178 "synthetic",
179 ]);
180 expect(activities.at(-1)?.prominent).toBe(true);
181 });
182
183 it("keeps orchestration relationship cards visible when a completed run is folded", () => {
184 const { providerThreadId: _providerThreadId, ...forkBase } = base(
185 "item-fork",
186 "2026-06-20T00:00:02.500Z",
187 2,
188 );
189 const feed = buildThreadFeed([
190 projected(userMessage(), 0),
191 projected(command(), 1),
192 projected(
193 {
194 ...forkBase,
195 type: "fork",
196 source: { type: "run", threadId, runId },
197 targetThreadId: sourceThreadId,
198 },
199 2,
200 ),
201 projected(assistantMessage(), 3),
202 ]);
203
204 const collapsed = deriveThreadFeedPresentation(
205 feed,
206 {
207 runId,
208 status: "completed",
209 startedAt: "2026-06-20T00:00:01.000Z",
210 completedAt: "2026-06-20T00:00:03.000Z",
211 },
212 new Set(),
213 );
214
215 expect(
216 collapsed.some(
217 (entry) =>
218 entry.type === "activity-group" &&
219 entry.activities.some((activity) => activity.projectedItem.item.type === "fork"),
220 ),
221 ).toBe(true);
222 expect(
223 collapsed.some(
224 (entry) =>
225 entry.type === "activity-group" &&
226 entry.activities.some(
227 (activity) => activity.projectedItem.item.type === "command_execution",
228 ),
229 ),
230 ).toBe(false);
231 });
232
233 it("folds settled V2 run work while keeping the terminal assistant message visible", () => {
234 const feed = buildThreadFeed([
235 projected(userMessage(), 0),
236 projected(command(), 1),
237 projected(assistantMessage(), 2),
238 ]);
239 const latestRun = {
240 runId,
241 status: "completed" as const,
242 startedAt: "2026-06-20T00:00:01.000Z",
243 completedAt: "2026-06-20T00:00:03.000Z",
244 };
245
246 const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set());
247 expect(collapsed.map((entry) => entry.type)).toEqual(["message", "run-fold", "message"]);
248
249 const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId]));
250 expect(expanded.map((entry) => entry.type)).toEqual([
251 "message",
252 "run-fold",
253 "activity-group",
254 "message",
255 ]);
256 });
257
258 it("keeps an active run expanded and marks failed tools as failures", () => {
259 const failedCommand: OrchestrationV2TurnItem = {
260 ...command(),
261 status: "failed",
262 completedAt: DateTime.makeUnsafe("2026-06-20T00:00:02.000Z"),
263 };
264 const feed = buildThreadFeed([projected(userMessage(), 0), projected(failedCommand, 1)]);
265 const presented = deriveThreadFeedPresentation(
266 feed,
267 {
268 runId,
269 status: "running",
270 startedAt: "2026-06-20T00:00:01.000Z",
271 completedAt: null,
272 },
273 new Set(),
274 );
275
276 expect(presented.some((entry) => entry.type === "run-fold")).toBe(false);
277 expect(presented.find((entry) => entry.type === "activity-group")?.activities[0]?.status).toBe(
278 "failure",
279 );
280 });
281
282 it("appends active work as a normal timeline row", () => {
283 const startedAt = "2026-04-01T00:00:01.000Z";
284 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
285
286 expect(presented).toEqual([
287 {
288 type: "working",
289 id: "working-indicator-row",
290 createdAt: startedAt,
291 },
292 ]);
293 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
294 });
295
296 it("models work-log overflow as list rows", () => {
297 const activity = (
298 id: string,
299 createdAt: string,
300 status: ThreadFeedActivity["status"] = "success",
301 ): ThreadFeedActivity => ({
302 id,
303 createdAt,
304 runId: null,
305 summary: `Tool ${id}`,
306 detail: null,
307 canExpand: false,
308 getFullDetail: () => null,
309 getCopyText: () => id,
310 icon: "command",
311 logo: null,
312 toolLike: true,
313 prominent: false,
314 status,
315 projectedItem: projected(command(createdAt), 0),
316 });
317 const feed: ThreadFeedEntry[] = [
318 {
319 type: "activity-group",
320 id: "work-group-1",
321 createdAt: "2026-04-01T00:00:01.000Z",
322 runId: null,
323 activities: [
324 activity("activity-1", "2026-04-01T00:00:01.000Z"),
325 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
326 activity("activity-2", "2026-04-01T00:00:03.000Z"),
327 activity("activity-3", "2026-04-01T00:00:04.000Z"),
328 ],
329 },
330 ];
331
332 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
333 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
334 expect(collapsed[1]).toMatchObject({
335 type: "work-toggle",
336 groupId: "work-group-1",
337 hiddenCount: 2,
338 expanded: false,
339 });
340
341 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
342 expect(expanded.map((entry) => entry.id)).toEqual([
343 "activity-1",
344 "activity-2",
345 "activity-3",
346 "work-toggle:work-group-1",
347 ]);
348 expect(expanded.at(-1)).toMatchObject({
349 type: "work-toggle",
350 expanded: true,
351 });
352 });
353
354 it("pretty prints T3 MCP dynamic tool activities and attaches the product logo", () => {
355 const toolItem: OrchestrationV2TurnItem = {
356 ...base("item-t3-tool", "2026-06-20T00:00:04.000Z", 3),
357 type: "dynamic_tool",
358 toolName: "mcp__t3-code__t3_thread_read",
359 input: { threadId: "thread-child" },
360 output: { messages: [] },
361 };
362
363 const feed = buildThreadFeed([projected(toolItem, 0)]);
364 const activity = feed[0]?.type === "activity-group" ? feed[0].activities[0] : null;
365
366 expect(activity?.summary).toBe("Read a T3 thread");
367 expect(activity?.logo).toBe("t3-code");
368 expect(activity?.getCopyText().split("\n")[0]).toBe("Read a T3 thread");
369 });
370});