/**
 * Tests unitaires pour TfsService
 */

/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { TfsService } from '../tfs';

// Mock de fetch global
global.fetch = vi.fn();

describe('TfsService', () => {
  let service: TfsService;

  const mockConfig = {
    enabled: true,
    organizationUrl: 'https://dev.azure.com/test',
    personalAccessToken: 'token123',
    repositories: [],
    ignoredRepositories: [],
  };

  beforeEach(() => {
    vi.clearAllMocks();
    service = new TfsService(mockConfig);
  });

  describe('testConnection', () => {
    it('devrait retourner true si la connexion réussit', async () => {
      vi.mocked(fetch).mockResolvedValue({
        ok: true,
        status: 200,
      } as any);

      const result = await service.testConnection();

      expect(result).toBe(true);
      expect(fetch).toHaveBeenCalled();
    });

    it('devrait retourner false si authentification échoue', async () => {
      vi.mocked(fetch).mockResolvedValue({
        ok: false,
        status: 401,
      } as any);

      const result = await service.testConnection();

      expect(result).toBe(false);
    });

    it('devrait retourner false si accès refusé', async () => {
      vi.mocked(fetch).mockResolvedValue({
        ok: false,
        status: 403,
      } as any);

      const result = await service.testConnection();

      expect(result).toBe(false);
    });

    it('devrait gérer les erreurs de réseau', async () => {
      vi.mocked(fetch).mockRejectedValue(new Error('Network error'));

      const result = await service.testConnection();

      expect(result).toBe(false);
    });
  });
});
