/**
 * Scheduled Credential Issuance Tests
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { initializeScheduledIssuance } from './scheduled-issuance';
import { KMSClient } from '@the-order/crypto';

vi.mock('@the-order/jobs');
vi.mock('@the-order/database');
vi.mock('@the-order/events');
vi.mock('@the-order/shared');
vi.mock('@the-order/crypto');

describe('Scheduled Credential Issuance', () => {
  let kmsClient: KMSClient;

  beforeEach(() => {
    kmsClient = new KMSClient({
      provider: 'aws',
      keyId: 'test-key-id',
      region: 'us-east-1',
    });
    vi.clearAllMocks();
  });

  describe('initializeScheduledIssuance', () => {
    it('should initialize scheduled issuance with expiration detection', async () => {
      const config = {
        kmsClient,
        enableExpirationDetection: true,
        enableBatchRenewal: true,
        enableScheduledIssuance: true,
      };

      await expect(initializeScheduledIssuance(config)).resolves.not.toThrow();
    });

    it('should throw error if issuer DID not configured', async () => {
      const config = {
        kmsClient,
      };

      // Mock getEnv to return no issuer DID
      const { getEnv } = await import('@the-order/shared');
      vi.mocked(getEnv).mockReturnValue({
        VC_ISSUER_DID: undefined,
        VC_ISSUER_DOMAIN: undefined,
      } as any);

      await expect(initializeScheduledIssuance(config)).rejects.toThrow(
        'VC_ISSUER_DID or VC_ISSUER_DOMAIN must be configured'
      );
    });
  });
});

