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