import { describe, it, expect, beforeEach } from 'vitest';
import Database from 'better-sqlite3';
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;
}
describe('chat tables', () => {
    let db;
    beforeEach(() => {
        db = createTestDb();
    });
    it('should insert and retrieve a conversation', () => {
        const id = '2026-03-13T10:00:00Z-abc123';
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(id, 'content-team', '2026-03-13T10:00:00Z');
        const row = db.prepare('SELECT * FROM conversations WHERE id = ?').get(id);
        expect(row.id).toBe(id);
        expect(row.squad_name).toBe('content-team');
    });
    it('should insert and retrieve chat messages', () => {
        const convId = '2026-03-13T10:00:00Z-conv1';
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(convId, 'content-team', '2026-03-13T10:00:00Z');
        const msgId = '2026-03-13T10:00:01Z-msg1';
        db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, agent_id, agent_name, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)').run(msgId, convId, 'user', 'Hello', null, null, '2026-03-13T10:00:01Z');
        const msg = db.prepare('SELECT * FROM chat_messages WHERE id = ?').get(msgId);
        expect(msg.role).toBe('user');
        expect(msg.content).toBe('Hello');
        expect(msg.conversation_id).toBe(convId);
    });
    it('should insert agent_dm messages with agent metadata', () => {
        const convId = '2026-03-13T10:00:00Z-conv2';
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(convId, 'content-team', '2026-03-13T10:00:00Z');
        const msgId = '2026-03-13T10:00:02Z-dm1';
        db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, agent_id, agent_name, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)').run(msgId, convId, 'agent_dm', 'Found 5 topics', 'researcher', 'Researcher', '2026-03-13T10:00:02Z');
        const msg = db.prepare('SELECT * FROM chat_messages WHERE id = ?').get(msgId);
        expect(msg.role).toBe('agent_dm');
        expect(msg.agent_id).toBe('researcher');
        expect(msg.agent_name).toBe('Researcher');
    });
    it('should list conversations newest-first by id', () => {
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run('2026-03-13T10:00:00Z-aaa', 'content-team', '2026-03-13T10:00:00Z');
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run('2026-03-13T11:00:00Z-bbb', 'content-team', '2026-03-13T11:00:00Z');
        const rows = db.prepare('SELECT * FROM conversations WHERE squad_name = ? ORDER BY id DESC').all('content-team');
        expect(rows[0].id).toBe('2026-03-13T11:00:00Z-bbb');
        expect(rows[1].id).toBe('2026-03-13T10:00:00Z-aaa');
    });
    it('should list messages oldest-first by id with cursor pagination', () => {
        const convId = '2026-03-13T10:00:00Z-conv3';
        db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(convId, 'content-team', '2026-03-13T10:00:00Z');
        for (let i = 1; i <= 5; i++) {
            const ts = `2026-03-13T10:00:0${i}Z`;
            db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)').run(`${ts}-m${i}`, convId, 'user', `msg ${i}`, ts);
        }
        // First page (limit 2)
        const page1 = db.prepare('SELECT * FROM chat_messages WHERE conversation_id = ? ORDER BY id ASC LIMIT ?').all(convId, 2);
        expect(page1).toHaveLength(2);
        expect(page1[0].content).toBe('msg 1');
        // Second page using cursor
        const cursor = page1[1].id;
        const page2 = db.prepare('SELECT * FROM chat_messages WHERE conversation_id = ? AND id > ? ORDER BY id ASC LIMIT ?').all(convId, cursor, 2);
        expect(page2).toHaveLength(2);
        expect(page2[0].content).toBe('msg 3');
    });
});
//# sourceMappingURL=chat-tables.test.js.map