Open History to see the last commit
1import { MessageId, ThreadId, TurnId } 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";
6
7import { ProjectionThreadMessageRepository } from "../Services/ProjectionThreadMessages.ts";
8import { ProjectionThreadMessageRepositoryLive } from "./ProjectionThreadMessages.ts";
9import { SqlitePersistenceMemory } from "./Sqlite.ts";
10
11const layer = it.layer(
12 ProjectionThreadMessageRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
13);
14
15layer("ProjectionThreadMessageRepository", (it) => {
16 it.effect("finds the latest live user-message time within one thread", () =>
17 Effect.gen(function* () {
18 const repository = yield* ProjectionThreadMessageRepository;
19 const threadId = ThreadId.make("thread-latest-user-message");
20 assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
21
22 yield* repository.upsert({
23 messageId: MessageId.make("import:codex:latest-user-message:000000"),
24 threadId,
25 turnId: null,
26 role: "user",
27 text: "Imported prompt",
28 isStreaming: false,
29 createdAt: "2026-02-28T19:05:06.000Z",
30 updatedAt: "2026-02-28T19:05:06.000Z",
31 });
32 assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
33
34 const messages = [
35 { role: "user", createdAt: "2026-02-28T19:05:02.000Z" },
36 { role: "user", createdAt: "2026-02-28T19:05:01.000Z" },
37 { role: "assistant", createdAt: "2026-02-28T19:05:03.000Z" },
38 { role: "system", createdAt: "2026-02-28T19:05:04.000Z" },
39 ] as const;
40 for (const [index, message] of messages.entries()) {
41 yield* repository.upsert({
42 messageId: MessageId.make(`latest-user-message-${index}`),
43 threadId,
44 turnId: null,
45 ...message,
46 text: "Message body",
47 isStreaming: false,
48 updatedAt: "2026-02-28T19:06:00.000Z",
49 });
50 }
51 yield* repository.upsert({
52 messageId: MessageId.make("latest-user-message-other-thread"),
53 threadId: ThreadId.make("thread-latest-user-message-other"),
54 turnId: null,
55 role: "user",
56 text: "Other thread",
57 isStreaming: false,
58 createdAt: "2026-02-28T19:05:05.000Z",
59 updatedAt: "2026-02-28T19:05:05.000Z",
60 });
61
62 assert.strictEqual(
63 yield* repository.getLatestUserMessageAt({ threadId }),
64 "2026-02-28T19:05:02.000Z",
65 );
66 yield* repository.deleteByThreadId({ threadId });
67 assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
68 }),
69 );
70
71 it.effect("persists structured context and keeps it across updates without context", () =>
72 Effect.gen(function* () {
73 const repository = yield* ProjectionThreadMessageRepository;
74 const threadId = ThreadId.make("thread-context");
75 const messageId = MessageId.make("message-context");
76 const createdAt = "2026-02-28T19:05:00.000Z";
77 const context = {
78 version: 1 as const,
79 records: [
80 {
81 version: 1 as const,
82 contextId: "ctx_1" as never,
83 kind: "terminal" as const,
84 label: "Terminal 1 line 4",
85 terminalId: "default",
86 terminalLabel: "Terminal 1",
87 lineStart: 4,
88 lineEnd: 4,
89 text: "boom",
90 },
91 ],
92 };
93 yield* repository.upsert({
94 messageId,
95 threadId,
96 turnId: null,
97 role: "user",
98 text: "see [Terminal 1 line 4](t3-context://v1/terminal/ctx_1)",
99 context,
100 isStreaming: false,
101 createdAt,
102 updatedAt: createdAt,
103 });
104 yield* repository.upsert({
105 messageId,
106 threadId,
107 turnId: null,
108 role: "user",
109 text: "see [Terminal 1 line 4](t3-context://v1/terminal/ctx_1)",
110 isStreaming: false,
111 createdAt,
112 updatedAt: "2026-02-28T19:05:01.000Z",
113 });
114 const rows = yield* repository.listByThreadId({ threadId });
115 assert.deepStrictEqual(rows[0]?.context, context);
116 }),
117 );
118
119 it.effect("appends streaming text and applies attachment updates", () =>
120 Effect.gen(function* () {
121 const repository = yield* ProjectionThreadMessageRepository;
122 const threadId = ThreadId.make("thread-streaming-append");
123 const messageId = MessageId.make("message-streaming-append");
124 const createdAt = "2026-02-28T19:05:00.000Z";
125 const attachments = [
126 {
127 type: "image" as const,
128 id: "thread-streaming-append-att-1",
129 name: "example.png",
130 mimeType: "image/png",
131 sizeBytes: 5,
132 },
133 ];
134
135 yield* repository.appendStreaming({
136 messageId,
137 threadId,
138 turnId: null,
139 role: "assistant",
140 text: "hello",
141 attachments,
142 createdAt,
143 updatedAt: createdAt,
144 });
145 yield* repository.appendStreaming({
146 messageId,
147 threadId,
148 turnId: null,
149 role: "assistant",
150 text: " world",
151 createdAt: "2026-02-28T19:05:01.000Z",
152 updatedAt: "2026-02-28T19:05:01.000Z",
153 });
154
155 const rowWithPreservedAttachments = yield* repository.getByMessageId({ messageId });
156 assert.equal(rowWithPreservedAttachments._tag, "Some");
157 if (rowWithPreservedAttachments._tag === "Some") {
158 assert.deepEqual(rowWithPreservedAttachments.value.attachments, attachments);
159 }
160
161 yield* repository.appendStreaming({
162 messageId,
163 threadId,
164 turnId: null,
165 role: "assistant",
166 text: "",
167 attachments: [],
168 createdAt: "2026-02-28T19:05:02.000Z",
169 updatedAt: "2026-02-28T19:05:02.000Z",
170 });
171
172 const row = yield* repository.getByMessageId({ messageId });
173 assert.equal(row._tag, "Some");
174 if (row._tag === "Some") {
175 assert.equal(row.value.text, "hello world");
176 assert.deepEqual(row.value.attachments, []);
177 assert.equal(row.value.createdAt, createdAt);
178 assert.equal(row.value.updatedAt, "2026-02-28T19:05:02.000Z");
179 assert.isTrue(row.value.isStreaming);
180 }
181 }),
182 );
183
184 it.effect("persists actual model metadata across later partial upserts", () =>
185 Effect.gen(function* () {
186 const repository = yield* ProjectionThreadMessageRepository;
187 const threadId = ThreadId.make("thread-actual-model");
188 const messageId = MessageId.make("message-actual-model");
189 const createdAt = "2026-08-14T00:00:00.000Z";
190
191 yield* repository.upsert({
192 messageId,
193 threadId,
194 turnId: null,
195 role: "assistant",
196 text: "complete",
197 actualModel: "gpt-5.6-luna",
198 isStreaming: false,
199 createdAt,
200 updatedAt: createdAt,
201 });
202 yield* repository.upsert({
203 messageId,
204 threadId,
205 turnId: null,
206 role: "assistant",
207 text: "complete",
208 isStreaming: false,
209 createdAt,
210 updatedAt: "2026-08-14T00:00:01.000Z",
211 });
212
213 const row = yield* repository.getByMessageId({ messageId });
214 assert.equal(row._tag, "Some");
215 if (row._tag === "Some") {
216 assert.equal(row.value.actualModel, "gpt-5.6-luna");
217 }
218 }),
219 );
220
221 it.effect("preserves existing attachments when upsert omits attachments", () =>
222 Effect.gen(function* () {
223 const repository = yield* ProjectionThreadMessageRepository;
224 const threadId = ThreadId.make("thread-preserve-attachments");
225 const messageId = MessageId.make("message-preserve-attachments");
226 const createdAt = "2026-02-28T19:00:00.000Z";
227 const updatedAt = "2026-02-28T19:00:01.000Z";
228 const persistedAttachments = [
229 {
230 type: "image" as const,
231 id: "thread-preserve-attachments-att-1",
232 name: "example.png",
233 mimeType: "image/png",
234 sizeBytes: 5,
235 },
236 ];
237
238 yield* repository.upsert({
239 messageId,
240 threadId,
241 turnId: null,
242 role: "user",
243 text: "initial",
244 attachments: persistedAttachments,
245 isStreaming: false,
246 createdAt,
247 updatedAt,
248 });
249
250 yield* repository.upsert({
251 messageId,
252 threadId,
253 turnId: null,
254 role: "user",
255 text: "updated",
256 isStreaming: false,
257 createdAt,
258 updatedAt: "2026-02-28T19:00:02.000Z",
259 });
260
261 const rows = yield* repository.listByThreadId({ threadId });
262 assert.equal(rows.length, 1);
263 assert.equal(rows[0]?.text, "updated");
264 assert.deepEqual(rows[0]?.attachments, persistedAttachments);
265
266 const rowById = yield* repository.getByMessageId({ messageId });
267 assert.equal(rowById._tag, "Some");
268 if (rowById._tag === "Some") {
269 assert.equal(rowById.value.text, "updated");
270 assert.deepEqual(rowById.value.attachments, persistedAttachments);
271 }
272 }),
273 );
274
275 it.effect("allows explicit attachment clearing with an empty array", () =>
276 Effect.gen(function* () {
277 const repository = yield* ProjectionThreadMessageRepository;
278 const threadId = ThreadId.make("thread-clear-attachments");
279 const messageId = MessageId.make("message-clear-attachments");
280 const createdAt = "2026-02-28T19:10:00.000Z";
281
282 yield* repository.upsert({
283 messageId,
284 threadId,
285 turnId: null,
286 role: "assistant",
287 text: "with attachment",
288 attachments: [
289 {
290 type: "image",
291 id: "thread-clear-attachments-att-1",
292 name: "example.png",
293 mimeType: "image/png",
294 sizeBytes: 5,
295 },
296 ],
297 isStreaming: false,
298 createdAt,
299 updatedAt: "2026-02-28T19:10:01.000Z",
300 });
301
302 yield* repository.upsert({
303 messageId,
304 threadId,
305 turnId: null,
306 role: "assistant",
307 text: "cleared",
308 attachments: [],
309 isStreaming: false,
310 createdAt,
311 updatedAt: "2026-02-28T19:10:02.000Z",
312 });
313
314 const rows = yield* repository.listByThreadId({ threadId });
315 assert.equal(rows.length, 1);
316 assert.equal(rows[0]?.text, "cleared");
317 assert.deepEqual(rows[0]?.attachments, []);
318 }),
319 );
320
321 it.effect("checks assistant turn state without hydrating message text", () =>
322 Effect.gen(function* () {
323 const repository = yield* ProjectionThreadMessageRepository;
324 const threadId = ThreadId.make("thread-assistant-turn-state");
325 const turnId = TurnId.make("turn-assistant-state");
326 const createdAt = "2026-03-01T00:00:00.000Z";
327
328 yield* repository.upsert({
329 messageId: MessageId.make("message-assistant-turn-state"),
330 threadId,
331 turnId,
332 role: "assistant",
333 text: "large text that the existence query must not select",
334 isStreaming: false,
335 createdAt,
336 updatedAt: createdAt,
337 });
338
339 assert.equal(
340 yield* repository.hasAssistantMessageForTurn({
341 threadId,
342 turnId,
343 streamingOnly: false,
344 }),
345 true,
346 );
347 assert.equal(
348 yield* repository.hasAssistantMessageForTurn({
349 threadId,
350 turnId,
351 streamingOnly: true,
352 }),
353 false,
354 );
355 assert.equal(
356 yield* repository.hasAssistantMessageForTurn({
357 threadId,
358 turnId: TurnId.make("turn-assistant-state-missing"),
359 streamingOnly: false,
360 }),
361 false,
362 );
363 }),
364 );
365
366 it.effect("finds the latest assistant message id for a turn without hydrating message text", () =>
367 Effect.gen(function* () {
368 const repository = yield* ProjectionThreadMessageRepository;
369 const threadId = ThreadId.make("thread-latest-assistant-message");
370 const turnId = TurnId.make("turn-latest-assistant-message");
371
372 assert.isTrue(
373 Option.isNone(yield* repository.getLatestAssistantMessageIdForTurn({ threadId, turnId })),
374 );
375 for (const [index, createdAt] of [
376 "2026-03-01T00:00:00.000Z",
377 "2026-03-01T00:00:01.000Z",
378 ].entries()) {
379 yield* repository.upsert({
380 messageId: MessageId.make(`message-latest-assistant-${index}`),
381 threadId,
382 turnId,
383 role: "assistant",
384 text: "large text that the id query must not select",
385 isStreaming: false,
386 createdAt,
387 updatedAt: createdAt,
388 });
389 }
390
391 assert.deepEqual(
392 yield* repository.getLatestAssistantMessageIdForTurn({ threadId, turnId }),
393 Option.some(MessageId.make("message-latest-assistant-1")),
394 );
395 }),
396 );
397});