Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 CommandId,
4 ComposerContextId,
5 EnvironmentId,
6 MessageId,
7 ProjectId,
8 ThreadId,
9} from "@t3tools/contracts";
10import type { QueuedThreadMessage } from "../../state/thread-outbox-model";
11import {
12 appendPendingThreadMessages,
13 isCompactionCommandFeedEntry,
14 selectPendingAcknowledgedMessages,
15} from "./pending-thread-feed";
16
17const pending = (id: string): QueuedThreadMessage => ({
18 environmentId: EnvironmentId.make("env"),
19 threadId: ThreadId.make("thread"),
20 messageId: MessageId.make(id),
21 commandId: CommandId.make(id),
22 text: id,
23 attachments: [],
24 createdAt: "2026-09-06T10:00:00.000Z",
25});
26
27describe("pending timeline messages", () => {
28 it.each([false, true])(
29 "keeps a queued compact command in the pending message row (attachment: %s)",
30 (withAttachment) => {
31 const queued = {
32 ...pending("compact"),
33 text: "/compact",
34 attachments: withAttachment
35 ? [
36 {
37 id: "image-1",
38 type: "image" as const,
39 name: "photo.png",
40 mimeType: "image/png",
41 sizeBytes: 3,
42 fileUri: "file:///documents/photo.png",
43 previewUri: "file:///documents/photo.png",
44 },
45 ]
46 : [],
47 };
48 const entry = appendPendingThreadMessages([], [], [queued])[0]!;
49 expect(isCompactionCommandFeedEntry(entry)).toBe(false);
50 expect(entry.pendingMessage).toEqual(queued);
51 if (!withAttachment) {
52 expect(isCompactionCommandFeedEntry({ ...entry, pendingMessage: undefined })).toBe(true);
53 }
54 },
55 );
56
57 it("marks the synthetic creation prompt pending without duplicating it", () => {
58 const queued = {
59 ...pending("creation"),
60 creation: {
61 projectId: ProjectId.make("project"),
62 workspaceMode: "local" as const,
63 branch: "main",
64 worktreePath: null,
65 },
66 };
67 const prompt = appendPendingThreadMessages([], [], [queued])[0]!;
68 const synthetic = { ...prompt, pendingMessage: undefined };
69 const result = appendPendingThreadMessages([synthetic], [synthetic], [queued]);
70 expect(result).toHaveLength(1);
71 expect(result[0]?.pendingMessage).toEqual(queued);
72 expect(result[0]?.acknowledged).toBe(false);
73 });
74
75 it("retains context records while a message is waiting for delivery", () => {
76 const record = {
77 version: 1 as const,
78 kind: "mention" as const,
79 contextId: ComposerContextId.make("setup-file"),
80 label: "Checkout.tsx",
81 path: "src/Checkout.tsx",
82 };
83 const context = { version: 1 as const, records: [record] };
84 const text = "[Checkout.tsx](t3-context://v1/mention/setup-file)";
85 const entries = appendPendingThreadMessages([], [], [{ ...pending("context"), text, context }]);
86 const entry = entries[0];
87 expect(entry?.type).toBe("message");
88 if (entry?.type !== "message") throw new Error("Expected a pending message");
89 expect(entry.message.text).toBe(text);
90 expect(entry.message.context).toEqual(context);
91 });
92
93 it("keeps pending messages after newer agent activity in queue order", () => {
94 const activity = {
95 type: "thinking",
96 runId: null,
97 id: "thinking",
98 createdAt: "2026-09-06T11:00:00.000Z",
99 } as const;
100 const entries = appendPendingThreadMessages(
101 [activity],
102 [],
103 [pending("first"), pending("second")],
104 );
105 expect(entries.map((entry) => entry.id)).toEqual(["thinking", "first", "second"]);
106 expect(entries[1]?.pendingMessage?.text).toBe("first");
107 });
108
109 it("reuses the message id and suppresses the pending copy when delivery appears", () => {
110 const queued = pending("sent");
111 const optimistic = appendPendingThreadMessages([], [], [queued])[0]!;
112 const delivered = { ...optimistic, pendingMessage: undefined };
113 expect(appendPendingThreadMessages([delivered], [delivered], [queued])).toEqual([delivered]);
114 expect(appendPendingThreadMessages([], [delivered], [queued])).toEqual([]);
115 });
116
117 it("stops showing an acknowledged message once the projection holds it, even when the feed hides it", () => {
118 // A rolled-back or cancelled queued run keeps its message in projection.messages
119 // while its user_message item leaves the visible feed.
120 const echoed = pending("rolled-back");
121 const waiting = pending("waiting");
122 const shown = selectPendingAcknowledgedMessages([echoed, waiting], {
123 threadKey: "env:thread",
124 queuedMessages: [],
125 projectedMessages: [{ id: echoed.messageId }],
126 });
127 expect(shown).toEqual([waiting]);
128 expect(appendPendingThreadMessages([], [], shown).map((entry) => entry.id)).toEqual([
129 "waiting",
130 ]);
131 });
132});