Open History to see the last commit
1import {
2 DEFAULT_MODEL_BY_PROVIDER,
3 ProjectId,
4 ThreadId,
5 TurnId,
6 type OrchestrationReadModel,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vitest";
9
10import { markThreadUnread, reorderProjects, syncServerReadModel, type AppState } from "./store";
11import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, type Thread } from "./types";
12
13function makeThread(overrides: Partial<Thread> = {}): Thread {
14 return {
15 id: ThreadId.makeUnsafe("thread-1"),
16 codexThreadId: null,
17 projectId: ProjectId.makeUnsafe("project-1"),
18 title: "Thread",
19 titleSummaryState: "missing",
20 model: "gpt-5-codex",
21 runtimeMode: DEFAULT_RUNTIME_MODE,
22 interactionMode: DEFAULT_INTERACTION_MODE,
23 session: null,
24 messages: [],
25 turnDiffSummaries: [],
26 activities: [],
27 proposedPlans: [],
28 error: null,
29 createdAt: "2026-02-13T00:00:00.000Z",
30 latestTurn: null,
31 branch: null,
32 worktreePath: null,
33 ...overrides,
34 };
35}
36
37function makeState(thread: Thread): AppState {
38 return {
39 projects: [
40 {
41 id: ProjectId.makeUnsafe("project-1"),
42 name: "Project",
43 cwd: "/tmp/project",
44 model: "gpt-5-codex",
45 expanded: true,
46 scripts: [],
47 },
48 ],
49 threads: [thread],
50 threadsHydrated: true,
51 };
52}
53
54function makeReadModelThread(overrides: Partial<OrchestrationReadModel["threads"][number]>) {
55 return {
56 id: ThreadId.makeUnsafe("thread-1"),
57 projectId: ProjectId.makeUnsafe("project-1"),
58 title: "Thread",
59 titleSummaryState: "missing",
60 model: "gpt-5.3-codex",
61 runtimeMode: DEFAULT_RUNTIME_MODE,
62 interactionMode: DEFAULT_INTERACTION_MODE,
63 branch: null,
64 worktreePath: null,
65 latestTurn: null,
66 createdAt: "2026-02-27T00:00:00.000Z",
67 updatedAt: "2026-02-27T00:00:00.000Z",
68 deletedAt: null,
69 messages: [],
70 activities: [],
71 proposedPlans: [],
72 checkpoints: [],
73 session: null,
74 ...overrides,
75 } satisfies OrchestrationReadModel["threads"][number];
76}
77
78function makeReadModel(thread: OrchestrationReadModel["threads"][number]): OrchestrationReadModel {
79 return {
80 snapshotSequence: 1,
81 updatedAt: "2026-02-27T00:00:00.000Z",
82 projects: [
83 {
84 id: ProjectId.makeUnsafe("project-1"),
85 title: "Project",
86 workspaceRoot: "/tmp/project",
87 defaultModel: "gpt-5.3-codex",
88 createdAt: "2026-02-27T00:00:00.000Z",
89 updatedAt: "2026-02-27T00:00:00.000Z",
90 deletedAt: null,
91 scripts: [],
92 },
93 ],
94 threads: [thread],
95 };
96}
97
98function makeReadModelProject(
99 overrides: Partial<OrchestrationReadModel["projects"][number]>,
100): OrchestrationReadModel["projects"][number] {
101 return {
102 id: ProjectId.makeUnsafe("project-1"),
103 title: "Project",
104 workspaceRoot: "/tmp/project",
105 defaultModel: "gpt-5.3-codex",
106 createdAt: "2026-02-27T00:00:00.000Z",
107 updatedAt: "2026-02-27T00:00:00.000Z",
108 deletedAt: null,
109 scripts: [],
110 ...overrides,
111 };
112}
113
114describe("store pure functions", () => {
115 it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
116 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
117 const initialState = makeState(
118 makeThread({
119 latestTurn: {
120 turnId: TurnId.makeUnsafe("turn-1"),
121 state: "completed",
122 requestedAt: "2026-02-25T12:28:00.000Z",
123 startedAt: "2026-02-25T12:28:30.000Z",
124 completedAt: latestTurnCompletedAt,
125 assistantMessageId: null,
126 },
127 lastVisitedAt: "2026-02-25T12:35:00.000Z",
128 }),
129 );
130
131 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
132
133 const updatedThread = next.threads[0];
134 expect(updatedThread).toBeDefined();
135 expect(updatedThread?.lastVisitedAt).toBe("2026-02-25T12:29:59.999Z");
136 expect(Date.parse(updatedThread?.lastVisitedAt ?? "")).toBeLessThan(
137 Date.parse(latestTurnCompletedAt),
138 );
139 });
140
141 it("markThreadUnread does not change a thread without a completed turn", () => {
142 const initialState = makeState(
143 makeThread({
144 latestTurn: null,
145 lastVisitedAt: "2026-02-25T12:35:00.000Z",
146 }),
147 );
148
149 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
150
151 expect(next).toEqual(initialState);
152 });
153
154 it("reorderProjects moves a project to a target index", () => {
155 const project1 = ProjectId.makeUnsafe("project-1");
156 const project2 = ProjectId.makeUnsafe("project-2");
157 const project3 = ProjectId.makeUnsafe("project-3");
158 const state: AppState = {
159 projects: [
160 {
161 id: project1,
162 name: "Project 1",
163 cwd: "/tmp/project-1",
164 model: DEFAULT_MODEL_BY_PROVIDER.codex,
165 expanded: true,
166 scripts: [],
167 },
168 {
169 id: project2,
170 name: "Project 2",
171 cwd: "/tmp/project-2",
172 model: DEFAULT_MODEL_BY_PROVIDER.codex,
173 expanded: true,
174 scripts: [],
175 },
176 {
177 id: project3,
178 name: "Project 3",
179 cwd: "/tmp/project-3",
180 model: DEFAULT_MODEL_BY_PROVIDER.codex,
181 expanded: true,
182 scripts: [],
183 },
184 ],
185 threads: [],
186 threadsHydrated: true,
187 };
188
189 const next = reorderProjects(state, project1, project3);
190
191 expect(next.projects.map((project) => project.id)).toEqual([project2, project3, project1]);
192 });
193});
194
195describe("store read model sync", () => {
196 it("preserves claude model slugs without an active session", () => {
197 const initialState = makeState(makeThread());
198 const readModel = makeReadModel(
199 makeReadModelThread({
200 model: "claude-opus-4-6",
201 }),
202 );
203
204 const next = syncServerReadModel(initialState, readModel);
205
206 expect(next.threads[0]?.model).toBe("claude-opus-4-6");
207 });
208
209 it("resolves claude aliases when session provider is claudeAgent", () => {
210 const initialState = makeState(makeThread());
211 const readModel = makeReadModel(
212 makeReadModelThread({
213 model: "sonnet",
214 session: {
215 threadId: ThreadId.makeUnsafe("thread-1"),
216 status: "ready",
217 providerName: "claudeAgent",
218 runtimeMode: "approval-required",
219 activeTurnId: null,
220 lastError: null,
221 updatedAt: "2026-02-27T00:00:00.000Z",
222 },
223 }),
224 );
225
226 const next = syncServerReadModel(initialState, readModel);
227
228 expect(next.threads[0]?.model).toBe("claude-sonnet-4-6");
229 });
230
231 it("preserves the current project order when syncing incoming read model updates", () => {
232 const project1 = ProjectId.makeUnsafe("project-1");
233 const project2 = ProjectId.makeUnsafe("project-2");
234 const project3 = ProjectId.makeUnsafe("project-3");
235 const initialState: AppState = {
236 projects: [
237 {
238 id: project2,
239 name: "Project 2",
240 cwd: "/tmp/project-2",
241 model: DEFAULT_MODEL_BY_PROVIDER.codex,
242 expanded: true,
243 scripts: [],
244 },
245 {
246 id: project1,
247 name: "Project 1",
248 cwd: "/tmp/project-1",
249 model: DEFAULT_MODEL_BY_PROVIDER.codex,
250 expanded: true,
251 scripts: [],
252 },
253 ],
254 threads: [],
255 threadsHydrated: true,
256 };
257 const readModel: OrchestrationReadModel = {
258 snapshotSequence: 2,
259 updatedAt: "2026-02-27T00:00:00.000Z",
260 projects: [
261 makeReadModelProject({
262 id: project1,
263 title: "Project 1",
264 workspaceRoot: "/tmp/project-1",
265 }),
266 makeReadModelProject({
267 id: project2,
268 title: "Project 2",
269 workspaceRoot: "/tmp/project-2",
270 }),
271 makeReadModelProject({
272 id: project3,
273 title: "Project 3",
274 workspaceRoot: "/tmp/project-3",
275 }),
276 ],
277 threads: [],
278 };
279
280 const next = syncServerReadModel(initialState, readModel);
281
282 expect(next.projects.map((project) => project.id)).toEqual([project2, project1, project3]);
283 });
284});