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

ProjectionThreadMessages.test.ts

106 lines3.2 KB

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 );
60​
61 it.effect("allows explicit attachment clearing with an empty array", () =>
62 Effect.gen(function* () {
63 const repository = yield* ProjectionThreadMessageRepository;
64 const threadId = ThreadId.makeUnsafe("thread-clear-attachments");
65 const messageId = MessageId.makeUnsafe("message-clear-attachments");
66 const createdAt = "2026-02-28T19:10:00.000Z";
67​
68 yield* repository.upsert({
69 messageId,
70 threadId,
71 turnId: null,
72 role: "assistant",
73 text: "with attachment",
74 attachments: [
75 {
76 type: "image",
77 id: "thread-clear-attachments-att-1",
78 name: "example.png",
79 mimeType: "image/png",
80 sizeBytes: 5,
81 },
82 ],
83 isStreaming: false,
84 createdAt,
85 updatedAt: "2026-02-28T19:10:01.000Z",
86 });
87​
88 yield* repository.upsert({
89 messageId,
90 threadId,
91 turnId: null,
92 role: "assistant",
93 text: "cleared",
94 attachments: [],
95 isStreaming: false,
96 createdAt,
97 updatedAt: "2026-02-28T19:10:02.000Z",
98 });
99​
100 const rows = yield* repository.listByThreadId({ threadId });
101 assert.equal(rows.length, 1);
102 assert.equal(rows[0]?.text, "cleared");
103 assert.deepEqual(rows[0]?.attachments, []);
104 }),
105 );
106});