@activity-sdk(§8.2 / ADR-004): - 基座六件接口(契约 / 交互 / 记忆 / 数据 / 工具 / LLM)—— 活动看到的全部世界 - defineActivity() 的 ABI;本地开发宿主(mock 工具表 + 模拟工位 + 录制夹具), 宿主自身就守三规矩:裸调用拒发、视图含脚本拒推、监听无时限拒登记 activities/quote-intake —— 统一报价 A1 · RFQ 需求分析(flow): - 契约七件 / 步序声明五步(LLM 抽取 → 条件跳步补全 → 查历史价 → DMN 打标 → 验收放行) - 三分类规则 P-03 落 DMN 决策表(C 优先于 A,命中行留痕) - 台账声明两对象、监听时限必配、SLA 落 Harness depcruise 实测拦下活动包越界 import(activities → apps 双向隔离), 测试遂按边界拆分:活动侧只用 @sdk/@contracts/@sfeel 358 个测试通过,门禁五道全绿 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
160 lines
6.3 KiB
TypeScript
160 lines
6.3 KiB
TypeScript
/**
|
|
* 活动包的自测 —— **只准 import @sdk / @contracts 与白名单纯库**(编码规范 §5.1 / ADR-004)。
|
|
* 安装两审那一侧的验收在 `apps/api/.../activity-registry/__tests__` —— 二者之间只有安装通道,
|
|
* 没有代码通道,depcruise 双向隔离在守着。
|
|
*/
|
|
import { describe, expect, it } from 'vitest';
|
|
import { activityManifestSchema } from '@contracts';
|
|
import { evaluate, evaluateUnaryTest } from '@sfeel';
|
|
import { createMockHost } from '@sdk';
|
|
import { manifest, P03_DMN } from '../src/manifest.ts';
|
|
import activity from '../src/index.ts';
|
|
|
|
describe('清单本身合规', () => {
|
|
it('过 @contracts 的 schema', () => {
|
|
expect(() => activityManifestSchema.parse(manifest)).not.toThrow();
|
|
});
|
|
|
|
it('监听时限已配 —— 无悬空监听(AM 4.4)', () => {
|
|
expect(manifest.declarations.listeners.every((l) => /^P/.test(l.deadline))).toBe(true);
|
|
});
|
|
|
|
it('L1/L2 判据都给了可机检的谓词 —— 验收等级不得谎报', () => {
|
|
for (const c of manifest.contract.acceptanceCriteria) {
|
|
if (c.level === 'L1' || c.level === 'L2') expect(c.predicate ?? c.modelRef).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it('禁区短而硬,授权边界给了调人预算', () => {
|
|
expect(manifest.contract.prohibitions.length).toBeGreaterThan(0);
|
|
expect(manifest.contract.authorization.humanCallBudget).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe('步序声明(AM 12.3)', () => {
|
|
const model = manifest.assets.businessModel;
|
|
if (model.paradigm !== 'flow') throw new Error('A1 应是 flow 范式');
|
|
|
|
it('末步是验收工单 —— 完成只出自放行通过', () => {
|
|
expect(model.steps.at(-1)).toMatchObject({ kind: 'human', workOrder: 'acceptance' });
|
|
});
|
|
|
|
it('每步六件齐备:步型 / 入参映射 / 出参写入 / 完成条件 / 失败策略', () => {
|
|
for (const step of model.steps) {
|
|
expect(step.name).not.toBe('');
|
|
expect(step.failure.retry.times).toBeGreaterThanOrEqual(0);
|
|
expect(step.failure.fallback.kind).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it('条件跳步是任务数据上的确定性谓词,由引擎求值', () => {
|
|
const askBack = model.steps.find((s) => s.name === 'S2_ask_back');
|
|
expect(askBack?.when).toBe('missingKeyFields > 0');
|
|
expect(evaluate(askBack!.when!, { missingKeyFields: 2 })).toBe(true);
|
|
expect(evaluate(askBack!.when!, { missingKeyFields: 0 })).toBe(false);
|
|
});
|
|
|
|
it('人类工具步都配了时限 —— 无死等', () => {
|
|
for (const step of model.steps) {
|
|
if (step.kind === 'human') expect(step.deadline).toMatch(/^P/);
|
|
}
|
|
});
|
|
|
|
it('LLM 步的模板与出参 schema 随声明版本冻结', () => {
|
|
const llm = model.steps.find((s) => s.kind === 'llm');
|
|
expect(llm).toBeDefined();
|
|
if (llm?.kind === 'llm') {
|
|
expect(llm.template).toContain('{{rfqRef}}');
|
|
expect(llm.outputSchema['type']).toBe('object');
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('三分类规则 P-03 —— 能算的不问 LLM', () => {
|
|
const table = P03_DMN.tables[0]!;
|
|
|
|
/** 按 DMN 语义逐行求值:FIRST 命中即定,单元格谓词复用 @sfeel */
|
|
const decide = (scope: Record<string, unknown>) => {
|
|
for (const rule of table.rules) {
|
|
const hit = table.inputs.every((input, i) => evaluateUnaryTest(rule.when[i] ?? '-', scope[input.expression], scope));
|
|
if (hit) return { type: rule.then[0], play: rule.then[1], rule: rule.id };
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
it('历史价 ≤90 天且行情稳 → A · 直接快答', () => {
|
|
expect(decide({ historyDays: 30, marketStable: true, lifecycle: '在产' })).toEqual({
|
|
type: 'A',
|
|
play: '直接快答',
|
|
rule: 'p03-a',
|
|
});
|
|
});
|
|
|
|
it('无历史 / 过期 / 波动 → B · 重估后报', () => {
|
|
expect(decide({ historyDays: 200, marketStable: false, lifecycle: '在产' })?.type).toBe('B');
|
|
});
|
|
|
|
it('EOL / 缺货 / 要求替代 → C · 转案处理(优先于 A)', () => {
|
|
expect(decide({ historyDays: 10, marketStable: true, lifecycle: 'EOL' })).toMatchObject({ type: 'C', rule: 'p03-c' });
|
|
});
|
|
|
|
it('同输入必同结论 —— 零方差、零 token', () => {
|
|
const scope = { historyDays: 30, marketStable: true, lifecycle: '在产' };
|
|
expect(decide(scope)).toEqual(decide(scope));
|
|
});
|
|
});
|
|
|
|
describe('代码入口 —— 零环境能力,一切经基座六件', () => {
|
|
const contract = {
|
|
activityId: 'quote-intake',
|
|
version: '0.1.0',
|
|
data: { customerCode: 'C001', rfqRef: 'mail:m-1001' },
|
|
message: undefined,
|
|
outputPorts: { mandatory: ['intake'], selective: [] },
|
|
prohibitions: [],
|
|
discretion: [],
|
|
} as const;
|
|
|
|
it('全部打标后提交放行', async () => {
|
|
const host = createMockHost({
|
|
contract,
|
|
ledger: {
|
|
'quote_intake.record': [
|
|
{ customer_code: 'C001', pn: 'LM358DR', brand: 'TI', eau: 5000, type_tag: 'A' },
|
|
{ customer_code: 'C001', pn: 'NE555', brand: 'TI', eau: 2000, type_tag: 'B' },
|
|
],
|
|
'quote_intake.type_table': [{ pn: 'LM358DR', type_tag: 'A' }],
|
|
},
|
|
});
|
|
const outcome = await host.run(activity);
|
|
expect(outcome).toMatchObject({ kind: 'submit', deliverable: 'intake-record' });
|
|
expect(host.recording.views).toHaveLength(1);
|
|
expect(host.recording.says.join()).toContain('A 1 条直通定价');
|
|
});
|
|
|
|
it('尚有未打标即等待,不提交放行', async () => {
|
|
const host = createMockHost({
|
|
contract,
|
|
ledger: { 'quote_intake.record': [{ customer_code: 'C001', pn: 'LM358DR' }], 'quote_intake.type_table': [] },
|
|
});
|
|
expect(await host.run(activity)).toMatchObject({ kind: 'await' });
|
|
});
|
|
|
|
it('视图模版含脚本时宿主拒推 —— 容器无脚本执行权', async () => {
|
|
const host = createMockHost({ contract });
|
|
await expect(host.base.interaction.pushHtml('<div><script>x()</script></div>')).rejects.toThrow(/无脚本执行权/);
|
|
});
|
|
|
|
it('裸调用被基座拒发 —— 调用必附上下文', async () => {
|
|
const host = createMockHost({ contract });
|
|
await expect(
|
|
host.base.interaction.issueWorkOrder({ mode: 'execution', title: 'x', basis: [], ask: 'y', fields: [] }),
|
|
).rejects.toThrow(/裸调用/);
|
|
});
|
|
|
|
it('监听时限必配 —— 无死等', async () => {
|
|
const host = createMockHost({ contract });
|
|
await expect(host.base.tools.listen({ eventType: 'data', keyFilter: {}, deadline: '' })).rejects.toThrow(/时限必配/);
|
|
});
|
|
});
|