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 latestUserMessageAt: null,
95 pendingApprovalCount: 0,
96 pendingUserInputCount: 0,
97 hasActionableProposedPlan: 0,
98 deletedAt: null,
99 });
100
101 const rows = yield* sql<{
102 readonly modelSelection: string | null;
103 }>`
104 SELECT model_selection_json AS "modelSelection"
105 FROM projection_threads
106 WHERE thread_id = 'thread-null-options'
107 `;
108 const row = rows[0];
109 if (!row) {
110 return yield* Effect.die("Expected projection_threads row to exist.");
111 }
112
113 assert.strictEqual(
114 row.modelSelection,
115 // @effect-diagnostics-next-line preferSchemaOverJson:off
116 JSON.stringify({
117 instanceId: ProviderInstanceId.make("claudeAgent"),
118 model: "claude-opus-4-6",
119 }),
120 );
121
122 const persisted = yield* threads.getById({
123 threadId: ThreadId.make("thread-null-options"),
124 });
125 assert.deepStrictEqual(Option.getOrNull(persisted)?.modelSelection, {
126 instanceId: ProviderInstanceId.make("claudeAgent"),
127 model: "claude-opus-4-6",
128 });
129 }),
130 );
131});