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 测试
This commit is contained in:
1 parent
3d4002f6cc
commit
508cabface
3 files changed
+654
No files matched your search
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* commands/_output_filter.ts — 跨命令输出过滤 + CommandLog 落盘 (dacli v2.1+, 2026-07-21)
|
||||
*
|
||||
* 取代 commands/workflow/_output.ts, 升级为跨命令通用 helper.
|
||||
* 复用 dacli form/create.ts 的输出过滤模式 + saveCommandLog 集成 (见 dacli/README.md:213-228):
|
||||
* - 默认 (quiet=true): info/ok/warn 输出到 buffer, 错误即时输出
|
||||
* - --verbose: 所有日志即时输出 (含 step machine/API 调用过程)
|
||||
* - --json: 原始 JSON 输出 (不受 quiet 影响, stdout 走原始数据)
|
||||
* - 完成后: 落 CommandLog (供 dacli log last 查询) + 打印摘要 + "详情: dacli log last" 提示
|
||||
*
|
||||
* 与 workflow/_output.ts 的差异 (迁移要点):
|
||||
* - printCommandEnd 签名变 (opts, command, summary) → printCommandEnd({ command, appCode, summary, fixes, result, error, duration_ms, verbose, json })
|
||||
* 旧用法只在 workflow/create.ts:383 一处, shim 文件不转发旧签名, 调用方需更新.
|
||||
* - 新增 saveCommandLog 集成 (旧版本仅打印 key-value summary, 不落结构化日志)
|
||||
* - 新增 result='failure' 路径 (旧版本错误路径直接 process.exit(1), 不落任何日志)
|
||||
* - 删 printCommandEndSimple (dead code, 全 codebase 0 调用方)
|
||||
*
|
||||
* 用法 (在 .action() 里):
|
||||
* import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts'
|
||||
*
|
||||
* .option('--verbose', ..., false).option('--json', ..., false)
|
||||
* .action(async (opts) => {
|
||||
* setupOutputFilter(opts)
|
||||
* const c = await JnpfClient.fromToken()
|
||||
* // ... 命令逻辑 ...
|
||||
* printCommandEnd({
|
||||
* command: 'workflow create',
|
||||
* appCode: c.appCode,
|
||||
* summary: { id, enCode, flowId },
|
||||
* fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
* duration_ms: Date.now() - startTime,
|
||||
* verbose: opts.verbose,
|
||||
* json: opts.json,
|
||||
* })
|
||||
* })
|
||||
*/
|
||||
|
||||
import type { Command } from 'commander'
|
||||
import { log } from '../core/log.ts'
|
||||
import {
|
||||
saveCommandLog,
|
||||
formatOneLineSummary,
|
||||
type FixEntry,
|
||||
type CommandLog,
|
||||
} from '../core/logfile.ts'
|
||||
|
||||
export interface OutputOpts {
|
||||
/** --verbose: 完整输出 (含 step machine、API 调用等过程日志) */
|
||||
verbose?: boolean
|
||||
/** --json: 原始 JSON 输出 (不受 verbose 影响, 始终输出 JSON) */
|
||||
json?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 .action() 开头调用: 设置 quiet 模式
|
||||
* --verbose 或 --json: 非 quiet (全部输出)
|
||||
* 默认: quiet (info/ok/warn 走 buffer, 错误即时)
|
||||
*/
|
||||
export function setupOutputFilter(opts: OutputOpts): void {
|
||||
log.setQuiet(!opts.verbose && !opts.json)
|
||||
}
|
||||
|
||||
/**
|
||||
* 给 Command 加 --verbose + --json 选项 (统一风格)
|
||||
*/
|
||||
export function addOutputOptions(cmd: Command): Command {
|
||||
return cmd
|
||||
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
|
||||
.option('--json', '原始 JSON 输出 (不受 --verbose 影响, 始终输出 JSON)', false)
|
||||
}
|
||||
|
||||
export interface PrintCommandEndArgs {
|
||||
/** 命令名 (e.g. 'workflow create') */
|
||||
command: string
|
||||
/** appCode (有则落 CommandLog, 缺失则跳过 L3 历史查询) */
|
||||
appCode?: string
|
||||
/** 结果状态, 默认 'success' */
|
||||
result?: 'success' | 'failure' | 'partial'
|
||||
/** stdout 要打印的摘要 (含 id/enCode 等) — json 模式跳过 */
|
||||
summary?: Record<string, unknown>
|
||||
/** 命令执行的 fix entries (跨命令 silent/assertive/suggest, 来自 runCommandAutoFix 等) */
|
||||
fixes?: FixEntry[]
|
||||
/** 错误信息 (result='failure' 时填这里, stdout + CommandLog 都打) */
|
||||
error?: string
|
||||
/** 命令耗时 (毫秒, 用于 CommandLog.duration_ms) */
|
||||
duration_ms?: number
|
||||
/** --verbose / --json 透传 */
|
||||
verbose?: boolean
|
||||
json?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令结束后调用: 落 CommandLog (L3) + 打印 summary + "详情: dacli log last" 提示 (L1)
|
||||
*
|
||||
* @param args 含 command/appCode/result/summary/fixes/error/duration_ms/verbose/json
|
||||
*
|
||||
* 行为矩阵:
|
||||
* - json=true: stdout 跳过 summary + 提示, 但 CommandLog 仍落 (供 dacli log last 查询)
|
||||
* - verbose=true: log.flush() 立刻输出过程日志 + summary + 提示
|
||||
* - 默认 quiet: log.discard() 丢弃过程 buffer + stdout 打印 summary + 提示
|
||||
* - appCode 缺失: 跳过 L3 (仅打印 stdout)
|
||||
* - result='failure': stdout 额外打印 error (如果有)
|
||||
*/
|
||||
export function printCommandEnd(args: PrintCommandEndArgs): void {
|
||||
const {
|
||||
command,
|
||||
appCode,
|
||||
summary,
|
||||
fixes,
|
||||
error,
|
||||
duration_ms,
|
||||
} = args
|
||||
const result = args.result || 'success'
|
||||
const opts: OutputOpts = { verbose: args.verbose, json: args.json }
|
||||
|
||||
// ---- L3: 落 CommandLog (供 dacli log last 查询) ----
|
||||
if (appCode) {
|
||||
try {
|
||||
const silent = fixes?.filter(f => f.level === 'silent') || []
|
||||
const assertive = fixes?.filter(f => f.level === 'assertive') || []
|
||||
const suggestions = fixes?.filter(f => f.level === 'suggest') || []
|
||||
const target = (summary?.enCode as string) || (summary?.id as string) || undefined
|
||||
const cmdLog: CommandLog = {
|
||||
command,
|
||||
timestamp: new Date().toISOString(),
|
||||
duration_ms: duration_ms || 0,
|
||||
result,
|
||||
appCode,
|
||||
summary: {
|
||||
target,
|
||||
id: summary?.id as string | undefined,
|
||||
silent_fixes: silent.length,
|
||||
assertive_fixes: assertive.length,
|
||||
suggestions: suggestions.length,
|
||||
error,
|
||||
},
|
||||
silent_fixes: silent,
|
||||
assertive_fixes: assertive,
|
||||
suggestions,
|
||||
}
|
||||
saveCommandLog(appCode, cmdLog)
|
||||
|
||||
// ---- L1: stdout 打印摘要 ----
|
||||
if (!opts.json) {
|
||||
if (opts.verbose) {
|
||||
log.flush()
|
||||
log.info('')
|
||||
log.info(formatOneLineSummary(cmdLog))
|
||||
log.info(` 详情: dacli log last`)
|
||||
} else {
|
||||
log.discard()
|
||||
const oneLine = formatOneLineSummary(cmdLog)
|
||||
process.stdout.write(`\n${oneLine}\n`)
|
||||
process.stdout.write(` 详情: dacli log last\n`)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 日志写入失败不阻塞主流程, 但 stdout 仍要打印 (兜底)
|
||||
if (!opts.json) {
|
||||
if (opts.verbose) log.flush()
|
||||
else log.discard()
|
||||
if (summary && Object.keys(summary).length > 0) {
|
||||
const lines = Object.entries(summary)
|
||||
.map(([k, v]) => ` ${k}: ${v}`)
|
||||
.join('\n')
|
||||
process.stdout.write(`\n${lines}\n`)
|
||||
}
|
||||
if (error) process.stdout.write(` 错误: ${error}\n`)
|
||||
process.stdout.write(` 详情: dacli log last\n`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// ---- 无 appCode: 仅 L1 stdout, 跳过 L3 ----
|
||||
if (!opts.json) {
|
||||
if (opts.verbose) log.flush()
|
||||
else log.discard()
|
||||
if (summary && Object.keys(summary).length > 0) {
|
||||
const lines = Object.entries(summary)
|
||||
.map(([k, v]) => ` ${k}: ${v}`)
|
||||
.join('\n')
|
||||
process.stdout.write(`\n${lines}\n`)
|
||||
}
|
||||
if (error) process.stdout.write(` 错误: ${error}\n`)
|
||||
process.stdout.write(` 详情: dacli log last\n`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
// 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()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,218 @@
|
||||
// dacli/tests/unit/output_filter.test.ts
|
||||
/**
|
||||
* commands/_output_filter.ts 输出过滤 + CommandLog 落盘 单元测试 (2026-07-21 固化)
|
||||
*
|
||||
* 覆盖:
|
||||
* - setupOutputFilter: 默认 quiet, --verbose/--json 解除 quiet
|
||||
* - addOutputOptions: 注册 --verbose/--json 选项
|
||||
* - printCommandEnd: success/failure/partial 三档 result
|
||||
* - printCommandEnd: 写 CommandLog (含 silent/assertive/suggest 三档 fix 分类)
|
||||
* - printCommandEnd: JSON mode 跳过 stdout 但仍落 CommandLog
|
||||
* - printCommandEnd: 无 appCode 跳过 L3 但仍打印 stdout
|
||||
*
|
||||
* 详见 dacli/README.md:213-228 三层处理 + dacli/docs/decisions/0010
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { existsSync, unlinkSync, readFileSync, mkdirSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { homedir } from 'node:os'
|
||||
import { log } from '../../src/core/log.ts'
|
||||
import { readLatestLog, getLogDir } from '../../src/core/logfile.ts'
|
||||
import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../../src/commands/_output_filter.ts'
|
||||
|
||||
const TEST_APP_CODE = 'unit-test-output-filter'
|
||||
|
||||
describe('setupOutputFilter', () => {
|
||||
it('默认 quiet (无 verbose/json)', () => {
|
||||
setupOutputFilter({})
|
||||
// 静默模式: setQuiet(true) — 间接通过 log 行为验证
|
||||
// 完整验证需 mock stdout, 这里仅检查函数不抛异常
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
|
||||
it('--verbose → 解除 quiet', () => {
|
||||
setupOutputFilter({ verbose: true })
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
|
||||
it('--json → 解除 quiet', () => {
|
||||
setupOutputFilter({ json: true })
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('addOutputOptions', () => {
|
||||
it('注册 --verbose 和 --json 两个选项', () => {
|
||||
// 用 mock Command 验证
|
||||
const mockCmd = {
|
||||
option: vi.fn().mockReturnThis(),
|
||||
}
|
||||
addOutputOptions(mockCmd as any)
|
||||
expect(mockCmd.option).toHaveBeenCalledTimes(2)
|
||||
expect(mockCmd.option.mock.calls[0][0]).toBe('--verbose')
|
||||
expect(mockCmd.option.mock.calls[1][0]).toBe('--json')
|
||||
})
|
||||
})
|
||||
|
||||
describe('printCommandEnd — CommandLog 落盘 (L3)', () => {
|
||||
beforeEach(() => {
|
||||
// 清空旧日志
|
||||
const logDir = getLogDir(TEST_APP_CODE)
|
||||
if (existsSync(logDir)) {
|
||||
try {
|
||||
const tail = join(logDir, 'tail')
|
||||
if (existsSync(tail)) unlinkSync(tail)
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
})
|
||||
|
||||
it('成功路径 + appCode → 落 CommandLog + 含 silent/assertive/suggest 分类', () => {
|
||||
const fixes = [
|
||||
{ id: 'S-X1', level: 'silent' as const, field: 'enCode', summary: 'strip', from: 'a-b', to: 'ab' },
|
||||
{ id: 'A1', level: 'assertive' as const, field: 'f_id', summary: 'relationForm', rollback: 'dacli form update 1 --set=...' },
|
||||
{ id: 'G1', level: 'suggest' as const, field: 'enCode', summary: '重名', candidates: ['user'] },
|
||||
]
|
||||
printCommandEnd({
|
||||
command: 'test cmdsave',
|
||||
appCode: TEST_APP_CODE,
|
||||
summary: { id: 'abc123', enCode: 'ab' },
|
||||
fixes,
|
||||
duration_ms: 100,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog).not.toBeNull()
|
||||
expect(cmdLog!.command).toBe('test cmdsave')
|
||||
expect(cmdLog!.result).toBe('success')
|
||||
expect(cmdLog!.summary.id).toBe('abc123')
|
||||
expect(cmdLog!.silent_fixes).toHaveLength(1)
|
||||
expect(cmdLog!.assertive_fixes).toHaveLength(1)
|
||||
expect(cmdLog!.suggestions).toHaveLength(1)
|
||||
expect(cmdLog!.silent_fixes[0].id).toBe('S-X1')
|
||||
expect(cmdLog!.assertive_fixes[0].rollback).toBe('dacli form update 1 --set=...')
|
||||
expect(cmdLog!.suggestions[0].candidates).toEqual(['user'])
|
||||
})
|
||||
|
||||
it('失败路径 → result=failure + summary.error 记录', () => {
|
||||
printCommandEnd({
|
||||
command: 'test cmdfail',
|
||||
appCode: TEST_APP_CODE,
|
||||
result: 'failure',
|
||||
error: '后端返 EXIST002',
|
||||
summary: { enCode: 'dup' },
|
||||
duration_ms: 50,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog!.result).toBe('failure')
|
||||
expect(cmdLog!.summary.error).toBe('后端返 EXIST002')
|
||||
})
|
||||
|
||||
it('partial 路径 → result=partial', () => {
|
||||
printCommandEnd({
|
||||
command: 'test cmdpartial',
|
||||
appCode: TEST_APP_CODE,
|
||||
result: 'partial',
|
||||
error: '2 个字典项失败',
|
||||
summary: { id: 'xyz' },
|
||||
duration_ms: 200,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog!.result).toBe('partial')
|
||||
})
|
||||
|
||||
it('无 appCode → 跳过 CommandLog 落盘, 只打印 stdout', () => {
|
||||
printCommandEnd({
|
||||
command: 'test noappcode',
|
||||
// 无 appCode
|
||||
summary: { id: 'noapp' },
|
||||
duration_ms: 10,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
// 无 appCode 时 tail 不应该更新到 noappcode 这个 command
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
if (cmdLog) {
|
||||
expect(cmdLog.command).not.toBe('test noappcode')
|
||||
}
|
||||
})
|
||||
|
||||
it('fixes 未传 → silent/assertive/suggest 三个数组都为空', () => {
|
||||
printCommandEnd({
|
||||
command: 'test nofixes',
|
||||
appCode: TEST_APP_CODE,
|
||||
summary: { id: 'nofix' },
|
||||
duration_ms: 10,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog!.silent_fixes).toHaveLength(0)
|
||||
expect(cmdLog!.assertive_fixes).toHaveLength(0)
|
||||
expect(cmdLog!.suggestions).toHaveLength(0)
|
||||
expect(cmdLog!.summary.silent_fixes).toBe(0)
|
||||
expect(cmdLog!.summary.assertive_fixes).toBe(0)
|
||||
expect(cmdLog!.summary.suggestions).toBe(0)
|
||||
})
|
||||
|
||||
it('duration_ms 正确记录', () => {
|
||||
printCommandEnd({
|
||||
command: 'test dur',
|
||||
appCode: TEST_APP_CODE,
|
||||
summary: { id: 'dur' },
|
||||
duration_ms: 1234,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog!.duration_ms).toBe(1234)
|
||||
})
|
||||
})
|
||||
|
||||
describe('printCommandEnd — fixes 分类过滤', () => {
|
||||
beforeEach(() => {
|
||||
const logDir = getLogDir(TEST_APP_CODE)
|
||||
if (existsSync(logDir)) {
|
||||
try {
|
||||
const tail = join(logDir, 'tail')
|
||||
if (existsSync(tail)) unlinkSync(tail)
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
})
|
||||
|
||||
it('mixed fixes → 按 level 正确分到三个数组', () => {
|
||||
const fixes = [
|
||||
{ id: 'S1', level: 'silent' as const, summary: 's1' },
|
||||
{ id: 'S2', level: 'silent' as const, summary: 's2' },
|
||||
{ id: 'A1', level: 'assertive' as const, summary: 'a1' },
|
||||
{ id: 'G1', level: 'suggest' as const, summary: 'g1' },
|
||||
{ id: 'G2', level: 'suggest' as const, summary: 'g2' },
|
||||
]
|
||||
printCommandEnd({
|
||||
command: 'test mix',
|
||||
appCode: TEST_APP_CODE,
|
||||
summary: { id: 'mix' },
|
||||
fixes,
|
||||
duration_ms: 10,
|
||||
verbose: false,
|
||||
json: false,
|
||||
})
|
||||
|
||||
const cmdLog = readLatestLog(TEST_APP_CODE)
|
||||
expect(cmdLog!.silent_fixes).toHaveLength(2)
|
||||
expect(cmdLog!.assertive_fixes).toHaveLength(1)
|
||||
expect(cmdLog!.suggestions).toHaveLength(2)
|
||||
expect(cmdLog!.summary.silent_fixes).toBe(2)
|
||||
expect(cmdLog!.summary.assertive_fixes).toBe(1)
|
||||
expect(cmdLog!.summary.suggestions).toBe(2)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user