import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { createFbaAppointment, getFbaAppointment, listFbaAppointmentCalendar, listInboundOrders, updateFbaAppointment } from './inboundOrders' vi.mock('@/shared/api/http', async (importOriginal) => ({ ...(await importOriginal()), http: { get: vi.fn(), post: vi.fn(), put: vi.fn() } })) describe('inbound order overseas API', () => { beforeEach(() => { vi.mocked(http.get).mockReset() vi.mocked(http.post).mockReset() vi.mocked(http.put).mockReset() }) it('loads a calendar range in one request and supports cancelling obsolete requests', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: [] }) const controller = new AbortController() const query = { from: '2026-08-31', to: '2026-10-12' } expect(await listFbaAppointmentCalendar(query, controller.signal)).toEqual([]) expect(http.get).toHaveBeenCalledTimes(1) expect(http.get).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans/fba-appointments/calendar', { params: query, signal: controller.signal, }) }) it('does not turn calendar business errors into an empty successful calendar', async () => { vi.mocked(http.get).mockResolvedValue({ code: '500', message: '日历查询失败', data: null }) await expect(listFbaAppointmentCalendar({ from: '2026-09-01', to: '2026-10-01' })).rejects.toThrow() }) it('loads appointment details and updates the existing appointment without replacing its lines', async () => { const detail = { version: 0, id: '1', customerId: '190000000000000001', appointmentNo: 'APT/1', appointmentAt: '2026-08-20T10:30:00', expectedLoadingAt: '2026-08-20T09:00:00', appointmentStatus: 'SUCCESS', selectedCount: 2, fbaWarehouseCode: 'ONT8', items: [] } vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: detail }) vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: detail }) await getFbaAppointment('APT/1') await updateFbaAppointment('APT/1', { version: detail.version, appointmentAt: detail.appointmentAt, expectedLoadingAt: detail.expectedLoadingAt, appointmentStatus: 'SUCCESS' }) expect(http.get).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans/fba-appointments/APT%2F1') expect(http.put).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans/fba-appointments/APT%2F1', expect.not.objectContaining({ customerId: expect.anything() })) }) it('loads the paginated list across all customers without a customer scope', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: 1, size: 20, total: 0, pages: 0 } }) await listInboundOrders({ page: 1, size: 20, appointmentStatus: 'UNAPPOINTED' }) expect(http.get).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans', { params: { page: 1, size: 20, appointmentStatus: 'UNAPPOINTED' }, }) }) it('preserves selected line ids as strings when creating an appointment', async () => { vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: { id: '1', appointmentNo: 'APT-1', appointmentStatus: 'SUCCESS', selectedCount: 1, fbaWarehouseCode: 'ONT8' } }) const payload = { lineIds: ['190000000000000099'], appointmentNo: 'APT-1', appointmentAt: '2026-08-20T10:30:00', expectedLoadingAt: '2026-08-20T09:00:00', appointmentStatus: 'SUCCESS' as const, } await createFbaAppointment(payload) expect(http.post).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans/fba-appointments/batch', payload) }) })