Open History to see the last commit
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 useQueuedMessageStore,
8 type QueuedComposerMessage,
9} from "./queuedMessageStore";
10
11function makeMessage(prompt: string): Omit<QueuedComposerMessage, "id"> {
12 return {
13 prompt,
14 images: [],
15 files: [],
16 terminalContexts: [],
17 previewAnnotations: [],
18 reviewComments: [],
19 sendSettings: {
20 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
21 runtimeMode: "full-access",
22 interactionMode: "default",
23 promptEffort: null,
24 },
25 createdAt: "2026-09-11T00:00:00.000Z",
26 };
27}
28
29const queue = (threadKey: string) =>
30 useQueuedMessageStore.getState().queuesByThreadKey[threadKey] ?? [];
31
32describe("queuedMessageStore", () => {
33 beforeEach(() => {
34 useQueuedMessageStore.setState({ queuesByThreadKey: {}, lastDispatchByThreadKey: {} });
35 });
36
37 it("keeps messages in submission order per thread", () => {
38 const { enqueue } = useQueuedMessageStore.getState();
39 enqueue("thread-a", makeMessage("first"));
40 enqueue("thread-a", makeMessage("second"));
41 enqueue("thread-b", makeMessage("other"));
42
43 expect(queue("thread-a").map((message) => message.prompt)).toEqual(["first", "second"]);
44 expect(queue("thread-b").map((message) => message.prompt)).toEqual(["other"]);
45 });
46
47 it("allows one send per thread", () => {
48 const { enqueue, beginSend } = useQueuedMessageStore.getState();
49 const first = enqueue("thread-a", makeMessage("first"));
50 const second = enqueue("thread-a", makeMessage("second"));
51
52 expect(beginSend("thread-a", first.id)?.prompt).toBe("first");
53 expect(beginSend("thread-a", first.id)).toBeNull();
54 expect(beginSend("thread-a", second.id)).toBeNull();
55
56 const [sending, waiting] = queue("thread-a");
57 expect(sending?.sending).toBe("preparing");
58 expect(waiting?.sending).toBeUndefined();
59 });
60
61 it("finishSend drops the sent message", () => {
62 const { enqueue, beginSend, finishSend } = useQueuedMessageStore.getState();
63 const first = enqueue("thread-a", makeMessage("first"));
64 beginSend("thread-a", first.id);
65
66 finishSend("thread-a", first.id);
67
68 expect(useQueuedMessageStore.getState().queuesByThreadKey["thread-a"]).toBeUndefined();
69 });
70
71 it("failSend returns the message to the head, held", () => {
72 const { enqueue, beginSend, failSend } = useQueuedMessageStore.getState();
73 enqueue("thread-a", makeMessage("first"));
74 const second = enqueue("thread-a", makeMessage("second"));
75 beginSend("thread-a", second.id);
76
77 expect(failSend("thread-a", second.id)).toBe(true);
78
79 const [head] = queue("thread-a");
80 expect(queue("thread-a").map((message) => message.prompt)).toEqual(["second", "first"]);
81 expect(head?.sending).toBeUndefined();
82 expect(isQueuedMessageDue({ message: head!, phase: "ready" })).toBe(false);
83 });
84
85 it("remove keeps the other messages and refuses a message being sent", () => {
86 const { enqueue, remove, beginSend } = useQueuedMessageStore.getState();
87 const first = enqueue("thread-a", makeMessage("first"));
88 const second = enqueue("thread-a", makeMessage("second"));
89
90 expect(remove("thread-a", second.id)?.prompt).toBe("second");
91 expect(remove("thread-a", second.id)).toBeNull();
92 expect(queue("thread-a")).toEqual([first]);
93
94 beginSend("thread-a", first.id);
95 expect(remove("thread-a", first.id)).toBeNull();
96 });
97
98 it("a failed send keeps waiting on the dispatch before it", () => {
99 const { enqueue, beginSend, markDispatching, finishSend, failSend } =
100 useQueuedMessageStore.getState();
101 const earlier = createLocalDispatchSnapshot(undefined);
102 const first = enqueue("thread-a", makeMessage("first"));
103 const second = enqueue("thread-a", makeMessage("second"));
104 const third = enqueue("thread-a", makeMessage("third"));
105 const lastDispatch = () => useQueuedMessageStore.getState().lastDispatchByThreadKey["thread-a"];
106 beginSend("thread-a", first.id);
107 markDispatching("thread-a", first.id, earlier);
108 finishSend("thread-a", first.id);
109
110 // Fails before its turn start went out: the first send is still the one to wait on.
111 beginSend("thread-a", second.id);
112 failSend("thread-a", second.id);
113 expect(lastDispatch()?.thread).toBe(earlier);
114
115 // Fails after going out: it never reached the server, so the first still counts.
116 beginSend("thread-a", third.id);
117 markDispatching("thread-a", third.id, { ...earlier, startedAt: "later" });
118 failSend("thread-a", third.id);
119 expect(lastDispatch()?.thread).toBe(earlier);
120 });
121
122 it("Stop takes back a preparing send but not one already dispatching", () => {
123 const { enqueue, beginSend, markDispatching, drain, failSend } =
124 useQueuedMessageStore.getState();
125 const preparing = enqueue("thread-a", makeMessage("preparing"));
126 enqueue("thread-a", makeMessage("waiting"));
127 beginSend("thread-a", preparing.id);
128
129 expect(drain("thread-a").map((message) => message.prompt)).toEqual(["preparing", "waiting"]);
130 expect(markDispatching("thread-a", preparing.id, createLocalDispatchSnapshot(undefined))).toBe(
131 false,
132 );
133 expect(failSend("thread-a", preparing.id)).toBe(false);
134
135 const dispatching = enqueue("thread-b", makeMessage("dispatching"));
136 enqueue("thread-b", makeMessage("waiting"));
137 beginSend("thread-b", dispatching.id);
138 expect(
139 markDispatching("thread-b", dispatching.id, createLocalDispatchSnapshot(undefined)),
140 ).toBe(true);
141
142 expect(drain("thread-b").map((message) => message.prompt)).toEqual(["waiting"]);
143 expect(queue("thread-b").map((message) => message.prompt)).toEqual(["dispatching"]);
144 });
145});
146
147describe("queued message dispatch timing", () => {
148 it("waits for the turn to end instead of sending mid-turn", () => {
149 expect(isQueuedMessageDue({ message: {}, phase: "running" })).toBe(false);
150 });
151
152 it("never auto-sends a message held for user action", () => {
153 expect(isQueuedMessageDue({ message: { holdUntilUserAction: true }, phase: "ready" })).toBe(
154 false,
155 );
156 });
157
158 it("is due as soon as the turn is over, but not while a send is connecting", () => {
159 expect(isQueuedMessageDue({ message: {}, phase: "ready" })).toBe(true);
160 expect(isQueuedMessageDue({ message: {}, phase: "connecting" })).toBe(false);
161 });
162});