import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { deliveryDocumentCategories, getDeliveryDocuments, saveDeliveryDocuments, uploadDeliveryDocument, downloadDeliveryDocument } from './deliveryDocuments' vi.mock('@/shared/api/http', async (importOriginal) => ({ ...(await importOriginal()), http: { get: vi.fn(), post: vi.fn(), put: vi.fn() } })) describe('delivery document API', () => { beforeEach(() => vi.clearAllMocks()) it('scopes reads and full three-category snapshots by customer and delivery number', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', data: {} }) vi.mocked(http.put).mockResolvedValue({ code: '200', data: {} }) await getDeliveryDocuments('DL/1', '9007199254740993') expect(http.get).toHaveBeenCalledWith('/platform/v1/last-leg/delivery-orders/DL%2F1/documents', { params: { customerId: '9007199254740993' } }) const body = { expectedVersion: 8, idempotencyKey: 'save-1', bolNo: null, documents: deliveryDocumentCategories.map(item => ({ category: item.category, fileIds: [] })) } await saveDeliveryDocuments('DL/1', '9007199254740993', body) expect(http.put).toHaveBeenCalledWith('/platform/v1/last-leg/delivery-orders/DL%2F1/documents', body, { params: { customerId: '9007199254740993' } }) }) it('uploads only the file and downloads through the authenticated category endpoint', async () => { vi.mocked(http.post).mockResolvedValue({ code: '200', data: { fileId: '9007199254740995' } }) const file = new File(['data'], 'proof.bin') await uploadDeliveryDocument('DL/1', '100', 'RECEIPT', file) const [url, body, config] = vi.mocked(http.post).mock.calls[0]! expect(url).toBe('/platform/v1/last-leg/delivery-orders/DL%2F1/documents/RECEIPT/files') expect(Array.from((body as FormData).keys())).toEqual(['file']) expect((body as FormData).get('file')).toBe(file) expect(config).toEqual({ params: { customerId: '100' } }) await downloadDeliveryDocument('DL/1', '100', 'RECEIPT', '9007199254740995') expect(http.get).toHaveBeenCalledWith('/platform/v1/last-leg/delivery-orders/DL%2F1/documents/RECEIPT/files/9007199254740995/download', { params: { customerId: '100' }, responseType: 'blob' }) }) })