/**
 * DID Resolver Tests
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DIDResolver } from './did';
import fetch from 'node-fetch';

vi.mock('node-fetch');

describe('DIDResolver', () => {
  let resolver: DIDResolver;

  beforeEach(() => {
    resolver = new DIDResolver();
    vi.clearAllMocks();
  });

  describe('resolve', () => {
    it('should resolve did:web DID', async () => {
      const did = 'did:web:example.com';
      const mockDocument = {
        id: did,
        '@context': ['https://www.w3.org/ns/did/v1'],
        verificationMethod: [
          {
            id: `${did}#keys-1`,
            type: 'Ed25519VerificationKey2020',
            controller: did,
            publicKeyMultibase: 'z6Mk...',
          },
        ],
        authentication: [`${did}#keys-1`],
      };

      (fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockDocument,
      });

      const result = await resolver.resolve(did);

      expect(result.id).toBe(did);
      expect(result.verificationMethod).toHaveLength(1);
      expect(fetch).toHaveBeenCalledWith(
        'https://example.com/.well-known/did.json'
      );
    });

    it('should resolve did:key DID', async () => {
      const did = 'did:key:z6Mk...';
      const publicKeyMultibase = 'z6Mk...';

      const result = await resolver.resolve(did);

      expect(result.id).toBe(did);
      expect(result.verificationMethod).toHaveLength(1);
      expect(result.verificationMethod[0]?.publicKeyMultibase).toBe(
        publicKeyMultibase
      );
    });

    it('should throw error for invalid DID format', async () => {
      const did = 'invalid-did';

      await expect(resolver.resolve(did)).rejects.toThrow(
        'Invalid DID format'
      );
    });

    it('should throw error for unsupported DID method', async () => {
      const did = 'did:unsupported:123';

      await expect(resolver.resolve(did)).rejects.toThrow(
        'Unsupported DID method'
      );
    });
  });

  describe('verifySignature', () => {
    it('should verify signature with multibase public key', async () => {
      const did = 'did:web:example.com';
      const message = 'test message';
      const signature = 'signature';

      const mockDocument = {
        id: did,
        '@context': ['https://www.w3.org/ns/did/v1'],
        verificationMethod: [
          {
            id: `${did}#keys-1`,
            type: 'Ed25519VerificationKey2020',
            controller: did,
            publicKeyMultibase: 'z6Mk...',
          },
        ],
        authentication: [`${did}#keys-1`],
      };

      (fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockDocument,
      });

      // Mock the verifyEd25519 method (would need to be exposed or mocked differently)
      // This is a simplified test - actual implementation would need proper crypto mocking
      const result = await resolver.verifySignature(did, message, signature);

      // The actual result depends on the implementation
      expect(typeof result).toBe('boolean');
    });
  });
});

