import { describe, it, expect, beforeEach } from 'vitest';
import {
  setRunningState,
  getErrorMessage,
  getSpecRegenerationStatus,
} from '@/routes/app-spec/common.js';

describe('app-spec/common.ts', () => {
  beforeEach(() => {
    // Reset state before each test
    setRunningState(false, null);
  });

  describe('setRunningState', () => {
    it('should set isRunning to true when running is true', () => {
      setRunningState(true);
      expect(getSpecRegenerationStatus().isRunning).toBe(true);
    });

    it('should set isRunning to false when running is false', () => {
      setRunningState(true);
      setRunningState(false);
      expect(getSpecRegenerationStatus().isRunning).toBe(false);
    });

    it('should set currentAbortController when provided', () => {
      const controller = new AbortController();
      setRunningState(true, controller);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(controller);
    });

    it('should set currentAbortController to null when not provided', () => {
      const controller = new AbortController();
      setRunningState(true, controller);
      setRunningState(false);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(null);
    });

    it('should set currentAbortController to null when explicitly passed null', () => {
      const controller = new AbortController();
      setRunningState(true, controller);
      setRunningState(true, null);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(null);
    });

    it('should update state multiple times correctly', () => {
      const controller1 = new AbortController();
      const controller2 = new AbortController();

      setRunningState(true, controller1);
      expect(getSpecRegenerationStatus().isRunning).toBe(true);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(controller1);

      setRunningState(true, controller2);
      expect(getSpecRegenerationStatus().isRunning).toBe(true);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(controller2);

      setRunningState(false, null);
      expect(getSpecRegenerationStatus().isRunning).toBe(false);
      expect(getSpecRegenerationStatus().currentAbortController).toBe(null);
    });
  });

  describe('getErrorMessage', () => {
    it('should return message from Error instance', () => {
      const error = new Error('Test error message');
      expect(getErrorMessage(error)).toBe('Test error message');
    });

    it("should return 'Unknown error' for non-Error objects", () => {
      expect(getErrorMessage('string error')).toBe('Unknown error');
      expect(getErrorMessage(123)).toBe('Unknown error');
      expect(getErrorMessage(null)).toBe('Unknown error');
      expect(getErrorMessage(undefined)).toBe('Unknown error');
      expect(getErrorMessage({})).toBe('Unknown error');
      expect(getErrorMessage([])).toBe('Unknown error');
    });

    it('should return message from Error with empty message', () => {
      const error = new Error('');
      expect(getErrorMessage(error)).toBe('');
    });

    it('should handle Error objects with custom properties', () => {
      const error = new Error('Base message');
      (error as any).customProp = 'custom value';
      expect(getErrorMessage(error)).toBe('Base message');
    });

    it('should handle Error objects created with different constructors', () => {
      class CustomError extends Error {
        constructor(message: string) {
          super(message);
          this.name = 'CustomError';
        }
      }

      const customError = new CustomError('Custom error message');
      expect(getErrorMessage(customError)).toBe('Custom error message');
    });
  });
});
