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

store.test.ts

577 lines16.8 KB

1import type { ProviderEvent, ProviderSession, TerminalEvent } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import { type AppState, reducer } from "./store";
5import { DEFAULT_THREAD_TERMINAL_HEIGHT, DEFAULT_THREAD_TERMINAL_ID } from "./types";
6import type { Thread } from "./types";
7​
8type TerminalStartedEvent = Extract<TerminalEvent, { type: "started" }>;
9type TerminalActivityEvent = Extract<TerminalEvent, { type: "activity" }>;
10​
11function makeSession(overrides: Partial<ProviderSession> = {}): ProviderSession {
12 return {
13 sessionId: "sess-1",
14 provider: "codex",
15 status: "ready",
16 createdAt: "2026-02-09T00:00:00.000Z",
17 updatedAt: "2026-02-09T00:00:00.000Z",
18 ...overrides,
19 };
21​
22function makeEvent(overrides: Partial<ProviderEvent> = {}): ProviderEvent {
23 return {
24 id: "evt-1",
25 kind: "notification",
26 provider: "codex",
27 sessionId: "sess-1",
28 createdAt: "2026-02-09T00:00:01.000Z",
29 method: "thread/started",
30 ...overrides,
31 };
33​
34function makeThread(overrides: Partial<Thread> = {}): Thread {
35 return {
36 id: "thread-local-1",
37 codexThreadId: null,
38 projectId: "project-1",
39 title: "Thread",
40 model: "gpt-5.3-codex",
41 terminalOpen: false,
42 terminalHeight: DEFAULT_THREAD_TERMINAL_HEIGHT,
43 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
44 runningTerminalIds: [],
45 activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
46 terminalGroups: [
47 {
48 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
49 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
50 },
51 ],
52 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
53 session: makeSession(),
54 messages: [],
55 events: [],
56 error: null,
57 createdAt: "2026-02-09T00:00:00.000Z",
58 branch: null,
59 worktreePath: null,
60 ...overrides,
61 };
63​
64function makeState(thread: Thread): AppState {
65 return {
66 projects: [
67 {
68 id: "project-1",
69 name: "Project",
70 cwd: "/tmp/project",
71 model: "gpt-5.3-codex",
72 expanded: true,
73 },
74 ],
75 threads: [thread],
76 activeThreadId: thread.id,
77 runtimeMode: "full-access",
78 diffOpen: false,
79 };
81​
82function makeTerminalStartedEvent(
83 overrides: Partial<TerminalStartedEvent> = {},
84): TerminalStartedEvent {
85 return {
86 type: "started",
87 threadId: "thread-local-1",
88 terminalId: DEFAULT_THREAD_TERMINAL_ID,
89 createdAt: "2026-02-09T00:00:01.000Z",
90 snapshot: {
91 threadId: "thread-local-1",
92 terminalId: DEFAULT_THREAD_TERMINAL_ID,
93 cwd: "/tmp/project",
94 status: "running",
95 pid: 1234,
96 history: "",
97 exitCode: null,
98 exitSignal: null,
99 updatedAt: "2026-02-09T00:00:01.000Z",
100 },
101 ...overrides,
102 };
104​
105function makeTerminalActivityEvent(
106 overrides: Partial<TerminalActivityEvent> = {},
107): TerminalActivityEvent {
108 return {
109 type: "activity",
110 threadId: "thread-local-1",
111 terminalId: DEFAULT_THREAD_TERMINAL_ID,
112 createdAt: "2026-02-09T00:00:02.000Z",
113 hasRunningSubprocess: true,
114 ...overrides,
115 };
117​
118describe("store reducer thread continuity", () => {
119 it("stores codexThreadId from UPDATE_SESSION", () => {
120 const state = makeState(
121 makeThread({
122 session: null,
123 }),
124 );
125 const next = reducer(state, {
126 type: "UPDATE_SESSION",
127 threadId: "thread-local-1",
128 session: makeSession({ threadId: "thr_123" }),
129 });
130​
131 expect(next.threads[0]?.codexThreadId).toBe("thr_123");
132 });
133​
134 it("toggles terminal open state per thread", () => {
135 const state = makeState(makeThread({ terminalOpen: false }));
136 const next = reducer(state, {
137 type: "TOGGLE_THREAD_TERMINAL",
138 threadId: "thread-local-1",
139 });
140 expect(next.threads[0]?.terminalOpen).toBe(true);
141 });
142​
143 it("sets terminal open state per thread", () => {
144 const state = makeState(makeThread({ terminalOpen: true }));
145 const next = reducer(state, {
146 type: "SET_THREAD_TERMINAL_OPEN",
147 threadId: "thread-local-1",
148 open: false,
149 });
150 expect(next.threads[0]?.terminalOpen).toBe(false);
151 });
152​
153 it("sets terminal height per thread", () => {
154 const state = makeState(makeThread({ terminalHeight: 280 }));
155 const next = reducer(state, {
156 type: "SET_THREAD_TERMINAL_HEIGHT",
157 threadId: "thread-local-1",
158 height: 360,
159 });
160 expect(next.threads[0]?.terminalHeight).toBe(360);
161 });
162​
163 it("splits the active terminal into side-by-side mode", () => {
164 const state = makeState(makeThread());
165 const next = reducer(state, {
166 type: "SPLIT_THREAD_TERMINAL",
167 threadId: "thread-local-1",
168 terminalId: "term-2",
169 });
170​
171 expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "term-2"]);
172 expect(next.threads[0]?.activeTerminalId).toBe("term-2");
173 expect(next.threads[0]?.terminalGroups).toEqual([
174 {
175 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
176 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
177 },
178 ]);
179 expect(next.threads[0]?.activeTerminalGroupId).toBe(`group-${DEFAULT_THREAD_TERMINAL_ID}`);
180 });
181​
182 it("creates a new full-width terminal and switches to tab mode", () => {
183 const state = makeState(makeThread());
184 const next = reducer(state, {
185 type: "NEW_THREAD_TERMINAL",
186 threadId: "thread-local-1",
187 terminalId: "term-2",
188 });
189​
190 expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "term-2"]);
191 expect(next.threads[0]?.activeTerminalId).toBe("term-2");
192 expect(next.threads[0]?.terminalGroups).toEqual([
193 {
194 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
195 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
196 },
197 { id: "group-term-2", terminalIds: ["term-2"] },
198 ]);
199 expect(next.threads[0]?.activeTerminalGroupId).toBe("group-term-2");
200 });
201​
202 it("switches the active terminal and restores its owning group", () => {
203 const state = makeState(
204 makeThread({
205 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2", "term-3"],
206 activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
207 terminalGroups: [
208 {
209 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
210 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
211 },
212 { id: "group-term-3", terminalIds: ["term-3"] },
213 ],
214 activeTerminalGroupId: "group-term-3",
215 }),
216 );
217 const next = reducer(state, {
218 type: "SET_THREAD_ACTIVE_TERMINAL",
219 threadId: "thread-local-1",
220 terminalId: "term-2",
221 });
222​
223 expect(next.threads[0]?.activeTerminalId).toBe("term-2");
224 expect(next.threads[0]?.activeTerminalGroupId).toBe(`group-${DEFAULT_THREAD_TERMINAL_ID}`);
225 });
226​
227 it("supports splitting beyond two terminals in the same group", () => {
228 const state = makeState(
229 makeThread({
230 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
231 activeTerminalId: "term-2",
232 terminalGroups: [
233 {
234 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
235 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
236 },
237 ],
238 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
239 }),
240 );
241 const next = reducer(state, {
242 type: "SPLIT_THREAD_TERMINAL",
243 threadId: "thread-local-1",
244 terminalId: "term-3",
245 });
246​
247 expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "term-2", "term-3"]);
248 expect(next.threads[0]?.terminalGroups).toEqual([
249 {
250 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
251 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2", "term-3"],
252 },
253 ]);
254 expect(next.threads[0]?.activeTerminalId).toBe("term-3");
255 expect(next.threads[0]?.activeTerminalGroupId).toBe(`group-${DEFAULT_THREAD_TERMINAL_ID}`);
256 });
257​
258 it("closes a terminal and keeps grouped layout coherent", () => {
259 const state = makeState(
260 makeThread({
261 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2", "term-3"],
262 activeTerminalId: "term-2",
263 terminalGroups: [
264 {
265 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
266 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
267 },
268 { id: "group-term-3", terminalIds: ["term-3"] },
269 ],
270 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
271 }),
272 );
273 const next = reducer(state, {
274 type: "CLOSE_THREAD_TERMINAL",
275 threadId: "thread-local-1",
276 terminalId: "term-2",
277 });
278​
279 expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "term-3"]);
280 expect(next.threads[0]?.activeTerminalId).toBe(DEFAULT_THREAD_TERMINAL_ID);
281 expect(next.threads[0]?.terminalGroups).toEqual([
282 {
283 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
284 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
285 },
286 { id: "group-term-3", terminalIds: ["term-3"] },
287 ]);
288 });
289​
290 it("closes the final terminal and hides the drawer", () => {
291 const state = makeState(
292 makeThread({
293 terminalOpen: true,
294 runningTerminalIds: [DEFAULT_THREAD_TERMINAL_ID],
295 }),
296 );
297 const next = reducer(state, {
298 type: "CLOSE_THREAD_TERMINAL",
299 threadId: "thread-local-1",
300 terminalId: DEFAULT_THREAD_TERMINAL_ID,
301 });
302​
303 expect(next.threads[0]?.terminalOpen).toBe(false);
304 expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]);
305 expect(next.threads[0]?.runningTerminalIds).toEqual([]);
306 expect(next.threads[0]?.activeTerminalId).toBe(DEFAULT_THREAD_TERMINAL_ID);
307 expect(next.threads[0]?.terminalGroups).toEqual([
308 {
309 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
310 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
311 },
312 ]);
313 });
314​
315 it("tracks running terminals from subprocess activity events", () => {
316 const state = makeState(makeThread());
317 const started = reducer(state, {
318 type: "APPLY_TERMINAL_EVENT",
319 event: makeTerminalStartedEvent(),
320 });
321 expect(started.threads[0]?.runningTerminalIds).toEqual([]);
322​
323 const active = reducer(started, {
324 type: "APPLY_TERMINAL_EVENT",
325 event: makeTerminalActivityEvent(),
326 });
327 expect(active.threads[0]?.runningTerminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]);
328​
329 const idle = reducer(active, {
330 type: "APPLY_TERMINAL_EVENT",
331 event: makeTerminalActivityEvent({ hasRunningSubprocess: false }),
332 });
333 expect(idle.threads[0]?.runningTerminalIds).toEqual([]);
334​
335 const exited = reducer(active, {
336 type: "APPLY_TERMINAL_EVENT",
337 event: {
338 type: "exited",
339 threadId: "thread-local-1",
340 terminalId: DEFAULT_THREAD_TERMINAL_ID,
341 createdAt: "2026-02-09T00:00:05.000Z",
342 exitCode: 0,
343 exitSignal: null,
344 },
345 });
346 expect(exited.threads[0]?.runningTerminalIds).toEqual([]);
347 });
348​
349 it("keeps running status when another terminal in the thread is still running", () => {
350 const state = makeState(
351 makeThread({
352 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
353 runningTerminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
354 activeTerminalId: "term-2",
355 terminalGroups: [
356 {
357 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
358 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
359 },
360 ],
361 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
362 }),
363 );
364​
365 const next = reducer(state, {
366 type: "APPLY_TERMINAL_EVENT",
367 event: {
368 type: "exited",
369 threadId: "thread-local-1",
370 terminalId: DEFAULT_THREAD_TERMINAL_ID,
371 createdAt: "2026-02-09T00:00:07.000Z",
372 exitCode: 0,
373 exitSignal: null,
374 },
375 });
376​
377 expect(next.threads[0]?.runningTerminalIds).toEqual(["term-2"]);
378 });
379​
380 it("backfills codexThreadId from routed provider events", () => {
381 const state = makeState(makeThread({ codexThreadId: null }));
382 const next = reducer(state, {
383 type: "APPLY_EVENT",
384 event: makeEvent({
385 method: "thread/started",
386 payload: { thread: { id: "thr_backfilled" } },
387 }),
388 activeAssistantItemRef: { current: null },
389 });
390​
391 expect(next.threads[0]?.codexThreadId).toBe("thr_backfilled");
392 });
393​
394 it("ignores events from a foreign thread within the same session", () => {
395 const state = makeState(makeThread({ codexThreadId: "thr_expected" }));
396 const next = reducer(state, {
397 type: "APPLY_EVENT",
398 event: makeEvent({
399 method: "turn/started",
400 threadId: "thr_unexpected",
401 payload: { turn: { id: "turn-1" } },
402 }),
403 activeAssistantItemRef: { current: null },
404 });
405​
406 expect(next).toBe(state);
407 });
408​
409 it("rebases thread identity on thread/started during connect", () => {
410 const state = makeState(
411 makeThread({
412 codexThreadId: "thr_old",
413 session: makeSession({
414 status: "connecting",
415 threadId: "thr_old",
416 }),
417 }),
418 );
419 const next = reducer(state, {
420 type: "APPLY_EVENT",
421 event: makeEvent({
422 method: "thread/started",
423 threadId: "thr_new",
424 payload: { thread: { id: "thr_new" } },
425 }),
426 activeAssistantItemRef: { current: null },
427 });
428​
429 expect(next.threads[0]?.codexThreadId).toBe("thr_new");
430 expect(next.threads[0]?.session?.threadId).toBe("thr_new");
431 });
432​
433 it("reconciles project ids by cwd when syncing backend projects", () => {
434 const state: AppState = {
435 projects: [
436 {
437 id: "project-old-a",
438 name: "A",
439 cwd: "/tmp/a",
440 model: "gpt-5.3-codex",
441 expanded: false,
442 },
443 {
444 id: "project-old-b",
445 name: "B",
446 cwd: "/tmp/b",
447 model: "gpt-5.3-codex",
448 expanded: true,
449 },
450 ],
451 threads: [
452 makeThread({
453 id: "thread-a",
454 projectId: "project-old-a",
455 }),
456 makeThread({
457 id: "thread-b",
458 projectId: "project-old-b",
459 }),
460 ],
461 activeThreadId: "thread-b",
462 runtimeMode: "full-access",
463 diffOpen: false,
464 };
465​
466 const next = reducer(state, {
467 type: "SYNC_PROJECTS",
468 projects: [
469 {
470 id: "project-new-a",
471 name: "A",
472 cwd: "/tmp/a",
473 model: "gpt-5.3-codex",
474 expanded: true,
475 },
476 ],
477 });
478​
479 expect(next.projects).toHaveLength(1);
480 expect(next.projects[0]?.id).toBe("project-new-a");
481 // Preserve existing project UI preferences by cwd
482 expect(next.projects[0]?.expanded).toBe(false);
483 expect(next.threads).toHaveLength(1);
484 expect(next.threads[0]?.id).toBe("thread-a");
485 expect(next.threads[0]?.projectId).toBe("project-new-a");
486 expect(next.activeThreadId).toBe("thread-a");
487 });
488​
489 it("deletes a project and all of its threads", () => {
490 const state: AppState = {
491 projects: [
492 {
493 id: "project-1",
494 name: "One",
495 cwd: "/tmp/one",
496 model: "gpt-5.3-codex",
497 expanded: true,
498 },
499 {
500 id: "project-2",
501 name: "Two",
502 cwd: "/tmp/two",
503 model: "gpt-5.3-codex",
504 expanded: true,
505 },
506 ],
507 threads: [
508 makeThread({
509 id: "thread-a",
510 projectId: "project-1",
511 }),
512 makeThread({
513 id: "thread-b",
514 projectId: "project-2",
515 }),
516 ],
517 activeThreadId: "thread-a",
518 runtimeMode: "full-access",
519 diffOpen: false,
520 };
521​
522 const next = reducer(state, {
523 type: "DELETE_PROJECT",
524 projectId: "project-1",
525 });
526​
527 expect(next.projects).toHaveLength(1);
528 expect(next.projects[0]?.id).toBe("project-2");
529 expect(next.threads).toHaveLength(1);
530 expect(next.threads[0]?.id).toBe("thread-b");
531 expect(next.activeThreadId).toBe("thread-b");
532 });
533​
534 it("marks the active thread as visited when selected", () => {
535 const state = makeState(
536 makeThread({
537 lastVisitedAt: "2000-01-01T00:00:00.000Z",
538 }),
539 );
540​
541 const next = reducer(state, {
542 type: "SET_ACTIVE_THREAD",
543 threadId: "thread-local-1",
544 });
545​
546 expect(next.activeThreadId).toBe("thread-local-1");
547 expect(next.threads[0]?.lastVisitedAt).toBeDefined();
548 expect(next.threads[0]?.lastVisitedAt).not.toBe("2000-01-01T00:00:00.000Z");
549 });
550​
551 it("marks completion as seen immediately for the active thread", () => {
552 const state = makeState(
553 makeThread({
554 session: makeSession({
555 status: "running",
556 activeTurnId: "turn-1",
557 }),
558 lastVisitedAt: "2026-02-08T10:00:00.000Z",
559 }),
560 );
561​
562 const completedAt = "2026-02-08T10:00:10.000Z";
563 const next = reducer(state, {
564 type: "APPLY_EVENT",
565 event: makeEvent({
566 method: "turn/completed",
567 turnId: "turn-1",
568 createdAt: completedAt,
569 payload: { turn: { id: "turn-1", status: "completed" } },
570 }),
571 activeAssistantItemRef: { current: null },
572 });
573​
574 expect(next.threads[0]?.latestTurnCompletedAt).toBe(completedAt);
575 expect(next.threads[0]?.lastVisitedAt).toBe(completedAt);
576 });
577});