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

ProjectionRepositories.test.ts

127 lines4.3 KB

1import { ProjectId, ThreadId, ProviderInstanceId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { Effect, Layer, Option } from "effect";
4import * as SqlClient from "effect/unstable/sql/SqlClient";
5​
6import { SqlitePersistenceMemory } from "./Sqlite.ts";
7import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts";
8import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts";
9import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts";
10import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts";
11​
12const projectionRepositoriesLayer = it.layer(
13 Layer.mergeAll(
14 ProjectionProjectRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
15 ProjectionThreadRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
16 SqlitePersistenceMemory,
17 ),
18);
19​
20projectionRepositoriesLayer("Projection repositories", (it) => {
21 it.effect("stores SQL NULL for missing project model options", () =>
22 Effect.gen(function* () {
23 const projects = yield* ProjectionProjectRepository;
24 const sql = yield* SqlClient.SqlClient;
25​
26 yield* projects.upsert({
27 projectId: ProjectId.make("project-null-options"),
28 title: "Null options project",
29 workspaceRoot: "/tmp/project-null-options",
30 defaultModelSelection: {
31 instanceId: ProviderInstanceId.make("codex"),
32 model: "gpt-5.4",
33 },
34 scripts: [],
35 createdAt: "2026-03-24T00:00:00.000Z",
36 updatedAt: "2026-03-24T00:00:00.000Z",
37 deletedAt: null,
38 });
39​
40 const rows = yield* sql<{
41 readonly defaultModelSelection: string | null;
42 }>`
43 SELECT default_model_selection_json AS "defaultModelSelection"
44 FROM projection_projects
45 WHERE project_id = 'project-null-options'
46 `;
47 const row = rows[0];
48 if (!row) {
49 return yield* Effect.fail(new Error("Expected projection_projects row to exist."));
50 }
51​
52 assert.strictEqual(
53 row.defaultModelSelection,
54 JSON.stringify({
55 instanceId: ProviderInstanceId.make("codex"),
56 model: "gpt-5.4",
57 }),
58 );
59​
60 const persisted = yield* projects.getById({
61 projectId: ProjectId.make("project-null-options"),
62 });
63 assert.deepStrictEqual(Option.getOrNull(persisted)?.defaultModelSelection, {
64 instanceId: ProviderInstanceId.make("codex"),
65 model: "gpt-5.4",
66 });
67 }),
68 );
69​
70 it.effect("stores JSON for thread model options", () =>
71 Effect.gen(function* () {
72 const threads = yield* ProjectionThreadRepository;
73 const sql = yield* SqlClient.SqlClient;
74​
75 yield* threads.upsert({
76 threadId: ThreadId.make("thread-null-options"),
77 projectId: ProjectId.make("project-null-options"),
78 title: "Null options thread",
79 modelSelection: {
80 instanceId: ProviderInstanceId.make("claudeAgent"),
81 model: "claude-opus-4-6",
82 },
83 runtimeMode: "full-access",
84 interactionMode: "default",
85 branch: null,
86 worktreePath: null,
87 latestTurnId: null,
88 createdAt: "2026-03-24T00:00:00.000Z",
89 updatedAt: "2026-03-24T00:00:00.000Z",
90 archivedAt: null,
91 latestUserMessageAt: null,
92 pendingApprovalCount: 0,
93 pendingUserInputCount: 0,
94 hasActionableProposedPlan: 0,
95 deletedAt: null,
96 });
97​
98 const rows = yield* sql<{
99 readonly modelSelection: string | null;
100 }>`
101 SELECT model_selection_json AS "modelSelection"
102 FROM projection_threads
103 WHERE thread_id = 'thread-null-options'
104 `;
105 const row = rows[0];
106 if (!row) {
107 return yield* Effect.fail(new Error("Expected projection_threads row to exist."));
108 }
109​
110 assert.strictEqual(
111 row.modelSelection,
112 JSON.stringify({
113 instanceId: ProviderInstanceId.make("claudeAgent"),
114 model: "claude-opus-4-6",
115 }),
116 );
117​
118 const persisted = yield* threads.getById({
119 threadId: ThreadId.make("thread-null-options"),
120 });
121 assert.deepStrictEqual(Option.getOrNull(persisted)?.modelSelection, {
122 instanceId: ProviderInstanceId.make("claudeAgent"),
123 model: "claude-opus-4-6",
124 });
125 }),
126 );
127});