@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 259 lines 9.76 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2 import {
3 CommandId,
4 ComposerContextId,
5 EnvironmentId,
6 MessageId,
7 ProjectId,
8 ThreadId,
9 } from "@t3tools/contracts";
10 import type { QueuedThreadMessage } from "../../state/thread-outbox-model";
11 import {
12 appendAcceptedThreadMessages,
13 appendPendingThreadMessages,
14 createPendingTurnAppender,
15 createThreadFeedGrouping,
16 isCompactionCommandFeedEntry,
17 selectPendingAcknowledgedMessages,
18 } from "./pending-thread-feed";
19
20 const pending = (id: string): QueuedThreadMessage => ({
21 environmentId: EnvironmentId.make("env"),
22 threadId: ThreadId.make("thread"),
23 messageId: MessageId.make(id),
24 commandId: CommandId.make(id),
25 text: id,
26 attachments: [],
27 createdAt: "2026-09-06T10:00:00.000Z",
28 });
29
30 describe("pending timeline messages", () => {
31 it("keeps unchanged row identities while text streams and updates adjacent grouping", () => {
32 const group = createThreadFeedGrouping();
33 const [first, second, third] = appendPendingThreadMessages(
34 [],
35 [],
36 [pending("first"), pending("second"), pending("third")],
37 );
38 if (first?.type !== "message" || second?.type !== "message" || third?.type !== "message") {
39 throw new Error("Expected message rows");
40 }
41 const reply = {
42 ...third,
43 id: "reply",
44 message: { ...third.message, role: "assistant" as const, text: "start" },
45 };
46 const initial = group([first, second, reply], {});
47 const streamed = group(
48 [first, second, { ...reply, message: { ...reply.message, text: "streamed" } }],
49 {},
50 );
51 expect(streamed[0]).toBe(initial[0]);
52 expect(streamed[1]).toBe(initial[1]);
53 expect(streamed[2]).not.toBe(initial[2]);
54 const appended = group([first, second, third, reply], {});
55 expect(appended[0]).toBe(initial[0]);
56 expect(appended[1]).not.toBe(initial[1]);
57 expect(appended[1]?.userMessageGrouping.groupedWithNext).toBe(true);
58 expect(appended[2]?.userMessageGrouping.groupedWithPrevious).toBe(true);
59 const removed = group([first, second, reply], {});
60 expect(removed[1]?.userMessageGrouping.groupedWithNext).toBe(false);
61 const steer = {
62 ...second,
63 message: { ...second.message, inputIntent: "promoted_queued_to_steer" as const },
64 };
65 const singleSteer = group([first, steer], {});
66 const steers = group(
67 [{ ...first, message: { ...first.message, inputIntent: "steer" } }, steer],
68 {},
69 );
70 expect(steers[1]?.userMessageGrouping.continuesSteer).toBe(true);
71 expect(singleSteer[1]?.userMessageGrouping.continuesSteer).toBe(false);
72 expect(steers[1]).not.toBe(singleSteer[1]);
73 });
74
75 it("preserves setup, workflow, and compaction grouping boundaries", () => {
76 const group = createThreadFeedGrouping();
77 const feed = appendPendingThreadMessages([], [], [pending("first"), pending("second")]);
78 expect(group(feed, {})[0]?.userMessageGrouping.groupedWithNext).toBe(true);
79 for (const options of [{ setupAnchorIndex: 0 }, { workflowOpeningMessageId: "first" }]) {
80 const rows = group(feed, options);
81 expect(rows[0]?.userMessageGrouping.groupedWithNext).toBe(false);
82 expect(rows[1]?.userMessageGrouping.groupedWithPrevious).toBe(false);
83 }
84 const compact = appendPendingThreadMessages(
85 [],
86 [],
87 [{ ...pending("compact"), text: "/compact" }],
88 )[0]!;
89 expect(group([feed[0]!, compact], {})[0]?.userMessageGrouping.groupedWithNext).toBe(true);
90 expect(
91 group([feed[0]!, { ...compact, pendingMessage: undefined }], {})[0]?.userMessageGrouping
92 .groupedWithNext,
93 ).toBe(false);
94 });
95
96 it.each([false, true])(
97 "keeps a queued compact command in the pending message row (attachment: %s)",
98 (withAttachment) => {
99 const queued = {
100 ...pending("compact"),
101 text: "/compact",
102 attachments: withAttachment
103 ? [
104 {
105 id: "image-1",
106 type: "image" as const,
107 name: "photo.png",
108 mimeType: "image/png",
109 sizeBytes: 3,
110 fileUri: "file:///documents/photo.png",
111 previewUri: "file:///documents/photo.png",
112 },
113 ]
114 : [],
115 };
116 const entry = appendPendingThreadMessages([], [], [queued])[0]!;
117 expect(isCompactionCommandFeedEntry(entry)).toBe(false);
118 expect(entry.pendingMessage).toEqual(queued);
119 if (!withAttachment) {
120 expect(isCompactionCommandFeedEntry({ ...entry, pendingMessage: undefined })).toBe(true);
121 }
122 },
123 );
124
125 it("marks the synthetic creation prompt pending without duplicating it", () => {
126 const queued = {
127 ...pending("creation"),
128 creation: {
129 projectId: ProjectId.make("project"),
130 workspaceMode: "local" as const,
131 branch: "main",
132 worktreePath: null,
133 },
134 };
135 const prompt = appendPendingThreadMessages([], [], [queued])[0]!;
136 const synthetic = { ...prompt, pendingMessage: undefined };
137 const result = appendPendingThreadMessages([synthetic], [synthetic], [queued]);
138 expect(result).toHaveLength(1);
139 expect(result[0]?.pendingMessage).toEqual(queued);
140 expect(result[0]?.acknowledged).toBe(false);
141 });
142
143 it("keeps a new thread's first message attachments after delivery until the server copy loads", () => {
144 const video = {
145 id: "video-1",
146 type: "file" as const,
147 name: "clip.mp4",
148 mimeType: "video/mp4",
149 sizeBytes: 3,
150 fileUri: "file:///documents/clip.mp4",
151 };
152 const creation = {
153 ...pending("creation"),
154 attachments: [video],
155 creation: {
156 projectId: ProjectId.make("project"),
157 workspaceMode: "worktree" as const,
158 branch: "main",
159 worktreePath: null,
160 },
161 };
162 const synthetic = {
163 ...appendPendingThreadMessages([], [], [creation])[0]!,
164 pendingMessage: undefined,
165 };
166 // Delivered and caught up by sequence, but the server's copy is not projected yet.
167 const result = appendAcceptedThreadMessages([synthetic], [], creation);
168 expect(result).toHaveLength(1);
169 expect(result[0]?.pendingMessage?.attachments).toEqual([video]);
170 expect(result[0]?.acknowledged).toBe(true);
171 });
172
173 it("retains context records while a message is waiting for delivery", () => {
174 const record = {
175 version: 1 as const,
176 kind: "mention" as const,
177 contextId: ComposerContextId.make("setup-file"),
178 label: "Checkout.tsx",
179 path: "src/Checkout.tsx",
180 };
181 const context = { version: 1 as const, records: [record] };
182 const text = "[Checkout.tsx](t3-context://v1/mention/setup-file)";
183 const entries = appendPendingThreadMessages([], [], [{ ...pending("context"), text, context }]);
184 const entry = entries[0];
185 expect(entry?.type).toBe("message");
186 if (entry?.type !== "message") throw new Error("Expected a pending message");
187 expect(entry.message.text).toBe(text);
188 expect(entry.message.context).toEqual(context);
189 });
190
191 it("keeps pending messages after newer agent activity in queue order", () => {
192 const activity = {
193 type: "thinking",
194 runId: null,
195 id: "thinking",
196 createdAt: "2026-09-06T11:00:00.000Z",
197 } as const;
198 const entries = appendPendingThreadMessages(
199 [activity],
200 [],
201 [pending("first"), pending("second")],
202 );
203 expect(entries.map((entry) => entry.id)).toEqual(["thinking", "first", "second"]);
204 expect(entries[1]?.pendingMessage?.text).toBe("first");
205 });
206
207 it("reuses the message id and suppresses the pending copy when delivery appears", () => {
208 const queued = pending("sent");
209 const optimistic = appendPendingThreadMessages([], [], [queued])[0]!;
210 const delivered = { ...optimistic, pendingMessage: undefined };
211 expect(appendPendingThreadMessages([delivered], [delivered], [queued])).toEqual([delivered]);
212 expect(appendPendingThreadMessages([], [delivered], [queued])).toEqual([]);
213 });
214
215 it("stops showing an acknowledged message once the thread catches up, even when the feed hides it", () => {
216 // A rolled-back or cancelled queued run keeps its message in projection.messages
217 // while its user_message item leaves the visible feed.
218 const echoed = pending("rolled-back");
219 // A bounded projection pages an applied message out; it must not come back as pending.
220 const pagedOut = { ...pending("paged-out"), sequence: 40 };
221 const waiting = { ...pending("waiting"), sequence: 50 };
222 const shown = selectPendingAcknowledgedMessages([echoed, pagedOut, waiting], {
223 threadKey: "env:thread",
224 queuedMessages: [],
225 sequence: 45,
226 projectedMessages: [{ id: echoed.messageId }],
227 });
228 expect(shown).toEqual([waiting]);
229 expect(appendPendingThreadMessages([], [], shown).map((entry) => entry.id)).toEqual([
230 "waiting",
231 ]);
232 });
233
234 it("holds the run's rows after a sent message until the run shows", () => {
235 const appendTurn = createPendingTurnAppender();
236 const sent = appendPendingThreadMessages([], [], [pending("sent")]);
237 const held = appendTurn(sent, { zen: false });
238 expect(held.map((entry) => [entry.id, "label" in entry ? entry.label : null])).toEqual([
239 ["sent", null],
240 ["pending-fold:sent", null],
241 ["live-activity-row", "Sending…"],
242 ]);
243 expect(appendTurn(sent, { zen: false })[1]).toBe(held[1]);
244 const delivered = sent.map((entry) => ({ ...entry, acknowledged: true }));
245 expect(appendTurn(delivered, { zen: true }).map((entry) => entry.id)).toEqual([
246 "sent",
247 "live-activity-row",
248 ]);
249 // A working run already shows its own rows.
250 const thinking = {
251 type: "thinking",
252 runId: null,
253 id: "thinking",
254 createdAt: "2026-09-06T11:00:00.000Z",
255 } as const;
256 const working = appendPendingThreadMessages([thinking], [], [pending("queued")]);
257 expect(appendTurn(working, { zen: false })).toBe(working);
258 });
259 });