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('agents repository', () => {
  beforeEach(() => {
    setProvider(new MemoryDatastoreProvider());
  });

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

  it('stores agents and versioned prompts', async () => {
    await repo.createAgent({
      id: 'agt_1',
      productId: 'lysnrai',
      key: 'incident-responder',
      name: 'Incident Responder',
      status: 'draft',
      visibility: 'internal',
      currentVersion: 1,
      tags: ['support'],
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createAgentVersion({
      id: 'agt_1:v1',
      agentId: 'agt_1',
      productId: 'lysnrai',
      version: 1,
      status: 'draft',
      prompt: 'Investigate the incident carefully.',
      toolBindings: ['support.runIncidentPipeline'],
      createdBy: 'admin_1',
      createdAt: '2026-03-15T00:00:00.000Z',
    });

    const agents = await repo.listAgents('lysnrai', { limit: 20 });
    const versions = await repo.listAgentVersions('agt_1');

    expect(agents).toHaveLength(1);
    expect(versions[0].prompt).toContain('Investigate');
  });
});
