import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { downloadInboundCartonLabels, getPlatformInbound, listPlatformInbounds } from './api' vi.mock('@/shared/api/http', () => ({ ApiResponseError: class ApiResponseError extends Error { code: string constructor(message: string, code: string) { super(message); this.code = code } }, http: { get: vi.fn(), post: vi.fn() }, unwrapApiResponse: (response: { data: T }) => response.data, })) describe('platform inbound query API', () => { beforeEach(() => { vi.mocked(http.get).mockReset() vi.mocked(http.post).mockReset() }) it('omits the backend tenant filter for an all-customer list query', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: 1, size: 20, total: 0, pages: 0 }, }) await listPlatformInbounds(undefined, { page: 1, size: 20 }) expect(http.get).toHaveBeenCalledWith('/platform/inbounds', { params: { page: 1, size: 20 } }) }) it('uses the implemented non-v1 list path and normalizes page values and IDs', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [{ id: 19001, tenantId: 10002, orderId: 18001, customerId: 10001, totalPackageCount: '6', palletCount: '2', actualGrossWeightKg: '21.5', actualNetWeightKg: '18.25', actualVolumeCbm: '0.456', holdFlag: 'false', }], page: '1', size: '20', total: '1', pages: '1', }, }) await expect(listPlatformInbounds('10001', { page: 1, size: 20, tab: 'WAIT_MEASURE', inboundCode: 'RK', holdFlag: false, })).resolves.toMatchObject({ records: [{ id: '19001', tenantId: '10002', orderId: '18001', customerId: '10001', totalPackageCount: 6, palletCount: 2, actualGrossWeightKg: 21.5, actualNetWeightKg: 18.25, actualVolumeCbm: 0.456, holdFlag: false, }], total: 1, pages: 1, }) expect(http.get).toHaveBeenCalledWith('/platform/inbounds', { params: { tenantId: '10001', page: 1, size: 20, tab: 'WAIT_MEASURE', inboundCode: 'RK', holdFlag: false }, }) }) it('loads a tenant-scoped aggregate detail and keeps nested IDs as strings', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { summary: { id: 19001, tenantId: 10002, orderId: 18001 }, publicRemark: null, palletLocations: [{ palletId: 21001, locationId: 22001 }], files: [{ fileId: 23001, uploadedBy: 2 }], internalNotes: [{ id: 24001, createdBy: 2 }], statusHistory: [{ operatorId: 2 }], }, }) await expect(getPlatformInbound('10001', '19001')).resolves.toMatchObject({ summary: { id: '19001', tenantId: '10002', orderId: '18001' }, palletLocations: [{ palletId: '21001', locationId: '22001' }], files: [{ fileId: '23001', uploadedBy: '2' }], internalNotes: [{ id: '24001', createdBy: '2' }], statusHistory: [{ operatorId: '2' }], }) expect(http.get).toHaveBeenCalledWith('/platform/inbounds/19001', { params: { tenantId: '10001' } }) }) it('passes the implemented order, process, measurement and fee review filters without renaming them', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: 1, size: 20, total: 0, pages: 0 }, }) await listPlatformInbounds('10001', { page: 1, size: 20, processStatus: 'MEASURE_PENDING', orderStatus: 'APPROVED', measureReviewStatus: 'PENDING', feeStatus: 'UNPRICED', feeReviewStatus: 'PENDING', warehouseId: '9007199254740993', deliveryWarehouseId: '9007199254740995', }) expect(http.get).toHaveBeenCalledWith('/platform/inbounds', { params: { tenantId: '10001', page: 1, size: 20, processStatus: 'MEASURE_PENDING', orderStatus: 'APPROVED', measureReviewStatus: 'PENDING', feeStatus: 'UNPRICED', feeReviewStatus: 'PENDING', warehouseId: '9007199254740993', deliveryWarehouseId: '9007199254740995', }, }) }) it('downloads stable carton labels and rejects JSON business errors', async () => { const pdf = new Blob(['pdf'], { type: 'application/pdf' }) vi.mocked(http.post).mockResolvedValueOnce(pdf) await expect(downloadInboundCartonLabels('10001', '19001', [1, 3])).resolves.toBe(pdf) expect(http.post).toHaveBeenCalledWith('/platform/inbounds/19001/carton-label-pdf', null, { params: { tenantId: '10001', cartonIndex: [1, 3] }, responseType: 'blob', timeout: 60_000, }) vi.mocked(http.post).mockResolvedValueOnce({ type: 'application/json', text: async () => JSON.stringify({ code: 'A0422', message: '箱号映射不完整' }), } as Blob) await expect(downloadInboundCartonLabels('10001', '19001')).rejects.toMatchObject({ message: '箱号映射不完整', code: 'A0422', }) }) })