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

threadActivity.test.ts

484 lines14.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 EventId,
5 MessageId,
6 ProjectId,
7 ProviderInstanceId,
8 ThreadId,
9 TurnId,
10 type OrchestrationThread,
11 type OrchestrationThreadActivity,
12} from "@t3tools/contracts";
13​
14import {
15 buildThreadFeed,
16 deriveThreadFeedPresentation,
17 type ThreadFeedActivity,
18 type ThreadFeedEntry,
19} from "./threadActivity";
20​
21function makeActivity(
22 input: Partial<OrchestrationThreadActivity> &
23 Pick<OrchestrationThreadActivity, "id" | "kind" | "summary" | "createdAt">,
24): OrchestrationThreadActivity {
25 return {
26 tone: "info",
27 payload: {},
28 turnId: null,
29 ...input,
30 };
32​
33function makeThread(
34 input: Partial<OrchestrationThread> & Pick<OrchestrationThread, "id" | "projectId" | "title">,
35): OrchestrationThread {
36 return {
37 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
38 runtimeMode: "full-access",
39 interactionMode: "default",
40 branch: null,
41 worktreePath: null,
42 latestTurn: null,
43 createdAt: "2026-04-01T00:00:00.000Z",
44 updatedAt: "2026-04-01T00:00:00.000Z",
45 archivedAt: null,
46 deletedAt: null,
47 messages: [],
48 proposedPlans: [],
49 activities: [],
50 checkpoints: [],
51 session: null,
52 ...input,
53 settledOverride: input.settledOverride ?? null,
54 settledAt: input.settledAt ?? null,
55 };
57​
58describe("buildThreadFeed", () => {
59 it("keeps historic work entries attributed to their turns", () => {
60 const thread = makeThread({
61 id: ThreadId.make("thread-1"),
62 projectId: ProjectId.make("project-1"),
63 title: "Runtime warning thread",
64 latestTurn: {
65 turnId: TurnId.make("turn-latest"),
66 state: "running",
67 requestedAt: "2026-04-01T00:00:00.000Z",
68 startedAt: "2026-04-01T00:00:01.000Z",
69 completedAt: null,
70 assistantMessageId: null,
71 },
72 activities: [
73 makeActivity({
74 id: EventId.make("activity-old"),
75 kind: "runtime.warning",
76 summary: "Runtime warning",
77 createdAt: "2026-04-01T00:00:02.000Z",
78 turnId: TurnId.make("turn-old"),
79 payload: {
80 message: "Old warning",
81 },
82 }),
83 makeActivity({
84 id: EventId.make("activity-latest"),
85 kind: "runtime.warning",
86 summary: "Runtime warning",
87 createdAt: "2026-04-01T00:00:03.000Z",
88 turnId: TurnId.make("turn-latest"),
89 payload: {
90 message: "Latest warning",
91 },
92 }),
93 ],
94 });
95​
96 const feed = buildThreadFeed(thread);
97 expect(feed).toMatchObject([
98 {
99 type: "activity-group",
100 turnId: "turn-old",
101 activities: [{ id: "activity-old", turnId: "turn-old" }],
102 },
103 {
104 type: "activity-group",
105 turnId: "turn-latest",
106 activities: [{ id: "activity-latest", turnId: "turn-latest" }],
107 },
108 ]);
109 });
110​
111 it("collapses matching tool lifecycle rows like desktop", () => {
112 const thread = makeThread({
113 id: ThreadId.make("thread-2"),
114 projectId: ProjectId.make("project-1"),
115 title: "Collapsed tools",
116 latestTurn: {
117 turnId: TurnId.make("turn-1"),
118 state: "completed",
119 requestedAt: "2026-04-01T00:00:00.000Z",
120 startedAt: "2026-04-01T00:00:01.000Z",
121 completedAt: "2026-04-01T00:00:03.000Z",
122 assistantMessageId: null,
123 },
124 activities: [
125 makeActivity({
126 id: EventId.make("tool-updated"),
127 kind: "tool.updated",
128 tone: "tool",
129 summary: "Run tests",
130 createdAt: "2026-04-01T00:00:01.000Z",
131 turnId: TurnId.make("turn-1"),
132 payload: {
133 title: "Run tests",
134 itemType: "command_execution",
135 detail: "/bin/zsh -lc 'bun run test'",
136 },
137 }),
138 makeActivity({
139 id: EventId.make("tool-completed"),
140 kind: "tool.completed",
141 tone: "tool",
142 summary: "Run tests completed",
143 createdAt: "2026-04-01T00:00:02.000Z",
144 turnId: TurnId.make("turn-1"),
145 payload: {
146 title: "Run tests",
147 itemType: "command_execution",
148 detail: "/bin/zsh -lc 'bun run test'",
149 },
150 }),
151 ],
152 });
153​
154 const feed = buildThreadFeed(thread);
155 const group = feed[0];
156​
157 expect(group).toMatchObject({
158 type: "activity-group",
159 });
160 if (!group || group.type !== "activity-group") {
161 return;
162 }
163​
164 expect(group.activities).toEqual([
165 {
166 id: "tool-completed",
167 createdAt: "2026-04-01T00:00:02.000Z",
168 turnId: "turn-1",
169 summary: "Run tests",
170 detail: "bun run test",
171 fullDetail: "/bin/zsh -lc 'bun run test'",
172 copyText: "Run tests\nbun run test\n/bin/zsh -lc 'bun run test'",
173 icon: "command",
174 toolLike: true,
175 status: "success",
176 },
177 ]);
178 });
179​
180 it("keeps MCP inputs available to expanded mobile work rows", () => {
181 const turnId = TurnId.make("turn-mcp");
182 const thread = makeThread({
183 id: ThreadId.make("thread-mcp"),
184 projectId: ProjectId.make("project-1"),
185 title: "Expandable MCP call",
186 latestTurn: {
187 turnId,
188 state: "completed",
189 requestedAt: "2026-04-01T00:00:00.000Z",
190 startedAt: "2026-04-01T00:00:01.000Z",
191 completedAt: "2026-04-01T00:00:03.000Z",
192 assistantMessageId: null,
193 },
194 activities: [
195 makeActivity({
196 id: EventId.make("mcp-completed"),
197 kind: "tool.completed",
198 tone: "tool",
199 summary: "Call repository tool",
200 createdAt: "2026-04-01T00:00:02.000Z",
201 turnId,
202 payload: {
203 title: "Call repository tool",
204 itemType: "mcp_tool_call",
205 detail: "repository.search",
206 status: "completed",
207 data: {
208 item: {
209 server: "repository",
210 tool: "search",
211 arguments: { query: "work log" },
212 },
213 },
214 },
215 }),
216 ],
217 });
218​
219 const group = buildThreadFeed(thread)[0];
220 expect(group).toMatchObject({ type: "activity-group" });
221 if (!group || group.type !== "activity-group") {
222 return;
223 }
224​
225 expect(group.activities[0]?.icon).toBe("wrench");
226 expect(group.activities[0]?.fullDetail).toContain('"query": "work log"');
227 expect(group.activities[0]?.fullDetail).toContain("repository.search");
228 });
229​
230 it("folds settled turn work while leaving the terminal answer visible", () => {
231 const turnId = TurnId.make("turn-1");
232 const thread = makeThread({
233 id: ThreadId.make("thread-3"),
234 projectId: ProjectId.make("project-1"),
235 title: "Folded work",
236 latestTurn: {
237 turnId,
238 state: "completed",
239 requestedAt: "2026-04-01T00:00:00.000Z",
240 startedAt: "2026-04-01T00:00:01.000Z",
241 completedAt: "2026-04-01T00:00:18.000Z",
242 assistantMessageId: MessageId.make("assistant-final"),
243 },
244 messages: [
245 {
246 id: MessageId.make("assistant-commentary"),
247 role: "assistant",
248 text: "I am checking.",
249 turnId,
250 streaming: false,
251 createdAt: "2026-04-01T00:00:02.000Z",
252 updatedAt: "2026-04-01T00:00:03.000Z",
253 },
254 {
255 id: MessageId.make("assistant-final"),
256 role: "assistant",
257 text: "Done.",
258 turnId,
259 streaming: false,
260 createdAt: "2026-04-01T00:00:17.000Z",
261 updatedAt: "2026-04-01T00:00:18.000Z",
262 },
263 ],
264 activities: [
265 makeActivity({
266 id: EventId.make("tool-completed"),
267 kind: "tool.completed",
268 tone: "tool",
269 summary: "Read files",
270 createdAt: "2026-04-01T00:00:05.000Z",
271 turnId,
272 payload: {
273 title: "Read files",
274 itemType: "file_read",
275 status: "completed",
276 },
277 }),
278 ],
279 });
280​
281 const feed = buildThreadFeed(thread);
282 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
283 expect(collapsed.map((entry) => entry.id)).toEqual(["turn-fold:turn-1", "assistant-final"]);
284 expect(collapsed[0]).toMatchObject({
285 type: "turn-fold",
286 label: "Worked for 17s",
287 expanded: false,
288 });
289​
290 const expanded = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set([turnId]));
291 expect(expanded.map((entry) => entry.id)).toEqual([
292 "turn-fold:turn-1",
293 "assistant-commentary",
294 "tool-completed",
295 "assistant-final",
296 ]);
297 });
298​
299 it("measures a steer-superseded turn from its user boundary through trailing work", () => {
300 const firstTurnId = TurnId.make("turn-1");
301 const secondTurnId = TurnId.make("turn-2");
302 const thread = makeThread({
303 id: ThreadId.make("thread-steered"),
304 projectId: ProjectId.make("project-1"),
305 title: "Steered work",
306 latestTurn: {
307 turnId: secondTurnId,
308 state: "running",
309 requestedAt: "2026-04-01T00:00:14.000Z",
310 startedAt: "2026-04-01T00:00:14.000Z",
311 completedAt: null,
312 assistantMessageId: MessageId.make("assistant-next"),
313 },
314 messages: [
315 {
316 id: MessageId.make("user-1"),
317 role: "user",
318 text: "Do it once more.",
319 turnId: null,
320 streaming: false,
321 createdAt: "2026-04-01T00:00:00.000Z",
322 updatedAt: "2026-04-01T00:00:00.000Z",
323 },
324 {
325 id: MessageId.make("assistant-commentary"),
326 role: "assistant",
327 text: "Kicking off call 1.",
328 turnId: firstTurnId,
329 streaming: false,
330 createdAt: "2026-04-01T00:00:09.000Z",
331 updatedAt: "2026-04-01T00:00:09.000Z",
332 },
333 {
334 id: MessageId.make("user-2"),
335 role: "user",
336 text: "Actually do 15.",
337 turnId: null,
338 streaming: false,
339 createdAt: "2026-04-01T00:00:14.000Z",
340 updatedAt: "2026-04-01T00:00:14.000Z",
341 },
342 {
343 id: MessageId.make("assistant-next"),
344 role: "assistant",
345 text: "One down - adjusting.",
346 turnId: secondTurnId,
347 streaming: true,
348 createdAt: "2026-04-01T00:00:17.000Z",
349 updatedAt: "2026-04-01T00:00:17.000Z",
350 },
351 ],
352 activities: [
353 makeActivity({
354 id: EventId.make("work-1"),
355 kind: "tool.completed",
356 tone: "tool",
357 summary: "Ran command",
358 createdAt: "2026-04-01T00:00:12.000Z",
359 turnId: firstTurnId,
360 payload: {
361 title: "Ran command",
362 itemType: "command_execution",
363 status: "completed",
364 },
365 }),
366 ],
367 });
368​
369 const feed = buildThreadFeed(thread);
370 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
371 expect(collapsed.find((entry) => entry.type === "turn-fold")).toMatchObject({
372 turnId: firstTurnId,
373 label: "Worked for 12s",
374 });
375 });
376​
377 it("keeps an active turn expanded and classifies error-shaped tool output", () => {
378 const turnId = TurnId.make("turn-running");
379 const thread = makeThread({
380 id: ThreadId.make("thread-4"),
381 projectId: ProjectId.make("project-1"),
382 title: "Running work",
383 latestTurn: {
384 turnId,
385 state: "running",
386 requestedAt: "2026-04-01T00:00:00.000Z",
387 startedAt: "2026-04-01T00:00:01.000Z",
388 completedAt: null,
389 assistantMessageId: null,
390 },
391 activities: [
392 makeActivity({
393 id: EventId.make("tool-failed"),
394 kind: "tool.completed",
395 tone: "tool",
396 summary: "Run command",
397 createdAt: "2026-04-01T00:00:05.000Z",
398 turnId,
399 payload: {
400 title: "Run command",
401 itemType: "command_execution",
402 detail: "zsh: command not found: nope",
403 status: "completed",
404 },
405 }),
406 ],
407 });
408​
409 const feed = buildThreadFeed(thread);
410 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toEqual(feed);
411 expect(feed[0]).toMatchObject({
412 type: "activity-group",
413 activities: [{ status: "failure" }],
414 });
415 });
416​
417 it("appends active work as a normal timeline row", () => {
418 const startedAt = "2026-04-01T00:00:01.000Z";
419 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
420​
421 expect(presented).toEqual([
422 {
423 type: "working",
424 id: "working-indicator-row",
425 createdAt: startedAt,
426 },
427 ]);
428 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
429 });
430​
431 it("models work-log overflow as list rows", () => {
432 const activity = (
433 id: string,
434 createdAt: string,
435 status: ThreadFeedActivity["status"] = "success",
436 ): ThreadFeedActivity => ({
437 id,
438 createdAt,
439 turnId: null,
440 summary: `Tool ${id}`,
441 detail: null,
442 fullDetail: null,
443 copyText: id,
444 icon: "command",
445 toolLike: true,
446 status,
447 });
448 const feed: ThreadFeedEntry[] = [
449 {
450 type: "activity-group",
451 id: "work-group-1",
452 createdAt: "2026-04-01T00:00:01.000Z",
453 turnId: null,
454 activities: [
455 activity("activity-1", "2026-04-01T00:00:01.000Z"),
456 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
457 activity("activity-2", "2026-04-01T00:00:03.000Z"),
458 activity("activity-3", "2026-04-01T00:00:04.000Z"),
459 ],
460 },
461 ];
462​
463 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
464 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
465 expect(collapsed[1]).toMatchObject({
466 type: "work-toggle",
467 groupId: "work-group-1",
468 hiddenCount: 2,
469 expanded: false,
470 });
471​
472 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
473 expect(expanded.map((entry) => entry.id)).toEqual([
474 "activity-1",
475 "activity-2",
476 "activity-3",
477 "work-toggle:work-group-1",
478 ]);
479 expect(expanded.at(-1)).toMatchObject({
480 type: "work-toggle",
481 expanded: true,
482 });
483 });
484});