import { describe, expect, test } from '@jest/globals';
import { NodeType } from '@hestia-earth/schema';

import {
  loadConfig,
  loadRunConfig,
  loadTriggerConfig,
  getMaxStage
} from './config';

describe('config', () => {
  describe('loadConfig', () => {
    test('load existing config', () => {
      const config = loadConfig(NodeType.Cycle);
      expect(config.models?.length > 0).toBeTruthy();
    });

    test('load non-existing config', () => {
      const t = () => loadConfig('unknown' as any);

      expect(t).toThrowError(
        'Invalid type unknown. Allowed types: Cycle, Site, ImpactAssessment'
      );
    });
  });

  describe('loadRunConfig', () => {
    test('load existing config', () => {
      const config = loadRunConfig(NodeType.ImpactAssessment, 1);
      expect(config.length > 0).toBeTruthy();
    });

    test('load non-existing config', () => {
      const t = () => loadRunConfig('unknown' as any, 1);

      expect(t).toThrowError(
        'Invalid type unknown. Allowed types: Cycle, Site, ImpactAssessment'
      );
    });

    test('load non-existing stage', () => {
      const t = () => loadRunConfig(NodeType.ImpactAssessment, 2);

      expect(t).toThrowError(
        'Invalid stage configuration for ImpactAssessment: 2'
      );
    });
  });

  describe('loadTriggerConfig', () => {
    test('load existing config', () => {
      const config = loadTriggerConfig(NodeType.ImpactAssessment, 1);
      expect(config.length > 0).toBeTruthy();
    });

    test('load non-existing config', () => {
      const t = () => loadTriggerConfig('unknown' as any, 1);

      expect(t).toThrowError(
        'Invalid type unknown. Allowed types: Cycle, Site, ImpactAssessment'
      );
    });

    test('load non-existing stage', () => {
      const t = () => loadTriggerConfig(NodeType.ImpactAssessment, 2);

      expect(t).toThrowError(
        'Invalid stage configuration for ImpactAssessment: 2'
      );
    });
  });

  describe('getMaxStage', () => {
    test('existing config', () => {
      expect(getMaxStage(NodeType.Cycle)).toEqual(2);
      expect(getMaxStage(NodeType.Site)).toEqual(2);
      expect(getMaxStage(NodeType.ImpactAssessment)).toEqual(1);
    });

    test('non-existing config', () => {
      const t = () => getMaxStage('unknown' as any);

      expect(t).toThrowError(
        'Invalid type unknown. Allowed types: Cycle, Site, ImpactAssessment'
      );
    });
  });
});
