import ElementPlus, { ElCheckboxButton, ElInputNumber, ElMessage, ElMessageBox } from 'element-plus' import { AxiosError } from 'axios' import { createPinia, setActivePinia } from 'pinia' import { flushPromises, mount } from '@vue/test-utils' import { beforeEach, describe, expect, it, vi } from 'vitest' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import QueryToolbar from '@/shared/components/QueryToolbar.vue' import GlobalSmartLoadingView from './GlobalSmartLoadingView.vue' import { assignSmartLoadingGroup, assignSmartLoadingItem, getSmartLoadingPlanByBooking, listContainers, listSmartLoadingCandidates, recommendSmartLoading, removeSmartLoadingGroup, removeSmartLoadingItem, } from '../api/platformContainerLoading' const { routerPush } = vi.hoisted(() => ({ routerPush: vi.fn() })) vi.mock('vue-router', () => ({ useRouter: () => ({ push: routerPush }), })) vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: () => true }), })) vi.mock('@/modules/iam', () => ({ listPlatformTenants: vi.fn().mockResolvedValue({ records: [], page: 1, size: 200, total: 0, pages: 0 }), })) vi.mock('../api/platformContainerLoading', () => ({ listContainers: vi.fn(), getSmartLoadingPlanByBooking: vi.fn(), listSmartLoadingCandidates: vi.fn(), createSmartLoadingPlan: vi.fn(), assignSmartLoadingItem: vi.fn(), assignSmartLoadingGroup: vi.fn(), recommendSmartLoading: vi.fn(), adjustSmartLoadingItem: vi.fn(), removeSmartLoadingItem: vi.fn(), removeSmartLoadingGroup: vi.fn(), pushSmartLoadingPreload: vi.fn(), })) vi.mock('../smartLoadingPlanGroups', () => ({ loadSmartLoadingPlanCoLoadGroups: vi.fn().mockResolvedValue(new Map()) })) const container = { id: '9007199254740993', planId: '9007199254740994', bookingNo: 'BK-1', cabinetNo: 'CB-1', containerNo: 'MSCU-1', warehouseId: '8', warehouseName: '义乌仓', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', containerType: '40HC', planStatus: 'EDITING' as const, version: 1, billOfLadingNo: null, vesselName: null, voyageNo: null, shippingCompanyId: '31', shippingCompanyName: '美森轮船', departurePortCode: null, departurePortName: null, destinationPortCode: null, destinationPortName: null, destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', capacityCbm: 76, maxWeightKg: 28000, terminalTruckingCompanyId: null, terminalTruckingCompany: null, loadingAt: null, estimatedSailingAt: null, actualSailingAt: null, estimatedPortArrivalAt: null, actualPortArrivalAt: null, estimatedWarehouseArrivalAt: null, actualWarehouseArrivalAt: null, } const plan = { id: '99', bookingId: container.id, planNo: 'LP-99', status: 'EDITING' as const, containerNo: 'MSCU-1', voyageNo: null, version: 2, canAssign: true, canEdit: true, blockedReason: null, finalCompletionStatus: 'NOT_STARTED' as const, pdaTaskId: null, pdaTaskStatus: null, pdaTaskVersion: null, items: [{ id: '101', loadingPlanId: '99', accountId: '1', inboundId: '2', inboundDetailId: '3', orderId: '4', orderNo: 'ORD-1', inboundCode: 'IN-1', customerName: '示例企业', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', skuCode: 'SKU-1', productNameZh: '服装', packageCount: 6, productQuantity: 6, grossWeightKg: 60, volumeCbm: 1.2, loadSequence: 1, version: 1, pickedPackageCount: 0, minPackageCount: 1, canAdjust: true, canRemove: true }], } const candidate = { accountId: '1', inboundId: '5', orderId: '7', orderNo: 'ORD-2', inboundCode: 'IN-2', orderStatusSnapshot: 'WAIT_SPLIT', expectedArrivalAt: '2026-09-10T09:30:00', warehouseId: '8', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', customerName: '另一企业', goodsSummary: '鞋类', availablePackageCount: 12, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 2.4, coLoadGroupId: null, groupNo: null, memberCount: null, groupTotalBoxes: null, locked: null, blockReason: null, } describe('GlobalSmartLoadingView', () => { beforeEach(() => { setActivePinia(createPinia()) vi.clearAllMocks() vi.mocked(listContainers).mockResolvedValue({ records: [container], page: 1, size: 100, total: 1, pages: 1 }) vi.mocked(getSmartLoadingPlanByBooking).mockResolvedValue(plan) vi.mocked(listSmartLoadingCandidates).mockResolvedValue([candidate]) }) it('loads real containers, candidates and the selected plan into the three-column workbench', async () => { const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(listContainers).toHaveBeenCalledWith({ page: 1, size: 100 }) expect(getSmartLoadingPlanByBooking).toHaveBeenCalledWith(container.id) expect(listSmartLoadingCandidates).toHaveBeenCalledWith(container.id, container.warehouseId) expect(wrapper.text()).toContain('候选装柜明细') expect(wrapper.text()).toContain('美森正班') expect(wrapper.findAll('.channel-tags .el-checkbox-button')).toHaveLength(9) expect(wrapper.text()).not.toContain('美森正班海卡包税') expect(wrapper.text()).toContain('另一企业') expect(wrapper.text()).toContain('MSCU-1 装柜方案') expect(wrapper.text()).not.toContain('LP-99') expect(wrapper.text()).toContain('示例企业') expect(wrapper.text()).not.toContain('服装') expect(wrapper.text()).not.toContain('鞋类') expect(wrapper.text()).not.toContain('SKU-1') expect(wrapper.text()).toContain('IN-1') expect(wrapper.text()).toContain('IN-2') expect(wrapper.text()).not.toContain('ORD-1') expect(wrapper.text()).not.toContain('ORD-2') expect(wrapper.get('.candidate-pane').text()).toContain('120 kg') expect(wrapper.get('.candidate-pane').text()).toContain('2.4 m³') }) it('uses the top-level container channel snapshot without expecting a nested snapshot', async () => { vi.mocked(listContainers).mockResolvedValue({ records: [{ ...container, shippingCompanyId: '32', shippingCompanyName: 'EXX', channelCode: 'EXX_RAIL_NY', channelName: '纽约EXX海铁' }], page: 1, size: 100, total: 1, pages: 1, }) vi.mocked(listSmartLoadingCandidates).mockResolvedValue([{ accountId: '1', inboundId: '5', orderId: '7', orderNo: 'ORD-2', inboundCode: 'IN-2', warehouseId: '8', channelCode: 'EXX_RAIL_NY', channelName: '纽约EXX海铁', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', customerName: '另一企业', goodsSummary: '鞋类', availablePackageCount: 12, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 2.4, coLoadGroupId: null, groupNo: null, memberCount: null, groupTotalBoxes: null, locked: null, blockReason: null }]) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('EXX') expect(wrapper.text()).not.toContain('纽约EXX海铁') expect(wrapper.text()).not.toContain('缺少渠道快照') expect(getSmartLoadingPlanByBooking).toHaveBeenCalledWith(container.id) }) it('shows one user-facing container number and hides internal manual or split references', async () => { vi.mocked(listContainers).mockResolvedValue({ records: [{ ...container, cabinetNo: 'CAB-260814-0001', containerNo: 'SPLIT-814022117898chain1' }], page: 1, size: 100, total: 1, pages: 1, }) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('CAB-260814-0001') expect(wrapper.text()).not.toContain('SPLIT-814022117898chain1') expect(wrapper.text()).not.toContain('LP-99') }) it('explains simulated empty cabinets instead of presenting an internal id as a container number', async () => { vi.mocked(listContainers).mockResolvedValue({ records: [{ ...container, cabinetNo: '2088475410134773762', containerNo: null, channelName: '美森正班海卡包税', warehouseName: '义乌国内仓', containerType: '40HC', }], page: 1, size: 100, total: 1, pages: 1, }) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() const option = wrapper.find('.container-option') expect(option.text()).toContain('模拟柜次') expect(option.text()).toContain('柜次号:待生成') expect(option.text()).toContain('集装箱号:待补录') expect(option.text()).not.toContain('开始装柜前录入集装箱号') expect(option.text()).toContain('渠道:美森正班') expect(option.text()).toContain('仓库:义乌国内仓') expect(option.text()).toContain('柜型:40HC') expect(option.text()).not.toContain('2088475410134773762') expect(wrapper.text()).not.toContain('真实柜号') }) it('keeps nine fixed channel tags and allows selecting multiple channels', async () => { const other = { ...container, id: '9007199254740998', planId: '9007199254740999', cabinetNo: 'CB-2', containerNo: 'MSCU-2', channelCode: 'HDHK', channelName: '合德海卡包税', } vi.mocked(listContainers).mockResolvedValue({ records: [container, other], page: 1, size: 100, total: 2, pages: 1 }) vi.mocked(getSmartLoadingPlanByBooking).mockImplementation(async (bookingId) => ({ ...plan, bookingId })) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.findAll('.container-option')).toHaveLength(1) expect(wrapper.text()).toContain('MSCU-1') expect(wrapper.text()).not.toContain('MSCU-2') expect(wrapper.text()).toContain('柜子数量1 个') expect(wrapper.findAll('.channel-tags .el-checkbox-button').map((item) => item.text())).toEqual([ '美森正班', '美森加班', 'EXX', '合德', '洛杉矶普船', '芝加哥普船', '休斯顿普船', '萨凡纳普船', '纽约普船海卡', ]) const hedeChannel = wrapper.findAllComponents(ElCheckboxButton).find((item) => item.text() === '合德')! await hedeChannel.find('input').setValue(true) await flushPromises() expect(wrapper.findAll('.container-option')).toHaveLength(2) expect(wrapper.text()).toContain('MSCU-1') expect(wrapper.text()).toContain('MSCU-2') expect(wrapper.text()).toContain('柜子数量2 个') }) it('merges detailed pricing channels into the same confirmed display group', async () => { const parcelContainer = { ...container, id: '9007199254740998', planId: '9007199254740999', cabinetNo: 'CB-2', containerNo: 'MSCU-2', channelCode: 'MATSON_CLX_PARCEL', channelName: '美森正班海派', } vi.mocked(listContainers).mockResolvedValue({ records: [container, parcelContainer], page: 1, size: 100, total: 2, pages: 1 }) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.findAll('.container-option')).toHaveLength(2) expect(wrapper.text()).toContain('MSCU-1') expect(wrapper.text()).toContain('MSCU-2') expect(wrapper.text()).not.toContain('美森正班海卡包税') expect(wrapper.text()).not.toContain('美森正班海派') }) it('moves the shipping warehouse into the query filters', async () => { const dongguanContainer = { ...container, id: '9007199254740998', planId: '9007199254740999', cabinetNo: 'CB-DG', containerNo: 'MSCU-DG', warehouseId: '18', warehouseName: '东莞仓', } vi.mocked(listContainers).mockResolvedValue({ records: [container, dongguanContainer], page: 1, size: 100, total: 2, pages: 1 }) vi.mocked(getSmartLoadingPlanByBooking).mockImplementation(async (bookingId) => ({ ...plan, bookingId })) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('MSCU-1') expect(wrapper.text()).toContain('MSCU-DG') wrapper.findComponent(QueryToolbar).vm.$emit('search', { warehouseId: '18', customerId: '', inboundCode: '', expectedArrivalRange: [], orderStatus: '' }) await flushPromises() expect(wrapper.text()).toContain('MSCU-DG') expect(wrapper.text()).not.toContain('MSCU-1') expect(listSmartLoadingCandidates).toHaveBeenLastCalledWith(dongguanContainer.id, dongguanContainer.warehouseId) }) it('keeps the selected channel when the warehouse changes even if that warehouse has no matching container', async () => { const dongguanOvertimeContainer = { ...container, id: '9007199254740998', planId: '9007199254740999', cabinetNo: 'CB-DG-MAX', containerNo: 'MSCU-DG-MAX', warehouseId: '18', warehouseName: '东莞仓', channelCode: 'MATSON_MAX_TRUCK', channelName: '美森加班海卡', } vi.mocked(listContainers).mockResolvedValue({ records: [container, dongguanOvertimeContainer], page: 1, size: 100, total: 2, pages: 1 }) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() wrapper.findComponent(QueryToolbar).vm.$emit('search', { warehouseId: '18', customerId: '', inboundCode: '', expectedArrivalRange: [], orderStatus: '' }) await flushPromises() expect(wrapper.find('.channel-tags .el-checkbox-button.is-checked').text()).toBe('美森正班') expect(wrapper.findAll('.container-option')).toHaveLength(0) expect(wrapper.text()).not.toContain('MSCU-DG-MAX') expect(listSmartLoadingCandidates).toHaveBeenCalledTimes(1) }) it('filters candidates by an active name-only customer selector and accountId', () => { const source = readFileSync(resolve(process.cwd(), 'src/modules/container-loading/pages/GlobalSmartLoadingView.vue'), 'utf8') expect(source).toContain("listPlatformTenants({ page: 1, size: 200, status: 'ACTIVE' })") expect(source).toContain("label: customer.tenantName") expect(source).toContain('member.accountId === customerId') expect(source).not.toContain('customer.tenantCode') }) it('defaults ordinary candidate assignment to all available boxes', () => { const source = readFileSync(resolve(process.cwd(), 'src/modules/container-loading/pages/GlobalSmartLoadingView.vue'), 'utf8') expect(source).toContain(': candidate.availablePackageCount') }) it('filters candidates by expected arrival range and order status', async () => { vi.mocked(listSmartLoadingCandidates).mockResolvedValue([ { ...candidate, inboundId: '51', inboundCode: 'IN-MATCH', orderStatusSnapshot: 'WAIT_SPLIT', expectedArrivalAt: '2026-09-11T08:00:00' }, { ...candidate, inboundId: '52', inboundCode: 'IN-WRONG-STATUS', orderStatusSnapshot: 'LOADING', expectedArrivalAt: '2026-09-11T08:00:00' }, { ...candidate, inboundId: '53', inboundCode: 'IN-WRONG-DATE', orderStatusSnapshot: 'WAIT_SPLIT', expectedArrivalAt: '2026-09-20T08:00:00' }, ]) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() wrapper.getComponent(QueryToolbar).vm.$emit('search', { customerId: '', inboundCode: '', orderStatus: 'WAIT_SPLIT', expectedArrivalRange: ['2026-09-10', '2026-09-12'], }) await flushPromises() const candidates = wrapper.get('.candidate-pane').text() expect(candidates).toContain('IN-MATCH') expect(candidates).not.toContain('IN-WRONG-STATUS') expect(candidates).not.toContain('IN-WRONG-DATE') }) it('paginates large inbound candidate results with twenty rows by default', async () => { vi.mocked(listSmartLoadingCandidates).mockResolvedValue(Array.from({ length: 25 }, (_, index) => ({ ...candidate, inboundId: String(100 + index), orderId: String(200 + index), inboundCode: `IN-${String(index + 1).padStart(3, '0')}`, }))) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() const pane = wrapper.get('.candidate-pane') expect(pane.text()).toContain('IN-020') expect(pane.text()).not.toContain('IN-021') expect(pane.text()).toContain('共 25 条 · 1/2 页') await pane.findAll('button').find((button) => button.attributes('aria-label') === '下一页')!.trigger('click') await flushPromises() expect(pane.text()).toContain('IN-021') expect(pane.text()).not.toContain('IN-001') }) it('aggregates a two-order candidate group and calls only whole-group assignment', async () => { vi.mocked(listSmartLoadingCandidates).mockResolvedValue([ { accountId: '1', inboundId: '201', orderId: '301', orderNo: 'ORD-201', inboundCode: 'NYEXX2608290051', warehouseId: '8', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', customerName: '客户一', goodsSummary: '货物一', availablePackageCount: 4, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 0.8, coLoadGroupId: '2093539779795795969', groupNo: 'CLG-2093539779795795969', memberCount: 3, groupTotalBoxes: 12, locked: false, blockReason: null }, { accountId: '1', inboundId: '203', orderId: '301', orderNo: 'ORD-201', inboundCode: 'NYEXX2608290051-B', warehouseId: '8', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', destinationWarehouseId: '10', destinationWarehouseCode: 'JFK8', destinationWarehouseName: 'JFK8', customerName: '客户一', goodsSummary: '货物一', availablePackageCount: 2, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 0.4, coLoadGroupId: '2093539779795795969', groupNo: 'CLG-2093539779795795969', memberCount: 3, groupTotalBoxes: 12, locked: false, blockReason: null }, { accountId: '2', inboundId: '202', orderId: '302', orderNo: 'ORD-202', inboundCode: 'NYEXX2608290052', warehouseId: '8', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', customerName: '客户二', goodsSummary: '货物二', availablePackageCount: 6, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 1.2, coLoadGroupId: '2093539779795795969', groupNo: 'CLG-2093539779795795969', memberCount: 3, groupTotalBoxes: 12, locked: false, blockReason: null }, ]) vi.mocked(assignSmartLoadingGroup).mockResolvedValue(plan) const wrapper = mount(GlobalSmartLoadingView, { attachTo: document.body, global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('CLG-2093539779795795969 · 2 票') expect(wrapper.text()).toContain('3 个入仓片段') expect(wrapper.text()).toContain('ORD-201:NYEXX2608290051、NYEXX2608290051-B') const buttons = wrapper.findAll('button').filter((button) => button.text().includes('整组分配')) expect(buttons).toHaveLength(1) await buttons[0].trigger('click') await flushPromises() expect(document.body.textContent).toContain('不能部分输入、单独分配或单独移除') expect(document.body.textContent).not.toContain('本次分配箱数') ;([...document.body.querySelectorAll('button')].find((button) => button.textContent?.includes('确认整组分配')) as HTMLButtonElement).click() await flushPromises() expect(assignSmartLoadingGroup).toHaveBeenCalledWith(plan.id, '201', plan.version) expect(assignSmartLoadingItem).not.toHaveBeenCalled() wrapper.unmount() }) it('blocks an incomplete three-member candidate group without exposing member assignment', async () => { vi.mocked(listSmartLoadingCandidates).mockResolvedValue(['401', '402', '403'].map((inboundId, index) => ({ accountId: `${index + 1}`, inboundId, orderId: `50${index}`, orderNo: `ORD-${index}`, inboundCode: `IN-${inboundId}`, warehouseId: '8', channelCode: 'ZBYHK', channelName: '美森正班海卡包税', destinationWarehouseId: '9', destinationWarehouseCode: 'LAX9', destinationWarehouseName: 'LAX9', customerName: `客户${index}`, goodsSummary: `货物${index}`, availablePackageCount: 6, unitGrossWeightKg: 10, unitNetWeightKg: 9, averageUnitVolumeCbm: 0.2, availableVolumeCbm: 1.2, coLoadGroupId: '9900', groupNo: 'CLG-9900', memberCount: 3, groupTotalBoxes: 18, locked: false, blockReason: '同柜不可拆组成员候选不齐,缺少 1 票', }))) const wrapper = mount(GlobalSmartLoadingView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('CLG-9900 · 3 票') expect(wrapper.text()).toContain('同柜不可拆组成员候选不齐,缺少 1 票') const button = wrapper.findAll('button').find((item) => item.text().includes('整组分配'))! expect(button.attributes('disabled')).toBeDefined() expect(assignSmartLoadingGroup).not.toHaveBeenCalled() }) it('keeps the package count editor inside its table cell without overflow ellipsis', () => { const source = readFileSync(resolve(process.cwd(), 'src/modules/container-loading/pages/GlobalSmartLoadingView.vue'), 'utf8') expect(source).toContain('