import { describe, it, expect, beforeEach } from 'vitest';
import { Repository } from './repository.js';

describe('Repository a2a delegations + space skills', () => {
  let repo: Repository;
  beforeEach(() => { repo = new Repository(':memory:'); });

  it('creates and looks up a delegation by grant_id', () => {
    repo.createA2aDelegation({
      id: 'd1', userId: 'u1', clientId: 'cli1', grantId: 'g1',
      grantedSpaceIds: ['s1', 's2'], grantedSkills: ['research'],
      audience: 'https://m/a2a', expiresAt: null, revokedAt: null,
    });
    const d = repo.getA2aDelegationByGrantId('g1');
    expect(d?.grantedSpaceIds).toEqual(['s1', 's2']);
    expect(d?.grantedSkills).toEqual(['research']);
    expect(d?.revokedAt).toBeNull();
  });

  it('revokes a delegation', () => {
    repo.createA2aDelegation({ id: 'd2', userId: 'u1', clientId: 'c', grantId: 'g2', grantedSpaceIds: [], grantedSkills: [], audience: null, expiresAt: null, revokedAt: null });
    repo.revokeA2aDelegation('d2', '2026-06-27T00:00:00Z');
    expect(repo.getA2aDelegationByGrantId('g2')?.revokedAt).toBe('2026-06-27T00:00:00Z');
  });

  it('space a2a_skills defaults to empty and round-trips', () => {
    // seed a space row directly (spaces table exists via migrations)
    (repo as any).db.prepare("INSERT INTO spaces (id, kind, title) VALUES ('s1', 'case', 'Test Space')").run();
    expect(repo.getSpaceA2aSkills('s1')).toEqual([]);
    repo.setSpaceA2aSkills('s1', ['research', 'summarize']);
    expect(repo.getSpaceA2aSkills('s1')).toEqual(['research', 'summarize']);
  });
});
