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