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

threadActivity.test.ts

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