/** * features/admin/e2e/shipping-audit.spec.ts * 用途:阶段 4 管理员审计中心契约冒烟——权限脱敏、Diff、关联链与哈希校验 DTO。 * 依赖:Vitest、shipping-audit.api 类型约定。 * 逻辑:断言列表不含快照;筛选含 Workflow/Item/Run/PDF/Case/Task;敏感脱敏字段存在。 */ import { describe, expect, it } from 'vitest'; import type { ShippingAuditAssociationChain, ShippingAuditDiffRow, ShippingAuditEventDetail, ShippingAuditEventSummary, ShippingAuditFilters, ShippingAuditHashVerifyResult, } from '@/features/admin/api/shipping-audit.api'; describe('shipping audit center e2e contract', () => { it('list summary must not carry snapshot bodies', () => { const summary: ShippingAuditEventSummary = { id: 1, event_type: 'candidate_promoted', aggregate_type: 'shipping_task', aggregate_id: 'ST-1', actor_type: 'user', has_snapshots: true, run_no: 'SNER-1', task_no: 'ST-1', workflow_id: 'WF-1', workflow_item_id: 'WFI-1', operation_id: 'op-1', }; // 关键断言:列表 DTO 无 before/after 快照字段,避免误渲染正文。 expect('before_snapshot' in summary).toBe(false); expect('after_snapshot' in summary).toBe(false); expect(summary.has_snapshots).toBe(true); }); it('detail diff only exposes whitelist change rows', () => { const detail: ShippingAuditEventDetail = { id: 2, event_type: 'order_extraction_run_status_changed', aggregate_type: 'order_extraction_run', aggregate_id: 'SNER-2', actor_type: 'system', has_snapshots: true, sensitive_redacted: false, before_snapshot: { status: 'queued' }, after_snapshot: { status: 'processing' }, diff: [ { key: 'status', change: 'changed', before: 'queued', after: 'processing' }, ] satisfies ShippingAuditDiffRow[], }; expect(detail.diff?.[0]?.key).toBe('status'); expect(JSON.stringify(detail)).not.toContain('email_body'); expect(JSON.stringify(detail)).not.toContain('https://'); }); it('sensitive detail can be redacted by permission', () => { const detail: ShippingAuditEventDetail = { id: 4, event_type: 'order_review_confirmed', aggregate_type: 'shipping_order_review_case', aggregate_id: 'SRC-4', actor_type: 'user', has_snapshots: true, sensitivity_level: 'sensitive', sensitive_redacted: true, before_snapshot: null, after_snapshot: null, diff: [], }; // 关键断言:无 audit_sensitive_read 时敏感正文脱敏。 expect(detail.sensitive_redacted).toBe(true); expect(detail.before_snapshot).toBeNull(); expect(detail.diff).toEqual([]); }); it('export disabled error code is stable for i18n', () => { const code = 'SHIPPING_NOTICE.AUDIT_EXPORT_FEATURE_DISABLED'; expect(code.startsWith('SHIPPING_NOTICE.')).toBe(true); }); it('audit filters carry workflow association query keys', () => { const filters: ShippingAuditFilters = { workflow_id: 'WF-1', workflow_item_id: 'WFI-1', run_no: 'SNER-1', document_id: '42', review_case_no: 'SRC-1', task_no: 'ST-1', actor_id: '9', result: 'success', }; // 关键断言:前端筛选 DTO 与 admin API _audit_filters_from_request 键对齐。 expect(Object.keys(filters).sort()).toEqual([ 'actor_id', 'document_id', 'result', 'review_case_no', 'run_no', 'task_no', 'workflow_id', 'workflow_item_id', ]); }); it('association chain and hash verify DTOs expose integrity fields', () => { const chain: ShippingAuditAssociationChain = { items: [], total: 0, association_keys: { workflow_id: ['WF-1'], task_no: ['ST-1'] }, truncated: false, limit: 500, }; const verify: ShippingAuditHashVerifyResult = { ok: true, checked_chains: 3, failed_chains: 0, failures_sample: [], mode: 'sample', }; expect(chain.association_keys.workflow_id).toEqual(['WF-1']); expect(verify.failed_chains).toBe(0); expect(verify.ok).toBe(true); }); });