/**
 * Tests for FrontendTestGenerator
 */

import { FrontendTestGenerator } from './frontend-test-generator';
import { WorkflowEngine } from '../../engine/workflow-engine';

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

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

  it('should have correct metadata', () => {
    expect(generator.id).toBe('frontend-unit-tests');
    expect(generator.name).toBe('Frontend Unit Test Generator');
    expect(generator.testFramework).toBe('vitest');
  });

  it('should generate empty result for workflow without unit tests', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        'web-app': {
          type: 'frontend',
          technology: 'react',
        },
      },
    };

    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 React component tests', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        'web-app': {
          type: 'frontend',
          technology: 'react',
          components: [
            {
              id: 'login-form',
              name: 'LoginForm',
              type: 'form-component',
              description: 'Login form',
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'web-app.login-form',
            scenarios: [
              'displays validation errors for empty fields',
              'shows loading state during submission',
            ],
          },
        ],
      },
    };

    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('LoginForm.test.tsx'));
    expect(testFile).toBeDefined();
    expect(testFile?.content).toContain('describe(\'LoginForm\'');
    expect(testFile?.content).toContain('displays validation errors for empty fields');
    expect(testFile?.content).toContain('shows loading state during submission');
  });

  it('should generate test setup file', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        'web-app': {
          type: 'frontend',
          technology: 'react',
          components: [
            {
              id: 'test-component',
              name: 'TestComponent',
              type: 'form-component',
              description: 'Test',
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'web-app.test-component',
            scenarios: ['test scenario'],
          },
        ],
      },
    };

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

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

    const setupFile = result.files.find(f => f.path.includes('test/setup.ts'));
    expect(setupFile).toBeDefined();
    expect(setupFile?.content).toContain('@testing-library/react');
    expect(setupFile?.content).toContain('cleanup');
  });

  it('should generate vitest config', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        'web-app': {
          type: 'frontend',
          technology: 'react',
          components: [
            {
              id: 'test-component',
              name: 'TestComponent',
              type: 'form-component',
              description: 'Test',
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'web-app.test-component',
            scenarios: ['test scenario'],
          },
        ],
      },
    };

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

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

    const vitestConfig = result.files.find(f => f.path.includes('vitest.config.ts'));
    expect(vitestConfig).toBeDefined();
    expect(vitestConfig?.content).toContain('defineConfig');
    expect(vitestConfig?.content).toContain('jsdom');
  });

  it('should not overwrite vitest config', () => {
    const workflow = {
      workflow: {
        id: 'test',
        name: 'Test',
        version: '1.0.0',
        description: 'Test',
      },
      stack: {
        'web-app': {
          type: 'frontend',
          technology: 'react',
          components: [
            {
              id: 'test-component',
              name: 'TestComponent',
              type: 'form-component',
              description: 'Test',
            },
          ],
        },
      },
      testing: {
        unit_tests: [
          {
            component: 'web-app.test-component',
            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 vitestConfig = result.files.find(f => f.path.includes('vitest.config.ts'));
    expect(vitestConfig?.overwrite).toBe(false);
  });
});
