import { describe, expect, it, vi } from 'vitest'; import type { Cursor } from '../common/http/cursor.js'; import type { Message } from './message.js'; import { MessagesRepository } from './messages.repository.js'; const message = (id: string, created_at: string): Message => ({ id, chat_id: 'chat', sender_id: 'sender', text: id, created_at, }); describe('MessagesRepository', () => { it('uses named SQL history and a keyset cursor', async () => { const query = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }); const repository = new MessagesRepository({ query, withTransaction: vi.fn(), } as never); const cursor: Cursor = { scope: 'messages:chat', created_at: '2026-01-01T00:00:01.000Z', id: '1', }; await repository.findPage('chat', 10, cursor); expect(query).toHaveBeenCalledWith( 'messages.history', expect.stringContaining('(created_at, id) >'), expect.arrayContaining(['chat', cursor.created_at, cursor.id, 11]), ); }); it('returns rows in response shape', async () => { const query = vi .fn() .mockResolvedValue({ rows: [message('1', '2026-01-01T00:00:00.000Z')], rowCount: 1, }); const repository = new MessagesRepository({ query } as never); await expect(repository.findPage('chat', 1)).resolves.toEqual({ items: [message('1', '2026-01-01T00:00:00.000Z')], hasMore: false, }); }); });