import ElementPlus from 'element-plus' import { flushPromises, mount } from '@vue/test-utils' import { beforeEach, describe, expect, it, vi } from 'vitest' import { ApiResponseError } from '@/shared/api/http' import { getDispatch, getDispatchEvidence } from '../api/dispatches' import DispatchDetailView from './DispatchDetailView.vue' import { detail, destination } from '../dispatchTestFixtures' import { DataTableStub } from './phase5TestStubs' const query = vi.hoisted(() => ({} as Record)) const push = vi.hoisted(() => vi.fn()) const can = vi.hoisted(() => vi.fn(() => false)) vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can }) })) vi.mock('vue-router', () => ({ useRoute: () => ({ params: { dispatchNo: 'PC/1' }, query }), useRouter: () => ({ push }) })) vi.mock('../api/dispatches', () => ({ getDispatch: vi.fn(), getDispatchEvidence: vi.fn() })) vi.mock('@/modules/overseas-master-data', async () => ({ vehicleStatusLabels: (await vi.importActual('../../overseas-master-data/presentation')).vehicleStatusLabels, inquiryReturnTarget: (value: Record) => value.fleetOfferNo ? { name: 'overseas-master-data-capacity-offer-detail', params: { offerNo: value.fleetOfferNo }, query: { inquiry: value.fleetBookingNo } } : null, })) const options = { global: { plugins: [ElementPlus], stubs: { DataTable: DataTableStub, DispatchWorkflowDialog: true } } } beforeEach(() => { vi.clearAllMocks(); vi.mocked(getDispatch).mockResolvedValue(detail()); can.mockReturnValue(false); push.mockReset(); Object.keys(query).forEach(key => delete query[key]) }) describe('whole-dispatch four-tab detail', () => { it('returns to the original appointment date and view from the dispatch detail', async () => { query.appointmentReturn = '/appointment-calendar?date=2026-10-21&mode=week' const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(wrapper.get('button.density-back').text()).toContain('返回预约日历') await wrapper.get('button.density-back').trigger('click') expect(push).toHaveBeenCalledWith('/appointment-calendar?date=2026-10-21&mode=week') wrapper.unmount() }) it('ignores an external calendar return URL and keeps the normal dispatch-list return', async () => { query.appointmentReturn = 'https://outside.invalid/appointment-calendar?date=2026-10-21' const wrapper = mount(DispatchDetailView, options); await flushPromises() await wrapper.get('button.density-back').trigger('click') expect(push).toHaveBeenCalledWith({ name: 'overseas-dispatches' }) wrapper.unmount() }) it('uses only four accessible colored tabs and switches without another network read', async () => { const value = detail(); value.dispatchInfo.fbaAppointments = [{ batchKey: 'FBA_APPOINTMENT:1', batchNo: 'BATCH-1', appointmentNo: 'FBA-AP-1', destinationKey: 'FBA:ONT8', destinationWarehouseCode: 'ONT8', status: 'CONFIRMED', confirmedAt: null, blockReason: null }] vi.mocked(getDispatch).mockResolvedValueOnce(value) const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(getDispatch).toHaveBeenCalledWith('PC/1') const tabs = wrapper.findAll('[role="tab"]') expect(tabs.map(tab => tab.text())).toEqual(['🚚车辆信息', '📋排车信息', '📝单据信息', '📦关联订单明细']) expect(wrapper.findAll('.density-tab-icon')).toHaveLength(4) await tabs[1]!.trigger('click'); await flushPromises() expect(tabs[1]!.attributes('aria-selected')).toBe('true') const dispatchInfo = wrapper.get(`#${tabs[1]!.attributes('aria-controls')}`) expect(dispatchInfo.text()).toContain('LAX → ONT8'); expect(dispatchInfo.text()).toContain('FBA预约'); expect(dispatchInfo.text()).toContain('ONT8 · FBA-AP-1 · CONFIRMED') expect(dispatchInfo.findAll('dt').map(field => field.text())).toEqual(['路线', '发车时间', '预计到达时间', 'FBA预约']) expect(getDispatch).toHaveBeenCalledTimes(1); expect(wrapper.text()).toContain('装车前'); wrapper.unmount() }) it('uses exact server order attribution including unknown measurements and real container numbers', async () => { const value = detail(); value.orderDetails.push({ ...value.orderDetails[0]!, customerId: 'C2', customerName: '客户乙', sourceKey: 'source-2', orderNo: 'LM-2', grossWeightKg: 12.25 }) value.orderDetails.forEach((row, index) => { row.sourceBusinessType = 'RELABEL_ORDER'; row.sourceBusinessNo = `RL-${index + 1}` }) vi.mocked(getDispatch).mockResolvedValueOnce(value) const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(wrapper.getComponent(DataTableStub).props('data')).toEqual(value.orderDetails) expect(wrapper.getComponent(DataTableStub).vm.$attrs.columns).toContainEqual(expect.objectContaining({ prop: 'sourceBusinessNo', label: '来源业务号' })) expect(value.orderDetails[0]!.grossWeightKg).toBeNull(); wrapper.unmount() }) it.each([ ['IN_SERVICE', '在役'], ['DISPATCHED', '外派中'], ['MAINTENANCE', '维修中'], ['INACTIVE', '停用'], ['RETURNED', '已归还'], ['FUTURE_STATUS', '—'], ['toString', '—'], [null, '—'], ])('presents vehicle status %s through the existing master-data labels or an explicit placeholder', async (status, label) => { const value = detail(); value.vehicleInfo.vehicle!.vehicleStatus = status vi.mocked(getDispatch).mockResolvedValueOnce(value) const wrapper = mount(DispatchDetailView, options); await flushPromises() const field = wrapper.findAll('.detail-fields > div').find(row => row.find('dt').text() === '车辆状态')! expect(field.get('dd').text()).toBe(label) if (status) expect(field.text()).not.toContain(status) wrapper.unmount() }) it('renders all customer delivery references and real update time/remark without leaking internal IDs', async () => { const value = detail(); value.documents.deliveries = ['DEL-A', 'DEL-B'].map((deliveryNo, index) => ({ deliveryNo, customerId: `C${index}`, customerName: `客户${index}`, outboundNo: `OUT-${index}`, groupKey: `group-${index}`, destination: destination(), status: 'WAIT_DELIVERY', packageCount: 1, palletCount: 1 })) vi.mocked(getDispatch).mockResolvedValueOnce(value) const wrapper = mount(DispatchDetailView, options); await flushPromises(); await wrapper.findAll('[role="tab"]')[2]!.trigger('click') expect(wrapper.text()).toContain('DEL-A'); expect(wrapper.text()).toContain('DEL-B'); expect(wrapper.text()).toContain('真实备注') expect(wrapper.findAll('button').some(button => button.text() === 'DEL-A')).toBe(false); wrapper.unmount() }) it('requires a literal service allowance and permission for a write action', async () => { can.mockReturnValue(true); vi.mocked(getDispatch).mockResolvedValueOnce(detail({ actions: { DEPART: { allowed: false, blockCode: 'NOT_ARRANGED', blockReason: '尚未安排派送' } } })) const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(wrapper.text()).not.toContain('尚未安排派送'); expect(wrapper.findAll('button').some(button => button.text() === '确认发车')).toBe(false); wrapper.unmount() }) it('keeps the allowed Admin action without presenting unrelated stage or PDA permission refusals as page warnings', async () => { can.mockReturnValue(true) vi.mocked(getDispatch).mockResolvedValueOnce(detail({ stage: 'AFTER_LOADING', actions: { ARRANGE_DELIVERY: { allowed: true, blockCode: null, blockReason: null }, EDIT: { allowed: false, blockCode: 'STAGE', blockReason: '当前阶段或作业事实不满足操作条件' }, PICK: { allowed: false, blockCode: 'PERMISSION', blockReason: '当前无此操作权限' }, } })) const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(wrapper.text()).toContain('装车后') expect(wrapper.text()).not.toContain('当前阶段或作业事实不满足操作条件') expect(wrapper.text()).not.toContain('当前无此操作权限') expect(wrapper.findAll('button').some(button => button.text() === '维护车辆信息')).toBe(false) await wrapper.findAll('button').find(button => button.text() === '安排派送')!.trigger('click') expect(wrapper.findComponent({ name: 'DispatchWorkflowDialog' }).props()).toMatchObject({ modelValue: true, dispatchNo: 'PC/1', action: 'ARRANGE_DELIVERY' }) wrapper.unmount() }) it('retains a real attachment A0404 failure and its request diagnostics in the vehicle tab', async () => { const value = detail(); value.vehicleInfo.vehicle!.evidenceFileIds = ['301'] vi.mocked(getDispatch).mockResolvedValueOnce(value) vi.mocked(getDispatchEvidence).mockRejectedValueOnce(new ApiResponseError('附件不存在', 'A0404', 404, undefined, 'evidence-request', 'evidence-trace')) const wrapper = mount(DispatchDetailView, options); await flushPromises() expect(getDispatchEvidence).toHaveBeenCalledWith('PC/1', 'VEHICLE', '301') expect(wrapper.text()).toContain('本趟附件操作未完成') expect(wrapper.text()).toContain('A0404') expect(wrapper.text()).toContain('evidence-request') expect(wrapper.text()).toContain('evidence-trace') expect(value.vehicleInfo.vehicle!.evidenceFileIds).toEqual(['301']) wrapper.unmount() }) it('retains a real API failure and exposes retry rather than rendering a successful empty detail', async () => { vi.mocked(getDispatch).mockRejectedValueOnce(new Error('permission denied')) const wrapper = mount(DispatchDetailView, options); await flushPromises(); expect(wrapper.text()).toContain('排车详情加载失败,请重试'); expect(wrapper.findAll('[role="tab"]')).toHaveLength(0); wrapper.unmount() }) it('keeps the original inquiry return target', async () => { Object.assign(query, { fleetOfferNo: 'FO-1', fleetBookingNo: 'FB-2' }); const wrapper = mount(DispatchDetailView, options); await flushPromises(); await wrapper.get('button.density-back').trigger('click') expect(push).toHaveBeenCalledWith({ name: 'overseas-master-data-capacity-offer-detail', params: { offerNo: 'FO-1' }, query: { inquiry: 'FB-2' } }); wrapper.unmount() }) it.each(['saved', 'rejected'] as const)('refreshes detail actions after the workflow %s event and retains a real rejection after closure', async event => { can.mockReturnValue(true) vi.mocked(getDispatch).mockResolvedValueOnce(detail({ actions: { SUBMIT_REVIEW: { allowed: true, blockCode: null, blockReason: null } } })) const wrapper = mount(DispatchDetailView, options); await flushPromises() await wrapper.findAll('button').find(button => button.text() === '提交调度审核')!.trigger('click') vi.mocked(getDispatch).mockResolvedValueOnce(detail({ recordVersion: 1, actions: { SUBMIT_REVIEW: { allowed: false, blockCode: 'ALREADY_SUBMITTED', blockReason: '已提交' } } })) const dialog = wrapper.getComponent({ name: 'DispatchWorkflowDialog' }) if (event === 'rejected') dialog.vm.$emit('rejected', { message: '本轮审核已经提交', code: 'A0409', requestId: 'detail-reject-request' }) else dialog.vm.$emit('saved') dialog.vm.$emit('update:modelValue', false) await flushPromises() expect(getDispatch).toHaveBeenCalledTimes(2) expect(wrapper.findAll('button').some(button => button.text() === '提交调度审核')).toBe(false) expect(wrapper.findAll('[role="tab"]')).toHaveLength(4) if (event === 'rejected') { expect(wrapper.text()).toContain('本轮审核已经提交') expect(wrapper.text()).toContain('A0409') expect(wrapper.text()).toContain('detail-reject-request') } expect(wrapper.text()).not.toContain('结果尚待核对') wrapper.unmount() }) it('retains a workflow rejection when the parent detail refresh also fails', async () => { const wrapper = mount(DispatchDetailView, options); await flushPromises() vi.mocked(getDispatch).mockRejectedValueOnce(new Error('detail read failed')) wrapper.getComponent({ name: 'DispatchWorkflowDialog' }).vm.$emit('rejected', { message: '已提交,请刷新', code: 'A0409' }) await flushPromises() expect(wrapper.text()).toContain('已提交,请刷新') expect(wrapper.text()).toContain('A0409') expect(wrapper.text()).toContain('排车详情加载失败') wrapper.unmount() }) it('clears an earlier input rejection after the corrected workflow succeeds', async () => { const wrapper = mount(DispatchDetailView, options); await flushPromises() const dialog = wrapper.getComponent({ name: 'DispatchWorkflowDialog' }) dialog.vm.$emit('rejected', { message: '请补齐本趟资料', code: 'A0400' }); await flushPromises() expect(wrapper.text()).toContain('请补齐本趟资料') dialog.vm.$emit('saved'); await flushPromises() expect(wrapper.text()).not.toContain('请补齐本趟资料') wrapper.unmount() }) })