Open History to see the last commit
1import { ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vitest";
3import { useStore } from "../store";
4
5import {
6 buildExpiredTerminalContextToastCopy,
7 createLocalDispatchSnapshot,
8 deriveComposerSendState,
9 hasServerAcknowledgedLocalDispatch,
10 waitForStartedServerThread,
11} from "./ChatView.logic";
12
13describe("deriveComposerSendState", () => {
14 it("treats expired terminal pills as non-sendable content", () => {
15 const state = deriveComposerSendState({
16 prompt: "\uFFFC",
17 imageCount: 0,
18 terminalContexts: [
19 {
20 id: "ctx-expired",
21 threadId: ThreadId.makeUnsafe("thread-1"),
22 terminalId: "default",
23 terminalLabel: "Terminal 1",
24 lineStart: 4,
25 lineEnd: 4,
26 text: "",
27 createdAt: "2026-03-17T12:52:29.000Z",
28 },
29 ],
30 });
31
32 expect(state.trimmedPrompt).toBe("");
33 expect(state.sendableTerminalContexts).toEqual([]);
34 expect(state.expiredTerminalContextCount).toBe(1);
35 expect(state.hasSendableContent).toBe(false);
36 });
37
38 it("keeps text sendable while excluding expired terminal pills", () => {
39 const state = deriveComposerSendState({
40 prompt: `yoo \uFFFC waddup`,
41 imageCount: 0,
42 terminalContexts: [
43 {
44 id: "ctx-expired",
45 threadId: ThreadId.makeUnsafe("thread-1"),
46 terminalId: "default",
47 terminalLabel: "Terminal 1",
48 lineStart: 4,
49 lineEnd: 4,
50 text: "",
51 createdAt: "2026-03-17T12:52:29.000Z",
52 },
53 ],
54 });
55
56 expect(state.trimmedPrompt).toBe("yoo waddup");
57 expect(state.expiredTerminalContextCount).toBe(1);
58 expect(state.hasSendableContent).toBe(true);
59 });
60});
61
62describe("buildExpiredTerminalContextToastCopy", () => {
63 it("formats clear empty-state guidance", () => {
64 expect(buildExpiredTerminalContextToastCopy(1, "empty")).toEqual({
65 title: "Expired terminal context won't be sent",
66 description: "Remove it or re-add it to include terminal output.",
67 });
68 });
69
70 it("formats omission guidance for sent messages", () => {
71 expect(buildExpiredTerminalContextToastCopy(2, "omitted")).toEqual({
72 title: "Expired terminal contexts omitted from message",
73 description: "Re-add it if you want that terminal output included.",
74 });
75 });
76});
77
78const makeThread = (input?: {
79 id?: ThreadId;
80 latestTurn?: {
81 turnId: TurnId;
82 state: "running" | "completed";
83 requestedAt: string;
84 startedAt: string | null;
85 completedAt: string | null;
86 } | null;
87}) => ({
88 id: input?.id ?? ThreadId.makeUnsafe("thread-1"),
89 codexThreadId: null,
90 projectId: ProjectId.makeUnsafe("project-1"),
91 title: "Thread",
92 modelSelection: { provider: "codex" as const, model: "gpt-5.4" },
93 runtimeMode: "full-access" as const,
94 interactionMode: "default" as const,
95 session: null,
96 messages: [],
97 proposedPlans: [],
98 error: null,
99 createdAt: "2026-03-29T00:00:00.000Z",
100 archivedAt: null,
101 updatedAt: "2026-03-29T00:00:00.000Z",
102 latestTurn: input?.latestTurn
103 ? {
104 ...input.latestTurn,
105 assistantMessageId: null,
106 }
107 : null,
108 branch: null,
109 worktreePath: null,
110 turnDiffSummaries: [],
111 activities: [],
112});
113
114afterEach(() => {
115 vi.useRealTimers();
116 vi.restoreAllMocks();
117 useStore.setState((state) => ({
118 ...state,
119 projects: [],
120 threads: [],
121 bootstrapComplete: true,
122 }));
123});
124
125describe("waitForStartedServerThread", () => {
126 it("resolves immediately when the thread is already started", async () => {
127 const threadId = ThreadId.makeUnsafe("thread-started");
128 useStore.setState((state) => ({
129 ...state,
130 threads: [
131 makeThread({
132 id: threadId,
133 latestTurn: {
134 turnId: TurnId.makeUnsafe("turn-started"),
135 state: "running",
136 requestedAt: "2026-03-29T00:00:01.000Z",
137 startedAt: "2026-03-29T00:00:01.000Z",
138 completedAt: null,
139 },
140 }),
141 ],
142 }));
143
144 await expect(waitForStartedServerThread(threadId)).resolves.toBe(true);
145 });
146
147 it("waits for the thread to start via subscription updates", async () => {
148 const threadId = ThreadId.makeUnsafe("thread-wait");
149 useStore.setState((state) => ({
150 ...state,
151 threads: [makeThread({ id: threadId })],
152 }));
153
154 const promise = waitForStartedServerThread(threadId, 500);
155
156 useStore.setState((state) => ({
157 ...state,
158 threads: [
159 makeThread({
160 id: threadId,
161 latestTurn: {
162 turnId: TurnId.makeUnsafe("turn-started"),
163 state: "running",
164 requestedAt: "2026-03-29T00:00:01.000Z",
165 startedAt: "2026-03-29T00:00:01.000Z",
166 completedAt: null,
167 },
168 }),
169 ],
170 }));
171
172 await expect(promise).resolves.toBe(true);
173 });
174
175 it("handles the thread starting between the initial read and subscription setup", async () => {
176 const threadId = ThreadId.makeUnsafe("thread-race");
177 useStore.setState((state) => ({
178 ...state,
179 threads: [makeThread({ id: threadId })],
180 }));
181
182 const originalSubscribe = useStore.subscribe.bind(useStore);
183 let raced = false;
184 vi.spyOn(useStore, "subscribe").mockImplementation((listener) => {
185 if (!raced) {
186 raced = true;
187 useStore.setState((state) => ({
188 ...state,
189 threads: [
190 makeThread({
191 id: threadId,
192 latestTurn: {
193 turnId: TurnId.makeUnsafe("turn-race"),
194 state: "running",
195 requestedAt: "2026-03-29T00:00:01.000Z",
196 startedAt: "2026-03-29T00:00:01.000Z",
197 completedAt: null,
198 },
199 }),
200 ],
201 }));
202 }
203 return originalSubscribe(listener);
204 });
205
206 await expect(waitForStartedServerThread(threadId, 500)).resolves.toBe(true);
207 });
208
209 it("returns false after the timeout when the thread never starts", async () => {
210 vi.useFakeTimers();
211
212 const threadId = ThreadId.makeUnsafe("thread-timeout");
213 useStore.setState((state) => ({
214 ...state,
215 threads: [makeThread({ id: threadId })],
216 }));
217 const promise = waitForStartedServerThread(threadId, 500);
218
219 await vi.advanceTimersByTimeAsync(500);
220
221 await expect(promise).resolves.toBe(false);
222 });
223});
224
225describe("hasServerAcknowledgedLocalDispatch", () => {
226 const projectId = ProjectId.makeUnsafe("project-1");
227 const previousLatestTurn = {
228 turnId: TurnId.makeUnsafe("turn-1"),
229 state: "completed" as const,
230 requestedAt: "2026-03-29T00:00:00.000Z",
231 startedAt: "2026-03-29T00:00:01.000Z",
232 completedAt: "2026-03-29T00:00:10.000Z",
233 assistantMessageId: null,
234 };
235
236 const previousSession = {
237 provider: "codex" as const,
238 status: "ready" as const,
239 createdAt: "2026-03-29T00:00:00.000Z",
240 updatedAt: "2026-03-29T00:00:10.000Z",
241 orchestrationStatus: "idle" as const,
242 };
243
244 it("does not clear local dispatch before server state changes", () => {
245 const localDispatch = createLocalDispatchSnapshot({
246 id: ThreadId.makeUnsafe("thread-1"),
247 codexThreadId: null,
248 projectId,
249 title: "Thread",
250 modelSelection: { provider: "codex", model: "gpt-5.4" },
251 runtimeMode: "full-access",
252 interactionMode: "default",
253 session: previousSession,
254 messages: [],
255 proposedPlans: [],
256 error: null,
257 createdAt: "2026-03-29T00:00:00.000Z",
258 archivedAt: null,
259 updatedAt: "2026-03-29T00:00:10.000Z",
260 latestTurn: previousLatestTurn,
261 branch: null,
262 worktreePath: null,
263 turnDiffSummaries: [],
264 activities: [],
265 });
266
267 expect(
268 hasServerAcknowledgedLocalDispatch({
269 localDispatch,
270 phase: "ready",
271 latestTurn: previousLatestTurn,
272 session: previousSession,
273 hasPendingApproval: false,
274 hasPendingUserInput: false,
275 threadError: null,
276 }),
277 ).toBe(false);
278 });
279
280 it("clears local dispatch when a new turn is already settled", () => {
281 const localDispatch = createLocalDispatchSnapshot({
282 id: ThreadId.makeUnsafe("thread-1"),
283 codexThreadId: null,
284 projectId,
285 title: "Thread",
286 modelSelection: { provider: "codex", model: "gpt-5.4" },
287 runtimeMode: "full-access",
288 interactionMode: "default",
289 session: previousSession,
290 messages: [],
291 proposedPlans: [],
292 error: null,
293 createdAt: "2026-03-29T00:00:00.000Z",
294 archivedAt: null,
295 updatedAt: "2026-03-29T00:00:10.000Z",
296 latestTurn: previousLatestTurn,
297 branch: null,
298 worktreePath: null,
299 turnDiffSummaries: [],
300 activities: [],
301 });
302
303 expect(
304 hasServerAcknowledgedLocalDispatch({
305 localDispatch,
306 phase: "ready",
307 latestTurn: {
308 ...previousLatestTurn,
309 turnId: TurnId.makeUnsafe("turn-2"),
310 requestedAt: "2026-03-29T00:01:00.000Z",
311 startedAt: "2026-03-29T00:01:01.000Z",
312 completedAt: "2026-03-29T00:01:30.000Z",
313 },
314 session: {
315 ...previousSession,
316 updatedAt: "2026-03-29T00:01:30.000Z",
317 },
318 hasPendingApproval: false,
319 hasPendingUserInput: false,
320 threadError: null,
321 }),
322 ).toBe(true);
323 });
324
325 it("clears local dispatch when the session changes without an observed running phase", () => {
326 const localDispatch = createLocalDispatchSnapshot({
327 id: ThreadId.makeUnsafe("thread-1"),
328 codexThreadId: null,
329 projectId,
330 title: "Thread",
331 modelSelection: { provider: "codex", model: "gpt-5.4" },
332 runtimeMode: "full-access",
333 interactionMode: "default",
334 session: previousSession,
335 messages: [],
336 proposedPlans: [],
337 error: null,
338 createdAt: "2026-03-29T00:00:00.000Z",
339 archivedAt: null,
340 updatedAt: "2026-03-29T00:00:10.000Z",
341 latestTurn: previousLatestTurn,
342 branch: null,
343 worktreePath: null,
344 turnDiffSummaries: [],
345 activities: [],
346 });
347
348 expect(
349 hasServerAcknowledgedLocalDispatch({
350 localDispatch,
351 phase: "ready",
352 latestTurn: previousLatestTurn,
353 session: {
354 ...previousSession,
355 updatedAt: "2026-03-29T00:00:11.000Z",
356 },
357 hasPendingApproval: false,
358 hasPendingUserInput: false,
359 threadError: null,
360 }),
361 ).toBe(true);
362 });
363});