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

ProjectionThreadMessages.test.ts

179 lines5.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("appends streaming text and applies attachment updates", () =>
16 Effect.gen(function* () {
17 const repository = yield* ProjectionThreadMessageRepository;
18 const threadId = ThreadId.make("thread-streaming-append");
19 const messageId = MessageId.make("message-streaming-append");
20 const createdAt = "2026-02-28T19:05:00.000Z";
21 const attachments = [
22 {
23 type: "image" as const,
24 id: "thread-streaming-append-att-1",
25 name: "example.png",
26 mimeType: "image/png",
27 sizeBytes: 5,
28 },
29 ];
30​
31 yield* repository.appendStreaming({
32 messageId,
33 threadId,
34 turnId: null,
35 role: "assistant",
36 text: "hello",
37 attachments,
38 createdAt,
39 updatedAt: createdAt,
40 });
41 yield* repository.appendStreaming({
42 messageId,
43 threadId,
44 turnId: null,
45 role: "assistant",
46 text: " world",
47 createdAt: "2026-02-28T19:05:01.000Z",
48 updatedAt: "2026-02-28T19:05:01.000Z",
49 });
50​
51 const rowWithPreservedAttachments = yield* repository.getByMessageId({ messageId });
52 assert.equal(rowWithPreservedAttachments._tag, "Some");
53 if (rowWithPreservedAttachments._tag === "Some") {
54 assert.deepEqual(rowWithPreservedAttachments.value.attachments, attachments);
55 }
56​
57 yield* repository.appendStreaming({
58 messageId,
59 threadId,
60 turnId: null,
61 role: "assistant",
62 text: "",
63 attachments: [],
64 createdAt: "2026-02-28T19:05:02.000Z",
65 updatedAt: "2026-02-28T19:05:02.000Z",
66 });
67​
68 const row = yield* repository.getByMessageId({ messageId });
69 assert.equal(row._tag, "Some");
70 if (row._tag === "Some") {
71 assert.equal(row.value.text, "hello world");
72 assert.deepEqual(row.value.attachments, []);
73 assert.equal(row.value.createdAt, createdAt);
74 assert.equal(row.value.updatedAt, "2026-02-28T19:05:02.000Z");
75 assert.isTrue(row.value.isStreaming);
76 }
77 }),
78 );
79​
80 it.effect("preserves existing attachments when upsert omits attachments", () =>
81 Effect.gen(function* () {
82 const repository = yield* ProjectionThreadMessageRepository;
83 const threadId = ThreadId.make("thread-preserve-attachments");
84 const messageId = MessageId.make("message-preserve-attachments");
85 const createdAt = "2026-02-28T19:00:00.000Z";
86 const updatedAt = "2026-02-28T19:00:01.000Z";
87 const persistedAttachments = [
88 {
89 type: "image" as const,
90 id: "thread-preserve-attachments-att-1",
91 name: "example.png",
92 mimeType: "image/png",
93 sizeBytes: 5,
94 },
95 ];
96​
97 yield* repository.upsert({
98 messageId,
99 threadId,
100 turnId: null,
101 role: "user",
102 text: "initial",
103 attachments: persistedAttachments,
104 isStreaming: false,
105 createdAt,
106 updatedAt,
107 });
108​
109 yield* repository.upsert({
110 messageId,
111 threadId,
112 turnId: null,
113 role: "user",
114 text: "updated",
115 isStreaming: false,
116 createdAt,
117 updatedAt: "2026-02-28T19:00:02.000Z",
118 });
119​
120 const rows = yield* repository.listByThreadId({ threadId });
121 assert.equal(rows.length, 1);
122 assert.equal(rows[0]?.text, "updated");
123 assert.deepEqual(rows[0]?.attachments, persistedAttachments);
124​
125 const rowById = yield* repository.getByMessageId({ messageId });
126 assert.equal(rowById._tag, "Some");
127 if (rowById._tag === "Some") {
128 assert.equal(rowById.value.text, "updated");
129 assert.deepEqual(rowById.value.attachments, persistedAttachments);
130 }
131 }),
132 );
133​
134 it.effect("allows explicit attachment clearing with an empty array", () =>
135 Effect.gen(function* () {
136 const repository = yield* ProjectionThreadMessageRepository;
137 const threadId = ThreadId.make("thread-clear-attachments");
138 const messageId = MessageId.make("message-clear-attachments");
139 const createdAt = "2026-02-28T19:10:00.000Z";
140​
141 yield* repository.upsert({
142 messageId,
143 threadId,
144 turnId: null,
145 role: "assistant",
146 text: "with attachment",
147 attachments: [
148 {
149 type: "image",
150 id: "thread-clear-attachments-att-1",
151 name: "example.png",
152 mimeType: "image/png",
153 sizeBytes: 5,
154 },
155 ],
156 isStreaming: false,
157 createdAt,
158 updatedAt: "2026-02-28T19:10:01.000Z",
159 });
160​
161 yield* repository.upsert({
162 messageId,
163 threadId,
164 turnId: null,
165 role: "assistant",
166 text: "cleared",
167 attachments: [],
168 isStreaming: false,
169 createdAt,
170 updatedAt: "2026-02-28T19:10:02.000Z",
171 });
172​
173 const rows = yield* repository.listByThreadId({ threadId });
174 assert.equal(rows.length, 1);
175 assert.equal(rows[0]?.text, "cleared");
176 assert.deepEqual(rows[0]?.attachments, []);
177 }),
178 );
179});