From c16bd298a79953590343c187531edf686cecb6ee Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 16 Jul 2026 16:26:29 +0800 Subject: [PATCH] feat(dacli): extend DacliHelp with 4 fields + add tests/core to vitest include --- src/core/help.ts | 33 +++++++++++++++++++++++++++------ tests/core/help.spec.ts | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 tests/core/help.spec.ts diff --git a/src/core/help.ts b/src/core/help.ts index fe32b79..634f6fe 100644 --- a/src/core/help.ts +++ b/src/core/help.ts @@ -7,24 +7,45 @@ */ import type { Command } from 'commander' -export interface AiHelp { +export interface DacliHelp { purpose: string required?: Record optional?: Record example?: string | string[] source?: { web?: string; api?: string; ref?: string } + + /** 反模式: ❌ 错 / ✅ 对 配对 */ + pitfalls?: Array<{ + wrong: string + right: string + severity?: 'P0' | 'P1' | 'P2' + }> + + /** 推荐做法 */ + bestPractices?: string[] + + /** 何时用 vs 何时不用 */ + decisionTree?: string + + /** 跨文件链接 */ + relatedRefs?: string[] + + /** legacy: 杂项 */ notes?: string[] } +/** Backward-compat alias during migration */ +export type AiHelp = DacliHelp + const AI_FLAGS = new Set(['--ai-help', '--example', '--source']) /** 把元数据挂到 command 上, cli.ts 顶层会读 */ -export function installAiHelpHook(cmd: Command, meta: AiHelp): void { - ;(cmd as unknown as { _aiMeta?: AiHelp })._aiMeta = meta +export function installAiHelpHook(cmd: Command, meta: DacliHelp): void { + ;(cmd as unknown as { _aiMeta?: DacliHelp })._aiMeta = meta } -function getMeta(cmd: Command): AiHelp | undefined { - return (cmd as unknown as { _aiMeta?: AiHelp })._aiMeta +function getMeta(cmd: Command): DacliHelp | undefined { + return (cmd as unknown as { _aiMeta?: DacliHelp })._aiMeta } /** @@ -65,7 +86,7 @@ export function maybeInterceptAiHelp(program: Command): boolean { process.exit(0) } -function renderAiHelp(cmd: Command, meta: AiHelp): string { +function renderAiHelp(cmd: Command, meta: DacliHelp): string { const lines: string[] = [] // 拼出完整路径 const path: string[] = [cmd.name()] diff --git a/tests/core/help.spec.ts b/tests/core/help.spec.ts new file mode 100644 index 0000000..d9079f1 --- /dev/null +++ b/tests/core/help.spec.ts @@ -0,0 +1,18 @@ +import { describe, it, expect } from 'vitest' +import type { DacliHelp } from '../../src/core/help' + +describe('DacliHelp interface', () => { + it('should accept the 4 new fields', () => { + const help: DacliHelp = { + purpose: 'test', + pitfalls: [{ wrong: 'wrong way', right: 'right way', severity: 'P0' }], + bestPractices: ['use --autoRelease'], + decisionTree: 'when A do B', + relatedRefs: ['docs/decisions/0001.md'], + } + expect(help.pitfalls).toHaveLength(1) + expect(help.bestPractices).toContain('use --autoRelease') + expect(help.decisionTree).toBe('when A do B') + expect(help.relatedRefs).toContain('docs/decisions/0001.md') + }) +})