import { MessageId, ThreadId } from "@t3tools/contracts"; import { assert, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import { ProjectionThreadMessageRepository } from "../Services/ProjectionThreadMessages.ts"; import { ProjectionThreadMessageRepositoryLive } from "./ProjectionThreadMessages.ts"; import { SqlitePersistenceMemory } from "./Sqlite.ts"; const layer = it.layer( ProjectionThreadMessageRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)), ); layer("ProjectionThreadMessageRepository", (it) => { it.effect("preserves existing attachments when upsert omits attachments", () => Effect.gen(function* () { const repository = yield* ProjectionThreadMessageRepository; const threadId = ThreadId.make("thread-preserve-attachments"); const messageId = MessageId.make("message-preserve-attachments"); const createdAt = "2026-02-28T19:00:00.000Z"; const updatedAt = "2026-02-28T19:00:01.000Z"; const persistedAttachments = [ { type: "image" as const, id: "thread-preserve-attachments-att-1", name: "example.png", mimeType: "image/png", sizeBytes: 5, }, ]; yield* repository.upsert({ messageId, threadId, turnId: null, role: "user", text: "initial", attachments: persistedAttachments, isStreaming: false, createdAt, updatedAt, }); yield* repository.upsert({ messageId, threadId, turnId: null, role: "user", text: "updated", isStreaming: false, createdAt, updatedAt: "2026-02-28T19:00:02.000Z", }); const rows = yield* repository.listByThreadId({ threadId }); assert.equal(rows.length, 1); assert.equal(rows[0]?.text, "updated"); assert.deepEqual(rows[0]?.attachments, persistedAttachments); const rowById = yield* repository.getByMessageId({ messageId }); assert.equal(rowById._tag, "Some"); if (rowById._tag === "Some") { assert.equal(rowById.value.text, "updated"); assert.deepEqual(rowById.value.attachments, persistedAttachments); } }), ); it.effect("round-trips notification provenance and preserves it across later upserts", () => Effect.gen(function* () { const repository = yield* ProjectionThreadMessageRepository; const threadId = ThreadId.make("thread-notification"); const messageId = MessageId.make("message-notification"); const createdAt = "2026-07-01T09:00:00.000Z"; const notification = { kind: "cron" as const, label: "Cron: daily-digest", deliveryId: "delivery-1", }; yield* repository.upsert({ messageId, threadId, turnId: null, role: "assistant", text: "Digest ready.", notification, isStreaming: false, createdAt, updatedAt: createdAt, }); const rows = yield* repository.listByThreadId({ threadId }); assert.equal(rows.length, 1); assert.deepEqual(rows[0]?.notification, notification); // Same COALESCE rule as attachments: an unrelated update that omits the // field must not erase a delivery's provenance. yield* repository.upsert({ messageId, threadId, turnId: null, role: "assistant", text: "Digest ready (edited).", isStreaming: false, createdAt, updatedAt: "2026-07-01T09:00:02.000Z", }); const rowById = yield* repository.getByMessageId({ messageId }); assert.equal(rowById._tag, "Some"); if (rowById._tag === "Some") { assert.equal(rowById.value.text, "Digest ready (edited)."); assert.deepEqual(rowById.value.notification, notification); } }), ); it.effect("leaves notification absent on ordinary messages", () => Effect.gen(function* () { const repository = yield* ProjectionThreadMessageRepository; const threadId = ThreadId.make("thread-ordinary-message"); const messageId = MessageId.make("message-ordinary"); yield* repository.upsert({ messageId, threadId, turnId: null, role: "user", text: "hello", isStreaming: false, createdAt: "2026-07-01T09:10:00.000Z", updatedAt: "2026-07-01T09:10:00.000Z", }); const rows = yield* repository.listByThreadId({ threadId }); // Absent, not a synthesized default — consumers key on its absence to // tell an ordinary message from a proactive delivery. assert.equal(rows[0]?.notification, undefined); }), ); it.effect("allows explicit attachment clearing with an empty array", () => Effect.gen(function* () { const repository = yield* ProjectionThreadMessageRepository; const threadId = ThreadId.make("thread-clear-attachments"); const messageId = MessageId.make("message-clear-attachments"); const createdAt = "2026-02-28T19:10:00.000Z"; yield* repository.upsert({ messageId, threadId, turnId: null, role: "assistant", text: "with attachment", attachments: [ { type: "image", id: "thread-clear-attachments-att-1", name: "example.png", mimeType: "image/png", sizeBytes: 5, }, ], isStreaming: false, createdAt, updatedAt: "2026-02-28T19:10:01.000Z", }); yield* repository.upsert({ messageId, threadId, turnId: null, role: "assistant", text: "cleared", attachments: [], isStreaming: false, createdAt, updatedAt: "2026-02-28T19:10:02.000Z", }); const rows = yield* repository.listByThreadId({ threadId }); assert.equal(rows.length, 1); assert.equal(rows[0]?.text, "cleared"); assert.deepEqual(rows[0]?.attachments, []); }), ); });