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