import { describe, it, expect } from 'vitest';
import { mkdtempSync, existsSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { loadOrCreateJwks } from './oidc-keys.js';

describe('loadOrCreateJwks', () => {
  it('creates a persistent RSA JWKS and reloads the same key', () => {
    const dir = mkdtempSync(join(tmpdir(), 'a2a-jwks-'));
    const first = loadOrCreateJwks(dir);
    expect(first.keys).toHaveLength(1);
    expect(first.keys[0]).toMatchObject({ kty: 'RSA' });
    expect(first.keys[0]).toHaveProperty('d');
    expect(existsSync(join(dir, 'a2a-jwks.json'))).toBe(true);
    const second = loadOrCreateJwks(dir);
    expect((second.keys[0] as any).kid).toBe((first.keys[0] as any).kid);
    rmSync(dir, { recursive: true, force: true });
  });
});
