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

ProjectionThreadMessages.test.ts

234 lines7.6 KB

1import { MessageId, ThreadId } 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("appends streaming text and applies attachment updates", () =>
71 Effect.gen(function* () {
72 const repository = yield* ProjectionThreadMessageRepository;
73 const threadId = ThreadId.make("thread-streaming-append");
74 const messageId = MessageId.make("message-streaming-append");
75 const createdAt = "2026-02-28T19:05:00.000Z";
76 const attachments = [
77 {
78 type: "image" as const,
79 id: "thread-streaming-append-att-1",
80 name: "example.png",
81 mimeType: "image/png",
82 sizeBytes: 5,
83 },
84 ];
85​
86 yield* repository.appendStreaming({
87 messageId,
88 threadId,
89 turnId: null,
90 role: "assistant",
91 text: "hello",
92 attachments,
93 createdAt,
94 updatedAt: createdAt,
95 });
96 yield* repository.appendStreaming({
97 messageId,
98 threadId,
99 turnId: null,
100 role: "assistant",
101 text: " world",
102 createdAt: "2026-02-28T19:05:01.000Z",
103 updatedAt: "2026-02-28T19:05:01.000Z",
104 });
105​
106 const rowWithPreservedAttachments = yield* repository.getByMessageId({ messageId });
107 assert.equal(rowWithPreservedAttachments._tag, "Some");
108 if (rowWithPreservedAttachments._tag === "Some") {
109 assert.deepEqual(rowWithPreservedAttachments.value.attachments, attachments);
110 }
111​
112 yield* repository.appendStreaming({
113 messageId,
114 threadId,
115 turnId: null,
116 role: "assistant",
117 text: "",
118 attachments: [],
119 createdAt: "2026-02-28T19:05:02.000Z",
120 updatedAt: "2026-02-28T19:05:02.000Z",
121 });
122​
123 const row = yield* repository.getByMessageId({ messageId });
124 assert.equal(row._tag, "Some");
125 if (row._tag === "Some") {
126 assert.equal(row.value.text, "hello world");
127 assert.deepEqual(row.value.attachments, []);
128 assert.equal(row.value.createdAt, createdAt);
129 assert.equal(row.value.updatedAt, "2026-02-28T19:05:02.000Z");
130 assert.isTrue(row.value.isStreaming);
131 }
132 }),
133 );
134​
135 it.effect("preserves existing attachments when upsert omits attachments", () =>
136 Effect.gen(function* () {
137 const repository = yield* ProjectionThreadMessageRepository;
138 const threadId = ThreadId.make("thread-preserve-attachments");
139 const messageId = MessageId.make("message-preserve-attachments");
140 const createdAt = "2026-02-28T19:00:00.000Z";
141 const updatedAt = "2026-02-28T19:00:01.000Z";
142 const persistedAttachments = [
143 {
144 type: "image" as const,
145 id: "thread-preserve-attachments-att-1",
146 name: "example.png",
147 mimeType: "image/png",
148 sizeBytes: 5,
149 },
150 ];
151​
152 yield* repository.upsert({
153 messageId,
154 threadId,
155 turnId: null,
156 role: "user",
157 text: "initial",
158 attachments: persistedAttachments,
159 isStreaming: false,
160 createdAt,
161 updatedAt,
162 });
163​
164 yield* repository.upsert({
165 messageId,
166 threadId,
167 turnId: null,
168 role: "user",
169 text: "updated",
170 isStreaming: false,
171 createdAt,
172 updatedAt: "2026-02-28T19:00:02.000Z",
173 });
174​
175 const rows = yield* repository.listByThreadId({ threadId });
176 assert.equal(rows.length, 1);
177 assert.equal(rows[0]?.text, "updated");
178 assert.deepEqual(rows[0]?.attachments, persistedAttachments);
179​
180 const rowById = yield* repository.getByMessageId({ messageId });
181 assert.equal(rowById._tag, "Some");
182 if (rowById._tag === "Some") {
183 assert.equal(rowById.value.text, "updated");
184 assert.deepEqual(rowById.value.attachments, persistedAttachments);
185 }
186 }),
187 );
188​
189 it.effect("allows explicit attachment clearing with an empty array", () =>
190 Effect.gen(function* () {
191 const repository = yield* ProjectionThreadMessageRepository;
192 const threadId = ThreadId.make("thread-clear-attachments");
193 const messageId = MessageId.make("message-clear-attachments");
194 const createdAt = "2026-02-28T19:10:00.000Z";
195​
196 yield* repository.upsert({
197 messageId,
198 threadId,
199 turnId: null,
200 role: "assistant",
201 text: "with attachment",
202 attachments: [
203 {
204 type: "image",
205 id: "thread-clear-attachments-att-1",
206 name: "example.png",
207 mimeType: "image/png",
208 sizeBytes: 5,
209 },
210 ],
211 isStreaming: false,
212 createdAt,
213 updatedAt: "2026-02-28T19:10:01.000Z",
214 });
215​
216 yield* repository.upsert({
217 messageId,
218 threadId,
219 turnId: null,
220 role: "assistant",
221 text: "cleared",
222 attachments: [],
223 isStreaming: false,
224 createdAt,
225 updatedAt: "2026-02-28T19:10:02.000Z",
226 });
227​
228 const rows = yield* repository.listByThreadId({ threadId });
229 assert.equal(rows.length, 1);
230 assert.equal(rows[0]?.text, "cleared");
231 assert.deepEqual(rows[0]?.attachments, []);
232 }),
233 );
234});