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

queuedMessageStore.test.ts

194 lines7.9 KB

1import { ProviderInstanceId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it } from "vite-plus/test";
3​
4import { createLocalDispatchSnapshot } from "./components/ChatView.logic";
5import {
6 isQueuedMessageDue,
7 latestCompletedToolActivityId,
8 useQueuedMessageStore,
9 type QueuedComposerMessage,
10} from "./queuedMessageStore";
11​
12function makeMessage(prompt: string): Omit<QueuedComposerMessage, "id"> {
13 return {
14 prompt,
15 images: [],
16 files: [],
17 terminalContexts: [],
18 previewAnnotations: [],
19 reviewComments: [],
20 sendSettings: {
21 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
22 runtimeMode: "full-access",
23 interactionMode: "default",
24 promptEffort: null,
25 },
26 queuedAfterToolActivityId: null,
27 createdAt: "2026-09-11T00:00:00.000Z",
28 };
30​
31const queue = (threadKey: string) =>
32 useQueuedMessageStore.getState().queuesByThreadKey[threadKey] ?? [];
33​
34describe("queuedMessageStore", () => {
35 beforeEach(() => {
36 useQueuedMessageStore.setState({ queuesByThreadKey: {}, lastDispatchByThreadKey: {} });
37 });
38​
39 it("keeps messages in submission order per thread", () => {
40 const { enqueue } = useQueuedMessageStore.getState();
41 enqueue("thread-a", makeMessage("first"));
42 enqueue("thread-a", makeMessage("second"));
43 enqueue("thread-b", makeMessage("other"));
44​
45 expect(queue("thread-a").map((message) => message.prompt)).toEqual(["first", "second"]);
46 expect(queue("thread-b").map((message) => message.prompt)).toEqual(["other"]);
47 });
48​
49 it("allows one send per thread and re-anchors the rest to the current tool boundary", () => {
50 const { enqueue, beginSend } = useQueuedMessageStore.getState();
51 const first = enqueue("thread-a", makeMessage("first"));
52 const second = enqueue("thread-a", makeMessage("second"));
53​
54 expect(beginSend("thread-a", first.id, "tool-2")?.prompt).toBe("first");
55 expect(beginSend("thread-a", first.id, "tool-2")).toBeNull();
56 expect(beginSend("thread-a", second.id, "tool-2")).toBeNull();
57​
58 const [sending, waiting] = queue("thread-a");
59 expect(sending?.sending).toBe("preparing");
60 expect(waiting?.queuedAfterToolActivityId).toBe("tool-2");
61 expect(
62 isQueuedMessageDue({ message: waiting!, phase: "running", latestToolActivityId: "tool-2" }),
63 ).toBe(false);
64 });
65​
66 it("finishSend drops the sent message", () => {
67 const { enqueue, beginSend, finishSend } = useQueuedMessageStore.getState();
68 const first = enqueue("thread-a", makeMessage("first"));
69 beginSend("thread-a", first.id, null);
70​
71 finishSend("thread-a", first.id);
72​
73 expect(useQueuedMessageStore.getState().queuesByThreadKey["thread-a"]).toBeUndefined();
74 });
75​
76 it("failSend returns the message to the head, held", () => {
77 const { enqueue, beginSend, failSend } = useQueuedMessageStore.getState();
78 enqueue("thread-a", makeMessage("first"));
79 const second = enqueue("thread-a", makeMessage("second"));
80 beginSend("thread-a", second.id, null);
81​
82 expect(failSend("thread-a", second.id)).toBe(true);
83​
84 const [head] = queue("thread-a");
85 expect(queue("thread-a").map((message) => message.prompt)).toEqual(["second", "first"]);
86 expect(head?.sending).toBeUndefined();
87 expect(isQueuedMessageDue({ message: head!, phase: "ready", latestToolActivityId: null })).toBe(
88 false,
89 );
90 });
91​
92 it("remove keeps the other messages' anchors and refuses a message being sent", () => {
93 const { enqueue, remove, beginSend } = useQueuedMessageStore.getState();
94 const first = enqueue("thread-a", { ...makeMessage("first"), queuedAfterToolActivityId: "t1" });
95 const second = enqueue("thread-a", makeMessage("second"));
96​
97 expect(remove("thread-a", second.id)?.prompt).toBe("second");
98 expect(remove("thread-a", second.id)).toBeNull();
99 expect(queue("thread-a")).toEqual([first]);
100​
101 beginSend("thread-a", first.id, "t1");
102 expect(remove("thread-a", first.id)).toBeNull();
103 });
104​
105 it("a failed send keeps waiting on the dispatch before it", () => {
106 const { enqueue, beginSend, markDispatching, finishSend, failSend } =
107 useQueuedMessageStore.getState();
108 const earlier = createLocalDispatchSnapshot(undefined);
109 const first = enqueue("thread-a", makeMessage("first"));
110 const second = enqueue("thread-a", makeMessage("second"));
111 const third = enqueue("thread-a", makeMessage("third"));
112 const lastDispatch = () => useQueuedMessageStore.getState().lastDispatchByThreadKey["thread-a"];
113 beginSend("thread-a", first.id, null);
114 markDispatching("thread-a", first.id, earlier);
115 finishSend("thread-a", first.id);
116​
117 // Fails before its turn start went out: the first send is still the one to wait on.
118 beginSend("thread-a", second.id, null);
119 failSend("thread-a", second.id);
120 expect(lastDispatch()?.thread).toBe(earlier);
121​
122 // Fails after going out: it never reached the server, so the first still counts.
123 beginSend("thread-a", third.id, null);
124 markDispatching("thread-a", third.id, { ...earlier, startedAt: "later" });
125 failSend("thread-a", third.id);
126 expect(lastDispatch()?.thread).toBe(earlier);
127 });
128​
129 it("Stop takes back a preparing send but not one already dispatching", () => {
130 const { enqueue, beginSend, markDispatching, drain, failSend } =
131 useQueuedMessageStore.getState();
132 const preparing = enqueue("thread-a", makeMessage("preparing"));
133 enqueue("thread-a", makeMessage("waiting"));
134 beginSend("thread-a", preparing.id, null);
135​
136 expect(drain("thread-a").map((message) => message.prompt)).toEqual(["preparing", "waiting"]);
137 expect(markDispatching("thread-a", preparing.id, createLocalDispatchSnapshot(undefined))).toBe(
138 false,
139 );
140 expect(failSend("thread-a", preparing.id)).toBe(false);
141​
142 const dispatching = enqueue("thread-b", makeMessage("dispatching"));
143 enqueue("thread-b", makeMessage("waiting"));
144 beginSend("thread-b", dispatching.id, null);
145 expect(
146 markDispatching("thread-b", dispatching.id, createLocalDispatchSnapshot(undefined)),
147 ).toBe(true);
148​
149 expect(drain("thread-b").map((message) => message.prompt)).toEqual(["waiting"]);
150 expect(queue("thread-b").map((message) => message.prompt)).toEqual(["dispatching"]);
151 });
152});
153​
154describe("queued message dispatch timing", () => {
155 const activities = [
156 { id: "a1", kind: "tool.started", sequence: 1, createdAt: "2026-01-01T00:00:01Z" },
157 { id: "a2", kind: "tool.completed", sequence: 2, createdAt: "2026-01-01T00:00:02Z" },
158 { id: "a3", kind: "tool.updated", sequence: 3, createdAt: "2026-01-01T00:00:03Z" },
159 ];
160​
161 it("finds the newest completed tool call by sequence, not position", () => {
162 expect(latestCompletedToolActivityId(activities)).toBe("a2");
163 expect(latestCompletedToolActivityId([])).toBeNull();
164 expect(
165 latestCompletedToolActivityId([
166 { id: "late", kind: "tool.completed", sequence: 9, createdAt: "2026-01-01T00:00:09Z" },
167 { id: "early", kind: "tool.completed", sequence: 4, createdAt: "2026-01-01T00:00:04Z" },
168 ]),
169 ).toBe("late");
170 });
171​
172 it("waits mid-turn until a tool call finishes after the message was queued", () => {
173 const message = { queuedAfterToolActivityId: "a2" };
174 expect(isQueuedMessageDue({ message, phase: "running", latestToolActivityId: "a2" })).toBe(
175 false,
176 );
177 expect(isQueuedMessageDue({ message, phase: "running", latestToolActivityId: "a4" })).toBe(
178 true,
179 );
180 });
181​
182 it("never auto-sends a message held for user action", () => {
183 const message = { queuedAfterToolActivityId: null, holdUntilUserAction: true };
184 expect(isQueuedMessageDue({ message, phase: "ready", latestToolActivityId: "a4" })).toBe(false);
185 });
186​
187 it("is due as soon as the turn is over, but not while a send is connecting", () => {
188 const message = { queuedAfterToolActivityId: "a2" };
189 expect(isQueuedMessageDue({ message, phase: "ready", latestToolActivityId: "a2" })).toBe(true);
190 expect(isQueuedMessageDue({ message, phase: "connecting", latestToolActivityId: "a4" })).toBe(
191 false,
192 );
193 });
194});