import { describe, it, expect, beforeEach } from 'vitest';
import Database from 'better-sqlite3';
import { createConversation, insertMessage, getConversations, getMessages, deleteConversation, } from '../../bridge/conversation.js';
function createTestDb() {
    const db = new Database(':memory:');
    db.pragma('journal_mode = WAL');
    db.exec(`
    CREATE TABLE IF NOT EXISTS conversations (
      id TEXT PRIMARY KEY,
      squad_name TEXT NOT NULL,
      created_at TEXT NOT NULL
    );
    CREATE TABLE IF NOT EXISTS chat_messages (
      id TEXT PRIMARY KEY,
      conversation_id TEXT NOT NULL REFERENCES conversations(id),
      role TEXT NOT NULL,
      content TEXT NOT NULL,
      agent_id TEXT,
      agent_name TEXT,
      created_at TEXT NOT NULL
    );
  `);
    return db;
}
// Simulates the handler logic
function handleGetConversations(db, squadName, cursor, limit = 20) {
    return getConversations(db, squadName, { limit, cursor });
}
function handleGetMessages(db, conversationId, cursor, limit = 50) {
    return getMessages(db, conversationId, { limit, cursor });
}
describe('conversation REST handlers', () => {
    let db;
    beforeEach(() => {
        db = createTestDb();
    });
    describe('GET /api/squads/:squadName/conversations', () => {
        it('should return empty list when no conversations', () => {
            const result = handleGetConversations(db, 'content-team');
            expect(result.data).toEqual([]);
            expect(result.nextCursor).toBeNull();
        });
        it('should return conversations for the specified squad', () => {
            createConversation(db, 'content-team');
            createConversation(db, 'other-squad');
            const result = handleGetConversations(db, 'content-team');
            expect(result.data).toHaveLength(1);
            expect(result.data[0].squad_name).toBe('content-team');
        });
        it('should paginate conversations', () => {
            for (let i = 0; i < 5; i++) {
                const ts = `2026-03-13T10:0${i}:00.000Z`;
                db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(`${ts}-c${i}`, 'content-team', ts);
            }
            const page1 = handleGetConversations(db, 'content-team', undefined, 2);
            expect(page1.data).toHaveLength(2);
            expect(page1.nextCursor).not.toBeNull();
            const page2 = handleGetConversations(db, 'content-team', page1.nextCursor, 2);
            expect(page2.data).toHaveLength(2);
        });
    });
    describe('GET /api/conversations/:id/messages', () => {
        it('should return empty list for conversation with no messages', () => {
            const conv = createConversation(db, 'content-team');
            const result = handleGetMessages(db, conv.id);
            expect(result.data).toEqual([]);
            expect(result.nextCursor).toBeNull();
        });
        it('should return messages oldest-first', () => {
            const conv = createConversation(db, 'content-team');
            // Use explicit IDs to guarantee ordering (same-millisecond generateId can randomize order)
            db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)').run('2026-03-13T10:00:01.000Z-aaa', conv.id, 'user', 'First', '2026-03-13T10:00:01.000Z');
            db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)').run('2026-03-13T10:00:02.000Z-bbb', conv.id, 'assistant', 'Second', '2026-03-13T10:00:02.000Z');
            const result = handleGetMessages(db, conv.id);
            expect(result.data).toHaveLength(2);
            expect(result.data[0].content).toBe('First');
            expect(result.data[1].content).toBe('Second');
        });
        it('should paginate messages', () => {
            const conv = createConversation(db, 'content-team');
            for (let i = 0; i < 5; i++) {
                const ts = `2026-03-13T10:00:0${i}.000Z`;
                db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)').run(`${ts}-m${i}`, conv.id, 'user', `msg-${i}`, ts);
            }
            const page1 = handleGetMessages(db, conv.id, undefined, 2);
            expect(page1.data).toHaveLength(2);
            expect(page1.nextCursor).not.toBeNull();
            const page2 = handleGetMessages(db, conv.id, page1.nextCursor, 2);
            expect(page2.data).toHaveLength(2);
            expect(page2.data[0].content).toBe('msg-2');
        });
    });
    describe('DELETE /api/conversations/:id', () => {
        it('should delete a conversation and its messages', () => {
            const conv = createConversation(db, 'content-team');
            insertMessage(db, { conversationId: conv.id, role: 'user', content: 'Hello' });
            insertMessage(db, { conversationId: conv.id, role: 'assistant', content: 'Hi!' });
            const deleted = deleteConversation(db, conv.id);
            expect(deleted).toBe(true);
            const convResult = getConversations(db, 'content-team', { limit: 10 });
            expect(convResult.data).toHaveLength(0);
            const msgResult = getMessages(db, conv.id, { limit: 10 });
            expect(msgResult.data).toHaveLength(0);
        });
        it('should return false for non-existent conversation', () => {
            const deleted = deleteConversation(db, 'non-existent-id');
            expect(deleted).toBe(false);
        });
    });
});
//# sourceMappingURL=conversations.test.js.map