import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ProductAdapter } from './ProductAdapter';
import { ProductError } from '../services/ProductService';

describe('ProductAdapter', () => {
  let adapter: ProductAdapter;
  const mockBaseUrl = 'http://api.example.com';

  beforeEach(() => {
    adapter = new ProductAdapter();
    vi.stubGlobal('fetch', vi.fn());
  });

  describe('getAll', () => {
    it('should fetch all entities', async () => {
      const mockEntities = [{ id: '1' }];
      (global.fetch as any).mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve(mockEntities),
      });

      const result = await adapter.getAll();
      expect(result).toEqual(mockEntities);
      expect(global.fetch).toHaveBeenCalledWith(
        `${mockBaseUrl}/product`,
        expect.any(Object)
      );
    });

    it('should throw error when request fails', async () => {
      (global.fetch as any).mockResolvedValueOnce({
        ok: false,
        status: 500,
      });

      await expect(adapter.getAll()).rejects.toThrow(ProductError);
    });
  });

  // Add more test cases for other methods...
});