import { MessageId, RunId, ThreadId, TurnItemId, type OrchestrationV2ProjectedTurnItem, type OrchestrationV2TurnItem, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import { describe, expect, it } from "vite-plus/test"; import { buildThreadFeed, deriveThreadFeedPresentation } from "./threadActivity"; const threadId = ThreadId.make("thread-1"); const sourceThreadId = ThreadId.make("thread-source"); const runId = RunId.make("run-1"); function base(id: string, updatedAt: string, ordinal: number) { const timestamp = DateTime.makeUnsafe(updatedAt); return { id: TurnItemId.make(id), threadId, runId, nodeId: null, providerThreadId: null, providerTurnId: null, nativeItemRef: null, parentItemId: null, ordinal, status: "completed" as const, title: null, startedAt: timestamp, completedAt: timestamp, updatedAt: timestamp, }; } function projected( item: OrchestrationV2TurnItem, position: number, visibility: OrchestrationV2ProjectedTurnItem["visibility"] = "local", ): OrchestrationV2ProjectedTurnItem { return { position, visibility, sourceThreadId: visibility === "local" ? threadId : sourceThreadId, sourceItemId: item.id, item, }; } function userMessage(updatedAt = "2026-06-20T00:00:01.000Z") { return { ...base("item-user", updatedAt, 0), type: "user_message" as const, messageId: MessageId.make("message-user"), createdBy: "user" as const, creationSource: "mobile" as const, inputIntent: "turn_start" as const, text: "Run checks", attachments: [], }; } function command(updatedAt = "2026-06-20T00:00:02.000Z") { return { ...base("item-command", updatedAt, 1), type: "command_execution" as const, input: "vp check", output: "ok", exitCode: 0, }; } function assistantMessage(updatedAt = "2026-06-20T00:00:03.000Z") { return { ...base("item-assistant", updatedAt, 2), type: "assistant_message" as const, messageId: MessageId.make("message-assistant"), text: "Done", streaming: false, }; } describe("buildThreadFeed", () => { it("preserves authoritative V2 order instead of sorting reconstructed collections", () => { const rows = [ projected(userMessage("2026-06-20T00:00:03.000Z"), 0), projected(command("2026-06-20T00:00:01.000Z"), 1), projected(assistantMessage("2026-06-20T00:00:02.000Z"), 2), ]; const feed = buildThreadFeed(rows); expect(feed.map((entry) => entry.type)).toEqual(["message", "activity-group", "message"]); expect(feed.map((entry) => entry.id)).toEqual([ "message-user", "local:thread-1:item-command", "message-assistant", ]); const activity = feed.find((entry) => entry.type === "activity-group")?.activities[0]; expect(activity?.projectedItem).toBe(rows[1]); expect(activity?.fullDetail).toContain('"input": "vp check"'); }); it("retains inherited and synthetic rows with their original projected identity", () => { const inherited = projected(command(), 0, "inherited"); const { providerThreadId: _providerThreadId, ...forkBase } = base( "item-fork", "2026-06-20T00:00:03.000Z", 2, ); const synthetic = projected( { ...forkBase, type: "fork", source: { type: "run", threadId: sourceThreadId, runId }, targetThreadId: threadId, }, 1, "synthetic", ); const feed = buildThreadFeed([inherited, synthetic]); const activities = feed.flatMap((entry) => entry.type === "activity-group" ? entry.activities : [], ); expect(activities.map((activity) => activity.projectedItem)).toEqual([inherited, synthetic]); expect(activities.map((activity) => activity.projectedItem.visibility)).toEqual([ "inherited", "synthetic", ]); }); it("folds settled V2 run work while keeping the terminal assistant message visible", () => { const feed = buildThreadFeed([ projected(userMessage(), 0), projected(command(), 1), projected(assistantMessage(), 2), ]); const latestRun = { runId, status: "completed" as const, startedAt: "2026-06-20T00:00:01.000Z", completedAt: "2026-06-20T00:00:03.000Z", }; const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set()); expect(collapsed.map((entry) => entry.type)).toEqual(["message", "run-fold", "message"]); const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId])); expect(expanded.map((entry) => entry.type)).toEqual([ "message", "run-fold", "activity-group", "message", ]); }); it("keeps an active run expanded and marks failed tools as failures", () => { const failedCommand: OrchestrationV2TurnItem = { ...command(), status: "failed", completedAt: DateTime.makeUnsafe("2026-06-20T00:00:02.000Z"), }; const feed = buildThreadFeed([projected(userMessage(), 0), projected(failedCommand, 1)]); const presented = deriveThreadFeedPresentation( feed, { runId, status: "running", startedAt: "2026-06-20T00:00:01.000Z", completedAt: null, }, new Set(), ); expect(presented.some((entry) => entry.type === "run-fold")).toBe(false); expect(presented.find((entry) => entry.type === "activity-group")?.activities[0]?.status).toBe( "failure", ); }); it("appends active work as a normal timeline row", () => { const startedAt = "2026-04-01T00:00:01.000Z"; const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt); expect(presented).toEqual([ { type: "working", id: "working-indicator-row", createdAt: startedAt, }, ]); expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]); }); it("models work-log overflow as list rows", () => { const activity = ( id: string, createdAt: string, status: ThreadFeedActivity["status"] = "success", ): ThreadFeedActivity => ({ id, createdAt, turnId: null, summary: `Tool ${id}`, detail: null, canExpand: false, getFullDetail: () => null, getCopyText: () => id, icon: "command", toolLike: true, status, }); const feed: ThreadFeedEntry[] = [ { type: "activity-group", id: "work-group-1", createdAt: "2026-04-01T00:00:01.000Z", turnId: null, activities: [ activity("activity-1", "2026-04-01T00:00:01.000Z"), activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"), activity("activity-2", "2026-04-01T00:00:03.000Z"), activity("activity-3", "2026-04-01T00:00:04.000Z"), ], }, ]; const collapsed = deriveThreadFeedPresentation(feed, null, new Set()); expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]); expect(collapsed[1]).toMatchObject({ type: "work-toggle", groupId: "work-group-1", hiddenCount: 2, expanded: false, }); const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"])); expect(expanded.map((entry) => entry.id)).toEqual([ "activity-1", "activity-2", "activity-3", "work-toggle:work-group-1", ]); expect(expanded.at(-1)).toMatchObject({ type: "work-toggle", expanded: true, }); }); }); describe("quiet timeline: nested agents", () => { it("keeps a nested agent's terminal row but hides its background work", () => { const thread = makeThread({ id: ThreadId.make("thread-nested"), projectId: ProjectId.make("project-1"), title: "Nested agents", activities: [ // A subagent's own shell: internal, covered by the owner's liveness. makeActivity({ id: EventId.make("shell-done"), kind: "task.completed", summary: "Task completed", createdAt: "2026-04-01T00:00:02.000Z", payload: { taskId: "sh-1", agentId: "owner", agentKind: "background" }, }), // A nested AGENT's completion: mobile has no Agents sheet, so this // terminal row is the only signal it ever finished. makeActivity({ id: EventId.make("nested-done"), kind: "task.completed", summary: "Task completed", createdAt: "2026-04-01T00:00:03.000Z", payload: { taskId: "n-1", agentId: "owner", agentKind: "agent" }, }), ], }); const feed = buildThreadFeed(thread); const ids = feed.flatMap((entry) => entry.type === "activity-group" ? entry.activities.map((row) => row.id) : [], ); expect(ids).toContain("nested-done"); expect(ids).not.toContain("shell-done"); }); });