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

threadActivity.test.ts

411 lines12.0 KB

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 };
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 };
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 };
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 };
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 };
90​
91describe("buildThreadFeed", () => {
92 it("hides synthetic workspace preparation activity", () => {
93 const workspacePreparation = projected(
94 {
95 ...command(),
96 title: "Workspace ready",
97 input: "Preparing workspace",
98 output: "Workspace preparation completed.",
99 },
100 0,
101 );
102​
103 expect(buildThreadFeed([workspacePreparation])).toEqual([]);
104 });
105​
106 it("does not treat a queued-only run as live feed activity", () => {
107 expect(
108 threadFeedRunIsUnsettled({
109 runId,
110 status: "queued",
111 startedAt: null,
112 completedAt: null,
113 }),
114 ).toBe(false);
115 expect(
116 threadFeedRunIsUnsettled({
117 runId,
118 status: "running",
119 startedAt: "2026-06-20T00:00:01.000Z",
120 completedAt: null,
121 }),
122 ).toBe(true);
123 });
124​
125 it("adds queued input only after dispatch creates its turn item", () => {
126 const dispatchedRunId = RunId.make("run-dispatched-queued");
127 const dispatchedMessageId = MessageId.make("message-dispatched-queued");
128 expect(buildThreadFeed([])).toEqual([]);
129​
130 const promotedEntries = buildThreadFeed([
131 projected(
132 {
133 ...userMessage(),
134 id: TurnItemId.make("item-dispatched-queued"),
135 runId: dispatchedRunId,
136 messageId: dispatchedMessageId,
137 inputIntent: "turn_start",
138 },
139 0,
140 ),
141 ]);
142 expect(promotedEntries.map((entry) => entry.id)).toEqual([dispatchedMessageId]);
143 expect(
144 promotedEntries[0]?.type === "message" ? promotedEntries[0].message.inputIntent : undefined,
145 ).toBe("turn_start");
146 });
147​
148 it("hides the interruption request and keeps the terminal result", () => {
149 const request = projected(
150 {
151 ...base("item-interrupt-request", "2026-06-20T00:00:02.000Z", 1),
152 type: "run_interrupt_request",
153 message: "Interrupt requested",
154 },
155 0,
156 );
157 const result = projected(
158 {
159 ...base("item-interrupt-result", "2026-06-20T00:00:03.000Z", 2),
160 type: "run_interrupt_result",
161 message: "Run interrupted before provider start",
162 },
163 1,
164 );
165​
166 const activities = buildThreadFeed([request, result]).flatMap((entry) =>
167 entry.type === "activity-group" ? entry.activities : [],
168 );
169​
170 expect(activities).toHaveLength(1);
171 expect(activities[0]?.summary).toBe("Run interrupted");
172 expect(activities[0]?.detail).toBe("Run interrupted before provider start");
173 });
174​
175 it("preserves authoritative V2 order instead of sorting reconstructed collections", () => {
176 const rows = [
177 projected(userMessage("2026-06-20T00:00:03.000Z"), 0),
178 projected(command("2026-06-20T00:00:01.000Z"), 1),
179 projected(assistantMessage("2026-06-20T00:00:02.000Z"), 2),
180 ];
181​
182 const feed = buildThreadFeed(rows);
183 expect(feed.map((entry) => entry.type)).toEqual(["message", "activity-group", "message"]);
184 expect(feed.map((entry) => entry.id)).toEqual([
185 "message-user",
186 "local:thread-1:item-command",
187 "message-assistant",
188 ]);
189 const activity = feed.find((entry) => entry.type === "activity-group")?.activities[0];
190 expect(activity?.projectedItem).toBe(rows[1]);
191 expect(activity?.getFullDetail()).toContain('"input": "vp check"');
192 });
193​
194 it("retains inherited and synthetic rows with their original projected identity", () => {
195 const inherited = projected(command(), 0, "inherited");
196 const { providerThreadId: _providerThreadId, ...forkBase } = base(
197 "item-fork",
198 "2026-06-20T00:00:03.000Z",
199 2,
200 );
201 const synthetic = projected(
202 {
203 ...forkBase,
204 type: "fork",
205 source: { type: "run", threadId: sourceThreadId, runId },
206 targetThreadId: threadId,
207 },
208 1,
209 "synthetic",
210 );
211​
212 const feed = buildThreadFeed([inherited, synthetic]);
213 const activities = feed.flatMap((entry) =>
214 entry.type === "activity-group" ? entry.activities : [],
215 );
216 expect(activities.map((activity) => activity.projectedItem)).toEqual([inherited, synthetic]);
217 expect(activities.map((activity) => activity.projectedItem.visibility)).toEqual([
218 "inherited",
219 "synthetic",
220 ]);
221 expect(activities.at(-1)?.prominent).toBe(true);
222 });
223​
224 it("keeps orchestration relationship cards visible when a completed run is folded", () => {
225 const { providerThreadId: _providerThreadId, ...forkBase } = base(
226 "item-fork",
227 "2026-06-20T00:00:02.500Z",
228 2,
229 );
230 const feed = buildThreadFeed([
231 projected(userMessage(), 0),
232 projected(command(), 1),
233 projected(
234 {
235 ...forkBase,
236 type: "fork",
237 source: { type: "run", threadId, runId },
238 targetThreadId: sourceThreadId,
239 },
240 2,
241 ),
242 projected(assistantMessage(), 3),
243 ]);
244​
245 const collapsed = deriveThreadFeedPresentation(
246 feed,
247 {
248 runId,
249 status: "completed",
250 startedAt: "2026-06-20T00:00:01.000Z",
251 completedAt: "2026-06-20T00:00:03.000Z",
252 },
253 new Set(),
254 );
255​
256 expect(
257 collapsed.some(
258 (entry) =>
259 entry.type === "activity-group" &&
260 entry.activities.some((activity) => activity.projectedItem.item.type === "fork"),
261 ),
262 ).toBe(true);
263 expect(
264 collapsed.some(
265 (entry) =>
266 entry.type === "activity-group" &&
267 entry.activities.some(
268 (activity) => activity.projectedItem.item.type === "command_execution",
269 ),
270 ),
271 ).toBe(false);
272 });
273​
274 it("folds settled V2 run work while keeping the terminal assistant message visible", () => {
275 const feed = buildThreadFeed([
276 projected(userMessage(), 0),
277 projected(command(), 1),
278 projected(assistantMessage(), 2),
279 ]);
280 const latestRun = {
281 runId,
282 status: "completed" as const,
283 startedAt: "2026-06-20T00:00:01.000Z",
284 completedAt: "2026-06-20T00:00:03.000Z",
285 };
286​
287 const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set());
288 expect(collapsed.map((entry) => entry.type)).toEqual(["message", "run-fold", "message"]);
289​
290 const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId]));
291 expect(expanded.map((entry) => entry.type)).toEqual([
292 "message",
293 "run-fold",
294 "activity-group",
295 "message",
296 ]);
297 });
298​
299 it("keeps an active run expanded and marks failed tools as failures", () => {
300 const failedCommand: OrchestrationV2TurnItem = {
301 ...command(),
302 status: "failed",
303 completedAt: DateTime.makeUnsafe("2026-06-20T00:00:02.000Z"),
304 };
305 const feed = buildThreadFeed([projected(userMessage(), 0), projected(failedCommand, 1)]);
306 const presented = deriveThreadFeedPresentation(
307 feed,
308 {
309 runId,
310 status: "running",
311 startedAt: "2026-06-20T00:00:01.000Z",
312 completedAt: null,
313 },
314 new Set(),
315 );
316​
317 expect(presented.some((entry) => entry.type === "run-fold")).toBe(false);
318 expect(presented.find((entry) => entry.type === "activity-group")?.activities[0]?.status).toBe(
319 "failure",
320 );
321 });
322​
323 it("appends active work as a normal timeline row", () => {
324 const startedAt = "2026-04-01T00:00:01.000Z";
325 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
326​
327 expect(presented).toEqual([
328 {
329 type: "working",
330 id: "working-indicator-row",
331 createdAt: startedAt,
332 },
333 ]);
334 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
335 });
336​
337 it("models work-log overflow as list rows", () => {
338 const activity = (
339 id: string,
340 createdAt: string,
341 status: ThreadFeedActivity["status"] = "success",
342 ): ThreadFeedActivity => ({
343 id,
344 createdAt,
345 runId: null,
346 summary: `Tool ${id}`,
347 detail: null,
348 canExpand: false,
349 getFullDetail: () => null,
350 getCopyText: () => id,
351 icon: "command",
352 logo: null,
353 toolLike: true,
354 prominent: false,
355 status,
356 projectedItem: projected(command(createdAt), 0),
357 });
358 const feed: ThreadFeedEntry[] = [
359 {
360 type: "activity-group",
361 id: "work-group-1",
362 createdAt: "2026-04-01T00:00:01.000Z",
363 runId: null,
364 activities: [
365 activity("activity-1", "2026-04-01T00:00:01.000Z"),
366 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
367 activity("activity-2", "2026-04-01T00:00:03.000Z"),
368 activity("activity-3", "2026-04-01T00:00:04.000Z"),
369 ],
370 },
371 ];
372​
373 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
374 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
375 expect(collapsed[1]).toMatchObject({
376 type: "work-toggle",
377 groupId: "work-group-1",
378 hiddenCount: 2,
379 expanded: false,
380 });
381​
382 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
383 expect(expanded.map((entry) => entry.id)).toEqual([
384 "activity-1",
385 "activity-2",
386 "activity-3",
387 "work-toggle:work-group-1",
388 ]);
389 expect(expanded.at(-1)).toMatchObject({
390 type: "work-toggle",
391 expanded: true,
392 });
393 });
394​
395 it("pretty prints T3 MCP dynamic tool activities and attaches the product logo", () => {
396 const toolItem: OrchestrationV2TurnItem = {
397 ...base("item-t3-tool", "2026-06-20T00:00:04.000Z", 3),
398 type: "dynamic_tool",
399 toolName: "mcp__t3-code__t3_thread_read",
400 input: { threadId: "thread-child" },
401 output: { messages: [] },
402 };
403​
404 const feed = buildThreadFeed([projected(toolItem, 0)]);
405 const activity = feed[0]?.type === "activity-group" ? feed[0].activities[0] : null;
406​
407 expect(activity?.summary).toBe("Read a T3 thread");
408 expect(activity?.logo).toBe("t3-code");
409 expect(activity?.getCopyText().split("\n")[0]).toBe("Read a T3 thread");
410 });
411});