Open History to see the last commit
1import { MessageId, ThreadId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { Effect, Layer } from "effect";
4
5import { ProjectionThreadMessageRepository } from "../Services/ProjectionThreadMessages.ts";
6import { ProjectionThreadMessageRepositoryLive } from "./ProjectionThreadMessages.ts";
7import { SqlitePersistenceMemory } from "./Sqlite.ts";
8
9const layer = it.layer(
10 ProjectionThreadMessageRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
11);
12
13layer("ProjectionThreadMessageRepository", (it) => {
14 it.effect("preserves existing attachments when upsert omits attachments", () =>
15 Effect.gen(function* () {
16 const repository = yield* ProjectionThreadMessageRepository;
17 const threadId = ThreadId.makeUnsafe("thread-preserve-attachments");
18 const messageId = MessageId.makeUnsafe("message-preserve-attachments");
19 const createdAt = "2026-02-28T19:00:00.000Z";
20 const updatedAt = "2026-02-28T19:00:01.000Z";
21 const persistedAttachments = [
22 {
23 type: "image" as const,
24 id: "thread-preserve-attachments-att-1",
25 name: "example.png",
26 mimeType: "image/png",
27 sizeBytes: 5,
28 },
29 ];
30
31 yield* repository.upsert({
32 messageId,
33 threadId,
34 turnId: null,
35 role: "user",
36 text: "initial",
37 attachments: persistedAttachments,
38 isStreaming: false,
39 createdAt,
40 updatedAt,
41 });
42
43 yield* repository.upsert({
44 messageId,
45 threadId,
46 turnId: null,
47 role: "user",
48 text: "updated",
49 isStreaming: false,
50 createdAt,
51 updatedAt: "2026-02-28T19:00:02.000Z",
52 });
53
54 const rows = yield* repository.listByThreadId({ threadId });
55 assert.equal(rows.length, 1);
56 assert.equal(rows[0]?.text, "updated");
57 assert.deepEqual(rows[0]?.attachments, persistedAttachments);
58
59 const rowById = yield* repository.getByMessageId({ messageId });
60 assert.equal(rowById._tag, "Some");
61 if (rowById._tag === "Some") {
62 assert.equal(rowById.value.text, "updated");
63 assert.deepEqual(rowById.value.attachments, persistedAttachments);
64 }
65 }),
66 );
67
68 it.effect("allows explicit attachment clearing with an empty array", () =>
69 Effect.gen(function* () {
70 const repository = yield* ProjectionThreadMessageRepository;
71 const threadId = ThreadId.makeUnsafe("thread-clear-attachments");
72 const messageId = MessageId.makeUnsafe("message-clear-attachments");
73 const createdAt = "2026-02-28T19:10:00.000Z";
74
75 yield* repository.upsert({
76 messageId,
77 threadId,
78 turnId: null,
79 role: "assistant",
80 text: "with attachment",
81 attachments: [
82 {
83 type: "image",
84 id: "thread-clear-attachments-att-1",
85 name: "example.png",
86 mimeType: "image/png",
87 sizeBytes: 5,
88 },
89 ],
90 isStreaming: false,
91 createdAt,
92 updatedAt: "2026-02-28T19:10:01.000Z",
93 });
94
95 yield* repository.upsert({
96 messageId,
97 threadId,
98 turnId: null,
99 role: "assistant",
100 text: "cleared",
101 attachments: [],
102 isStreaming: false,
103 createdAt,
104 updatedAt: "2026-02-28T19:10:02.000Z",
105 });
106
107 const rows = yield* repository.listByThreadId({ threadId });
108 assert.equal(rows.length, 1);
109 assert.equal(rows[0]?.text, "cleared");
110 assert.deepEqual(rows[0]?.attachments, []);
111 }),
112 );
113});