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 togglePendingUserInputOptionSelection,
19 setPendingUserInputCustomAnswer,
20 isPendingUserInputOptionSelected,
21 buildPendingUserInputAnswers,
22} from "./threadActivity";
23
24const threadId = ThreadId.make("thread-1");
25const sourceThreadId = ThreadId.make("thread-source");
26const runId = RunId.make("run-1");
27
28function base(id: string, updatedAt: string, ordinal: number) {
29 const timestamp = DateTime.makeUnsafe(updatedAt);
30 return {
31 id: TurnItemId.make(id),
32 threadId,
33 runId,
34 nodeId: null,
35 providerThreadId: null,
36 providerTurnId: null,
37 nativeItemRef: null,
38 parentItemId: null,
39 ordinal,
40 status: "completed" as const,
41 title: null,
42 startedAt: timestamp,
43 completedAt: timestamp,
44 updatedAt: timestamp,
45 };
46}
47
48function projected(
49 item: OrchestrationV2TurnItem,
50 position: number,
51 visibility: OrchestrationV2ProjectedTurnItem["visibility"] = "local",
52): OrchestrationV2ProjectedTurnItem {
53 return {
54 position,
55 visibility,
56 sourceThreadId: visibility === "local" ? threadId : sourceThreadId,
57 sourceItemId: item.id,
58 item,
59 };
60}
61
62function userMessage(updatedAt = "2026-06-20T00:00:01.000Z") {
63 return {
64 ...base("item-user", updatedAt, 0),
65 type: "user_message" as const,
66 messageId: MessageId.make("message-user"),
67 createdBy: "user" as const,
68 creationSource: "mobile" as const,
69 inputIntent: "turn_start" as const,
70 text: "Run checks",
71 attachments: [],
72 };
73}
74
75function command(updatedAt = "2026-06-20T00:00:02.000Z") {
76 return {
77 ...base("item-command", updatedAt, 1),
78 type: "command_execution" as const,
79 input: "vp check",
80 output: "ok",
81 exitCode: 0,
82 };
83}
84
85function assistantMessage(updatedAt = "2026-06-20T00:00:03.000Z") {
86 return {
87 ...base("item-assistant", updatedAt, 2),
88 type: "assistant_message" as const,
89 messageId: MessageId.make("message-assistant"),
90 text: "Done",
91 streaming: false,
92 };
93}
94
95describe("buildThreadFeed", () => {
96 it("hides synthetic workspace preparation activity", () => {
97 const workspacePreparation = projected(
98 {
99 ...command(),
100 title: "Workspace ready",
101 input: "Preparing workspace",
102 output: "Workspace preparation completed.",
103 },
104 0,
105 );
106
107 expect(buildThreadFeed([workspacePreparation])).toEqual([]);
108 });
109
110 it("does not treat a queued-only run as live feed activity", () => {
111 expect(
112 threadFeedRunIsUnsettled({
113 runId,
114 status: "queued",
115 startedAt: null,
116 completedAt: null,
117 }),
118 ).toBe(false);
119 expect(
120 threadFeedRunIsUnsettled({
121 runId,
122 status: "running",
123 startedAt: "2026-06-20T00:00:01.000Z",
124 completedAt: null,
125 }),
126 ).toBe(true);
127 });
128
129 it("adds queued input only after dispatch creates its turn item", () => {
130 const dispatchedRunId = RunId.make("run-dispatched-queued");
131 const dispatchedMessageId = MessageId.make("message-dispatched-queued");
132 expect(buildThreadFeed([])).toEqual([]);
133
134 const promotedEntries = buildThreadFeed([
135 projected(
136 {
137 ...userMessage(),
138 id: TurnItemId.make("item-dispatched-queued"),
139 runId: dispatchedRunId,
140 messageId: dispatchedMessageId,
141 inputIntent: "turn_start",
142 },
143 0,
144 ),
145 ]);
146 expect(promotedEntries.map((entry) => entry.id)).toEqual([dispatchedMessageId]);
147 expect(
148 promotedEntries[0]?.type === "message" ? promotedEntries[0].message.inputIntent : undefined,
149 ).toBe("turn_start");
150 });
151
152 it("hides the interruption request and keeps the terminal result", () => {
153 const request = projected(
154 {
155 ...base("item-interrupt-request", "2026-06-20T00:00:02.000Z", 1),
156 type: "run_interrupt_request",
157 message: "Interrupt requested",
158 },
159 0,
160 );
161 const result = projected(
162 {
163 ...base("item-interrupt-result", "2026-06-20T00:00:03.000Z", 2),
164 type: "run_interrupt_result",
165 message: "Run interrupted before provider start",
166 },
167 1,
168 );
169
170 const activities = buildThreadFeed([request, result]).flatMap((entry) =>
171 entry.type === "activity-group" ? entry.activities : [],
172 );
173
174 expect(activities).toHaveLength(1);
175 expect(activities[0]?.summary).toBe("Run interrupted");
176 expect(activities[0]?.detail).toBe("Run interrupted before provider start");
177 });
178
179 it("preserves authoritative V2 order instead of sorting reconstructed collections", () => {
180 const rows = [
181 projected(userMessage("2026-06-20T00:00:03.000Z"), 0),
182 projected(command("2026-06-20T00:00:01.000Z"), 1),
183 projected(assistantMessage("2026-06-20T00:00:02.000Z"), 2),
184 ];
185
186 const feed = buildThreadFeed(rows);
187 expect(feed.map((entry) => entry.type)).toEqual(["message", "activity-group", "message"]);
188 expect(feed.map((entry) => entry.id)).toEqual([
189 "message-user",
190 "local:thread-1:item-command",
191 "message-assistant",
192 ]);
193 const activity = feed.find((entry) => entry.type === "activity-group")?.activities[0];
194 expect(activity?.projectedItem).toBe(rows[1]);
195 expect(activity?.getFullDetail()).toContain('"input": "vp check"');
196 });
197
198 it("retains inherited and synthetic rows with their original projected identity", () => {
199 const inherited = projected(command(), 0, "inherited");
200 const { providerThreadId: _providerThreadId, ...forkBase } = base(
201 "item-fork",
202 "2026-06-20T00:00:03.000Z",
203 2,
204 );
205 const synthetic = projected(
206 {
207 ...forkBase,
208 type: "fork",
209 source: { type: "run", threadId: sourceThreadId, runId },
210 targetThreadId: threadId,
211 },
212 1,
213 "synthetic",
214 );
215
216 const feed = buildThreadFeed([inherited, synthetic]);
217 const activities = feed.flatMap((entry) =>
218 entry.type === "activity-group" ? entry.activities : [],
219 );
220 expect(activities.map((activity) => activity.projectedItem)).toEqual([inherited, synthetic]);
221 expect(activities.map((activity) => activity.projectedItem.visibility)).toEqual([
222 "inherited",
223 "synthetic",
224 ]);
225 expect(activities.at(-1)?.prominent).toBe(true);
226 });
227
228 it("keeps orchestration relationship cards visible when a completed run is folded", () => {
229 const { providerThreadId: _providerThreadId, ...forkBase } = base(
230 "item-fork",
231 "2026-06-20T00:00:02.500Z",
232 2,
233 );
234 const feed = buildThreadFeed([
235 projected(userMessage(), 0),
236 projected(command(), 1),
237 projected(
238 {
239 ...forkBase,
240 type: "fork",
241 source: { type: "run", threadId, runId },
242 targetThreadId: sourceThreadId,
243 },
244 2,
245 ),
246 projected(assistantMessage(), 3),
247 ]);
248
249 const collapsed = deriveThreadFeedPresentation(
250 feed,
251 {
252 runId,
253 status: "completed",
254 startedAt: "2026-06-20T00:00:01.000Z",
255 completedAt: "2026-06-20T00:00:03.000Z",
256 },
257 new Set(),
258 );
259
260 expect(
261 collapsed.some(
262 (entry) =>
263 entry.type === "activity-group" &&
264 entry.activities.some((activity) => activity.projectedItem.item.type === "fork"),
265 ),
266 ).toBe(true);
267 expect(
268 collapsed.some(
269 (entry) =>
270 entry.type === "activity-group" &&
271 entry.activities.some(
272 (activity) => activity.projectedItem.item.type === "command_execution",
273 ),
274 ),
275 ).toBe(false);
276 });
277
278 it("folds settled V2 run work while keeping the terminal assistant message visible", () => {
279 const feed = buildThreadFeed([
280 projected(userMessage(), 0),
281 projected(command(), 1),
282 projected(assistantMessage(), 2),
283 ]);
284 const latestRun = {
285 runId,
286 status: "completed" as const,
287 startedAt: "2026-06-20T00:00:01.000Z",
288 completedAt: "2026-06-20T00:00:03.000Z",
289 };
290
291 const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set());
292 expect(collapsed.map((entry) => entry.type)).toEqual(["message", "run-fold", "message"]);
293
294 const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId]));
295 expect(expanded.map((entry) => entry.type)).toEqual([
296 "message",
297 "run-fold",
298 "activity-group",
299 "message",
300 ]);
301 });
302
303 it("keeps an active run expanded and marks failed tools as failures", () => {
304 const failedCommand: OrchestrationV2TurnItem = {
305 ...command(),
306 status: "failed",
307 completedAt: DateTime.makeUnsafe("2026-06-20T00:00:02.000Z"),
308 };
309 const feed = buildThreadFeed([projected(userMessage(), 0), projected(failedCommand, 1)]);
310 const presented = deriveThreadFeedPresentation(
311 feed,
312 {
313 runId,
314 status: "running",
315 startedAt: "2026-06-20T00:00:01.000Z",
316 completedAt: null,
317 },
318 new Set(),
319 );
320
321 expect(presented.some((entry) => entry.type === "run-fold")).toBe(false);
322 expect(presented.find((entry) => entry.type === "activity-group")?.activities[0]?.status).toBe(
323 "failure",
324 );
325 });
326
327 it("appends active work as a normal timeline row", () => {
328 const startedAt = "2026-04-01T00:00:01.000Z";
329 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
330
331 expect(presented).toEqual([
332 {
333 type: "working",
334 id: "working-indicator-row",
335 createdAt: startedAt,
336 },
337 ]);
338 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
339 });
340
341 it("models work-log overflow as list rows", () => {
342 const activity = (
343 id: string,
344 createdAt: string,
345 status: ThreadFeedActivity["status"] = "success",
346 ): ThreadFeedActivity => ({
347 id,
348 createdAt,
349 runId: null,
350 summary: `Tool ${id}`,
351 detail: null,
352 canExpand: false,
353 getFullDetail: () => null,
354 getCopyText: () => id,
355 icon: "command",
356 logo: null,
357 toolLike: true,
358 prominent: false,
359 status,
360 projectedItem: projected(command(createdAt), 0),
361 });
362 const feed: ThreadFeedEntry[] = [
363 {
364 type: "activity-group",
365 id: "work-group-1",
366 createdAt: "2026-04-01T00:00:01.000Z",
367 runId: null,
368 activities: [
369 activity("activity-1", "2026-04-01T00:00:01.000Z"),
370 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
371 activity("activity-2", "2026-04-01T00:00:03.000Z"),
372 activity("activity-3", "2026-04-01T00:00:04.000Z"),
373 ],
374 },
375 ];
376
377 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
378 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
379 expect(collapsed[1]).toMatchObject({
380 type: "work-toggle",
381 groupId: "work-group-1",
382 hiddenCount: 2,
383 expanded: false,
384 });
385
386 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
387 expect(expanded.map((entry) => entry.id)).toEqual([
388 "activity-1",
389 "activity-2",
390 "activity-3",
391 "work-toggle:work-group-1",
392 ]);
393 expect(expanded.at(-1)).toMatchObject({
394 type: "work-toggle",
395 expanded: true,
396 });
397 });
398
399 it("pretty prints T3 MCP dynamic tool activities and attaches the product logo", () => {
400 const toolItem: OrchestrationV2TurnItem = {
401 ...base("item-t3-tool", "2026-06-20T00:00:04.000Z", 3),
402 type: "dynamic_tool",
403 toolName: "mcp__t3-code__t3_thread_read",
404 input: { threadId: "thread-child" },
405 output: { messages: [] },
406 };
407
408 const feed = buildThreadFeed([projected(toolItem, 0)]);
409 const activity = feed[0]?.type === "activity-group" ? feed[0].activities[0] : null;
410
411 expect(activity?.summary).toBe("Read a T3 thread");
412 expect(activity?.logo).toBe("t3-code");
413 expect(activity?.getCopyText().split("\n")[0]).toBe("Read a T3 thread");
414 });
415});
416
417const singleSelectQuestion = {
418 id: "runtime",
419 header: "Runtime",
420 question: "Which runtime should be used?",
421 options: [
422 { label: "Go", description: "One binary" },
423 { label: "Node.js", description: "Reuse TypeScript" },
424 ],
425 multiSelect: false,
426} as const;
427
428const multiSelectQuestion = {
429 id: "scope",
430 header: "Scope",
431 question: "Which data should be collected?",
432 options: [
433 { label: "Orders", description: "Receipts" },
434 { label: "Listings", description: "Inventory" },
435 ],
436 multiSelect: true,
437} as const;
438
439describe("pending user input answers", () => {
440 it("replaces single-select options and toggles multi-select options", () => {
441 expect(
442 togglePendingUserInputOptionSelection(
443 singleSelectQuestion,
444 { selectedOptionLabels: ["Go"] },
445 "Node.js",
446 ),
447 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Node.js"] });
448
449 const orders = togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Orders");
450 const ordersAndListings = togglePendingUserInputOptionSelection(
451 multiSelectQuestion,
452 orders,
453 "Listings",
454 );
455 expect(ordersAndListings).toEqual({
456 customAnswer: "",
457 selectedOptionLabels: ["Orders", "Listings"],
458 });
459 expect(
460 togglePendingUserInputOptionSelection(multiSelectQuestion, ordersAndListings, "Orders"),
461 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Listings"] });
462
463 const paddedOrders = togglePendingUserInputOptionSelection(
464 multiSelectQuestion,
465 undefined,
466 " Orders ",
467 );
468 expect(paddedOrders).toEqual({ customAnswer: "", selectedOptionLabels: ["Orders"] });
469 expect(
470 togglePendingUserInputOptionSelection(multiSelectQuestion, paddedOrders, " Orders "),
471 ).toEqual({ customAnswer: "" });
472 });
473
474 it("builds array answers for multi-select questions", () => {
475 expect(
476 buildPendingUserInputAnswers([singleSelectQuestion, multiSelectQuestion], {
477 runtime: { selectedOptionLabels: ["Go"] },
478 scope: { selectedOptionLabels: ["Orders", "Listings"] },
479 }),
480 ).toEqual({
481 runtime: "Go",
482 scope: ["Orders", "Listings"],
483 });
484 });
485
486 it("clears selected options while a custom answer is active", () => {
487 expect(
488 setPendingUserInputCustomAnswer(
489 { selectedOptionLabels: ["Orders", "Listings"] },
490 "Orders first",
491 ),
492 ).toEqual({ customAnswer: "Orders first" });
493 });
494
495 it("matches selected chips against normalized option labels", () => {
496 expect(
497 isPendingUserInputOptionSelected({ selectedOptionLabels: ["Orders"] }, " Orders "),
498 ).toBe(true);
499 expect(
500 isPendingUserInputOptionSelected(
501 { selectedOptionLabels: ["Orders"], customAnswer: "Orders first" },
502 " Orders ",
503 ),
504 ).toBe(false);
505 });
506});