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