Open History to see the last commit
1import { ProjectId, ThreadId, ProviderInstanceId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as SqlClient from "effect/unstable/sql/SqlClient";
7
8import { SqlitePersistenceMemory } from "./Sqlite.ts";
9import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts";
10import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts";
11import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts";
12import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts";
13
14const projectionRepositoriesLayer = it.layer(
15 Layer.mergeAll(
16 ProjectionProjectRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
17 ProjectionThreadRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
18 SqlitePersistenceMemory,
19 ),
20);
21
22projectionRepositoriesLayer("Projection repositories", (it) => {
23 it.effect("stores SQL NULL for missing project model options", () =>
24 Effect.gen(function* () {
25 const projects = yield* ProjectionProjectRepository;
26 const sql = yield* SqlClient.SqlClient;
27
28 yield* projects.upsert({
29 projectId: ProjectId.make("project-null-options"),
30 title: "Null options project",
31 workspaceRoot: "/tmp/project-null-options",
32 defaultModelSelection: {
33 instanceId: ProviderInstanceId.make("codex"),
34 model: "gpt-5.4",
35 },
36 defaultThreadEnvMode: null,
37 autoPull: false,
38 scripts: [],
39 createdAt: "2026-03-24T00:00:00.000Z",
40 updatedAt: "2026-03-24T00:00:00.000Z",
41 deletedAt: null,
42 });
43
44 const rows = yield* sql<{
45 readonly defaultModelSelection: string | null;
46 }>`
47 SELECT default_model_selection_json AS "defaultModelSelection"
48 FROM projection_projects
49 WHERE project_id = 'project-null-options'
50 `;
51 const row = rows[0];
52 if (!row) {
53 return yield* Effect.die("Expected projection_projects row to exist.");
54 }
55
56 assert.strictEqual(
57 row.defaultModelSelection,
58 // @effect-diagnostics-next-line preferSchemaOverJson:off
59 JSON.stringify({
60 instanceId: ProviderInstanceId.make("codex"),
61 model: "gpt-5.4",
62 }),
63 );
64
65 const persisted = yield* projects.getById({
66 projectId: ProjectId.make("project-null-options"),
67 });
68 assert.deepStrictEqual(Option.getOrNull(persisted)?.defaultModelSelection, {
69 instanceId: ProviderInstanceId.make("codex"),
70 model: "gpt-5.4",
71 });
72 }),
73 );
74
75 it.effect("stores JSON for thread model options", () =>
76 Effect.gen(function* () {
77 const threads = yield* ProjectionThreadRepository;
78 const sql = yield* SqlClient.SqlClient;
79
80 yield* threads.upsert({
81 threadId: ThreadId.make("thread-null-options"),
82 projectId: ProjectId.make("project-null-options"),
83 title: "Null options thread",
84 modelSelection: {
85 instanceId: ProviderInstanceId.make("claudeAgent"),
86 model: "claude-opus-4-6",
87 },
88 runtimeMode: "full-access",
89 interactionMode: "default",
90 branch: null,
91 worktreePath: null,
92 latestTurnId: null,
93 createdAt: "2026-03-24T00:00:00.000Z",
94 updatedAt: "2026-03-24T00:00:00.000Z",
95 archivedAt: null,
96 settledOverride: null,
97 settledAt: null,
98 unsettledAt: null,
99 snoozedUntil: null,
100 snoozedAt: null,
101 pinnedAt: null,
102 latestUserMessageAt: null,
103 pendingApprovalCount: 0,
104 pendingUserInputCount: 0,
105 hasActionableProposedPlan: 0,
106 deletedAt: null,
107 });
108
109 const rows = yield* sql<{
110 readonly modelSelection: string | null;
111 }>`
112 SELECT model_selection_json AS "modelSelection"
113 FROM projection_threads
114 WHERE thread_id = 'thread-null-options'
115 `;
116 const row = rows[0];
117 if (!row) {
118 return yield* Effect.die("Expected projection_threads row to exist.");
119 }
120
121 assert.strictEqual(
122 row.modelSelection,
123 // @effect-diagnostics-next-line preferSchemaOverJson:off
124 JSON.stringify({
125 instanceId: ProviderInstanceId.make("claudeAgent"),
126 model: "claude-opus-4-6",
127 }),
128 );
129
130 const persisted = yield* threads.getById({
131 threadId: ThreadId.make("thread-null-options"),
132 });
133 assert.deepStrictEqual(Option.getOrNull(persisted)?.modelSelection, {
134 instanceId: ProviderInstanceId.make("claudeAgent"),
135 model: "claude-opus-4-6",
136 });
137 }),
138 );
139
140 it.effect("round-trips non-null settlement values through the thread row", () =>
141 Effect.gen(function* () {
142 const threads = yield* ProjectionThreadRepository;
143
144 yield* threads.upsert({
145 threadId: ThreadId.make("thread-settled"),
146 projectId: ProjectId.make("project-1"),
147 title: "Settled thread",
148 modelSelection: {
149 instanceId: ProviderInstanceId.make("codex"),
150 model: "gpt-5.4",
151 },
152 runtimeMode: "full-access",
153 interactionMode: "default",
154 branch: null,
155 worktreePath: null,
156 latestTurnId: null,
157 createdAt: "2026-03-24T00:00:00.000Z",
158 updatedAt: "2026-03-25T00:00:00.000Z",
159 archivedAt: null,
160 settledOverride: "settled",
161 settledAt: "2026-03-25T00:00:00.000Z",
162 unsettledAt: null,
163 snoozedUntil: "2026-03-26T09:00:00.000Z",
164 snoozedAt: "2026-03-25T00:00:00.000Z",
165 pinnedAt: "2026-03-25T00:00:00.000Z",
166 latestUserMessageAt: null,
167 pendingApprovalCount: 0,
168 pendingUserInputCount: 0,
169 hasActionableProposedPlan: 0,
170 deletedAt: null,
171 });
172
173 const persisted = yield* threads.getById({
174 threadId: ThreadId.make("thread-settled"),
175 });
176 const row = Option.getOrNull(persisted);
177 if (!row) {
178 return yield* Effect.die("Expected settled projection_threads row to exist.");
179 }
180 assert.strictEqual(row.settledOverride, "settled");
181 assert.strictEqual(row.settledAt, "2026-03-25T00:00:00.000Z");
182 assert.strictEqual(row.snoozedUntil, "2026-03-26T09:00:00.000Z");
183 assert.strictEqual(row.snoozedAt, "2026-03-25T00:00:00.000Z");
184 assert.strictEqual(row.pinnedAt, "2026-03-25T00:00:00.000Z");
185
186 // Un-settle to the keep-active pin and wake the snooze; confirm the
187 // flips persist.
188 yield* threads.upsert({
189 ...row,
190 settledOverride: "active",
191 settledAt: null,
192 unsettledAt: "2026-03-26T00:00:00.000Z",
193 snoozedUntil: null,
194 snoozedAt: null,
195 pinnedAt: null,
196 });
197 const repersisted = yield* threads.getById({
198 threadId: ThreadId.make("thread-settled"),
199 });
200 const updated = Option.getOrNull(repersisted);
201 assert.strictEqual(updated?.settledOverride, "active");
202 assert.strictEqual(updated?.settledAt, null);
203 assert.strictEqual(updated?.unsettledAt, "2026-03-26T00:00:00.000Z");
204 assert.strictEqual(updated?.snoozedUntil, null);
205 assert.strictEqual(updated?.snoozedAt, null);
206 assert.strictEqual(updated?.pinnedAt, null);
207 }),
208 );
209
210 it.effect("round-trips a linked pull request through the thread row", () =>
211 Effect.gen(function* () {
212 const threads = yield* ProjectionThreadRepository;
213 const linkedPullRequest = {
214 projectId: ProjectId.make("project-linked-pr"),
215 repository: "pingdotgg/t3code",
216 number: 42,
217 url: "https://github.com/pingdotgg/t3code/pull/42",
218 };
219
220 yield* threads.upsert({
221 threadId: ThreadId.make("thread-linked-pr"),
222 projectId: ProjectId.make("project-linked-pr"),
223 title: "Linked pull request",
224 modelSelection: {
225 instanceId: ProviderInstanceId.make("codex"),
226 model: "gpt-5.4",
227 },
228 runtimeMode: "full-access",
229 interactionMode: "default",
230 branch: null,
231 worktreePath: null,
232 linkedPullRequest,
233 latestTurnId: null,
234 createdAt: "2026-03-24T00:00:00.000Z",
235 updatedAt: "2026-03-24T00:00:00.000Z",
236 archivedAt: null,
237 settledOverride: null,
238 settledAt: null,
239 unsettledAt: null,
240 snoozedUntil: null,
241 snoozedAt: null,
242 pinnedAt: null,
243 latestUserMessageAt: null,
244 pendingApprovalCount: 0,
245 pendingUserInputCount: 0,
246 hasActionableProposedPlan: 0,
247 deletedAt: null,
248 });
249
250 const persisted = yield* threads.getById({ threadId: ThreadId.make("thread-linked-pr") });
251 assert.deepStrictEqual(Option.getOrNull(persisted)?.linkedPullRequest, linkedPullRequest);
252
253 const row = Option.getOrNull(persisted);
254 if (row === null) return yield* Effect.die("Expected linked thread row to exist.");
255 yield* threads.upsert({ ...row, linkedPullRequest: null });
256
257 const cleared = yield* threads.getById({ threadId: ThreadId.make("thread-linked-pr") });
258 assert.strictEqual(Option.getOrNull(cleared)?.linkedPullRequest, null);
259 }),
260 );
261});