/**
 * Tests for BackendTestGenerator
 */

import { BackendTestGenerator } from './backend-test-generator';
import { WorkflowEngine } from '../../engine/workflow-engine';

describe('BackendTestGenerator', () => {
  let generator: BackendTestGenerator;

  beforeEach(() => {
    generator = new BackendTestGenerator();
  });

  it('should have correct metadata', () => {
    expect(generator.id).toBe('backend-integration-tests');
    expect(generator.name).toBe('Backend Integration Test Generator');
    expect(generator.testFramework).toBe('jest');
  });

  it('should generate empty result for workflow without tests', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
        },
      },
    };

    const engine = new WorkflowEngine();
    const executionPlan = engine.analyze(workflow as any);

    const result = generator.generate({
      workflow: workflow as any,
      executionPlan,
      outputDirectory: '/test',
    });

    expect(result.files).toHaveLength(0);
  });

  it('should generate API endpoint tests', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
          endpoints: [
            {
              id: 'login',
              path: '/api/auth/login',
              method: 'POST',
              name: 'Login',
              description: 'Login endpoint',
              responses: [
                {
                  status: 200,
                  description: 'Success',
                  body: {
                    type: 'object',
                    properties: {
                      token: { type: 'string' },
                    },
                  },
                },
              ],
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'api.login',
            scenarios: [
              'returns 200 with valid credentials',
              'returns 401 with invalid password',
            ],
          },
        ],
      },
    };

    const engine = new WorkflowEngine();
    const executionPlan = engine.analyze(workflow as any);

    const result = generator.generate({
      workflow: workflow as any,
      executionPlan,
      outputDirectory: '/test',
    });

    expect(result.files.length).toBeGreaterThan(0);

    const testFile = result.files.find(f => f.path.includes('login.test.ts'));
    expect(testFile).toBeDefined();
    expect(testFile?.content).toContain('supertest');
    expect(testFile?.content).toContain('POST /api/auth/login');
    expect(testFile?.content).toContain('returns 200 with valid credentials');
  });

  it('should generate integration tests', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
        },
      },
      testing: {
        integration_tests: [
          {
            name: 'complete-login-flow',
            description: 'Test complete login flow',
            steps: [
              'Create test user in database',
              'Submit login request',
            ],
          },
        ],
      },
    };

    const engine = new WorkflowEngine();
    const executionPlan = engine.analyze(workflow as any);

    const result = generator.generate({
      workflow: workflow as any,
      executionPlan,
      outputDirectory: '/test',
    });

    const integrationTest = result.files.find(f => f.path.includes('integration/complete-login-flow.test.ts'));
    expect(integrationTest).toBeDefined();
    expect(integrationTest?.content).toContain('complete-login-flow');
    expect(integrationTest?.content).toContain('Create test user in database');
  });

  it('should generate jest config', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
          endpoints: [
            {
              id: 'test',
              path: '/test',
              method: 'GET',
              name: 'Test',
              description: 'Test',
              responses: [],
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'api.test',
            scenarios: ['test'],
          },
        ],
      },
    };

    const engine = new WorkflowEngine();
    const executionPlan = engine.analyze(workflow as any);

    const result = generator.generate({
      workflow: workflow as any,
      executionPlan,
      outputDirectory: '/test',
    });

    const jestConfig = result.files.find(f => f.path.includes('jest.config.js'));
    expect(jestConfig).toBeDefined();
    expect(jestConfig?.content).toContain('ts-jest');
    expect(jestConfig?.content).toContain('testEnvironment');
  });

  it('should not overwrite jest config', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
          endpoints: [
            {
              id: 'test',
              path: '/test',
              method: 'GET',
              name: 'Test',
              description: 'Test',
              responses: [],
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'api.test',
            scenarios: ['test'],
          },
        ],
      },
    };

    const engine = new WorkflowEngine();
    const executionPlan = engine.analyze(workflow as any);

    const result = generator.generate({
      workflow: workflow as any,
      executionPlan,
      outputDirectory: '/test',
    });

    const jestConfig = result.files.find(f => f.path.includes('jest.config.js'));
    expect(jestConfig?.overwrite).toBe(false);
  });
});
