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 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 snoozedUntil: null,
98 snoozedAt: null,
99 pinnedAt: null,
100 latestUserMessageAt: null,
101 pendingApprovalCount: 0,
102 pendingUserInputCount: 0,
103 hasActionableProposedPlan: 0,
104 deletedAt: null,
105 });
106
107 const rows = yield* sql<{
108 readonly modelSelection: string | null;
109 }>`
110 SELECT model_selection_json AS "modelSelection"
111 FROM projection_threads
112 WHERE thread_id = 'thread-null-options'
113 `;
114 const row = rows[0];
115 if (!row) {
116 return yield* Effect.die("Expected projection_threads row to exist.");
117 }
118
119 assert.strictEqual(
120 row.modelSelection,
121 // @effect-diagnostics-next-line preferSchemaOverJson:off
122 JSON.stringify({
123 instanceId: ProviderInstanceId.make("claudeAgent"),
124 model: "claude-opus-4-6",
125 }),
126 );
127
128 const persisted = yield* threads.getById({
129 threadId: ThreadId.make("thread-null-options"),
130 });
131 assert.deepStrictEqual(Option.getOrNull(persisted)?.modelSelection, {
132 instanceId: ProviderInstanceId.make("claudeAgent"),
133 model: "claude-opus-4-6",
134 });
135 }),
136 );
137
138 it.effect("round-trips non-null settlement values through the thread row", () =>
139 Effect.gen(function* () {
140 const threads = yield* ProjectionThreadRepository;
141
142 yield* threads.upsert({
143 threadId: ThreadId.make("thread-settled"),
144 projectId: ProjectId.make("project-1"),
145 title: "Settled thread",
146 modelSelection: {
147 instanceId: ProviderInstanceId.make("codex"),
148 model: "gpt-5.4",
149 },
150 runtimeMode: "full-access",
151 interactionMode: "default",
152 branch: null,
153 worktreePath: null,
154 latestTurnId: null,
155 createdAt: "2026-03-24T00:00:00.000Z",
156 updatedAt: "2026-03-25T00:00:00.000Z",
157 archivedAt: null,
158 settledOverride: "settled",
159 settledAt: "2026-03-25T00:00:00.000Z",
160 snoozedUntil: "2026-03-26T09:00:00.000Z",
161 snoozedAt: "2026-03-25T00:00:00.000Z",
162 pinnedAt: "2026-03-25T00:00:00.000Z",
163 latestUserMessageAt: null,
164 pendingApprovalCount: 0,
165 pendingUserInputCount: 0,
166 hasActionableProposedPlan: 0,
167 deletedAt: null,
168 });
169
170 const persisted = yield* threads.getById({
171 threadId: ThreadId.make("thread-settled"),
172 });
173 const row = Option.getOrNull(persisted);
174 if (!row) {
175 return yield* Effect.die("Expected settled projection_threads row to exist.");
176 }
177 assert.strictEqual(row.settledOverride, "settled");
178 assert.strictEqual(row.settledAt, "2026-03-25T00:00:00.000Z");
179 assert.strictEqual(row.snoozedUntil, "2026-03-26T09:00:00.000Z");
180 assert.strictEqual(row.snoozedAt, "2026-03-25T00:00:00.000Z");
181 assert.strictEqual(row.pinnedAt, "2026-03-25T00:00:00.000Z");
182
183 // Un-settle to the keep-active pin and wake the snooze; confirm the
184 // flips persist.
185 yield* threads.upsert({
186 ...row,
187 settledOverride: "active",
188 settledAt: null,
189 snoozedUntil: null,
190 snoozedAt: null,
191 pinnedAt: null,
192 });
193 const repersisted = yield* threads.getById({
194 threadId: ThreadId.make("thread-settled"),
195 });
196 const updated = Option.getOrNull(repersisted);
197 assert.strictEqual(updated?.settledOverride, "active");
198 assert.strictEqual(updated?.settledAt, null);
199 assert.strictEqual(updated?.snoozedUntil, null);
200 assert.strictEqual(updated?.snoozedAt, null);
201 assert.strictEqual(updated?.pinnedAt, null);
202 }),
203 );
204});