import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { getCustomerVisit, listCustomerVisits, saveCustomerVisit, updateDeliveryOrderStatus, uploadCustomerVisitAttachment, } from './customerVisits' vi.mock('@/shared/api/http', async (importOriginal) => ({ ...(await importOriginal()), http: { get: vi.fn(), post: vi.fn(), put: vi.fn() } })) describe('customer visit API', () => { beforeEach(() => { vi.mocked(http.get).mockReset() vi.mocked(http.post).mockReset() vi.mocked(http.put).mockReset() }) it('loads the customer visit page and detail in customer scope', async () => { vi.mocked(http.get).mockResolvedValueOnce({ code: '200', data: { records: [], current: 1, size: 10, total: 0, pages: 0, visitable: 3, pending: 2, completed: 1 } }) await listCustomerVisits({ page: 1, size: 10, keyword: 'DEL' }) expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/last-leg/customer-visits', { params: { page: 1, size: 10, keyword: 'DEL' } }) vi.mocked(http.get).mockResolvedValueOnce({ code: '200', data: { visit: { id: '9' }, inboundOrders: [] } }) await getCustomerVisit('9', '100') expect(http.get).toHaveBeenNthCalledWith(2, '/platform/v1/last-leg/customer-visits/9', { params: { customerId: '100' } }) }) it('updates a delivery to an operator-managed nonterminal status', async () => { vi.mocked(http.put).mockResolvedValue({ code: '200', data: { deliveryStatus: 'DELIVERING' } }) await updateDeliveryOrderStatus('DEL/1', '100', { status: 'DELIVERING', expectedVersion: 7, remark: '开始派送' }) expect(http.put).toHaveBeenCalledWith('/platform/v1/last-leg/delivery-orders/DEL%2F1/status', { status: 'DELIVERING', expectedVersion: 7, remark: '开始派送' }, { params: { customerId: '100' } }) }) it('uploads and saves repeatable visit material', async () => { const file = new File(['data'], 'visit.pdf', { type: 'application/pdf' }) vi.mocked(http.post).mockResolvedValue({ code: '200', data: { fileId: '8', fileName: 'visit.pdf', sizeBytes: 4 } }) await uploadCustomerVisitAttachment('9', '100', file) const body = vi.mocked(http.post).mock.calls[0]?.[1] as FormData expect(body.get('file')).toBe(file) expect(http.post).toHaveBeenCalledWith( '/platform/v1/last-leg/customer-visits/9/attachments', body, { params: { customerId: '100' }, headers: { 'Content-Type': 'multipart/form-data' } }, ) vi.mocked(http.put).mockResolvedValue({ code: '200', data: { visit: { id: '9' }, inboundOrders: [] } }) await saveCustomerVisit('9', '100', { selectedInboundOrderId: '7', visitResult: 'NORMAL', attachments: [] }) expect(http.put).toHaveBeenCalledWith('/platform/v1/last-leg/customer-visits/9', expect.objectContaining({ selectedInboundOrderId: '7' }), { params: { customerId: '100' } }) }) })