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