/** * 触发器输入装配(OS 3.1)—— 三类触发的入参从哪来,这里是分界线。 */ import { describe, expect, it } from 'vitest'; import type { TriggerConfig } from '@contracts'; import { triggerConfigSchema } from '@contracts'; import { assembleInput } from '../internal/domain/matching.ts'; const noExpr = () => undefined; const registration = (mapping: TriggerConfig['mapping'] = []): TriggerConfig => triggerConfigSchema.parse({ id: 'trg-reg', name: '人工登记起单', processPackageId: 'unified-quote', enabled: true, spec: { kind: 'registration' }, mapping, }); const event = (mapping: TriggerConfig['mapping'] = []): TriggerConfig => triggerConfigSchema.parse({ id: 'trg-evt', name: '邮件到达起单', processPackageId: 'unified-quote', enabled: true, spec: { kind: 'event', eventType: 'data', rule: 'true' }, mapping, }); describe('assembleInput', () => { it('登记触发:表单填的就是入参 —— 不声明 mapping 也能起单', () => { // 登记表单由调用接口 schema 渲染,填出来的已经是合契约的形状,不必再装配 const r = assembleInput(registration(), { customerCode: 'C001', rfqRef: 'RFQ-1' }, ['customerCode', 'rfqRef'], noExpr); expect(r).toEqual({ kind: 'ready', data: { customerCode: 'C001', rfqRef: 'RFQ-1' } }); }); it('登记触发:声明了 mapping 就覆盖在表单之上', () => { const r = assembleInput( registration([{ target: 'customerCode', source: { from: 'constant', value: 'C999' } }]), { customerCode: 'C001', rfqRef: 'RFQ-1' }, ['customerCode'], noExpr, ); expect(r.kind === 'ready' && r.data['customerCode']).toBe('C999'); }); it('事件触发:scope 不打底 —— 事件载荷与调用接口是两套字段名', () => { // 不声明 mapping 就装配不出必填字段,应入死信而不是把事件载荷整个塞给流程包 const r = assembleInput(event(), { subject: '询价', customerCode: 'C001' }, ['customerCode'], noExpr); expect(r).toMatchObject({ kind: 'partial', fields: ['customerCode'] }); }); it('事件触发:mapping 声明了才装配得出来', () => { const r = assembleInput( event([{ target: 'customerCode', source: { from: 'initiation', path: 'customerCode' } }]), { customerCode: 'C001' }, ['customerCode'], noExpr, ); expect(r).toEqual({ kind: 'ready', data: { customerCode: 'C001' } }); }); it('缺必填即 partial,列出缺哪几个 —— 但已装上的照带', () => { const r = assembleInput(registration(), { rfqRef: 'RFQ-1' }, ['customerCode', 'rfqRef'], noExpr); expect(r).toMatchObject({ kind: 'partial', fields: ['customerCode'] }); }); });