"use strict";
/**
 * API Service Tests
 * Tests for the API service that integrates with the Bluefly API Client
 */
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../api");
// Mock localStorage
const localStorageMock = {
    getItem: jest.fn(),
    setItem: jest.fn(),
    removeItem: jest.fn()
};
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
// Mock location object
Object.defineProperty(window, 'location', {
    writable: true,
    value: { href: '' }
});
// Mock the @bluefly/api-client module
jest.mock('@bluefly/api-client', () => {
    // Mock implementations for the imported components
    const mockAddRequestInterceptor = jest.fn();
    const mockAddErrorInterceptor = jest.fn();
    const mockGet = jest.fn();
    const mockPost = jest.fn();
    const mockPut = jest.fn();
    const mockDelete = jest.fn();
    return {
        RestClient: jest.fn(() => ({
            addRequestInterceptor: mockAddRequestInterceptor,
            addErrorInterceptor: mockAddErrorInterceptor,
            get: mockGet,
            post: mockPost,
            put: mockPut,
            delete: mockDelete
        })),
        bearerTokenInterceptor: jest.fn(() => 'mock-bearer-interceptor'),
        retryInterceptor: jest.fn(() => 'mock-retry-interceptor'),
        requestLoggingInterceptor: jest.fn(() => 'mock-request-logging-interceptor'),
        errorLoggingInterceptor: jest.fn(() => 'mock-error-logging-interceptor'),
        InMemoryCache: jest.fn(),
        CacheManager: jest.fn()
    };
});
describe('API Service', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    describe('API Client initialization', () => {
        test('should create API client with correct configuration', () => {
            // Validate the API client was configured correctly
            expect(api_1.apiClient).toBeDefined();
            expect(api_1.apiClient.addRequestInterceptor).toBeCalled();
            expect(api_1.apiClient.addErrorInterceptor).toBeCalled();
        });
    });
    describe('Authentication', () => {
        test('should store token in localStorage when setupAuthentication is called', () => {
            (0, api_1.setupAuthentication)('test-token');
            expect(localStorageMock.setItem).toHaveBeenCalledWith('auth_token', 'test-token');
            expect(api_1.apiClient.addRequestInterceptor).toHaveBeenCalled();
        });
        test('should initialize auth from localStorage token when available', () => {
            localStorageMock.getItem.mockReturnValueOnce('stored-token');
            (0, api_1.initializeAuth)();
            expect(localStorageMock.getItem).toHaveBeenCalledWith('auth_token');
            expect(api_1.apiClient.addRequestInterceptor).toHaveBeenCalled();
        });
        test('should not add token interceptor when no token in localStorage', () => {
            localStorageMock.getItem.mockReturnValueOnce(null);
            (0, api_1.initializeAuth)();
            expect(localStorageMock.getItem).toHaveBeenCalledWith('auth_token');
            expect(api_1.apiClient.addRequestInterceptor).not.toHaveBeenCalled();
        });
        test('should clear token and redirect on logout', () => {
            (0, api_1.logout)();
            expect(localStorageMock.removeItem).toHaveBeenCalledWith('auth_token');
            expect(window.location.href).toBe('/login');
        });
    });
    describe('API Endpoints', () => {
        test('userApi.getProfile should call correct endpoint', async () => {
            api_1.apiClient.get.mockResolvedValueOnce({ data: { id: 'user1' } });
            await api_1.userApi.getProfile();
            expect(api_1.apiClient.get).toHaveBeenCalledWith('/user/profile');
        });
        test('contentApi.getArticles should call correct endpoint with params', async () => {
            api_1.apiClient.get.mockResolvedValueOnce({ data: { items: [] } });
            await api_1.contentApi.getArticles(2, 20);
            expect(api_1.apiClient.get).toHaveBeenCalledWith('/articles', {
                params: { page: 2, limit: 20 }
            });
        });
        test('rfpApi should make correct fetch call for uploadDocument', async () => {
            global.fetch = jest.fn().mockResolvedValueOnce({
                ok: true,
                json: jest.fn().mockResolvedValueOnce({ id: 'doc1' })
            });
            const file = new File(['content'], 'test.pdf');
            await api_1.rfpApi.uploadDocument(file);
            expect(global.fetch).toHaveBeenCalledWith('/api/rfp/upload', {
                method: 'POST',
                body: expect.any(FormData)
            });
        });
        test('agentApi.getAgents should call correct endpoint', async () => {
            api_1.apiClient.get.mockResolvedValueOnce({ data: [] });
            await api_1.agentApi.getAgents();
            expect(api_1.apiClient.get).toHaveBeenCalledWith('/agents');
        });
    });
    describe('Enhanced API Client', () => {
        test('should include all methods from different APIs', () => {
            expect(api_1.enhancedApiClient.getAgentDetails).toBeDefined();
            expect(api_1.enhancedApiClient.uploadDocument).toBeDefined();
            expect(api_1.enhancedApiClient.getAnalyses).toBeDefined();
            expect(api_1.enhancedApiClient.getAnalysisById).toBeDefined();
            expect(api_1.enhancedApiClient.generateRFPResponse).toBeDefined();
        });
    });
});
//# sourceMappingURL=api.test.js.map