// Capability Registry Service Tests

import { capabilityRegistryService } from '../registry/capability-registry.service';
import { CapabilityState } from '@/shared/solacenet/types';

describe('CapabilityRegistryService', () => {
  const testCapability = {
    capabilityId: 'test-capability',
    name: 'Test Capability',
    version: '1.0.0',
    description: 'Test description',
    defaultState: CapabilityState.DISABLED,
  };

  beforeEach(async () => {
    // Clean up test data
    try {
      await capabilityRegistryService.deleteCapability(testCapability.capabilityId);
    } catch {
      // Ignore if doesn't exist
    }
  });

  afterEach(async () => {
    // Clean up
    try {
      await capabilityRegistryService.deleteCapability(testCapability.capabilityId);
    } catch {
      // Ignore
    }
  });

  describe('createCapability', () => {
    it('should create a new capability', async () => {
      const capability = await capabilityRegistryService.createCapability(testCapability);

      expect(capability).toBeDefined();
      expect(capability.capabilityId).toBe(testCapability.capabilityId);
      expect(capability.name).toBe(testCapability.name);
      expect(capability.defaultState).toBe(CapabilityState.DISABLED);
    });

    it('should throw error if capability ID already exists', async () => {
      await capabilityRegistryService.createCapability(testCapability);

      await expect(
        capabilityRegistryService.createCapability(testCapability)
      ).rejects.toThrow('already exists');
    });
  });

  describe('getCapabilityById', () => {
    it('should return capability if exists', async () => {
      await capabilityRegistryService.createCapability(testCapability);
      const capability = await capabilityRegistryService.getCapabilityById(testCapability.capabilityId);

      expect(capability).toBeDefined();
      expect(capability?.capabilityId).toBe(testCapability.capabilityId);
    });

    it('should return null if capability does not exist', async () => {
      const capability = await capabilityRegistryService.getCapabilityById('non-existent');
      expect(capability).toBeNull();
    });
  });

  describe('validateDependencies', () => {
    it('should validate dependencies correctly', async () => {
      // Create dependency
      await capabilityRegistryService.createCapability({
        capabilityId: 'dependency-capability',
        name: 'Dependency',
        defaultState: CapabilityState.DISABLED,
      });

      const isValid = await capabilityRegistryService.validateDependencies(
        testCapability.capabilityId,
        ['dependency-capability']
      );

      expect(isValid).toBe(true);
    });
  });
});
