Files
JX-BPOS/apps/api/src/modules/process/__tests__/start-guard.test.ts
T
ChaiHaoandClaude Opus 5 c9ac058bdf 三面拆分⑯:流程包详情改三栏 · 字段只声明 · 版本可删可改名
**字段只声明,不做任何要求**(用户定):编排层不再碰字段 ——
  · 激活闸只剩两面:结构闭合(dangling-node / multi-default-edge)与活动装载
    (unknown-activity);字段类的检查(同名不同型、容忍分支被必填依赖)全部退役。
  · 节点属性卡的「数据接口」从「推来源」改成**纯声明**:照活动契约列出吃什么、吐什么,
    不标必填、不推来源、不提补全 —— 缺了怎么补是**活动内实现**的事,不在这一层现形。
  · 字段类型支持数组:统一写成 `T[]`(如 `any[]`),出口口的字段也带上类型
    (原来只有字段名)。实测 A2 的 benchmark 口显示「rows any[] · needSourcing boolean」。

**详情页改三栏**:左窄轨版本(132px) | 中流程图 | 右节点属性。
  · 版本条一版一行(v几 · 状态 · 点/边计数),动作只在**看着的那一版**上展开 ——
    窄栏放不下每行一排钮,也不该让人误点别版。
  · 删掉「激活 · 双判据」整卡:没选节点时,属性栏就地列出还差哪几条(点一条定位过去);
    两面都齐时盖一枚小检验章,不再占一整张卡。
  · 删掉节点拣选行 —— 直接在图上点节点。
  · 属性卡用包壳绝对定位:行高由中栏的图定,属性长了在卡内滚,不把图拉长
    (实测卡高 467、明细内容 1039、可滚,图框不受影响)。窄轨里属性小卡改单列竖排。
  · 顺带校正:上一轮那条「图框下沿对齐左列底栏顶线」的 20px 留白,
    三栏改版后左栏已无底栏,没有对齐对象了 —— 改回四边等距(18px)。

**版本可删可改名**(新):
  · 删:生效版不可删(先停用,ActiveVersionError → 409)、在途钉版不可删
    (那些单要按它跑完,PinnedVersionError → 409);删掉正看的那版会落到剩下的最新版,
    一版不剩就回货架。
  · 改名:名字是**这条流的身份**,一次改及于所有版本 —— 版本列表上只显示 v1/v2…,
    若只改一版的名,列表上什么也看不见,人会以为没生效。
  · 5 条单测守两道闸与改名口径;实测走查:改名落库 → 改回;生效版删 409、停用版删 200。

process CONTRACT.md 与产品设计.md 同步。五闸绿。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
2026-07-29 16:14:54 +08:00

122 lines
4.8 KiB
TypeScript

/**
* 起单守卫:不合调用接口契约的输入,**一条工作流都不留**。
*
* 用例层测试 —— 端口给的是**真实实现**(内存版),不 mock 内部机制;
* 类型一律走 schema 解析而来的真值,不用断言绕过(§12.5#4)。
*/
import { describe, expect, it } from 'vitest';
import { processPackageSchema } from '@contracts';
import type { ProcessPackage } from '@contracts';
import { createProcessService } from '../internal/usecase/process-service.ts';
import type { ProcessServiceDeps } from '../internal/usecase/process-service.ts';
import type { WorkflowRecord } from '../port.ts';
import type { NodeInstance } from '../internal/domain/owner-resolution.ts';
/** 最小可发布包:一个开始、一个结束、一条边 —— 起单守卫不需要更复杂的图 */
const PKG: ProcessPackage = processPackageSchema.parse({
id: 'p1',
name: '测试包',
version: 1,
state: 'active',
callInterface: {
input: { type: 'object', required: ['customerCode'], properties: { customerCode: { type: 'string' } } },
output: { type: 'object' },
},
nodes: [
{ kind: 'start', id: 'S', label: '开始' },
{ kind: 'end', id: 'E', label: '结束' },
],
edges: [{ id: 'e1', from: 'S', to: 'E', mapping: [], reopen: false }],
variables: [],
});
type Harness = {
readonly service: ReturnType<typeof createProcessService>;
readonly inserted: WorkflowRecord[];
readonly published: unknown[];
};
function harness(validationErrors: readonly string[]): Harness {
const inserted: WorkflowRecord[] = [];
const published: unknown[] = [];
const instances: NodeInstance[] = [];
const deps: ProcessServiceDeps = {
store: {
savePackage: async () => undefined,
updatePackageDefinition: async () => undefined,
countPinnedInFlight: async () => 0,
setPackageState: async () => undefined,
getPackage: async () => PKG,
listPackages: async () => [PKG],
insertWorkflow: async (record) => {
inserted.push(record);
},
getWorkflow: async (id) => inserted.find((w) => w.id === id),
listWorkflows: async () => inserted,
// 内存版要**真的落笔**:用例读回它自己刚写的状态,假实现敷衍就测不出真行为
updateWorkflow: async (id, patch) => {
const at = inserted.findIndex((w) => w.id === id);
if (at < 0) throw new Error(`测试:工作流不存在 ${id}`);
const next = { ...inserted[at]!, ...patch };
inserted[at] = next;
return next;
},
listInstances: async () => instances,
insertInstance: async (_workflowId, instance) => {
instances.push(instance);
},
updateInstance: async () => undefined,
findInstanceByTask: async () => undefined,
deletePackage: async () => undefined,
renameFlow: async () => undefined,
nodeOwners: async () => ({}),
setNodeOwner: async () => undefined,
},
activities: { faces: async () => new Map() },
org: { positions: async () => new Set<string>(), candidates: async () => [], workGroupBinding: async () => undefined, isActive: async () => true },
tasks: { start: async () => ({ id: 'task-1' }) },
approvals: { raise: async () => ({ id: 'apr-1' }) },
sfeel: { condition: () => true, expression: () => undefined },
schemas: {
validate: () => ({ ok: validationErrors.length === 0, errors: [...validationErrors] }),
},
events: {
publish: async (input) => {
published.push(input);
},
},
receipts: { notify: async () => undefined },
clock: { now: () => new Date('2026-07-27T00:00:00.000Z') },
ids: { next: () => 'wf-1' },
};
return { service: createProcessService(deps), inserted, published };
}
describe('起单:宽进 —— 输入可以不定或为空', () => {
const input = {
packageId: 'p1',
subject: '测试单',
source: { kind: 'registration' as const, ref: 'p1', by: 's1' },
data: {},
};
it('宽进(2026-07-30):入参缺项**照样建单** —— 收敛点在活动,不在门口', async () => {
const { service, inserted } = harness(["must have required property 'customerCode'"]);
const record = await service.start(input);
expect(record.id).toBeDefined();
expect(inserted).toHaveLength(1);
// 缺的字段就是缺着 —— 进活动后受理归一,信息不全由任务侧派补全单收齐
});
it('入参合契约 → 正常建单,来源记在单上', async () => {
const { service, inserted } = harness([]);
const record = await service.start({ ...input, data: { customerCode: 'ACME' } });
expect(record.id).toBe('wf-1');
expect(inserted).toHaveLength(1);
// 凡单必有来源:登记起单的来源就是登记人
expect(inserted[0]?.source).toEqual({ kind: 'registration', ref: 'p1', by: 's1' });
});
});