diff --git a/tests/integration/design-jnpf-execute.spec.ts b/tests/integration/design-jnpf-execute.spec.ts new file mode 100644 index 0000000..4f4e030 --- /dev/null +++ b/tests/integration/design-jnpf-execute.spec.ts @@ -0,0 +1,57 @@ +/** + * design-jnpf-execute.spec.ts — dispatch 协议集成测 (spec §5 T2.1-T2.4) + * + * mock 子代理: 用 bash 跑一段预制输出, 验证主代理的"摘要校验 + 协议违约检测" + */ +import { describe, it, expect } from 'vitest' +import { execFileSync } from 'node:child_process' + +const MOCK_GOOD = 'tdss_user_ext 835168490678657989 state=1 validate=PASS BS-23=PASS/BS-31=PASS/BS-32=N/A idempotency=created keyDecision=ok' +const MOCK_FAIL_VALIDATE = 'tdss_x 123 state=0 validate=FAIL BS-23=FAIL/BS-31=PASS idempotency=created keyDecision=label缺' +const MOCK_BAD_JSON = 'tdss_user_ext\n{"formData": {"fields": [...]}}' // 吐了 formData JSON + +function mockSubagent(output: string): string { + return execFileSync('bash', ['-c', `cat <<'EOF' +${output} +EOF`], { encoding: 'utf-8' }) +} + +function checkProtocol(out: string): { lines: number; protocol: 'OK' | 'VIOLATION' } { + const lines = out.split('\n').filter(l => l.includes('state=')) + return { lines: lines.length, protocol: lines.length === 1 ? 'OK' : 'VIOLATION' } +} + +describe('dispatch protocol 校验', () => { + it('T2.1 正常输出 → 1 行, < 200 字符', () => { + const out = mockSubagent(MOCK_GOOD) + const r = checkProtocol(out) + expect(r.lines).toBe(1) + expect(out.length).toBeLessThan(200) + }) + + it('T2.2 摘要正则严格匹配', () => { + const out = mockSubagent(MOCK_GOOD) + const line = out.split('\n').find(l => l.includes('state='))! + expect(line).toMatch(/^\w+\s+\d+\s+state=[01]\s+validate=(PASS|FAIL)\s+BS-\d+=\w+\/BS-\d+=\w+/) + }) + + it('T2.3 摘要 idempotency 字段存在', () => { + const out = mockSubagent(MOCK_GOOD) + const line = out.split('\n').find(l => l.includes('state='))! + expect(line).toMatch(/idempotency=(reused|created)/) + }) + + it('T2.4 子代理 FAIL 时不返 formData 完整 JSON', () => { + const out = mockSubagent(MOCK_BAD_JSON) + expect(out).toContain('formData') // 含 formData 关键词 + const r = checkProtocol(out) + expect(r.protocol).toBe('VIOLATION') // 主代理应识别为协议违约 + }) + + it('T2.4b FAIL 但不漏 formData → 协议 OK', () => { + const out = mockSubagent(MOCK_FAIL_VALIDATE) + const r = checkProtocol(out) + expect(r.protocol).toBe('OK') + expect(out).toContain('FAIL') + }) +}) \ No newline at end of file diff --git a/vitest.config.ts b/vitest.config.ts index fa9648d..abde287 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -5,7 +5,7 @@ import { defineConfig } from 'vitest/config' export default defineConfig({ test: { - include: ['tests/unit/**/*.test.ts'], + include: ['tests/unit/**/*.test.ts', 'tests/integration/**/*.spec.ts'], exclude: ['node_modules', 'tests/e2e/**'], testTimeout: 60_000, pool: 'forks',