143 lines
4.9 KiB
TypeScript
143 lines
4.9 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { renderDacliHelp, installHelpHook, installHelpHookWithPatterns } 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')
|
|
})
|
|
})
|
|
|
|
describe('installHelpHookWithPatterns', () => {
|
|
it('should auto-inject ANTI_PATTERNS when command name matches', () => {
|
|
const cmd = new Command('form')
|
|
const sub = cmd.command('create')
|
|
installHelpHookWithPatterns(sub, 'form create', { purpose: 'Create form' })
|
|
|
|
let captured = ''
|
|
sub.configureOutput({ writeOut: (s) => { captured += s } })
|
|
sub.outputHelp()
|
|
|
|
// Pitfalls section auto-injected from ANTI_PATTERNS via enrichHelp
|
|
expect(captured).toContain('Pitfalls')
|
|
expect(captured).toContain('❌')
|
|
// The purpose should come from Commander .description()
|
|
expect(captured).toContain('Create form')
|
|
})
|
|
})
|