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 type ThreadFeedActivity,
16 type ThreadFeedEntry,
17} from "./threadActivity";
18
19const threadId = ThreadId.make("thread-1");
20const sourceThreadId = ThreadId.make("thread-source");
21const runId = RunId.make("run-1");
22
23function base(id: string, updatedAt: string, ordinal: number) {
24 const timestamp = DateTime.makeUnsafe(updatedAt);
25 return {
26 id: TurnItemId.make(id),
27 threadId,
28 runId,
29 nodeId: null,
30 providerThreadId: null,
31 providerTurnId: null,
32 nativeItemRef: null,
33 parentItemId: null,
34 ordinal,
35 status: "completed" as const,
36 title: null,
37 startedAt: timestamp,
38 completedAt: timestamp,
39 updatedAt: timestamp,
40 };
41}
42
43function projected(
44 item: OrchestrationV2TurnItem,
45 position: number,
46 visibility: OrchestrationV2ProjectedTurnItem["visibility"] = "local",
47): OrchestrationV2ProjectedTurnItem {
48 return {
49 position,
50 visibility,
51 sourceThreadId: visibility === "local" ? threadId : sourceThreadId,
52 sourceItemId: item.id,
53 item,
54 };
55}
56
57function userMessage(updatedAt = "2026-06-20T00:00:01.000Z") {
58 return {
59 ...base("item-user", updatedAt, 0),
60 type: "user_message" as const,
61 messageId: MessageId.make("message-user"),
62 createdBy: "user" as const,
63 creationSource: "mobile" as const,
64 inputIntent: "turn_start" as const,
65 text: "Run checks",
66 attachments: [],
67 };
68}
69
70function command(updatedAt = "2026-06-20T00:00:02.000Z") {
71 return {
72 ...base("item-command", updatedAt, 1),
73 type: "command_execution" as const,
74 input: "vp check",
75 output: "ok",
76 exitCode: 0,
77 };
78}
79
80function assistantMessage(updatedAt = "2026-06-20T00:00:03.000Z") {
81 return {
82 ...base("item-assistant", updatedAt, 2),
83 type: "assistant_message" as const,
84 messageId: MessageId.make("message-assistant"),
85 text: "Done",
86 streaming: false,
87 };
88}
89
90describe("buildThreadFeed", () => {
91 it("preserves authoritative V2 order instead of sorting reconstructed collections", () => {
92 const rows = [
93 projected(userMessage("2026-06-20T00:00:03.000Z"), 0),
94 projected(command("2026-06-20T00:00:01.000Z"), 1),
95 projected(assistantMessage("2026-06-20T00:00:02.000Z"), 2),
96 ];
97
98 const feed = buildThreadFeed(rows);
99 expect(feed.map((entry) => entry.type)).toEqual(["message", "activity-group", "message"]);
100 expect(feed.map((entry) => entry.id)).toEqual([
101 "message-user",
102 "local:thread-1:item-command",
103 "message-assistant",
104 ]);
105 const activity = feed.find((entry) => entry.type === "activity-group")?.activities[0];
106 expect(activity?.projectedItem).toBe(rows[1]);
107 expect(activity?.fullDetail).toContain('"input": "vp check"');
108 });
109
110 it("retains inherited and synthetic rows with their original projected identity", () => {
111 const inherited = projected(command(), 0, "inherited");
112 const { providerThreadId: _providerThreadId, ...forkBase } = base(
113 "item-fork",
114 "2026-06-20T00:00:03.000Z",
115 2,
116 );
117 const synthetic = projected(
118 {
119 ...forkBase,
120 type: "fork",
121 source: { type: "run", threadId: sourceThreadId, runId },
122 targetThreadId: threadId,
123 },
124 1,
125 "synthetic",
126 );
127
128 const feed = buildThreadFeed([inherited, synthetic]);
129 const activities = feed.flatMap((entry) =>
130 entry.type === "activity-group" ? entry.activities : [],
131 );
132 expect(activities.map((activity) => activity.projectedItem)).toEqual([inherited, synthetic]);
133 expect(activities.map((activity) => activity.projectedItem.visibility)).toEqual([
134 "inherited",
135 "synthetic",
136 ]);
137 expect(activities.at(-1)?.prominent).toBe(true);
138 });
139
140 it("keeps orchestration relationship cards visible when a completed run is folded", () => {
141 const { providerThreadId: _providerThreadId, ...forkBase } = base(
142 "item-fork",
143 "2026-06-20T00:00:02.500Z",
144 2,
145 );
146 const feed = buildThreadFeed([
147 projected(userMessage(), 0),
148 projected(command(), 1),
149 projected(
150 {
151 ...forkBase,
152 type: "fork",
153 source: { type: "run", threadId, runId },
154 targetThreadId: sourceThreadId,
155 },
156 2,
157 ),
158 projected(assistantMessage(), 3),
159 ]);
160
161 const collapsed = deriveThreadFeedPresentation(
162 feed,
163 {
164 runId,
165 status: "completed",
166 startedAt: "2026-06-20T00:00:01.000Z",
167 completedAt: "2026-06-20T00:00:03.000Z",
168 },
169 new Set(),
170 );
171
172 expect(
173 collapsed.some(
174 (entry) =>
175 entry.type === "activity-group" &&
176 entry.activities.some((activity) => activity.projectedItem.item.type === "fork"),
177 ),
178 ).toBe(true);
179 expect(
180 collapsed.some(
181 (entry) =>
182 entry.type === "activity-group" &&
183 entry.activities.some(
184 (activity) => activity.projectedItem.item.type === "command_execution",
185 ),
186 ),
187 ).toBe(false);
188 });
189
190 it("folds settled V2 run work while keeping the terminal assistant message visible", () => {
191 const feed = buildThreadFeed([
192 projected(userMessage(), 0),
193 projected(command(), 1),
194 projected(assistantMessage(), 2),
195 ]);
196 const latestRun = {
197 runId,
198 status: "completed" as const,
199 startedAt: "2026-06-20T00:00:01.000Z",
200 completedAt: "2026-06-20T00:00:03.000Z",
201 };
202
203 const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set());
204 expect(collapsed.map((entry) => entry.type)).toEqual(["message", "run-fold", "message"]);
205
206 const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId]));
207 expect(expanded.map((entry) => entry.type)).toEqual([
208 "message",
209 "run-fold",
210 "activity-group",
211 "message",
212 ]);
213 });
214
215 it("keeps an active run expanded and marks failed tools as failures", () => {
216 const failedCommand: OrchestrationV2TurnItem = {
217 ...command(),
218 status: "failed",
219 completedAt: DateTime.makeUnsafe("2026-06-20T00:00:02.000Z"),
220 };
221 const feed = buildThreadFeed([projected(userMessage(), 0), projected(failedCommand, 1)]);
222 const presented = deriveThreadFeedPresentation(
223 feed,
224 {
225 runId,
226 status: "running",
227 startedAt: "2026-06-20T00:00:01.000Z",
228 completedAt: null,
229 },
230 new Set(),
231 );
232
233 expect(presented.some((entry) => entry.type === "run-fold")).toBe(false);
234 expect(presented.find((entry) => entry.type === "activity-group")?.activities[0]?.status).toBe(
235 "failure",
236 );
237 });
238
239 it("keeps work-log overflow inside grouped activity rows", () => {
240 const activity = (
241 id: string,
242 createdAt: string,
243 status: ThreadFeedActivity["status"] = "success",
244 ): ThreadFeedActivity => ({
245 id,
246 createdAt,
247 runId: null,
248 summary: `Tool ${id}`,
249 detail: null,
250 fullDetail: null,
251 copyText: id,
252 icon: "command",
253 toolLike: true,
254 prominent: false,
255 status,
256 projectedItem: projected(command(createdAt), 0),
257 });
258 const feed: ThreadFeedEntry[] = [
259 {
260 type: "activity-group",
261 id: "work-group-1",
262 createdAt: "2026-04-01T00:00:01.000Z",
263 runId: null,
264 activities: [
265 activity("activity-1", "2026-04-01T00:00:01.000Z"),
266 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
267 activity("activity-2", "2026-04-01T00:00:03.000Z"),
268 activity("activity-3", "2026-04-01T00:00:04.000Z"),
269 ],
270 },
271 ];
272
273 const presented = deriveThreadFeedPresentation(feed, null, new Set());
274 expect(presented).toHaveLength(1);
275 expect(presented[0]).toMatchObject({
276 type: "activity-group",
277 id: "work-group-1",
278 });
279 expect(
280 presented[0]?.type === "activity-group"
281 ? presented[0].activities.map((entry) => entry.id)
282 : [],
283 ).toEqual(["activity-1", "activity-neutral", "activity-2", "activity-3"]);
284 });
285});