import ElementPlus from 'element-plus' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import QueryToolbar, { type QueryModel } from '@/shared/components/QueryToolbar.vue' import { cancelDispatch, getDispatchBolPdf, listDispatches, removeDispatchItem, transferDispatchToDelivery, updateDispatch } from '../api/dispatches' import DispatchesView from './DispatchesView.vue' const canPermission = vi.hoisted(() => vi.fn<(permissionCode: string) => boolean>(() => true)) vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: canPermission }) })) vi.mock('../api/dispatches', () => ({ listDispatches: vi.fn(), removeDispatchItem: vi.fn(), getDispatchBolPdf: vi.fn(), updateDispatch: vi.fn(), cancelDispatch: vi.fn(), transferDispatchToDelivery: vi.fn() })) class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} } function memoryStorage(): Storage { const values = new Map() return { get length() { return values.size }, clear: () => values.clear(), getItem: (key) => values.get(key) ?? null, key: (index) => [...values.keys()][index] ?? null, removeItem: (key) => values.delete(key), setItem: (key, value) => values.set(key, value), } } enableAutoUnmount(afterEach) afterAll(() => vi.unstubAllGlobals()) describe('DispatchesView', () => { beforeEach(() => { vi.stubGlobal('ResizeObserver', ResizeObserverStub) vi.stubGlobal('localStorage', memoryStorage()) vi.mocked(listDispatches).mockReset().mockResolvedValue({ records: [{ id: '190000000000000099', dispatchNo: 'PC-1', vehiclePlate: 'CA-100', driverName: '张师傅', driverPhone: '555-0100', carrierName: 'Carrier A', orderNos: 'LM-1,LM-2', route: 'LAX -> ONT8', overseasWarehouseName: '洛杉矶仓', departAt: '2026-08-20T09:00:00', fbaAppointmentNo: 'FBA-1', fbaAppointmentStatus: 'CONFIRMED', vehicleAppointmentStatus: 'BOOKED', estimatedArrivalAt: '2026-08-20T18:00:00', status: 'PENDING_DEPARTURE', palletCount: 12, packageCount: 80, createdAt: '2026-08-19T16:00:00', }], page: 1, size: 20, total: 1, pages: 1, }) vi.mocked(updateDispatch).mockReset() vi.mocked(getDispatchBolPdf).mockReset().mockResolvedValue(new Blob(['%PDF-test'], { type: 'application/pdf' })) vi.mocked(removeDispatchItem).mockReset() vi.mocked(cancelDispatch).mockReset() vi.mocked(transferDispatchToDelivery).mockReset() canPermission.mockReset().mockReturnValue(true) }) it.each([ [[], ['查看', '打印BOL'], false], [['edit'], ['查看', '打印BOL', '编辑'], false], [['cancel'], ['查看', '打印BOL', '取消'], false], [['transfer-delivery'], ['查看', '打印BOL', '转派送'], false], [['edit', 'transfer-delivery'], ['查看', '打印BOL', '转派送', ''], true], [['edit', 'cancel'], ['查看', '打印BOL', '编辑', ''], true], [['edit', 'cancel', 'transfer-delivery'], ['查看', '打印BOL', '转派送', ''], true], ])('uses the row action threshold for permissions %j', async (allowed, labels, overflow) => { canPermission.mockImplementation(code => allowed.some(action => code === 'platform:overseas-warehouse:dispatch:' + action)) const wrapper = mount(DispatchesView, { global: { plugins: [ElementPlus] } }) await flushPromises() const actions = wrapper.get('.el-table__body-wrapper .density-actions') expect(actions.findAll('button').map(button => button.text().trim())).toEqual(labels) expect(actions.find('.el-dropdown').exists()).toBe(overflow) }) it('loads immediately and sends only supported page filters', async () => { const wrapper = mount(DispatchesView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(listDispatches).toHaveBeenCalledWith({ page: 1, size: 10, dispatchNo: undefined, vehiclePlate: undefined, status: undefined, fbaAppointmentNo: undefined }) const model: QueryModel = { dispatchNo: 'PC-1', vehiclePlate: 'CA-100', status: 'PENDING_DEPARTURE', fbaAppointmentNo: 'FBA-1', } wrapper.findComponent(QueryToolbar).vm.$emit('search', model) await flushPromises() expect(listDispatches).toHaveBeenCalledWith({ page: 1, size: 10, dispatchNo: 'PC-1', vehiclePlate: 'CA-100', status: 'PENDING_DEPARTURE', fbaAppointmentNo: 'FBA-1', }) expect(wrapper.text()).toContain('PC-1') expect(wrapper.text()).toContain('待发车') expect(wrapper.text()).toContain('FBA-1 · 已确认') expect(wrapper.text()).toContain('已预约') }) it('keeps long dispatch identifiers as strings', async () => { vi.mocked(listDispatches).mockResolvedValueOnce({ records: [{ id: '190000000000000099', dispatchNo: '190000000000000099' }], page: 1, size: 20, total: 1, pages: 1, }) const wrapper = mount(DispatchesView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.html()).toContain('190000000000000099') }) it('opens the server-generated BOL PDF with one request', async () => { const pdfWindow = { location: { href: '' }, close: vi.fn() } vi.spyOn(window, 'open').mockReturnValue(pdfWindow as unknown as Window) vi.stubGlobal('URL', { createObjectURL: vi.fn(() => 'blob:dispatch-bol'), revokeObjectURL: vi.fn() }) const wrapper = mount(DispatchesView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(getDispatchBolPdf).not.toHaveBeenCalled() await wrapper.findAll('.el-table__body-wrapper button').find((button) => button.text().trim() === '打印BOL')!.trigger('click') await flushPromises() expect(getDispatchBolPdf).toHaveBeenCalledOnce() expect(getDispatchBolPdf).toHaveBeenCalledWith('PC-1') expect(pdfWindow.location.href).toBe('blob:dispatch-bol') }) it('documents the backend feature flag required by a missing endpoint', () => { const source = readFileSync(resolve(process.cwd(), 'src/modules/overseas-warehouse/pages/DispatchesView.vue'), 'utf8') expect(source).toContain('请确认海外尾程排车接口已发布') }) it('gates edit, cancel and transfer with independent permissions and service statuses', async () => { const source = readFileSync(resolve(process.cwd(), 'src/modules/overseas-warehouse/pages/DispatchesView.vue'), 'utf8') expect(source).toContain('permission.dispatchEdit') expect(source).toContain('permission.dispatchCancel') expect(source).toContain('permission.dispatchTransferDelivery') expect(source).toContain("import RowActions, { type RowActionItem } from '@/shared/components/RowActions.vue'") expect(source).toContain("{ key: 'transfer', label: '转派送', priority: 250") expect(source).toMatch(/function isPendingDeparture\(row: DispatchListItem\)\s*{\s*return row\.statusCode === 'PENDING_DEPARTURE'/) expect(source).toMatch(/access\.can\(permission\.dispatchEdit\)\s*&&\s*isPendingDeparture\(row\)/) expect(source).toMatch(/access\.can\(permission\.dispatchTransferDelivery\)\s*&&\s*\['PENDING_DEPARTURE', 'PICKING', 'READY_TO_DEPART'\]\.includes\(row\.statusCode\)/) expect(source).toContain('未生成出库单前可移除托盘') expect(source).toContain('removeDispatchItem') expect(source).not.toContain('托盘箱唛确认') expect(source).not.toContain('palletCartonMarks') expect(source).toContain('v-model="editForm.route" readonly') expect(source).not.toContain('route: editForm.route.trim()') expect(source).toContain('dispatchVehicleSizeOptions') expect(source).not.toContain('departDispatch') canPermission.mockReturnValue(false) const wrapper = mount(DispatchesView, { global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).not.toContain('转派送') }) })