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

store.test.ts

153 lines4.3 KB

1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import { reducer, type AppState } from "./store";
5import { DEFAULT_THREAD_TERMINAL_HEIGHT, DEFAULT_THREAD_TERMINAL_ID, type Thread } from "./types";
6​
7function makeThread(overrides: Partial<Thread> = {}): Thread {
8 return {
9 id: ThreadId.makeUnsafe("thread-1"),
10 codexThreadId: null,
11 projectId: ProjectId.makeUnsafe("project-1"),
12 title: "Thread",
13 model: "gpt-5-codex",
14 terminalOpen: false,
15 terminalHeight: DEFAULT_THREAD_TERMINAL_HEIGHT,
16 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
17 runningTerminalIds: [],
18 activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
19 terminalGroups: [
20 {
21 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
22 terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
23 },
24 ],
25 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
26 session: null,
27 messages: [],
28 turnDiffSummaries: [],
29 activities: [],
30 error: null,
31 createdAt: "2026-02-13T00:00:00.000Z",
32 branch: null,
33 worktreePath: null,
34 ...overrides,
35 };
37​
38function makeState(thread: Thread): AppState {
39 return {
40 projects: [
41 {
42 id: ProjectId.makeUnsafe("project-1"),
43 name: "Project",
44 cwd: "/tmp/project",
45 model: "gpt-5-codex",
46 expanded: true,
47 scripts: [],
48 },
49 ],
50 threads: [thread],
51 threadsHydrated: true,
52 runtimeMode: "full-access",
53 };
55​
56describe("store reducer", () => {
57 it("marks a completed thread as unread by moving lastVisitedAt before completion", () => {
58 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
59 const initialState = makeState(
60 makeThread({
61 latestTurnCompletedAt,
62 lastVisitedAt: "2026-02-25T12:35:00.000Z",
63 }),
64 );
65​
66 const next = reducer(initialState, {
67 type: "MARK_THREAD_UNREAD",
68 threadId: ThreadId.makeUnsafe("thread-1"),
69 });
70​
71 const updatedThread = next.threads[0];
72 expect(updatedThread).toBeDefined();
73 expect(updatedThread?.lastVisitedAt).toBe("2026-02-25T12:29:59.999Z");
74 expect(Date.parse(updatedThread?.lastVisitedAt ?? "")).toBeLessThan(
75 Date.parse(latestTurnCompletedAt),
76 );
77 });
78​
79 it("does not change a thread without a completed turn", () => {
80 const initialState = makeState(
81 makeThread({
82 latestTurnCompletedAt: undefined,
83 lastVisitedAt: "2026-02-25T12:35:00.000Z",
84 }),
85 );
86​
87 const next = reducer(initialState, {
88 type: "MARK_THREAD_UNREAD",
89 threadId: ThreadId.makeUnsafe("thread-1"),
90 });
91​
92 expect(next).toEqual(initialState);
93 });
94});
95​
96describe("store terminal activity reducer", () => {
97 it("adds a terminal to runningTerminalIds when subprocess activity starts", () => {
98 const state = makeState(
99 makeThread({
100 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "alt"],
101 terminalGroups: [
102 {
103 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
104 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "alt"],
105 },
106 ],
107 }),
108 );
109 const next = reducer(state, {
110 type: "SET_THREAD_TERMINAL_ACTIVITY",
111 threadId: ThreadId.makeUnsafe("thread-1"),
112 terminalId: "alt",
113 hasRunningSubprocess: true,
114 });
115​
116 expect(next.threads[0]?.runningTerminalIds).toEqual(["alt"]);
117 });
118​
119 it("removes a terminal from runningTerminalIds when subprocess activity stops", () => {
120 const state = makeState(
121 makeThread({
122 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "alt"],
123 terminalGroups: [
124 {
125 id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
126 terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "alt"],
127 },
128 ],
129 runningTerminalIds: ["alt"],
130 }),
131 );
132 const next = reducer(state, {
133 type: "SET_THREAD_TERMINAL_ACTIVITY",
134 threadId: ThreadId.makeUnsafe("thread-1"),
135 terminalId: "alt",
136 hasRunningSubprocess: false,
137 });
138​
139 expect(next.threads[0]?.runningTerminalIds).toEqual([]);
140 });
141​
142 it("ignores activity events for unknown terminal ids", () => {
143 const state = makeState(makeThread());
144 const next = reducer(state, {
145 type: "SET_THREAD_TERMINAL_ACTIVITY",
146 threadId: ThreadId.makeUnsafe("thread-1"),
147 terminalId: "missing",
148 hasRunningSubprocess: true,
149 });
150​
151 expect(next.threads[0]?.runningTerminalIds).toEqual([]);
152 });
153});