Open History to see the last commit
1import { ProjectId, ThreadId, TurnId, type OrchestrationReadModel } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3
4import { markThreadUnread, syncServerReadModel, type AppState } from "./store";
5import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, 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 runtimeMode: DEFAULT_RUNTIME_MODE,
15 interactionMode: DEFAULT_INTERACTION_MODE,
16 session: null,
17 messages: [],
18 turnDiffSummaries: [],
19 activities: [],
20 proposedPlans: [],
21 error: null,
22 createdAt: "2026-02-13T00:00:00.000Z",
23 latestTurn: null,
24 branch: null,
25 worktreePath: null,
26 ...overrides,
27 };
28}
29
30function makeState(thread: Thread): AppState {
31 return {
32 projects: [
33 {
34 id: ProjectId.makeUnsafe("project-1"),
35 name: "Project",
36 cwd: "/tmp/project",
37 model: "gpt-5-codex",
38 expanded: true,
39 scripts: [],
40 },
41 ],
42 threads: [thread],
43 threadsHydrated: true,
44 };
45}
46
47function makeReadModelThread(overrides: Partial<OrchestrationReadModel["threads"][number]>) {
48 return {
49 id: ThreadId.makeUnsafe("thread-1"),
50 projectId: ProjectId.makeUnsafe("project-1"),
51 title: "Thread",
52 model: "gpt-5.3-codex",
53 runtimeMode: DEFAULT_RUNTIME_MODE,
54 interactionMode: DEFAULT_INTERACTION_MODE,
55 branch: null,
56 worktreePath: null,
57 latestTurn: null,
58 createdAt: "2026-02-27T00:00:00.000Z",
59 updatedAt: "2026-02-27T00:00:00.000Z",
60 deletedAt: null,
61 messages: [],
62 activities: [],
63 proposedPlans: [],
64 checkpoints: [],
65 session: null,
66 ...overrides,
67 } satisfies OrchestrationReadModel["threads"][number];
68}
69
70function makeReadModel(thread: OrchestrationReadModel["threads"][number]): OrchestrationReadModel {
71 return {
72 snapshotSequence: 1,
73 updatedAt: "2026-02-27T00:00:00.000Z",
74 projects: [
75 {
76 id: ProjectId.makeUnsafe("project-1"),
77 title: "Project",
78 workspaceRoot: "/tmp/project",
79 defaultModel: "gpt-5.3-codex",
80 createdAt: "2026-02-27T00:00:00.000Z",
81 updatedAt: "2026-02-27T00:00:00.000Z",
82 deletedAt: null,
83 scripts: [],
84 },
85 ],
86 threads: [thread],
87 };
88}
89
90describe("store pure functions", () => {
91 it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
92 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
93 const initialState = makeState(
94 makeThread({
95 latestTurn: {
96 turnId: TurnId.makeUnsafe("turn-1"),
97 state: "completed",
98 requestedAt: "2026-02-25T12:28:00.000Z",
99 startedAt: "2026-02-25T12:28:30.000Z",
100 completedAt: latestTurnCompletedAt,
101 assistantMessageId: null,
102 },
103 lastVisitedAt: "2026-02-25T12:35:00.000Z",
104 }),
105 );
106
107 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
108
109 const updatedThread = next.threads[0];
110 expect(updatedThread).toBeDefined();
111 expect(updatedThread?.lastVisitedAt).toBe("2026-02-25T12:29:59.999Z");
112 expect(Date.parse(updatedThread?.lastVisitedAt ?? "")).toBeLessThan(
113 Date.parse(latestTurnCompletedAt),
114 );
115 });
116
117 it("markThreadUnread does not change a thread without a completed turn", () => {
118 const initialState = makeState(
119 makeThread({
120 latestTurn: null,
121 lastVisitedAt: "2026-02-25T12:35:00.000Z",
122 }),
123 );
124
125 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
126
127 expect(next).toEqual(initialState);
128 });
129});
130
131describe("store read model sync", () => {
132 it("preserves claude model slugs without an active session", () => {
133 const initialState = makeState(makeThread());
134 const readModel = makeReadModel(
135 makeReadModelThread({
136 model: "claude-opus-4-6",
137 }),
138 );
139
140 const next = syncServerReadModel(initialState, readModel);
141
142 expect(next.threads[0]?.model).toBe("claude-opus-4-6");
143 });
144
145 it("resolves claude aliases when session provider is claudeCode", () => {
146 const initialState = makeState(makeThread());
147 const readModel = makeReadModel(
148 makeReadModelThread({
149 model: "sonnet",
150 session: {
151 threadId: ThreadId.makeUnsafe("thread-1"),
152 status: "ready",
153 providerName: "claudeCode",
154 runtimeMode: "approval-required",
155 activeTurnId: null,
156 lastError: null,
157 updatedAt: "2026-02-27T00:00:00.000Z",
158 },
159 }),
160 );
161
162 const next = syncServerReadModel(initialState, readModel);
163
164 expect(next.threads[0]?.model).toBe("claude-sonnet-4-6");
165 });
166
167 it("resolves cursor aliases when session provider is cursor", () => {
168 const initialState = makeState(makeThread());
169 const readModel = makeReadModel(
170 makeReadModelThread({
171 model: "composer",
172 session: {
173 threadId: ThreadId.makeUnsafe("thread-1"),
174 status: "ready",
175 providerName: "cursor",
176 runtimeMode: "approval-required",
177 activeTurnId: null,
178 lastError: null,
179 updatedAt: "2026-02-27T00:00:00.000Z",
180 },
181 }),
182 );
183
184 const next = syncServerReadModel(initialState, readModel);
185
186 expect(next.threads[0]?.model).toBe("composer-1.5");
187 expect(next.threads[0]?.session?.provider).toBe("cursor");
188 });
189});