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

ProjectionThreadMessages.test.ts

222 lines7.2 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 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 const messages = [
22 { role: "user", createdAt: "2026-02-28T19:05:02.000Z" },
23 { role: "user", createdAt: "2026-02-28T19:05:01.000Z" },
24 { role: "assistant", createdAt: "2026-02-28T19:05:03.000Z" },
25 { role: "system", createdAt: "2026-02-28T19:05:04.000Z" },
26 ] as const;
27 for (const [index, message] of messages.entries()) {
28 yield* repository.upsert({
29 messageId: MessageId.make(`latest-user-message-${index}`),
30 threadId,
31 turnId: null,
32 ...message,
33 text: "Message body",
34 isStreaming: false,
35 updatedAt: "2026-02-28T19:06:00.000Z",
36 });
37 }
38 yield* repository.upsert({
39 messageId: MessageId.make("latest-user-message-other-thread"),
40 threadId: ThreadId.make("thread-latest-user-message-other"),
41 turnId: null,
42 role: "user",
43 text: "Other thread",
44 isStreaming: false,
45 createdAt: "2026-02-28T19:05:05.000Z",
46 updatedAt: "2026-02-28T19:05:05.000Z",
47 });
48​
49 assert.strictEqual(
50 yield* repository.getLatestUserMessageAt({ threadId }),
51 "2026-02-28T19:05:02.000Z",
52 );
53 yield* repository.deleteByThreadId({ threadId });
54 assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
55 }),
56 );
57​
58 it.effect("appends streaming text and applies attachment updates", () =>
59 Effect.gen(function* () {
60 const repository = yield* ProjectionThreadMessageRepository;
61 const threadId = ThreadId.make("thread-streaming-append");
62 const messageId = MessageId.make("message-streaming-append");
63 const createdAt = "2026-02-28T19:05:00.000Z";
64 const attachments = [
65 {
66 type: "image" as const,
67 id: "thread-streaming-append-att-1",
68 name: "example.png",
69 mimeType: "image/png",
70 sizeBytes: 5,
71 },
72 ];
73​
74 yield* repository.appendStreaming({
75 messageId,
76 threadId,
77 turnId: null,
78 role: "assistant",
79 text: "hello",
80 attachments,
81 createdAt,
82 updatedAt: createdAt,
83 });
84 yield* repository.appendStreaming({
85 messageId,
86 threadId,
87 turnId: null,
88 role: "assistant",
89 text: " world",
90 createdAt: "2026-02-28T19:05:01.000Z",
91 updatedAt: "2026-02-28T19:05:01.000Z",
92 });
93​
94 const rowWithPreservedAttachments = yield* repository.getByMessageId({ messageId });
95 assert.equal(rowWithPreservedAttachments._tag, "Some");
96 if (rowWithPreservedAttachments._tag === "Some") {
97 assert.deepEqual(rowWithPreservedAttachments.value.attachments, attachments);
98 }
99​
100 yield* repository.appendStreaming({
101 messageId,
102 threadId,
103 turnId: null,
104 role: "assistant",
105 text: "",
106 attachments: [],
107 createdAt: "2026-02-28T19:05:02.000Z",
108 updatedAt: "2026-02-28T19:05:02.000Z",
109 });
110​
111 const row = yield* repository.getByMessageId({ messageId });
112 assert.equal(row._tag, "Some");
113 if (row._tag === "Some") {
114 assert.equal(row.value.text, "hello world");
115 assert.deepEqual(row.value.attachments, []);
116 assert.equal(row.value.createdAt, createdAt);
117 assert.equal(row.value.updatedAt, "2026-02-28T19:05:02.000Z");
118 assert.isTrue(row.value.isStreaming);
119 }
120 }),
121 );
122​
123 it.effect("preserves existing attachments when upsert omits attachments", () =>
124 Effect.gen(function* () {
125 const repository = yield* ProjectionThreadMessageRepository;
126 const threadId = ThreadId.make("thread-preserve-attachments");
127 const messageId = MessageId.make("message-preserve-attachments");
128 const createdAt = "2026-02-28T19:00:00.000Z";
129 const updatedAt = "2026-02-28T19:00:01.000Z";
130 const persistedAttachments = [
131 {
132 type: "image" as const,
133 id: "thread-preserve-attachments-att-1",
134 name: "example.png",
135 mimeType: "image/png",
136 sizeBytes: 5,
137 },
138 ];
139​
140 yield* repository.upsert({
141 messageId,
142 threadId,
143 turnId: null,
144 role: "user",
145 text: "initial",
146 attachments: persistedAttachments,
147 isStreaming: false,
148 createdAt,
149 updatedAt,
150 });
151​
152 yield* repository.upsert({
153 messageId,
154 threadId,
155 turnId: null,
156 role: "user",
157 text: "updated",
158 isStreaming: false,
159 createdAt,
160 updatedAt: "2026-02-28T19:00:02.000Z",
161 });
162​
163 const rows = yield* repository.listByThreadId({ threadId });
164 assert.equal(rows.length, 1);
165 assert.equal(rows[0]?.text, "updated");
166 assert.deepEqual(rows[0]?.attachments, persistedAttachments);
167​
168 const rowById = yield* repository.getByMessageId({ messageId });
169 assert.equal(rowById._tag, "Some");
170 if (rowById._tag === "Some") {
171 assert.equal(rowById.value.text, "updated");
172 assert.deepEqual(rowById.value.attachments, persistedAttachments);
173 }
174 }),
175 );
176​
177 it.effect("allows explicit attachment clearing with an empty array", () =>
178 Effect.gen(function* () {
179 const repository = yield* ProjectionThreadMessageRepository;
180 const threadId = ThreadId.make("thread-clear-attachments");
181 const messageId = MessageId.make("message-clear-attachments");
182 const createdAt = "2026-02-28T19:10:00.000Z";
183​
184 yield* repository.upsert({
185 messageId,
186 threadId,
187 turnId: null,
188 role: "assistant",
189 text: "with attachment",
190 attachments: [
191 {
192 type: "image",
193 id: "thread-clear-attachments-att-1",
194 name: "example.png",
195 mimeType: "image/png",
196 sizeBytes: 5,
197 },
198 ],
199 isStreaming: false,
200 createdAt,
201 updatedAt: "2026-02-28T19:10:01.000Z",
202 });
203​
204 yield* repository.upsert({
205 messageId,
206 threadId,
207 turnId: null,
208 role: "assistant",
209 text: "cleared",
210 attachments: [],
211 isStreaming: false,
212 createdAt,
213 updatedAt: "2026-02-28T19:10:02.000Z",
214 });
215​
216 const rows = yield* repository.listByThreadId({ threadId });
217 assert.equal(rows.length, 1);
218 assert.equal(rows[0]?.text, "cleared");
219 assert.deepEqual(rows[0]?.attachments, []);
220 }),
221 );
222});