import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { approvePlatformUnifiedOrder, addPlatformUnifiedOrderNote, getPlatformUnifiedOrder, listPlatformUnifiedOrders, listPlatformUnifiedOrderFeeDetails, } from './unifiedOrders' vi.mock('@/shared/api/http', () => ({ http: { get: vi.fn(), post: vi.fn(), put: vi.fn() }, unwrapApiResponse: (response: { data: T }) => response.data, })) describe('unified orders API contract', () => { beforeEach(() => { vi.mocked(http.get).mockReset() vi.mocked(http.post).mockReset() vi.mocked(http.put).mockReset() }) it('uses the C02 platform orders endpoint and contract filters', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [], page: '2', size: '20', total: '0', pages: '0' } }) await expect(listPlatformUnifiedOrders({ keyword: 'CC', orderNo: 'CC-1', mainStatus: 'WAIT_SPLIT', customerId: '30001', receivingWarehouseId: '11', destinationWarehouseId: '22', inboundCode: 'RK-1', internalOrderStatus: 'ACCEPTED', expectedArrivalDate: '2026-08-10', feeReviewStatus: 'APPROVED', channelName: '海运', containerNo: 'CONT-1', cabinetNo: 'CAB-1', sailingDate: '2026-08-12', loaded: true, fullContainer: false, measurementReviewStatus: 'PENDING', fbaCode: 'FBA-1', createdFrom: '2026-08-01T00:00:00', createdTo: '2026-08-31T23:59:59', page: 2, size: 20, })).resolves.toMatchObject({ page: 2, size: 20, total: 0, pages: 0 }) expect(http.get).toHaveBeenCalledWith('/platform/v1/orders', { params: { keyword: 'CC', orderNo: 'CC-1', mainStatus: 'WAIT_SPLIT', customerId: '30001', receivingWarehouseId: '11', destinationWarehouseId: '22', inboundCode: 'RK-1', internalOrderStatus: 'ACCEPTED', expectedArrivalDate: '2026-08-10', feeReviewStatus: 'APPROVED', channelName: '海运', containerNo: 'CONT-1', cabinetNo: 'CAB-1', sailingDate: '2026-08-12', loaded: true, fullContainer: false, measurementReviewStatus: 'PENDING', fbaCode: 'FBA-1', createdFrom: '2026-08-01T00:00:00', createdTo: '2026-08-31T23:59:59', page: 2, size: 20, }, }) }) it('omits empty optional filters while preserving false boolean values', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [], page: 1, size: 20, total: 0, pages: 0 } }) await listPlatformUnifiedOrders({ keyword: '', loaded: false, fullContainer: false, page: 1, size: 20 }) expect(http.get).toHaveBeenCalledWith('/platform/v1/orders', { params: { loaded: false, fullContainer: false, page: 1, size: 20 }, }) }) it('normalizes C02 row IDs and keeps inboundId for measurement routing', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [{ orderId: '90001', tenantId: '208', inboundId: '77', inboundVersion: 3, mainStatus: 'WAIT_MEASURE', customerRemark: '客户备注', latestInternalNote: '最新内部备注', measurement: null, preload: null, loading: null, delivery: null, transport: null }], page: 1, size: 20, total: 1, pages: 1, } }) await expect(listPlatformUnifiedOrders({ page: 1, size: 20 })).resolves.toMatchObject({ records: [{ orderId: '90001', tenantId: '208', inboundId: '77', inboundVersion: 3, customerRemark: '客户备注', latestInternalNote: '最新内部备注' }], }) }) it('loads the C02 detail shape and passes tenantId only on the detail boundary', async () => { vi.mocked(http.get).mockResolvedValue({ data: { summary: { orderId: '90001', tenantId: '208', mainStatus: 'WAIT_SPLIT' }, order: { order: {}, cargoes: [], parties: [], internalNotes: [], statusHistory: [] }, timeline: [], } }) await expect(getPlatformUnifiedOrder('208', '90001')).resolves.toMatchObject({ summary: { orderId: '90001' }, order: { internalNotes: [] }, timeline: [], }) expect(http.get).toHaveBeenCalledWith('/platform/v1/orders/90001', { params: { tenantId: '208' } }) }) it('uses the explicit first-leg approval boundary for pending review', async () => { vi.mocked(http.post).mockResolvedValue({ data: null }) await approvePlatformUnifiedOrder('208', '90001') expect(http.post).toHaveBeenCalledWith('/platform/v1/orders/first-leg/90001/approve', undefined, { params: { tenantId: '208' } }) }) it('writes platform internal notes through the unified order boundary', async () => { vi.mocked(http.post).mockResolvedValue({ data: '91001' }) await expect(addPlatformUnifiedOrderNote('208', '90001', '已确认船期')).resolves.toBe('91001') expect(http.post).toHaveBeenCalledWith('/platform/v1/orders/90001/internal-notes', { content: '已确认船期' }, { params: { tenantId: '208' } }) }) it('loads fee detail rows from the inbound fee-detail contract', async () => { vi.mocked(http.get).mockResolvedValue({ data: [{ id: '1', inboundId: '88', reviewStatus: 'APPROVED', amount: 100 }] }) await expect(listPlatformUnifiedOrderFeeDetails('208', '88')).resolves.toHaveLength(1) expect(http.get).toHaveBeenCalledWith('/platform/v1/inbounds/88/fee-details', { params: { accountId: '208' } }) }) })