import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { adjustContainerFee, approveContainerFee, approveContainerFeeAllocationGroup, createContainerFee, createContainerFeeAllocationGroup, getContainerSettlementDetails, listContainerFeeSupplierOptions, listContainerFees, rejectContainerFee, uploadContainerFeePaymentVoucher, voidContainerFee, voidContainerFeeAllocationGroup } from './containerFees' import { listSuppliers } from '@/modules/suppliers' vi.mock('@/modules/suppliers', () => ({ listSuppliers: vi.fn() })) describe('container fee API', () => { beforeEach(() => vi.restoreAllMocks()) it('passes accountId and exact controller DTOs', async () => { const get = vi.spyOn(http, 'get').mockResolvedValue({ code: '200', data: [] }) const post = vi.spyOn(http, 'post').mockResolvedValue({ code: '200', data: { id: 3, loadingPlanId: 9, containerBookingId: 99, feeItemId: 7, feeItemNameSnapshot: '操作费', direction: 'PAYABLE', supplierId: 41, supplierName: '承运商A', supplierCategory: 'TRUCKING', settlementMode: 'DIRECT_PAID', paidAt: '2026-08-12T10:30:00', paymentMethod: 'BANK_TRANSFER', paymentReferenceNo: 'BANK-7', paymentVoucherFileIds: [9001], businessDate: '2026-08-12', amount: 100, currency: 'CNY', reviewStatus: 'PENDING', sourceStatus: 'ACTIVE', sourceVersion: 1 } }) const values = { feeItemId: '7', direction: 'PAYABLE' as const, supplierId: '41', settlementMode: 'DIRECT_PAID' as const, paidAt: '2026-08-12T10:30:00', paymentMethod: 'BANK_TRANSFER' as const, paymentReferenceNo: 'BANK-7', paymentVoucherFileIds: ['9001'], businessDate: '2026-08-12', amount: 100, currency: 'CNY' as const, remark: null } await listContainerFees('9', '11'); const created = await createContainerFee('9', '11', values) await approveContainerFee('9', '11', '3'); await rejectContainerFee('9', '11', '3', '资料不全') await adjustContainerFee('9', '11', '3', { expectedSourceVersion: 2, businessDate: '2026-08-13', amount: 120 }); await voidContainerFee('9', '11', '3', 3) expect(get).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details', { params: { accountId: '11' } }) expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details', values, { params: { accountId: '11' } }) expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/3/adjust', { expectedSourceVersion: 2, businessDate: '2026-08-13', amount: 120 }, { params: { accountId: '11' } }) expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/3/void', { expectedSourceVersion: 3 }, { params: { accountId: '11' } }) expect(created).toMatchObject({ id: '3', loadingPlanId: '9', containerBookingId: '99', direction: 'PAYABLE', supplierId: '41', supplierName: '承运商A', supplierCategory: 'TRUCKING', settlementMode: 'DIRECT_PAID', paymentVoucherFileIds: ['9001'] }) }) it('writes and reviews one atomic allocation group without an accountId shortcut', async () => { const post = vi.spyOn(http, 'post').mockResolvedValue({ code: '200', data: [] }) const fee = { feeItemId: '7', direction: 'RECEIVABLE' as const, supplierId: null, settlementMode: null, paidAt: null, paymentMethod: null, paymentReferenceNo: null, paymentVoucherFileIds: null, businessDate: '2026-09-02', amount: 300, currency: 'CNY' as const, remark: null } const allocations = [ { customerId: '11', orderId: '501', orderNo: 'FL-501', inboundId: null, inboundCode: null, amount: 120 }, { customerId: '12', orderId: '502', orderNo: 'FL-502', inboundId: '602', inboundCode: 'IB-602', amount: 180 }, ] await createContainerFeeAllocationGroup('9', { fee, allocations }) await approveContainerFeeAllocationGroup('9', '71') await voidContainerFeeAllocationGroup('9', '71', 1) expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/allocation-groups', { fee, allocations }) expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/allocation-groups/71/approve') expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/allocation-groups/71/void', { expectedSourceVersion: 1 }) }) it('uploads a payment voucher in the current customer scope and rejects a mismatched echo', async () => { const post = vi.spyOn(http, 'post').mockResolvedValueOnce({ code: '200', data: { fileId: 9001, tenantId: 11, fileName: '付款凭证.png' } }) await expect(uploadContainerFeePaymentVoucher('11', new File(['x'], '付款凭证.png'))).resolves.toEqual({ fileId: '9001', fileName: '付款凭证.png' }) expect(post).toHaveBeenCalledWith('/platform/finance/v1/vouchers', expect.any(FormData), { params: { customerId: '11' }, headers: { 'Content-Type': 'multipart/form-data' } }) post.mockResolvedValueOnce({ code: '200', data: { fileId: 9002, tenantId: 12, fileName: '错客户.png' } }) await expect(uploadContainerFeePaymentVoucher('11', new File(['x'], '错客户.png'))).rejects.toThrow('付款凭证与当前客户不匹配') }) it('loads only ACTIVE suppliers in the requested category', async () => { vi.mocked(listSuppliers).mockResolvedValue({ records: [ { id: '41', name: '承运商A', status: 'ACTIVE', category: 'TRUCKING' }, { id: '42', name: '停用承运商', status: 'INACTIVE', category: 'TRUCKING' }, { id: '43', name: '报关行', status: 'ACTIVE', category: 'CUSTOMS' }, ], page: 1, size: 200, total: 3, pages: 1 } as never) await expect(listContainerFeeSupplierOptions('TRUCKING')).resolves.toEqual([{ id: '41', supplierName: '承运商A', category: 'TRUCKING' }]) expect(listSuppliers).toHaveBeenCalledWith({ status: 'ACTIVE', category: 'TRUCKING', page: 1, size: 200 }) }) it('loads every ACTIVE supplier without a category filter when the fee item category is empty', async () => { vi.mocked(listSuppliers).mockResolvedValue({ records: [ { id: '41', name: '承运商A', status: 'ACTIVE', category: 'TRUCKING' }, { id: '42', name: '停用承运商', status: 'INACTIVE', category: 'TRUCKING' }, { id: '43', name: '报关行', status: 'ACTIVE', category: 'CUSTOMS' }, ], page: 1, size: 200, total: 3, pages: 1 } as never) await expect(listContainerFeeSupplierOptions(null)).resolves.toEqual([ { id: '41', supplierName: '承运商A', category: 'TRUCKING' }, { id: '43', supplierName: '报关行', category: 'CUSTOMS' }, ]) expect(listSuppliers).toHaveBeenCalledWith({ status: 'ACTIVE', category: undefined, page: 1, size: 200 }) }) it('maps settled fees and allocation rows from one container aggregate request', async () => { const get = vi.spyOn(http, 'get').mockResolvedValue({ code: '200', data: { customerId: 11, containerBookingId: 99, settledSourceFeeIds: [7], costs: [{ costId: 81, costNo: 'COST-81', costType: 'TRUCKING', costOrigin: 'CONTAINER_FEE', sourceFeeId: 7, sourceFeeVersion: 2, containerBookingId: 99, supplierName: '承运商A', feeItemName: '拖车费', amount: 300, currency: 'CNY', status: 'CONFIRMED', allocations: [{ allocationId: 1, orderId: 8, inboundId: 9, volumeCbm: 2, totalVolumeCbm: 5, allocationRate: 0.4, allocatedAmount: 120, roundingAdjustment: false }] }], } }) const result = await getContainerSettlementDetails('11', '99') expect(result).toMatchObject({ customerId: '11', containerBookingId: '99', settledSourceFeeIds: ['7'], costs: [{ id: '81', containerBookingId: '99', sourceFeeId: '7', amount: 300, allocations: [{ id: '1', orderId: '8', inboundId: '9', allocatedAmount: 120 }] }] }) expect(get).toHaveBeenCalledWith('/platform/finance/v1/container-settlement-details', { params: { customerId: '11', containerBookingId: '99' } }) expect(get).toHaveBeenCalledTimes(1) }) it('fails closed when the Finance aggregate does not echo the selected customer and container', async () => { vi.spyOn(http, 'get').mockResolvedValue({ code: '200', data: { customerId: 12, containerBookingId: 99, settledSourceFeeIds: [], costs: [] } }) await expect(getContainerSettlementDetails('11', '99')).rejects.toThrow('Finance 聚合结果与柜次或客户上下文不一致') }) })