Files
dacli/tests/unit/auto_fix.test.ts
T
ssw 508cabface feat(dacli): v2.1+ 命令输出 + auto-fix helper (含 workflow G-X1 扩展)
src/commands/_output_filter.ts (新): 跨命令输出过滤 + CommandLog 落盘
  - setupOutputFilter: 默认 quiet, --verbose/--json 解除
  - addOutputOptions: 注册 --verbose/--json
  - printCommandEnd: 落 L3 CommandLog + 打印 L1 summary + 'dacli log last' 提示
  - 三档 result: success / partial / failure

src/commands/_auto_fix.ts (改): workflow enCode 也参与 G-X1 BS-47 重名查重
  - CommandFixContext 新增 existingWorkflows 字段
  - fetchExistingLists 加 workflows flag + GET /workflow/template 调用
  - fixUniquenessSuggest 把 workflows 加入 allExisting 冲突检查

tests/unit/output_filter.test.ts (新): 11 个 helper 单元测试

tests/unit/auto_fix.test.ts (新): 23 baseline + 3 本 session workflow G-X1 测试
2026-07-21 13:00:16 +08:00

250 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// dacli/tests/unit/auto_fix.test.ts
/**
* commands/_auto_fix.ts 跨命令 auto-fix pipeline 单元测试 (2026-07-21 固化)
*
* 覆盖:
* - S-X1 enCode strip silent (BS-新38)
* - S-X2 fullName soften silent (BS-新52)
* - G-X1 BS-47 重名 suggest (含 <appCode> 前缀建议)
* - fieldAliases 映射 (dm/table, user/account, bigscreen 等)
* - runCommandAutoFix 端到端组合
*
* 详见 dacli/docs/decisions/0010-dacli-v2-self-correcting-cli.md §3 + ADR-0010 §3
*/
import { describe, it, expect } from 'vitest'
import { runCommandAutoFix } from '../../src/commands/_auto_fix.ts'
describe('S-X1 enCode strip silent (BS-新38)', () => {
it('enCode 含 `-` → 自动 strip + silent FixEntry', () => {
const result = runCommandAutoFix({ enCode: 'test-strip-flow' })
expect(result.opts.enCode).toBe('teststripflow')
expect(result.silent).toHaveLength(1)
expect(result.silent[0].id).toBe('S-X1')
expect(result.silent[0].level).toBe('silent')
expect(result.silent[0].from).toBe('test-strip-flow')
expect(result.silent[0].to).toBe('teststripflow')
expect(result.silent[0].summary).toContain('BS-新38')
})
it('enCode 含 `_` → 自动 strip', () => {
const result = runCommandAutoFix({ enCode: 'with_underscore' })
expect(result.opts.enCode).toBe('withunderscore')
expect(result.silent).toHaveLength(1)
})
it('enCode 含 `.` → 保留不动', () => {
const result = runCommandAutoFix({ enCode: 'workflow.r12' })
expect(result.opts.enCode).toBe('workflow.r12')
expect(result.silent).toHaveLength(0)
})
it('enCode 干净 → 不触发 fix', () => {
const result = runCommandAutoFix({ enCode: 'cleanname' })
expect(result.opts.enCode).toBe('cleanname')
expect(result.silent).toHaveLength(0)
})
it('enCode 为空 → 不触发 fix', () => {
const result = runCommandAutoFix({ enCode: '' })
expect(result.silent).toHaveLength(0)
})
})
describe('S-X2 fullName soften silent (BS-新52)', () => {
it('fullName 含空格 → 自动 strip + silent FixEntry', () => {
const result = runCommandAutoFix({ fullName: 'Round21 测试 strip 流程' })
expect(result.opts.fullName).toBe('Round21测试strip流程')
expect(result.silent.some((e) => e.id === 'S-X2')).toBe(true)
})
it('fullName 含全角括号 → 自动 strip', () => {
const result = runCommandAutoFix({ fullName: 'Round21 测试(strip)' })
expect(result.opts.fullName).toBe('Round21测试strip')
})
it('fullName 含半角括号 → 自动 strip', () => {
const result = runCommandAutoFix({ fullName: 'Test (Demo)' })
expect(result.opts.fullName).toBe('TestDemo')
})
it('fullName 干净 → 不触发 fix', () => {
const result = runCommandAutoFix({ fullName: '干净名' })
expect(result.silent).toHaveLength(0)
})
it('fullName 含 `.` → 保留不动 (BS-52 例外)', () => {
const result = runCommandAutoFix({ fullName: 'round.12' })
expect(result.opts.fullName).toBe('round.12')
expect(result.silent).toHaveLength(0)
})
})
describe('G-X1 BS-47 重名 suggest', () => {
it('enCode 与 existingForms 冲突 → suggest 含冲突项 + <appCode> 前缀建议', () => {
const result = runCommandAutoFix(
{ enCode: 'user' },
{ existingForms: [{ id: '1', enCode: 'user' }], appCode: 'testspace' },
)
expect(result.suggestions).toHaveLength(1)
expect(result.suggestions[0].id).toBe('G-X1')
expect(result.suggestions[0].level).toBe('suggest')
expect(result.suggestions[0].field).toBe('enCode')
expect(result.suggestions[0].candidates).toContain('user')
expect(result.suggestions[0].summary).toContain('testspace') // appCode 前缀建议
expect(result.suggestions[0].summary).toContain('BS-47')
})
it('enCode 不冲突 → 不触发 suggest', () => {
const result = runCommandAutoFix(
{ enCode: 'unique' },
{ existingForms: [{ id: '1', enCode: 'user' }] },
)
expect(result.suggestions).toHaveLength(0)
})
it('扫多个 existing 列表 (Forms + Dicts + Menus + Roles)', () => {
const result = runCommandAutoFix(
{ enCode: 'shared' },
{
existingForms: [{ id: '1', enCode: 'shared' }],
existingDicts: [{ id: '2', enCode: 'shared' }],
},
)
expect(result.suggestions[0].candidates).toContain('shared')
// candidates 至少 2 项 (form + dict)
expect(result.suggestions[0].candidates!.length).toBeGreaterThanOrEqual(2)
})
it('无 appCode 时 suggest 含 <appCode> 占位符', () => {
const result = runCommandAutoFix(
{ enCode: 'conflict' },
{ existingForms: [{ id: '1', enCode: 'conflict' }] },
)
expect(result.suggestions[0].summary).toContain('<appCode>')
})
// v2.1+ (2026-07-21): workflow enCode 也参与 G-X1 (BS-47 全平台唯一性)
it('扫 existingWorkflows (workflow enCode 重名也能触发 G-X1)', () => {
const result = runCommandAutoFix(
{ enCode: 'leaveFlow' },
{
existingForms: [],
existingWorkflows: [{ id: '99', enCode: 'leaveFlow' }],
appCode: 'testapp',
},
)
const gX1 = result.suggestions.find((s) => s.id === 'G-X1')
expect(gX1).toBeDefined()
expect(gX1!.candidates).toContain('leaveFlow')
expect(gX1!.summary).toContain('已有 1 项冲突')
})
it('Forms + Workflows 同时冲突 (candidates 含双重)', () => {
const result = runCommandAutoFix(
{ enCode: 'shared' },
{
existingForms: [{ id: '1', enCode: 'shared' }],
existingWorkflows: [{ id: '99', enCode: 'shared' }],
appCode: 'testapp',
},
)
const gX1 = result.suggestions.find((s) => s.id === 'G-X1')
expect(gX1).toBeDefined()
expect(gX1!.candidates.length).toBeGreaterThanOrEqual(1)
expect(gX1!.candidates).toContain('shared')
})
it('无冲突时不触发 G-X1 (workflows 空也不误判)', () => {
const result = runCommandAutoFix(
{ enCode: 'unique' },
{
existingForms: [{ id: '1', enCode: 'other1' }],
existingWorkflows: [{ id: '99', enCode: 'other2' }],
},
)
const gX1 = result.suggestions.find((s) => s.id === 'G-X1')
expect(gX1).toBeUndefined()
})
})
describe('fieldAliases 映射', () => {
it('dm 模式: table ↔ enCode', () => {
const result = runCommandAutoFix(
{ table: 'dm-with-dash' },
{ fieldAliases: { enCode: 'table' } },
)
expect(result.opts.table).toBe('dmwithdash')
expect(result.silent[0].field).toBe('table')
expect(result.silent[0].id).toBe('S-X1')
})
it('user 模式: account ↔ enCode + realName ↔ fullName', () => {
const result = runCommandAutoFix(
{ account: 'user-test', realName: '测试 用户(name)' },
{ fieldAliases: { enCode: 'account', fullName: 'realName' } },
)
expect(result.opts.account).toBe('usertest')
expect(result.opts.realName).toBe('测试用户name')
expect(result.silent.find((e) => e.field === 'account')).toBeDefined()
expect(result.silent.find((e) => e.field === 'realName')).toBeDefined()
})
it('dm 模式: tableName ↔ fullName 也走 soften', () => {
const result = runCommandAutoFix(
{ table: 'clean', tableName: '表 名(test)' },
{ fieldAliases: { enCode: 'table', fullName: 'tableName' } },
)
expect(result.opts.tableName).toBe('表名test')
})
it('无 aliases → 默认 enCode/fullName', () => {
const result = runCommandAutoFix({ enCode: 'has-dash', fullName: 'has name' })
expect(result.opts.enCode).toBe('hasdash')
expect(result.opts.fullName).toBe('hasname')
})
})
describe('runCommandAutoFix 端到端组合', () => {
it('S-X1 + S-X2 + G-X1 同时触发', () => {
// 注意: G-X1 在 S-X1/S-X2 之后跑, 所以 enCode 必须先干净 (无 `-` `_`),
// 否则 S-X1 strip 后就不冲突了, G-X1 自然不触发
const result = runCommandAutoFix(
{ enCode: 'duplicate', fullName: '重复 名称' },
{
existingForms: [{ id: '1', enCode: 'duplicate' }],
appCode: 'testspace',
},
)
expect(result.silent.length).toBeGreaterThanOrEqual(1) // S-X2 (fullName soften)
expect(result.suggestions.length).toBe(1) // G-X1
expect(result.opts.enCode).toBe('duplicate')
expect(result.opts.fullName).toBe('重复名称')
expect(result.suggestions[0].candidates).toContain('duplicate')
})
it('silent 修正合入 opts.opts, 调用方需 Object.assign', () => {
const orig = { enCode: 'has-dash' }
const result = runCommandAutoFix(orig)
// runCommandAutoFix 不修改原对象 (返回新对象)
expect(orig.enCode).toBe('has-dash')
expect(result.opts.enCode).toBe('hasdash')
})
it('assertive 永远为空 (silent + suggest 两档, 中间无 assertive)', () => {
const result = runCommandAutoFix(
{ enCode: 'a-b' },
{ existingForms: [{ id: '1', enCode: 'a-b' }] },
)
expect(result.assertive).toHaveLength(0)
expect(result.silent.length + result.suggestions.length).toBeGreaterThan(0)
})
it('空 opts 不报错', () => {
expect(() => runCommandAutoFix({})).not.toThrow()
})
it('空 ctx 不报错', () => {
expect(() => runCommandAutoFix({ enCode: 'a-b' })).not.toThrow()
})
})