// src/services/processingErrors.test.ts
import { describe, it, expect } from 'vitest';
import {
  FlyerProcessingError,
  PdfConversionError,
  AiDataValidationError,
} from './processingErrors';

describe('Processing Errors', () => {
  describe('FlyerProcessingError', () => {
    it('should create an error with the correct message, name, and inheritance', () => {
      const message = 'A generic processing error occurred.';
      const error = new FlyerProcessingError(message);

      expect(error).toBeInstanceOf(Error);
      expect(error).toBeInstanceOf(FlyerProcessingError);
      expect(error.message).toBe(message);
      expect(error.name).toBe('FlyerProcessingError');
    });
  });

  describe('PdfConversionError', () => {
    it('should create an error that inherits from FlyerProcessingError', () => {
      const message = 'PDF conversion failed.';
      const error = new PdfConversionError(message);

      expect(error).toBeInstanceOf(Error);
      expect(error).toBeInstanceOf(FlyerProcessingError);
      expect(error).toBeInstanceOf(PdfConversionError);
      expect(error.message).toBe(message);
      expect(error.name).toBe('PdfConversionError');
      expect(error.stderr).toBeUndefined();
    });

    it('should store the stderr property if provided', () => {
      const message = 'pdftocairo failed.';
      const stderr = 'pdftocairo: command not found';
      const error = new PdfConversionError(message, stderr);

      expect(error.message).toBe(message);
      expect(error.stderr).toBe(stderr);
    });
  });

  describe('AiDataValidationError', () => {
    it('should create an error with validation details and raw data', () => {
      const message = 'AI response validation failed.';
      const validationErrors = { fieldErrors: { store_name: ['Store name cannot be empty'] } };
      const rawData = { store_name: '', items: [] };
      const error = new AiDataValidationError(message, validationErrors, rawData);

      expect(error).toBeInstanceOf(Error);
      expect(error).toBeInstanceOf(FlyerProcessingError);
      expect(error).toBeInstanceOf(AiDataValidationError);
      expect(error.message).toBe(message);
      expect(error.name).toBe('AiDataValidationError');
      expect(error.validationErrors).toEqual(validationErrors);
      expect(error.rawData).toEqual(rawData);
    });
  });
});