import { describe, it, expect } from 'vitest';
import { buildBaseCard, buildExtendedCard } from './agent-card.js';

const base = { baseUrl: 'https://m', issuer: 'https://m/oidc', version: '1.0.0' };

describe('agent card', () => {
  it('base card has no sensitive skills and advertises oauth2 + extended support', () => {
    const c = buildBaseCard(base);
    expect(c.name).toBeTruthy();
    expect(c.protocolVersion).toBeTruthy();
    expect(c.skills).toEqual([]);
    expect(c.supportsAuthenticatedExtendedCard).toBe(true);
    expect(c.securitySchemes && Object.values(c.securitySchemes)[0]).toMatchObject({ type: 'oauth2' });
    expect(c.capabilities).toMatchObject({ streaming: true });
  });

  it('extended card lists only effective (granted ∩ visible ∩ allowlist) skills', () => {
    const principal = {
      actingUserId: 'u1', clientId: 'c', grantId: 'g',
      delegation: { userId: 'u1', clientId: 'c', grantId: 'g', grantedSpaceIds: ['s1', 's2'], grantedSkills: ['research', 'admin'], expiresAt: null, revokedAt: null },
    };
    const c = buildExtendedCard(principal, {
      ...base,
      userVisibleSpaceIds: ['s1'],                       // s2 not visible → dropped
      allowlistsBySpace: { s1: ['research'], s2: ['admin'] },
    });
    const ids = c.skills.map(s => s.id);
    expect(ids).toEqual(['research']);                    // admin dropped (s2 not visible)
  });
});
