1044 lines
33 KiB
TypeScript
1044 lines
33 KiB
TypeScript
/**
|
|
* features/chat/shipping-review/useShippingOrderReview.spec.ts
|
|
* 用途:阶段 3 复核页历史版本只读、草稿版本冲突、建单资格门禁、库存候选与路由恢复写入冒烟。
|
|
* 依赖:isReviewCaseHistorical / isDraftVersionMismatchError / summarizeDraftConflictDiff /
|
|
* isBuildConfirmBlocked / lineNeedsManualInventorySelection /
|
|
* lineHasPendingRoutingSuggestion / lineNeedsInventoryRoutingRecovery、useShippingOrderReview。
|
|
* 逻辑:断言 superseded 只读、版本冲突码识别、差异摘要、confirm 门禁、inventory PUT、
|
|
* reroute/adopt/keep 与 suggestion_pending 行筛选。
|
|
*/
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { BusinessError } from '@/shared/api/client';
|
|
|
|
import {
|
|
isBuildConfirmBlocked,
|
|
isDraftVersionMismatchError,
|
|
isReviewCaseHistorical,
|
|
isReviewReadOnly,
|
|
isReviewWorkItemClosed,
|
|
lineNeedsManualInventorySelection,
|
|
lineHasPendingRoutingSuggestion,
|
|
lineNeedsInventoryRoutingRecovery,
|
|
mapReviewContextLifecycleStatus,
|
|
REVIEW_BUILD_ELIGIBILITY_BLOCKED,
|
|
|
|
REVIEW_DRAFT_VERSION_MISMATCH,
|
|
|
|
summarizeDraftConflictDiff,
|
|
|
|
useShippingOrderReview,
|
|
|
|
} from './useShippingOrderReview';
|
|
|
|
import type { ShippingOrderReviewCase } from '../types/shipping.api.types';
|
|
|
|
|
|
|
|
const api = vi.hoisted(() => ({
|
|
|
|
getCase: vi.fn(),
|
|
|
|
getRun: vi.fn(),
|
|
|
|
getEvidence: vi.fn(),
|
|
|
|
getPreview: vi.fn(),
|
|
|
|
getExtractionRun: vi.fn(),
|
|
getProcessingWorkItem: vi.fn(),
|
|
|
|
reparseDocument: vi.fn(),
|
|
|
|
putInventoryCandidates: vi.fn(),
|
|
rerouteInventoryRouting: vi.fn(),
|
|
adoptInventoryRoutingSuggestion: vi.fn(),
|
|
keepManualInventoryRouting: vi.fn(),
|
|
|
|
confirmCase: vi.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
vi.mock('../api/shipping.api', () => ({
|
|
|
|
getWorkspaceShippingOrderReviewCase: (...args: unknown[]) => Promise.resolve(api.getCase(...args)).then((value: any) => (
|
|
value && typeof value === 'object' && 'case' in value
|
|
? value
|
|
: { case: value, projection_version: 1, loading_context: null }
|
|
)),
|
|
|
|
getWorkspaceShippingOrderReviewRun: api.getRun,
|
|
|
|
getWorkspaceShippingOrderReviewEvidence: api.getEvidence,
|
|
|
|
getWorkspaceShippingOrderReviewPreview: api.getPreview,
|
|
|
|
getWorkspaceShippingOrderExtractionRun: api.getExtractionRun,
|
|
getWorkspaceShippingProcessingWorkItem: api.getProcessingWorkItem,
|
|
|
|
putWorkspaceShippingOrderReviewInventoryCandidates: api.putInventoryCandidates,
|
|
postWorkspaceShippingOrderReviewInventoryRoutingReroute: api.rerouteInventoryRouting,
|
|
postWorkspaceShippingOrderReviewInventoryRoutingAdoptSuggestion: api.adoptInventoryRoutingSuggestion,
|
|
postWorkspaceShippingOrderReviewInventoryRoutingKeepManual: api.keepManualInventoryRouting,
|
|
|
|
confirmWorkspaceShippingOrderReviewCase: api.confirmCase,
|
|
|
|
patchWorkspaceShippingOrderReviewHeaderField: vi.fn(),
|
|
|
|
patchWorkspaceShippingOrderReviewLineField: vi.fn(),
|
|
|
|
addWorkspaceShippingOrderReviewLine: vi.fn(),
|
|
|
|
copyWorkspaceShippingOrderReviewLine: vi.fn(),
|
|
|
|
deleteWorkspaceShippingOrderReviewLine: vi.fn(),
|
|
|
|
validateWorkspaceShippingOrderReviewDraft: vi.fn(),
|
|
|
|
createWorkspaceShippingOrderReviewManualDraft: vi.fn(),
|
|
|
|
markWorkspaceShippingOrderReviewNonOrder: vi.fn(),
|
|
|
|
markWorkspaceShippingOrderReviewQualityIssue: vi.fn(),
|
|
|
|
reparseWorkspaceShippingOrderReviewDocument: api.reparseDocument,
|
|
|
|
createWorkspaceShippingOrderReviewReplacementIntake: vi.fn(),
|
|
|
|
createWorkspaceShippingOrderReviewReplacementRun: vi.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const buildCase = (patch: Partial<ShippingOrderReviewCase> = {}): ShippingOrderReviewCase => ({
|
|
|
|
case_no: 'CASE-1',
|
|
|
|
run_no: 'RUN-1',
|
|
|
|
document: { id: 1, file_name: 'order.pdf' },
|
|
|
|
status: 'draft',
|
|
|
|
schema: { schema_version_id: 1, fields: [] },
|
|
|
|
draft: {
|
|
|
|
draft_version: 2,
|
|
|
|
header_values: {},
|
|
|
|
lines: [{ line_key: 'line-1' }],
|
|
|
|
issues: [],
|
|
|
|
validation: {},
|
|
|
|
build_eligibility: {
|
|
|
|
can_build: false,
|
|
|
|
status: 'needs_review',
|
|
|
|
line_projections: [{
|
|
|
|
line_key: 'line-1',
|
|
|
|
line_index: 0,
|
|
|
|
effective_reservation_id: '3000000001',
|
|
|
|
candidate_inventory_source_codes: ['DSV360_AWSDU', 'DSV360_AWSHK'],
|
|
|
|
routing_status: 'conflicted',
|
|
|
|
can_build: false,
|
|
|
|
block_reason: 'require_manual_review',
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
available_actions: ['edit_draft', 'validate', 'confirm'],
|
|
|
|
...patch,
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useShippingOrderReview historical readonly', () => {
|
|
|
|
it('forces readonly when case status is superseded', () => {
|
|
|
|
expect(isReviewCaseHistorical(
|
|
|
|
{ case_no: 'CASE-OLD', status: 'superseded' },
|
|
|
|
{ current_case_no: 'CASE-NEW' },
|
|
|
|
)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forces readonly when viewing non-current work item case', () => {
|
|
|
|
expect(isReviewCaseHistorical(
|
|
|
|
{ case_no: 'CASE-OLD', status: 'draft' },
|
|
|
|
{ current_case_no: 'CASE-NEW', version_replaced: true },
|
|
|
|
)).toBe(true);
|
|
|
|
});
|
|
|
|
it('does not treat peer PDF cases in the same run as historical', () => {
|
|
// 关键断言:同 Run 多 PDF 并列 Case,不得因 current_case_no 指向另一份而只读。
|
|
expect(isReviewCaseHistorical(
|
|
{ case_no: 'CASE-B', status: 'draft' },
|
|
{ current_case_no: 'CASE-A', version_replaced: false },
|
|
)).toBe(false);
|
|
expect(isReviewCaseHistorical(
|
|
{ case_no: 'CASE-B', status: 'ready' },
|
|
{ current_case_no: 'CASE-A' },
|
|
)).toBe(false);
|
|
});
|
|
|
|
it('allows edit when viewing current case', () => {
|
|
|
|
expect(isReviewCaseHistorical(
|
|
|
|
{ case_no: 'CASE-1', status: 'draft' },
|
|
|
|
{ current_case_no: 'CASE-1', version_replaced: false },
|
|
|
|
)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forces readonly when session work item is closed', () => {
|
|
expect(isReviewWorkItemClosed({ lifecycle_status: 'closed', current_case_no: 'CASE-1' })).toBe(true);
|
|
expect(isReviewWorkItemClosed({ lifecycle_status: 'manual_upload_pending', current_case_no: 'CASE-1' })).toBe(false);
|
|
expect(isReviewReadOnly(
|
|
{ case_no: 'CASE-1', status: 'draft' },
|
|
{ current_case_no: 'CASE-1', lifecycle_status: 'closed' },
|
|
)).toBe(true);
|
|
});
|
|
|
|
it('maps confirmed case to non-closed lifecycle for post-confirm automation', () => {
|
|
// 关键断言:confirmed 不得映射 closed,否则统一进度轨道会被误判为终态。
|
|
expect(mapReviewContextLifecycleStatus('confirmed', null)).toBe('building_order');
|
|
expect(mapReviewContextLifecycleStatus('confirmed', 'inventory_matching')).toBe('inventory_matching');
|
|
expect(mapReviewContextLifecycleStatus('closed', null)).toBe('closed');
|
|
expect(mapReviewContextLifecycleStatus('quality_issue', null)).toBe('closed');
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('draft version conflict helpers', () => {
|
|
|
|
it('recognizes REVIEW_DRAFT_VERSION_MISMATCH BusinessError', () => {
|
|
|
|
expect(isDraftVersionMismatchError(
|
|
|
|
new BusinessError({ message: 'stale', code: REVIEW_DRAFT_VERSION_MISMATCH }),
|
|
|
|
)).toBe(true);
|
|
|
|
expect(isDraftVersionMismatchError(
|
|
|
|
new BusinessError({ message: 'other', code: 'SHIPPING.REVIEW_FIELD_INVALID' }),
|
|
|
|
)).toBe(false);
|
|
|
|
expect(isDraftVersionMismatchError(new Error('nope'))).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('summarizes local vs server draft version and header values', () => {
|
|
|
|
const local = {
|
|
|
|
status: 'draft',
|
|
|
|
draft: {
|
|
|
|
draft_version: 3,
|
|
|
|
issues: [],
|
|
|
|
header_values: { values: { 'po-id': { value: 'PO-LOCAL' } } },
|
|
|
|
},
|
|
|
|
} as unknown as ShippingOrderReviewCase;
|
|
|
|
const server = {
|
|
|
|
status: 'ready',
|
|
|
|
draft: {
|
|
|
|
draft_version: 5,
|
|
|
|
issues: [{ code: 'x' }],
|
|
|
|
header_values: { values: { 'po-id': { value: 'PO-SERVER' } } },
|
|
|
|
},
|
|
|
|
} as unknown as ShippingOrderReviewCase;
|
|
|
|
const lines = summarizeDraftConflictDiff(local, server);
|
|
|
|
expect(lines.some((line) => line.includes('draft_version'))).toBe(true);
|
|
|
|
expect(lines.some((line) => line.includes('header.po-id'))).toBe(true);
|
|
|
|
expect(lines.some((line) => line.includes('status'))).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('build eligibility helpers', () => {
|
|
|
|
it('blocks confirm only when can_build is explicitly false', () => {
|
|
|
|
expect(isBuildConfirmBlocked(null)).toBe(false);
|
|
|
|
expect(isBuildConfirmBlocked(undefined)).toBe(false);
|
|
|
|
expect(isBuildConfirmBlocked({ can_build: true, status: 'ready', line_projections: [] })).toBe(false);
|
|
|
|
expect(isBuildConfirmBlocked({ can_build: false, status: 'needs_review', line_projections: [] })).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects lines needing manual inventory selection', () => {
|
|
|
|
expect(lineNeedsManualInventorySelection({ can_build: true })).toBe(false);
|
|
|
|
expect(lineNeedsManualInventorySelection({
|
|
|
|
can_build: false,
|
|
|
|
candidate_inventory_source_codes: ['DSV360_AWSDU'],
|
|
|
|
})).toBe(true);
|
|
|
|
expect(lineNeedsManualInventorySelection({
|
|
|
|
can_build: false,
|
|
|
|
block_reason: 'require_manual_review',
|
|
|
|
})).toBe(true);
|
|
|
|
expect(lineNeedsManualInventorySelection({
|
|
|
|
can_build: false,
|
|
|
|
block_reason: 'customer_unresolved',
|
|
|
|
})).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useShippingOrderReview build eligibility writes', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
api.getRun.mockResolvedValue({
|
|
|
|
run_no: 'RUN-1',
|
|
|
|
items: [{ case_no: 'CASE-1', status: 'draft', issue_count: 0 }],
|
|
|
|
});
|
|
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
|
|
api.getPreview.mockRejectedValue(new Error('preview unavailable'));
|
|
|
|
api.getProcessingWorkItem.mockRejectedValue(
|
|
Object.assign(new Error('not found'), { name: 'BusinessError', status: 404, code: 'NOT_FOUND' }),
|
|
);
|
|
|
|
api.getCase.mockResolvedValue(buildCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
vi.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('exposes buildEligibility and manualInventoryLines from active case draft', async () => {
|
|
|
|
const review = useShippingOrderReview();
|
|
|
|
await review.load('RUN-1', 'CASE-1');
|
|
|
|
expect(review.buildEligibility.value?.can_build).toBe(false);
|
|
|
|
expect(review.canConfirmBuild.value).toBe(false);
|
|
|
|
expect(review.manualInventoryLines.value).toHaveLength(1);
|
|
|
|
expect(review.manualInventoryLines.value[0]?.line_key).toBe('line-1');
|
|
|
|
});
|
|
|
|
it('does not prefetch PDF preview on load or selectCase; ensurePreviewLoaded fetches on demand', async () => {
|
|
// 关键断言:进入工作台/切换文档不触发 preview 审计与下载。
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1');
|
|
expect(api.getPreview).not.toHaveBeenCalled();
|
|
|
|
await review.selectCase('CASE-1');
|
|
expect(api.getPreview).not.toHaveBeenCalled();
|
|
|
|
const blob = new Blob(['%PDF'], { type: 'application/pdf' });
|
|
api.getPreview.mockResolvedValue(blob);
|
|
const ok = await review.ensurePreviewLoaded('CASE-1');
|
|
expect(ok).toBe(true);
|
|
expect(api.getPreview).toHaveBeenCalledTimes(1);
|
|
expect(review.previewUrl.value).toMatch(/^blob:/);
|
|
});
|
|
|
|
it('clears activeCase immediately while switching and keeps empty shell on failure (UI-01)', async () => {
|
|
// 关键断言:loading 期间不得保留旧 Case 业务对象;失败后也不得回显旧值。
|
|
let resolveCase: ((value: ShippingOrderReviewCase) => void) | null = null;
|
|
const caseOld = buildCase({ case_no: 'CASE-OLD', document: { id: 1, file_name: 'old.pdf' } });
|
|
const caseNew = buildCase({ case_no: 'CASE-NEW', document: { id: 2, file_name: 'new.pdf' } });
|
|
api.getCase.mockImplementation((caseNo: string) => {
|
|
if (caseNo === 'CASE-NEW') {
|
|
return new Promise<ShippingOrderReviewCase>((resolve) => {
|
|
resolveCase = resolve;
|
|
});
|
|
}
|
|
return Promise.resolve(caseOld);
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.selectCase('CASE-OLD');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-OLD');
|
|
|
|
const pending = review.selectCase('CASE-NEW');
|
|
expect(review.caseSwitchLoading.value).toBe(true);
|
|
// 切换期间保留已确认快照,避免整个工作台清空。
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-OLD');
|
|
expect(review.requestedCaseNo.value).toBe('CASE-NEW');
|
|
|
|
resolveCase?.(caseNew);
|
|
await pending;
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-NEW');
|
|
expect(review.caseSwitchLoading.value).toBe(false);
|
|
});
|
|
|
|
it('keeps empty skeleton when selectCase fails (UI-01 failure mode 2)', async () => {
|
|
api.getCase.mockResolvedValueOnce(buildCase({ case_no: 'CASE-OLD' }));
|
|
api.getEvidence.mockResolvedValueOnce({ items: [] });
|
|
const review = useShippingOrderReview();
|
|
await review.selectCase('CASE-OLD');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-OLD');
|
|
|
|
api.getCase.mockRejectedValueOnce(new Error('boom'));
|
|
api.getEvidence.mockRejectedValueOnce(new Error('boom'));
|
|
await review.selectCase('CASE-FAIL');
|
|
expect(review.caseSwitchFailed.value).toBe(true);
|
|
// 新请求失败时仍保留最近一次稳定 Case,避免白屏。
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-OLD');
|
|
expect(review.requestedCaseNo.value).toBe('CASE-FAIL');
|
|
});
|
|
|
|
it('discards stale selectCase responses when switching PDFs quickly', async () => {
|
|
// 关键断言:A 后发先至不得覆盖后选的 B;切换时先清空瞬态。
|
|
let resolveA: ((value: ShippingOrderReviewCase) => void) | null = null;
|
|
const caseA = buildCase({ case_no: 'CASE-A', document: { id: 1, file_name: 'a.pdf' } });
|
|
const caseB = buildCase({ case_no: 'CASE-B', document: { id: 2, file_name: 'b.pdf' } });
|
|
api.getCase.mockImplementation((caseNo: string) => {
|
|
if (caseNo === 'CASE-A') {
|
|
return new Promise<ShippingOrderReviewCase>((resolve) => {
|
|
resolveA = resolve;
|
|
});
|
|
}
|
|
return Promise.resolve(caseB);
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
|
|
const review = useShippingOrderReview();
|
|
const pendingA = review.selectCase('CASE-A');
|
|
await review.selectCase('CASE-B');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-B');
|
|
expect(review.requestedCaseNo.value).toBe('CASE-B');
|
|
|
|
resolveA?.(caseA);
|
|
await pendingA;
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-B');
|
|
});
|
|
|
|
it('submits inventory candidates via PUT and refreshes case', async () => {
|
|
|
|
const updated = buildCase({
|
|
|
|
draft: {
|
|
|
|
...buildCase().draft!,
|
|
|
|
draft_version: 3,
|
|
|
|
build_eligibility: {
|
|
|
|
can_build: true,
|
|
|
|
status: 'ready',
|
|
|
|
line_projections: [{
|
|
|
|
line_key: 'line-1',
|
|
|
|
line_index: 0,
|
|
|
|
can_build: true,
|
|
|
|
selection_mode: 'manual',
|
|
|
|
manual_reason: 'Confirmed with planner',
|
|
|
|
candidate_inventory_source_codes: ['DSV360_AWSDU'],
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
api.putInventoryCandidates.mockResolvedValue(updated);
|
|
|
|
|
|
|
|
const review = useShippingOrderReview();
|
|
|
|
await review.load('RUN-1', 'CASE-1');
|
|
|
|
await review.setLineInventoryCandidates('line-1', ['DSV360_AWSDU'], 'Confirmed with planner');
|
|
|
|
|
|
|
|
expect(api.putInventoryCandidates).toHaveBeenCalledWith('CASE-1', 'line-1', {
|
|
|
|
expected_draft_version: 2,
|
|
|
|
source_codes: ['DSV360_AWSDU'],
|
|
|
|
reason: 'Confirmed with planner',
|
|
|
|
});
|
|
|
|
expect(review.activeCase.value?.draft?.draft_version).toBe(3);
|
|
|
|
expect(review.buildEligibility.value?.can_build).toBe(true);
|
|
|
|
expect(review.canConfirmBuild.value).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('blocks confirm when build eligibility cannot build', async () => {
|
|
|
|
const review = useShippingOrderReview();
|
|
|
|
await review.load('RUN-1', 'CASE-1');
|
|
|
|
|
|
|
|
await expect(review.confirm()).rejects.toMatchObject({
|
|
|
|
code: REVIEW_BUILD_ELIGIBILITY_BLOCKED,
|
|
|
|
});
|
|
|
|
expect(api.confirmCase).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
it('starts workflow progress polling after successful confirm', async () => {
|
|
vi.useFakeTimers();
|
|
const readyCase: ShippingOrderReviewCase = {
|
|
case_no: 'CASE-1',
|
|
run_no: 'RUN-1',
|
|
document: { id: 1, file_name: 'a.pdf' },
|
|
status: 'ready',
|
|
schema: { schema_version_id: 1, fields: [] },
|
|
draft: {
|
|
draft_version: 2,
|
|
header_values: { values: {} },
|
|
lines: [],
|
|
issues: [],
|
|
validation: {},
|
|
build_eligibility: {
|
|
can_build: true,
|
|
status: 'ready',
|
|
line_projections: [],
|
|
},
|
|
},
|
|
available_actions: ['edit_draft', 'confirm'],
|
|
};
|
|
api.getCase.mockResolvedValue(readyCase);
|
|
api.getRun.mockResolvedValue({
|
|
run_no: 'RUN-1',
|
|
summary: {},
|
|
items: [{ case_no: 'CASE-1', document_id: 1, status: 'ready', available_actions: ['confirm'] }],
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
api.getPreview.mockResolvedValue({ preview_url: '' });
|
|
api.getProcessingWorkItem.mockResolvedValueOnce({
|
|
work_item_no: 'OT-1',
|
|
status: 'building_order',
|
|
});
|
|
api.getProcessingWorkItem.mockResolvedValueOnce({
|
|
work_item_no: 'OT-1',
|
|
status: 'completed',
|
|
});
|
|
api.confirmCase.mockResolvedValue({
|
|
task_no: 'SN-1',
|
|
order_task_no: 'OT-1',
|
|
shipping_notice_no: 'SN-1',
|
|
idempotent_replay: false,
|
|
workflow_id: 'WF-1',
|
|
order_task_no: 'OT-1',
|
|
shipping_notice_no: 'SN-1',
|
|
case: { ...readyCase, status: 'confirmed', order_task_no: 'OT-1' },
|
|
auto_progress_started: true,
|
|
});
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'WFI-1' });
|
|
const response = await review.confirm();
|
|
|
|
expect(response?.order_task_no).toBe('OT-1');
|
|
expect(response?.shipping_notice_no || response?.task_no).toBeTruthy();
|
|
expect(review.reviewProgressContext.value?.user_task_no).toBe('OT-1');
|
|
expect(review.showWorkflowProgress.value).toBe(true);
|
|
expect(review.workflowProgressPolling.value).toBe(true);
|
|
expect(api.getProcessingWorkItem).toHaveBeenCalled();
|
|
|
|
await vi.advanceTimersByTimeAsync(2600);
|
|
await Promise.resolve();
|
|
expect(review.workflowProgressPolling.value).toBe(false);
|
|
|
|
review.stopWorkflowProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('notifies the conversation workflow when auto progress reaches a retryable terminal state', async () => {
|
|
vi.useFakeTimers();
|
|
const onWorkflowRefresh = vi.fn();
|
|
const readyCase: ShippingOrderReviewCase = {
|
|
case_no: 'CASE-1', run_no: 'RUN-1', document: { id: 1, file_name: 'a.pdf' }, status: 'ready',
|
|
schema: { schema_version_id: 1, fields: [] },
|
|
draft: { draft_version: 2, header_values: { values: {} }, lines: [], issues: [], validation: {}, build_eligibility: { can_build: true, status: 'ready', line_projections: [] } },
|
|
available_actions: ['edit_draft', 'confirm'],
|
|
};
|
|
api.getCase.mockResolvedValue(readyCase);
|
|
api.getRun.mockResolvedValue({ run_no: 'RUN-1', summary: {}, items: [{ case_no: 'CASE-1', document_id: 1, status: 'ready', available_actions: ['confirm'] }] });
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
api.getPreview.mockResolvedValue({ preview_url: '' });
|
|
api.getProcessingWorkItem
|
|
.mockResolvedValueOnce({ work_item_no: 'OT-1', status: 'building_order' })
|
|
.mockResolvedValueOnce({ work_item_no: 'OT-1', status: 'retryable', retryable: true });
|
|
api.confirmCase.mockResolvedValue({
|
|
task_no: 'SN-1', order_task_no: 'OT-1', shipping_notice_no: 'SN-1', idempotent_replay: false,
|
|
workflow_id: 'WF-1', case: { ...readyCase, status: 'confirmed', order_task_no: 'OT-1' }, auto_progress_started: true,
|
|
});
|
|
|
|
const review = useShippingOrderReview({ onWorkflowRefresh });
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'WFI-1' });
|
|
await review.confirm();
|
|
await vi.advanceTimersByTimeAsync(2600);
|
|
await Promise.resolve();
|
|
|
|
expect(onWorkflowRefresh).toHaveBeenCalled();
|
|
expect(review.workflowProgressPolling.value).toBe(false);
|
|
review.stopWorkflowProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('does not fall back to extraction run root OT when case has candidate binding', async () => {
|
|
api.getCase.mockResolvedValue(buildCase({
|
|
candidate_id: 42,
|
|
candidate: { candidate_no: 'CAND-42', validation_status: 'ready' },
|
|
order_task_no: 'OT-CAND',
|
|
}));
|
|
api.getRun.mockResolvedValue({
|
|
run_no: 'RUN-1',
|
|
summary: {},
|
|
items: [{ case_no: 'CASE-1', document_id: 1, status: 'draft', available_actions: ['edit_draft'] }],
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1' });
|
|
|
|
expect(review.reviewProgressContext.value?.user_task_no).toBe('OT-CAND');
|
|
expect(review.reviewProgressContext.value?.user_task_authoritative).toBe(true);
|
|
});
|
|
|
|
it('leaves OT unset during reparse polling when candidate-bound case lacks order_task_no', async () => {
|
|
vi.useFakeTimers();
|
|
api.getCase.mockResolvedValue(buildCase({
|
|
candidate_id: 42,
|
|
candidate: { candidate_no: 'CAND-42', validation_status: 'ready' },
|
|
}));
|
|
api.getRun.mockResolvedValue({
|
|
run_no: 'RUN-1',
|
|
summary: {},
|
|
items: [{ case_no: 'CASE-1', document_id: 1, status: 'reparse_pending', available_actions: [] }],
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
api.reparseDocument.mockResolvedValue({
|
|
run: { run_no: 'RUN-2' },
|
|
reused: false,
|
|
});
|
|
api.getExtractionRun.mockResolvedValue({
|
|
run_no: 'RUN-2',
|
|
status: 'extracting',
|
|
user_task_no: 'OT-ROOT',
|
|
current_activity: { user_visible_stage: 'extracting' },
|
|
});
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1' });
|
|
await review.reparseDocument('layout_issue');
|
|
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
await Promise.resolve();
|
|
|
|
expect(review.reviewProgressContext.value?.user_task_no).toBeNull();
|
|
expect(review.reviewProgressContext.value?.user_task_authoritative).toBe(false);
|
|
|
|
review.stopReparseProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('polls reparse progress and loads the new review version when ready', async () => {
|
|
vi.useFakeTimers();
|
|
let reparsePollCount = 0;
|
|
api.reparseDocument.mockImplementation(async () => ({
|
|
run: { run_no: 'RUN-2', items: [], summary: {} },
|
|
reused: false,
|
|
}));
|
|
api.getRun.mockImplementation(async (runNo: string) => ({
|
|
run_no: runNo,
|
|
status: runNo === 'RUN-2' ? 'review_required' : 'review_required',
|
|
summary: {},
|
|
items: [{ case_no: runNo === 'RUN-2' ? 'CASE-2' : 'CASE-1', status: 'draft', issue_count: 0 }],
|
|
}));
|
|
api.getExtractionRun.mockImplementation(async (runNo: string) => {
|
|
reparsePollCount += 1;
|
|
const ready = runNo === 'RUN-2' && reparsePollCount >= 2;
|
|
return {
|
|
run_no: runNo,
|
|
status: ready ? 'review_required' : 'extracting',
|
|
current_activity: { user_visible_stage: ready ? 'review_required' : 'extracting' },
|
|
};
|
|
});
|
|
api.getCase.mockImplementation(async (caseNo: string) => buildCase({
|
|
case_no: caseNo,
|
|
run_no: caseNo === 'CASE-2' ? 'RUN-2' : 'RUN-1',
|
|
}));
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'SWI-1' });
|
|
await review.reparseDocument('document_quality');
|
|
|
|
expect(review.reparseProgressActive.value).toBe(true);
|
|
await vi.advanceTimersByTimeAsync(2600);
|
|
await Promise.resolve();
|
|
|
|
expect(review.reparseProgressStage.value).toBe('draft_ready');
|
|
expect(review.reparseProgressPolling.value).toBe(false);
|
|
expect(review.reviewRun.value?.run_no).toBe('RUN-2');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-2');
|
|
review.stopReparseProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('loads a partially completed reparse result and clears the read-only progress state', async () => {
|
|
vi.useFakeTimers();
|
|
api.reparseDocument.mockImplementation(async () => ({
|
|
run: { run_no: 'RUN-PARTIAL', items: [], summary: {} },
|
|
reused: false,
|
|
}));
|
|
api.getRun.mockImplementation(async (runNo: string) => ({
|
|
run_no: runNo,
|
|
status: runNo === 'RUN-PARTIAL' ? 'partially_completed' : 'review_required',
|
|
summary: {},
|
|
items: [{ case_no: runNo === 'RUN-PARTIAL' ? 'CASE-PARTIAL' : 'CASE-1', status: 'draft', issue_count: 0 }],
|
|
}));
|
|
api.getExtractionRun.mockImplementation(async (runNo: string) => ({
|
|
run_no: runNo,
|
|
status: runNo === 'RUN-PARTIAL' ? 'partially_completed' : 'review_required',
|
|
current_activity: { user_visible_stage: 'review_required' },
|
|
}));
|
|
api.getCase.mockImplementation(async (caseNo: string) => buildCase({
|
|
case_no: caseNo,
|
|
run_no: caseNo === 'CASE-PARTIAL' ? 'RUN-PARTIAL' : 'RUN-1',
|
|
}));
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'SWI-1' });
|
|
await review.reparseDocument('document_quality');
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
await Promise.resolve();
|
|
|
|
expect(review.reviewRun.value?.run_no).toBe('RUN-PARTIAL');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-PARTIAL');
|
|
expect(review.reparseProgressStage.value).toBe('draft_ready');
|
|
expect(review.reparseProgressPolling.value).toBe(false);
|
|
expect(review.reparseProgressActive.value).toBe(false);
|
|
expect(review.isReadOnly.value).toBe(false);
|
|
expect(review.statusMessage.value).not.toBe('reparse_in_progress');
|
|
review.stopReparseProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('resolves the replacement run when superseded_by_run_no is set', async () => {
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'SWI-1' });
|
|
review.reviewRun.value!.superseded_by_run_no = 'RUN-2';
|
|
api.getRun.mockResolvedValue({
|
|
run_no: 'RUN-2', summary: {}, items: [{ case_no: 'CASE-2', status: 'draft', issue_count: 0 }],
|
|
});
|
|
api.getCase.mockResolvedValue(buildCase({ case_no: 'CASE-2', run_no: 'RUN-2' }));
|
|
|
|
const target = await review.openCurrentVersion();
|
|
|
|
expect(target).toEqual({ runNo: 'RUN-2', caseNo: 'CASE-2' });
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-2');
|
|
});
|
|
|
|
it('resolves the replacement run when the historical item still points at the open case', async () => {
|
|
api.getRun.mockImplementation(async (runNo: string) => runNo === 'RUN-1'
|
|
? {
|
|
run_no: 'RUN-1',
|
|
summary: {},
|
|
items: [{ case_no: 'CASE-1', status: 'superseded', issue_count: 0 }],
|
|
}
|
|
: {
|
|
run_no: 'RUN-2',
|
|
summary: {},
|
|
items: [{ case_no: 'CASE-2', status: 'draft', issue_count: 0 }],
|
|
});
|
|
api.getCase.mockImplementation(async (caseNo: string) => buildCase({
|
|
case_no: caseNo,
|
|
run_no: caseNo === 'CASE-2' ? 'RUN-2' : 'RUN-1',
|
|
status: caseNo === 'CASE-2' ? 'draft' : 'superseded',
|
|
}));
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'SWI-1' });
|
|
review.reviewRun.value!.superseded_by_run_no = 'RUN-2';
|
|
const target = await review.openCurrentVersion();
|
|
|
|
expect(target).toEqual({ runNo: 'RUN-2', caseNo: 'CASE-2' });
|
|
expect(review.reviewRun.value?.run_no).toBe('RUN-2');
|
|
expect(review.activeCase.value?.case_no).toBe('CASE-2');
|
|
});
|
|
|
|
it('shows a terminal failed stage when reparse fails', async () => {
|
|
vi.useFakeTimers();
|
|
api.reparseDocument.mockResolvedValue({
|
|
run: { run_no: 'RUN-FAILED', items: [], summary: {} }, reused: false,
|
|
});
|
|
api.getExtractionRun.mockResolvedValue({ run_no: 'RUN-FAILED', status: 'failed' });
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1', { conversationId: 'conv-1', itemNo: 'SWI-1' });
|
|
await review.reparseDocument('document_quality');
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
await Promise.resolve();
|
|
await vi.advanceTimersByTimeAsync(2600);
|
|
await Promise.resolve();
|
|
|
|
expect(review.reparseProgressStage.value).toBe('failed');
|
|
expect(review.reparseProgressPolling.value).toBe(false);
|
|
review.stopReparseProgressPolling();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
});
|
|
|
|
|
|
describe('inventory routing recovery', () => {
|
|
beforeEach(() => {
|
|
api.getRun.mockResolvedValue({
|
|
run_no: 'RUN-1',
|
|
summary: {},
|
|
items: [{
|
|
case_no: 'CASE-1',
|
|
document_id: 1,
|
|
file_name: 'order.pdf',
|
|
status: 'draft',
|
|
draft_version: 2,
|
|
issue_count: 0,
|
|
available_actions: ['edit_draft', 'validate', 'confirm'],
|
|
}],
|
|
});
|
|
api.getEvidence.mockResolvedValue({ items: [] });
|
|
api.getPreview.mockResolvedValue(new Blob());
|
|
api.getProcessingWorkItem.mockRejectedValue(
|
|
Object.assign(new Error('not found'), { name: 'BusinessError', status: 404, code: 'NOT_FOUND' }),
|
|
);
|
|
api.getCase.mockResolvedValue(buildCase());
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('detects pending suggestion and recovery lines beyond manual selection', () => {
|
|
// 关键断言:suggestion_pending 即使 can_build true 也应进入恢复集合。
|
|
expect(lineHasPendingRoutingSuggestion({ suggestion_pending: true })).toBe(true);
|
|
expect(lineNeedsInventoryRoutingRecovery({
|
|
can_build: true,
|
|
suggestion_pending: true,
|
|
})).toBe(true);
|
|
expect(lineNeedsInventoryRoutingRecovery({
|
|
can_build: true,
|
|
unavailable_source_codes: ['OFFLINE_SRC'],
|
|
})).toBe(true);
|
|
expect(lineNeedsManualInventorySelection({
|
|
can_build: true,
|
|
suggestion_pending: true,
|
|
})).toBe(false);
|
|
});
|
|
|
|
it('exposes routingRecoveryLines for suggestion_pending rows', async () => {
|
|
api.getCase.mockResolvedValue(buildCase({
|
|
draft: {
|
|
...buildCase().draft!,
|
|
build_eligibility: {
|
|
can_build: true,
|
|
status: 'ready',
|
|
line_projections: [{
|
|
line_key: 'line-pending',
|
|
line_index: 0,
|
|
can_build: true,
|
|
suggestion_pending: true,
|
|
rule_suggestion_source_codes: ['DSV360_AWSDU'],
|
|
candidate_inventory_source_codes: ['DSV360_AWSHK'],
|
|
selection_mode: 'manual',
|
|
}],
|
|
},
|
|
},
|
|
}));
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1');
|
|
expect(review.manualInventoryLines.value).toHaveLength(0);
|
|
expect(review.routingRecoveryLines.value).toHaveLength(1);
|
|
expect(review.routingRecoveryLines.value[0]?.line_key).toBe('line-pending');
|
|
});
|
|
|
|
it('reroutes inventory routing with idempotency_key and expected_draft_version', async () => {
|
|
const updated = buildCase({ draft: { ...buildCase().draft!, draft_version: 3 } });
|
|
api.rerouteInventoryRouting.mockResolvedValue(updated);
|
|
const uuidSpy = vi.spyOn(crypto, 'randomUUID').mockReturnValue('idem-reroute-1');
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1');
|
|
await review.rerouteInventoryRouting('line-1');
|
|
|
|
expect(api.rerouteInventoryRouting).toHaveBeenCalledWith('CASE-1', {
|
|
expected_draft_version: 2,
|
|
idempotency_key: 'idem-reroute-1',
|
|
line_key: 'line-1',
|
|
});
|
|
expect(review.activeCase.value?.draft?.draft_version).toBe(3);
|
|
uuidSpy.mockRestore();
|
|
});
|
|
|
|
it('adopts and keeps inventory routing suggestions via POST APIs', async () => {
|
|
const updated = buildCase({ draft: { ...buildCase().draft!, draft_version: 4 } });
|
|
api.adoptInventoryRoutingSuggestion.mockResolvedValue(updated);
|
|
api.keepManualInventoryRouting.mockResolvedValue(updated);
|
|
const uuidSpy = vi.spyOn(crypto, 'randomUUID')
|
|
.mockReturnValueOnce('idem-adopt-1')
|
|
.mockReturnValueOnce('idem-keep-1');
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1');
|
|
await review.adoptInventoryRoutingSuggestion('line-1', 'use rule');
|
|
await review.keepManualInventoryRouting('line-1', 'keep manual');
|
|
|
|
expect(api.adoptInventoryRoutingSuggestion).toHaveBeenCalledWith('CASE-1', 'line-1', {
|
|
expected_draft_version: 2,
|
|
idempotency_key: 'idem-adopt-1',
|
|
reason: 'use rule',
|
|
});
|
|
expect(api.keepManualInventoryRouting).toHaveBeenCalledWith('CASE-1', 'line-1', {
|
|
expected_draft_version: 4,
|
|
idempotency_key: 'idem-keep-1',
|
|
reason: 'keep manual',
|
|
});
|
|
uuidSpy.mockRestore();
|
|
});
|
|
|
|
it('sets draftConflict on 409 VERSION_MISMATCH during reroute', async () => {
|
|
api.rerouteInventoryRouting.mockRejectedValue(
|
|
new BusinessError({ message: 'stale', code: REVIEW_DRAFT_VERSION_MISMATCH }),
|
|
);
|
|
api.getCase
|
|
.mockResolvedValueOnce(buildCase())
|
|
.mockResolvedValueOnce(buildCase({
|
|
draft: { ...buildCase().draft!, draft_version: 9 },
|
|
}));
|
|
|
|
const review = useShippingOrderReview();
|
|
await review.load('RUN-1', 'CASE-1');
|
|
await expect(review.rerouteInventoryRouting('line-1')).rejects.toMatchObject({
|
|
code: REVIEW_DRAFT_VERSION_MISMATCH,
|
|
});
|
|
expect(review.draftConflict.value).toMatchObject({
|
|
localVersion: 2,
|
|
serverVersion: 9,
|
|
});
|
|
expect(review.saveFailed.value).toBe(true);
|
|
});
|
|
});
|