From 394b8ce8673c8ff6f2c4309d6f04e9386b3bc732 Mon Sep 17 00:00:00 2001 From: ssw Date: Fri, 17 Jul 2026 11:46:07 +0800 Subject: [PATCH] feat(dacli): implement enrichHelp() to merge ANTI_PATTERNS into DacliHelp --- src/commands/_anti_patterns.ts | 32 +++++++++++++++++++++++++ tests/commands/anti_patterns.spec.ts | 35 +++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/commands/_anti_patterns.ts b/src/commands/_anti_patterns.ts index b180eac..2c79d4b 100644 --- a/src/commands/_anti_patterns.ts +++ b/src/commands/_anti_patterns.ts @@ -253,3 +253,35 @@ export const ANTI_PATTERNS: Record = { }, ], } + +import type { DacliHelp } from '../core/help' + +/** enrichHelp 返回类型 (DacliHelp with pitfalls/relatedRefs auto-populated) */ +export interface DacliHelpEnriched extends DacliHelp { + pitfalls?: NonNullable + relatedRefs?: NonNullable +} + +/** 把 ANTI_PATTERNS 注入到 DacliHelp */ +export function enrichHelp(fullCmd: string, local: DacliHelp): DacliHelpEnriched { + const patterns = ANTI_PATTERNS[fullCmd as CommandKey] ?? [] + + const localPitfalls = local.pitfalls ?? [] + const patternPitfalls = patterns.map(p => ({ + wrong: p.wrong, + right: p.right, + rationale: p.rationale, + severity: p.severity, + })) + const mergedPitfalls = [...localPitfalls, ...patternPitfalls] + + const localRefs = local.relatedRefs ?? [] + const patternRefs = patterns.flatMap(p => p.crossRefs ?? []) + const mergedRefs = [...new Set([...localRefs, ...patternRefs])] + + return { + ...local, + pitfalls: mergedPitfalls.length > 0 ? mergedPitfalls : undefined, + relatedRefs: mergedRefs.length > 0 ? mergedRefs : undefined, + } +} diff --git a/tests/commands/anti_patterns.spec.ts b/tests/commands/anti_patterns.spec.ts index 5416a5d..28d801b 100644 --- a/tests/commands/anti_patterns.spec.ts +++ b/tests/commands/anti_patterns.spec.ts @@ -1,6 +1,7 @@ import { describe, it, expect } from 'vitest' import type { AntiPattern } from '../../src/commands/_anti_patterns' -import { ANTI_PATTERNS } from '../../src/commands/_anti_patterns' +import { ANTI_PATTERNS, enrichHelp } from '../../src/commands/_anti_patterns' +import type { DacliHelp } from '../../src/core/help' describe('AntiPattern interface', () => { it('should accept all 6 required fields', () => { @@ -49,3 +50,35 @@ describe('ANTI_PATTERNS total count', () => { expect(uniqueIds.size).toBe(allIds.length) }) }) + +describe('enrichHelp', () => { + it('should merge ANTI_PATTERNS into DacliHelp pitfalls', () => { + const local: DacliHelp = { purpose: 'test' } + const enriched = enrichHelp('form create', local) + expect(enriched.pitfalls).toBeDefined() + expect(enriched.pitfalls!.length).toBeGreaterThanOrEqual(8) + }) + + it('should preserve local pitfalls and append ANTI_PATTERNS', () => { + const local: DacliHelp = { + purpose: 'test', + pitfalls: [{ wrong: 'local wrong', right: 'local right', severity: 'P0' }], + } + const enriched = enrichHelp('form create', local) + expect(enriched.pitfalls![0]).toEqual({ wrong: 'local wrong', right: 'local right', severity: 'P0' }) + expect(enriched.pitfalls!.length).toBeGreaterThan(1) + }) + + it('should populate relatedRefs from crossRefs', () => { + const enriched = enrichHelp('form create', { purpose: 'test' }) + expect(enriched.relatedRefs).toBeDefined() + expect(enriched.relatedRefs!.some(r => r.includes('decisions/0001'))).toBe(true) + }) + + it('should return original help if command key not in ANTI_PATTERNS', () => { + const local: DacliHelp = { purpose: 'unknown command' } + const enriched = enrichHelp('nonexistent command', local) + expect(enriched.pitfalls).toBeUndefined() + expect(enriched.purpose).toBe('unknown command') + }) +})