Files
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

125 lines
4.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { renderDacliHelp, installHelpHook } from '../../src/core/help'
import type { DacliHelp } from '../../src/core/help'
import { Command } from 'commander'
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')
})
})
describe('renderDacliHelp', () => {
it('should render Pitfalls section when provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
pitfalls: [{ wrong: 'wrong', right: 'right', severity: 'P0' }],
})
expect(out).toContain('Pitfalls (踩过的坑)')
expect(out).toContain('❌ wrong')
expect(out).toContain('✅ right')
expect(out).toContain('[P0]')
})
it('should render Best Practices section when provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
bestPractices: ['use --autoRelease', 'read --help first'],
})
expect(out).toContain('Best Practices:')
expect(out).toContain('• use --autoRelease')
expect(out).toContain('• read --help first')
})
it('should render Decision Tree section when provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
decisionTree: 'simple → --ai; complex → --tables',
})
expect(out).toContain('Decision Tree (何时用 vs 何时不用)')
expect(out).toContain('simple → --ai')
})
it('should render Related Refs section when provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
relatedRefs: ['dacli/docs/decisions/0001-fk-relation-form-required.md'],
})
expect(out).toContain('Related Refs:')
expect(out).toContain('dacli/docs/decisions/0001-fk-relation-form-required.md')
})
it('should omit sections when fields are empty', () => {
const out = renderDacliHelp({ purpose: 'Create a form' })
expect(out).not.toContain('Pitfalls')
expect(out).not.toContain('Best Practices')
expect(out).not.toContain('Decision Tree')
expect(out).not.toContain('Related Refs')
})
})
describe('renderDacliHelp pitfalls rationale', () => {
it('should render rationale as intermediate → line when provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
pitfalls: [{
wrong: 'wrong way',
right: 'right way',
rationale: 'AI defaults to wrong',
severity: 'P0',
}],
})
expect(out).toContain('→ AI defaults to wrong')
expect(out).toContain('❌ wrong way [P0]')
expect(out).toContain('✅ right way')
})
it('should omit rationale line when not provided', () => {
const out = renderDacliHelp({
purpose: 'Create a form',
pitfalls: [{ wrong: 'wrong', right: 'right', severity: 'P0' }],
})
expect(out).not.toContain('→')
})
})
describe('installHelpHook', () => {
it('should register addHelpText on Commander command', () => {
const cmd = new Command('test')
installHelpHook(cmd, { purpose: 'test purpose' })
const helpOutput = cmd.helpInformation()
expect(helpOutput).toContain('test purpose')
})
it('should inject sections via addHelpText into actual --help output', () => {
const cmd = new Command('test')
installHelpHook(cmd, {
purpose: 'test purpose',
example: 'dacli foo --bar',
bestPractices: ['use --autoRelease'],
})
// Capture the full output via configureOutput
let captured = ''
cmd.configureOutput({ writeOut: (s) => { captured += s } })
cmd.outputHelp()
// Sections must appear in actual --help output (NOT helpInformation)
expect(captured).toContain('test purpose') // from .description()
expect(captured).toContain('Examples:')
expect(captured).toContain('dacli foo --bar')
expect(captured).toContain('Best Practices:')
expect(captured).toContain('use --autoRelease')
})
})