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

ProjectionThreadMessages.test.ts

327 lines10.3 KB

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