import { describe, it, expect } from 'vitest';
import { createTestDatabase } from './test-helpers.js';
import { agentMetrics } from '../../schema.js';

describe('agentMetrics table', () => {
  it('select from empty agentMetrics returns []', async () => {
    const db = createTestDatabase();
    const rows = await db.select().from(agentMetrics);
    expect(rows).toEqual([]);
  });

  it('insert and select a metrics row round-trips correctly', async () => {
    const db = createTestDatabase();
    await db.insert(agentMetrics).values({
      agentId: 'agent-abc',
      questionsCount: 3,
      subagentsCount: 1,
      compactionsCount: 0,
      updatedAt: new Date('2024-01-01T00:00:00Z'),
    });
    const rows = await db.select().from(agentMetrics);
    expect(rows).toHaveLength(1);
    expect(rows[0].agentId).toBe('agent-abc');
    expect(rows[0].questionsCount).toBe(3);
    expect(rows[0].subagentsCount).toBe(1);
    expect(rows[0].compactionsCount).toBe(0);
  });

  it('agentId is primary key — duplicate insert throws', async () => {
    const db = createTestDatabase();
    await db.insert(agentMetrics).values({
      agentId: 'agent-dup',
      questionsCount: 0,
      subagentsCount: 0,
      compactionsCount: 0,
      updatedAt: new Date(),
    });
    await expect(
      db.insert(agentMetrics).values({
        agentId: 'agent-dup',
        questionsCount: 1,
        subagentsCount: 0,
        compactionsCount: 0,
        updatedAt: new Date(),
      })
    ).rejects.toThrow();
  });
});
