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

store.test.ts

645 lines18.9 KB

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