Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionRepositories.test.ts

260 lines9.0 KB

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