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

pending-thread-feed.test.ts

112 lines4.0 KB

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 { appendPendingThreadMessages, isCompactionCommandFeedEntry } from "./pending-thread-feed";
12​
13const pending = (id: string): QueuedThreadMessage => ({
14 environmentId: EnvironmentId.make("env"),
15 threadId: ThreadId.make("thread"),
16 messageId: MessageId.make(id),
17 commandId: CommandId.make(id),
18 text: id,
19 attachments: [],
20 createdAt: "2026-09-06T10:00:00.000Z",
21});
22​
23describe("pending timeline messages", () => {
24 it.each([false, true])(
25 "keeps a queued compact command in the pending message row (attachment: %s)",
26 (withAttachment) => {
27 const queued = {
28 ...pending("compact"),
29 text: "/compact",
30 attachments: withAttachment
31 ? [
32 {
33 id: "image-1",
34 type: "image" as const,
35 name: "photo.png",
36 mimeType: "image/png",
37 sizeBytes: 3,
38 fileUri: "file:///documents/photo.png",
39 previewUri: "file:///documents/photo.png",
40 },
41 ]
42 : [],
43 };
44 const entry = appendPendingThreadMessages([], [], [queued])[0]!;
45 expect(isCompactionCommandFeedEntry(entry)).toBe(false);
46 expect(entry.pendingMessage).toEqual(queued);
47 if (!withAttachment) {
48 expect(isCompactionCommandFeedEntry({ ...entry, pendingMessage: undefined })).toBe(true);
49 }
50 },
51 );
52​
53 it("marks the synthetic creation prompt pending without duplicating it", () => {
54 const queued = {
55 ...pending("creation"),
56 creation: {
57 projectId: ProjectId.make("project"),
58 workspaceMode: "local" as const,
59 branch: "main",
60 worktreePath: null,
61 },
62 };
63 const prompt = appendPendingThreadMessages([], [], [queued])[0]!;
64 const synthetic = { ...prompt, pendingMessage: undefined };
65 const result = appendPendingThreadMessages([synthetic], [synthetic], [queued]);
66 expect(result).toHaveLength(1);
67 expect(result[0]?.pendingMessage).toEqual(queued);
68 expect(result[0]?.acknowledged).toBe(false);
69 });
70​
71 it("retains context records while a message is waiting for delivery", () => {
72 const record = {
73 version: 1 as const,
74 kind: "mention" as const,
75 contextId: ComposerContextId.make("setup-file"),
76 label: "Checkout.tsx",
77 path: "src/Checkout.tsx",
78 };
79 const context = { version: 1 as const, records: [record] };
80 const text = "[Checkout.tsx](t3-context://v1/mention/setup-file)";
81 const entries = appendPendingThreadMessages([], [], [{ ...pending("context"), text, context }]);
82 const entry = entries[0];
83 expect(entry?.type).toBe("message");
84 if (entry?.type !== "message") throw new Error("Expected a pending message");
85 expect(entry.message.text).toBe(text);
86 expect(entry.message.context).toEqual(context);
87 });
88​
89 it("keeps pending messages after newer agent activity in queue order", () => {
90 const activity = {
91 type: "thinking",
92 runId: null,
93 id: "thinking",
94 createdAt: "2026-09-06T11:00:00.000Z",
95 } as const;
96 const entries = appendPendingThreadMessages(
97 [activity],
98 [],
99 [pending("first"), pending("second")],
100 );
101 expect(entries.map((entry) => entry.id)).toEqual(["thinking", "first", "second"]);
102 expect(entries[1]?.pendingMessage?.text).toBe("first");
103 });
104​
105 it("reuses the message id and suppresses the pending copy when delivery appears", () => {
106 const queued = pending("sent");
107 const optimistic = appendPendingThreadMessages([], [], [queued])[0]!;
108 const delivered = { ...optimistic, pendingMessage: undefined };
109 expect(appendPendingThreadMessages([delivered], [delivered], [queued])).toEqual([delivered]);
110 expect(appendPendingThreadMessages([], [delivered], [queued])).toEqual([]);
111 });
112});