feat(dacli): implement enrichHelp() to merge ANTI_PATTERNS into DacliHelp

This commit is contained in:
ssw committed 2026-07-17 11:46:07 +08:00
1 parent 4c114229a8
commit 394b8ce867
2 files changed
+66 -1

No files matched your search

+32
View File
@@ -253,3 +253,35 @@ export const ANTI_PATTERNS: Record<CommandKey, AntiPattern[]> = {
},
],
}
import type { DacliHelp } from '../core/help'
/** enrichHelp 返回类型 (DacliHelp with pitfalls/relatedRefs auto-populated) */
export interface DacliHelpEnriched extends DacliHelp {
pitfalls?: NonNullable<DacliHelp['pitfalls']>
relatedRefs?: NonNullable<DacliHelp['relatedRefs']>
}
/** 把 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,
}
}
+34 -1
View File
@@ -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')
})
})