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

threadActivity.test.ts

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