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, 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
96describe("store pure functions", () => {
97 it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
98 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
99 const initialState = makeState(
100 makeThread({
101 latestTurn: {
102 turnId: TurnId.makeUnsafe("turn-1"),
103 state: "completed",
104 requestedAt: "2026-02-25T12:28:00.000Z",
105 startedAt: "2026-02-25T12:28:30.000Z",
106 completedAt: latestTurnCompletedAt,
107 assistantMessageId: null,
108 },
109 lastVisitedAt: "2026-02-25T12:35:00.000Z",
110 }),
111 );
112
113 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
114
115 const updatedThread = next.threads[0];
116 expect(updatedThread).toBeDefined();
117 expect(updatedThread?.lastVisitedAt).toBe("2026-02-25T12:29:59.999Z");
118 expect(Date.parse(updatedThread?.lastVisitedAt ?? "")).toBeLessThan(
119 Date.parse(latestTurnCompletedAt),
120 );
121 });
122
123 it("markThreadUnread does not change a thread without a completed turn", () => {
124 const initialState = makeState(
125 makeThread({
126 latestTurn: null,
127 lastVisitedAt: "2026-02-25T12:35:00.000Z",
128 }),
129 );
130
131 const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
132
133 expect(next).toEqual(initialState);
134 });
135});
136
137describe("store read model sync", () => {
138 it("falls back to the codex default for unsupported provider models without an active session", () => {
139 const initialState = makeState(makeThread());
140 const readModel = makeReadModel(
141 makeReadModelThread({
142 model: "claude-opus-4-6",
143 }),
144 );
145
146 const next = syncServerReadModel(initialState, readModel);
147
148 expect(next.threads[0]?.model).toBe(DEFAULT_MODEL_BY_PROVIDER.codex);
149 });
150});