|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import axios, { AxiosInstance } from 'axios'; |
| 3 | +import { DocumentApiClient } from '../src/api/document-api'; |
| 4 | + |
| 5 | +vi.mock('axios'); |
| 6 | + |
| 7 | +describe('DocumentApiClient', () => { |
| 8 | + let apiClient: DocumentApiClient; |
| 9 | + let mockAxiosInstance: Partial<AxiosInstance>; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + mockAxiosInstance = { |
| 13 | + get: vi.fn(), |
| 14 | + interceptors: { |
| 15 | + response: { |
| 16 | + use: vi.fn() |
| 17 | + } |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + (axios.create as ReturnType<typeof vi.fn>).mockReturnValue(mockAxiosInstance as AxiosInstance); |
| 22 | + |
| 23 | + apiClient = new DocumentApiClient({ |
| 24 | + apiKey: 'test-api-key' |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('getDocumentMetadata', () => { |
| 29 | + it('should fetch document metadata', async () => { |
| 30 | + const mockResponse = { |
| 31 | + data: { |
| 32 | + company_number: '12345678', |
| 33 | + barcode: 'ABC123', |
| 34 | + category: 'accounts', |
| 35 | + pages: 10, |
| 36 | + filename: 'document.pdf', |
| 37 | + links: { |
| 38 | + self: '/document/doc123', |
| 39 | + document: '/document/doc123/content' |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + (mockAxiosInstance.get as ReturnType<typeof vi.fn>).mockResolvedValue(mockResponse); |
| 45 | + |
| 46 | + const result = await apiClient.getDocumentMetadata({ document_id: 'doc123' }); |
| 47 | + |
| 48 | + expect(mockAxiosInstance.get).toHaveBeenCalledWith('/document/doc123'); |
| 49 | + expect(result).toEqual(mockResponse.data); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle errors when fetching metadata', async () => { |
| 53 | + const error = new Error('API Error'); |
| 54 | + (mockAxiosInstance.get as ReturnType<typeof vi.fn>).mockRejectedValue(error); |
| 55 | + |
| 56 | + await expect(apiClient.getDocumentMetadata({ document_id: 'doc123' })).rejects.toThrow( |
| 57 | + 'API Error' |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('getDocumentContent', () => { |
| 63 | + it('should fetch document content as buffer', async () => { |
| 64 | + const mockPdfData = new ArrayBuffer(1024); |
| 65 | + const mockResponse = { |
| 66 | + data: mockPdfData |
| 67 | + }; |
| 68 | + |
| 69 | + (mockAxiosInstance.get as ReturnType<typeof vi.fn>).mockResolvedValue(mockResponse); |
| 70 | + |
| 71 | + const result = await apiClient.getDocumentContent({ document_id: 'doc123' }); |
| 72 | + |
| 73 | + expect(mockAxiosInstance.get).toHaveBeenCalledWith('/document/doc123/content', { |
| 74 | + responseType: 'arraybuffer', |
| 75 | + headers: { |
| 76 | + Accept: 'application/pdf' |
| 77 | + } |
| 78 | + }); |
| 79 | + expect(result).toBeInstanceOf(Buffer); |
| 80 | + expect(result.length).toBe(1024); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle errors when fetching content', async () => { |
| 84 | + const error = new Error('Failed to fetch document'); |
| 85 | + (mockAxiosInstance.get as ReturnType<typeof vi.fn>).mockRejectedValue(error); |
| 86 | + |
| 87 | + await expect(apiClient.getDocumentContent({ document_id: 'doc123' })).rejects.toThrow( |
| 88 | + 'Failed to fetch document' |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments