import { describe, it, expect, beforeEach, vi } from '@jest/globals';
import { CoreEngineService } from '../../core/engine/CoreEngineService';
import { RuleEngineService } from '../../core/engine/RuleEngineService';
import { WorkflowEngineService } from '../../core/engine/WorkflowEngineService';
import { RedisService } from '../../core/cache/RedisService';

describe('CoreEngineService Integration Tests', () => {
  let coreEngineService: CoreEngineService;
  let ruleEngineService: RuleEngineService;
  let workflowEngineService: WorkflowEngineService;
  let redisService: RedisService;

  beforeEach(() => {
    // 创建真实的服务实例，而不是模拟对象
    ruleEngineService = new RuleEngineService();
    workflowEngineService = new WorkflowEngineService();
    redisService = new RedisService();

    // 初始化CoreEngineService
    coreEngineService = new CoreEngineService(
      ruleEngineService,
      workflowEngineService,
      redisService
    );
  });

  describe('完整业务流程测试', () => {
    it('应该能够处理完整的业务请求流程', async () => {
      // 1. 注册业务规则
      const rule = {
        id: 'test-rule',
        condition: 'true',
        action: 'set workflowId to workflow-1'
      };
      const ruleId = await coreEngineService.registerBusinessRule(rule);
      expect(typeof ruleId).toBe('string');

      // 2. 注册工作流
      const workflow = {
        id: 'workflow-1',
        steps: [
          {
            id: 'step-1',
            action: 'process data'
          }
        ]
      };
      const workflowId = await coreEngineService.registerWorkflow(workflow);
      expect(typeof workflowId).toBe('string');

      // 3. 处理业务请求
      const request = {
        id: 'test-request',
        type: 'business',
        data: { key: 'value' }
      };
      const context = {
        tenantId: 'test-tenant',
        userId: 'test-user'
      };

      const result = await coreEngineService.processBusinessRequest(request, context);
      expect(result).toHaveProperty('success');

      // 4. 验证缓存是否工作
      const cachedResult = await redisService.get('core-engine:{"id":"test-request","type":"business","data":{"key":"value"}}');
      expect(cachedResult).toEqual(result);

      // 5. 检查系统状态
      const status = await coreEngineService.getSystemStatus();
      expect(status.status).toBe('healthy');
      expect(status.services.ruleEngine).toHaveProperty('status');
      expect(status.services.workflowEngine).toHaveProperty('status');
      expect(status.services.redis).toHaveProperty('status');
    });

    it('应该能够处理并发请求', async () => {
      const concurrentRequests = 10;
      const promises = [];

      for (let i = 0; i < concurrentRequests; i++) {
        const request = {
          id: `test-request-${i}`,
          type: 'business',
          data: { key: `value-${i}` }
        };
        const context = {
          tenantId: 'test-tenant',
          userId: `test-user-${i}`
        };

        const promise = coreEngineService.processBusinessRequest(request, context);
        promises.push(promise);
      }

      const results = await Promise.all(promises);
      expect(results.length).toBe(concurrentRequests);
      results.forEach(result => {
        expect(result).toHaveProperty('success');
      });
    });
  });
});
