import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { MemoryDatastoreProvider } from '@bytelyst/datastore';
import { _resetDatastoreProvider, setProvider } from '../../lib/datastore.js';
import * as repo from './repository.js';

describe('agent eval repository', () => {
  beforeEach(() => {
    setProvider(new MemoryDatastoreProvider());
  });

  afterEach(() => {
    _resetDatastoreProvider();
  });

  it('stores suites, cases, runs, and results', async () => {
    await repo.createSuite({
      id: 'evals_1',
      productId: 'lysnrai',
      agentId: 'agt_1',
      name: 'Release Gate',
      status: 'draft',
      passThreshold: 0.85,
      tags: ['release'],
      createdBy: 'admin_1',
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createCase({
      id: 'evals_1:case:1',
      suiteId: 'evals_1',
      productId: 'lysnrai',
      name: 'Handles incident prompt',
      input: { prompt: 'Investigate outage' },
      critical: true,
      tags: ['critical'],
      createdAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createRun({
      id: 'evalrun_1',
      suiteId: 'evals_1',
      agentId: 'agt_1',
      productId: 'lysnrai',
      agentVersionId: 'agt_1:v2',
      agentVersion: 2,
      status: 'queued',
      passThreshold: 0.85,
      releaseGate: true,
      totalCases: 1,
      passedCases: 0,
      failedCases: 0,
      triggeredBy: 'admin_1',
      startedAt: '2026-03-15T00:00:00.000Z',
      reviewRequired: false,
    });

    await repo.createResults([
      {
        id: 'evalrun_1:evals_1:case:1',
        runId: 'evalrun_1',
        caseId: 'evals_1:case:1',
        productId: 'lysnrai',
        passed: true,
        score: 1,
        createdAt: '2026-03-15T00:01:00.000Z',
      },
    ]);

    const suites = await repo.listSuites('lysnrai', { limit: 20 });
    const cases = await repo.listCases('evals_1');
    const run = await repo.getRun('evalrun_1', 'lysnrai');
    const results = await repo.listResults('evalrun_1');

    expect(suites).toHaveLength(1);
    expect(cases[0].critical).toBe(true);
    expect(run.agentVersion).toBe(2);
    expect(results[0].passed).toBe(true);
  });
});
