diff --git a/server/src/__tests__/chat-service.test.ts b/server/src/__tests__/chat-service.test.ts index eae6da19..706f651f 100644 --- a/server/src/__tests__/chat-service.test.ts +++ b/server/src/__tests__/chat-service.test.ts @@ -300,7 +300,7 @@ describe("chatService", () => { expect(insertChain.values).toHaveBeenCalledWith( expect.objectContaining({ conversationId: "conv-1", role: "user", content: "Hi" }), ); - expect(result).toEqual(message); + expect(result).toEqual({ ...message, files: [] }); }); it("bumps conversation updatedAt after insert (Pitfall 3)", async () => { diff --git a/server/src/services/chat.ts b/server/src/services/chat.ts index d0e71e38..c4687dbc 100644 --- a/server/src/services/chat.ts +++ b/server/src/services/chat.ts @@ -134,18 +134,22 @@ export function chatService(db: Db) { // Load files for these messages and attach them const messageIds = items.map((m) => m.id); - let filesByMessageId = new Map(); + const filesByMessageId = new Map(); if (messageIds.length > 0) { - const fileRows = await db - .select() - .from(chatFiles) - .where(inArray(chatFiles.messageId, messageIds)) - .orderBy(asc(chatFiles.createdAt)); - for (const f of fileRows) { - if (!f.messageId) continue; - const existing = filesByMessageId.get(f.messageId) ?? []; - existing.push(f); - filesByMessageId.set(f.messageId, existing); + try { + const fileRows = await db + .select() + .from(chatFiles) + .where(inArray(chatFiles.messageId, messageIds)) + .orderBy(asc(chatFiles.createdAt)); + for (const f of fileRows) { + if (!f.messageId) continue; + const existing = filesByMessageId.get(f.messageId) ?? []; + existing.push(f); + filesByMessageId.set(f.messageId, existing); + } + } catch { + // chat_files table may not exist yet — treat as no files } }