- 4 AppContext fixtures (blindspot_guards/bs54_guard/dbLinkId/verify_option):
add topLevelMenus:[] + currentSystemId:'' (fields added in BS-109, fixtures drifted)
- CreateOpts: add internal __aiBuildFormData?:{formData} hook (was (opts as any));
fixes 3 form_create_smartprops_ai.test.ts errors
- e2e/_helpers/nav.ts: /// <reference lib=dom /> for page.evaluate browser context
(resolves document/HTMLElement + item/e unknown)
- bs66-70 test: type formVm as Set<string>
- auto_fix.test: gX1!.candidates! ; defhttp test: HeadersInit → Record<string,string>
- output_filter.test: cast stdout spy to declared MockInstance type
Verified: tsc 0 errors; vitest unit 361 pass / 19 fail == pre-existing
(blindspot_guards registry drift + doc-md-slice fixture, audit §12.2) — no regression.
Co-Authored-By: Claude <[email protected]>
271 lines
10 KiB
TypeScript
271 lines
10 KiB
TypeScript
// 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 映射', () => {
|
||
// 2026-07-22 eventhub 阻断修复:
|
||
// - dm table **不再** 走 S-X1 enCode strip (drift loop 根因: 'event_signup' → 'eventsignup').
|
||
// dm 表名由 dm/_table_name.ts normalizeDmTableName 内部规范化 (snake_case 保留).
|
||
// - dm create 在 runCommandAutoFix 调用时 fieldAliases 仅映射 fullName=tableName,
|
||
// 不再 alias enCode=table, 防止 enCode strip helper 触发表名.
|
||
// - 此处旧契约 `dm 模式: table ↔ enCode` + `dm 模式: tableName ↔ fullName`
|
||
// (期望 'dm-with-dash' → 'dmwithdash' / '表 名(test)' → '表名test')
|
||
// 第一个用例完全废弃, 第二个仅保留 tableName/fullName 软化语义 (tableName 不动 S-X1).
|
||
//
|
||
// 重新表达契约: dm alias **不** 含 enCode→table, 含 fullName→tableName 用于 softener.
|
||
it('dm 模式: table 不走 S-X1 enCode strip (snake_case 保留)', () => {
|
||
const result = runCommandAutoFix(
|
||
{ table: 'event_signup', tableName: '活动报名' },
|
||
{ fieldAliases: { fullName: 'tableName' } },
|
||
)
|
||
// snake_case 表名不再被 strip
|
||
expect(result.opts.table).toBe('event_signup')
|
||
expect(result.silent.find((e) => e.field === 'table')).toBeUndefined()
|
||
})
|
||
|
||
it('dm 模式 tableName ↔ fullName 仍走 S-X2 softener (fullName alias)', () => {
|
||
const result = runCommandAutoFix(
|
||
{ table: 'clean', tableName: '表 名(test)' },
|
||
{ fieldAliases: { fullName: 'tableName' } },
|
||
)
|
||
expect(result.opts.tableName).toBe('表名test')
|
||
const fnEntry = result.silent.find((e) => e.field === 'tableName')
|
||
expect(fnEntry).toBeDefined()
|
||
expect(fnEntry!.id).toBe('S-X2')
|
||
})
|
||
|
||
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()
|
||
})
|
||
})
|