src/commands/report/{create,list,info}.ts: 4 步 pattern (imports → addOutputOptions → setupOutputFilter + runCommandAutoFix → printCommandEnd)
- report/create.ts: 删 inline autoStripEnCode (改由 runCommandAutoFix S-X1 统一)
- report/create: 5 失败 + 1 成功 printCommandEnd 调用
- report/list + info: try/catch/finally, process.exit(1) 在 finally 兜底
tests/unit/report_wired.test.ts (新): 22 个静态契约测试
- 防止未来被悄悄撤掉 3 个 helper (regression detection)
- runtime smoke: --help 注册 --verbose/--json
198 lines
8.1 KiB
TypeScript
198 lines
8.1 KiB
TypeScript
// dacli/tests/unit/report_wired.test.ts
|
|
/**
|
|
* report/{create,list,info}.ts 三层接入契约测试 (2026-07-21 固化)
|
|
*
|
|
* 为什么需要这个测试 (rationale):
|
|
* v2.1+ 三层接入 (runCommandAutoFix + setupOutputFilter + printCommandEnd) 已经固化在
|
|
* 16 个 create/list 文件 (handoff 2026-07-21). report/{create,list,info}.ts 是
|
|
* 这波接入的第 17/18/19 个文件. 静态契约测试防止未来被悄悄撤掉 (regression 检测).
|
|
*
|
|
* 覆盖:
|
|
* - report/create.ts: 静态检查包含 runCommandAutoFix + setupOutputFilter + printCommandEnd
|
|
* + addOutputOptions 调用
|
|
* - report/list.ts: 静态检查包含 setupOutputFilter + printCommandEnd (read 类无需 runCommandAutoFix)
|
|
* - report/info.ts: 同 list
|
|
* - printCommandEnd 调用次数: create 至少 6 次 (1 success + 5 failure)
|
|
* - runtime smoke: --help 命令注册 --verbose 和 --json (验证 addOutputOptions)
|
|
*
|
|
* 不覆盖:
|
|
* - 真实 API 调用 (需 testspace, 见 handoff 的 testspace 验证流程)
|
|
* - BS-新12 silent fail 等已知问题
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest'
|
|
import { readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { Command } from 'commander'
|
|
|
|
import { buildReportCreateCommand } from '../../src/commands/report/create.ts'
|
|
import { buildReportListCommand } from '../../src/commands/report/list.ts'
|
|
import { buildReportInfoCommand } from '../../src/commands/report/info.ts'
|
|
|
|
const REPORT_DIR = join(process.cwd(), 'src/commands/report')
|
|
|
|
function readReportFile(name: string): string {
|
|
return readFileSync(join(REPORT_DIR, name), 'utf-8')
|
|
}
|
|
|
|
// ============================================================
|
|
// report/create.ts: create 类完整接入 (auto-fix + 输出过滤 + CommandLog)
|
|
// ============================================================
|
|
describe('report/create.ts 三层接入契约', () => {
|
|
const src = readReportFile('create.ts')
|
|
|
|
it('import runCommandAutoFix + fetchExistingLists (命令级 auto-fix)', () => {
|
|
expect(src).toMatch(/import\s+\{[^}]*runCommandAutoFix[^}]*\}\s+from\s+['"]\.\.\/_auto_fix\.ts['"]/)
|
|
expect(src).toMatch(/fetchExistingLists/)
|
|
})
|
|
|
|
it('import setupOutputFilter + addOutputOptions + printCommandEnd (输出过滤)', () => {
|
|
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
|
|
expect(src).toMatch(/addOutputOptions/)
|
|
expect(src).toMatch(/printCommandEnd/)
|
|
})
|
|
|
|
it('调用 setupOutputFilter(opts) 在 action 入口', () => {
|
|
expect(src).toMatch(/setupOutputFilter\(opts\)/)
|
|
})
|
|
|
|
it('调用 addOutputOptions(cmd) (注册 --verbose --json)', () => {
|
|
expect(src).toMatch(/addOutputOptions\(cmd\)/)
|
|
})
|
|
|
|
it('调用 runCommandAutoFix + fetchExistingLists 组合', () => {
|
|
expect(src).toMatch(/await\s+fetchExistingLists\(c,\s*c\.appCode\)/)
|
|
expect(src).toMatch(/runCommandAutoFix\(opts/)
|
|
expect(src).toMatch(/Object\.assign\(opts,\s*cmdFix\.opts\)/)
|
|
})
|
|
|
|
it('移除原 inline autoStripEnCode (被 runCommandAutoFix 替代)', () => {
|
|
// 不应再 import autoStripEnCode, 因为已用 runCommandAutoFix
|
|
expect(src).not.toMatch(/import\s+\{[^}]*autoStripEnCode[^}]*\}\s+from\s+['"]\.\.\/_encode_validator\.ts['"]/)
|
|
})
|
|
|
|
it('printCommandEnd 至少调用 6 次 (1 success + 5 failure paths)', () => {
|
|
const matches = src.match(/printCommandEnd\(\{/g) || []
|
|
expect(matches.length).toBeGreaterThanOrEqual(6)
|
|
})
|
|
|
|
it('成功路径 printCommandEnd 不带 result (默认 success)', () => {
|
|
// 找最后一个 printCommandEnd 调用, 应该是 success 路径
|
|
const lastIdx = src.lastIndexOf('printCommandEnd({')
|
|
expect(lastIdx).toBeGreaterThan(-1)
|
|
const successBlock = src.slice(lastIdx)
|
|
expect(successBlock).toMatch(/command:\s*['"]report create['"]/)
|
|
expect(successBlock).toMatch(/summary:\s*\{/)
|
|
// 不应有 result: 'failure' (默认是 success)
|
|
expect(successBlock).not.toMatch(/result:\s*['"]failure['"]/)
|
|
})
|
|
|
|
it('interfase CreateOpts 含 verbose 字段', () => {
|
|
expect(src).toMatch(/interface\s+CreateOpts\s*\{[^}]*verbose\??:\s*boolean[^}]*\}/)
|
|
})
|
|
})
|
|
|
|
// ============================================================
|
|
// report/list.ts: read 类接入 (输出过滤 + CommandLog, 无 auto-fix)
|
|
// ============================================================
|
|
describe('report/list.ts 三层接入契约', () => {
|
|
const src = readReportFile('list.ts')
|
|
|
|
it('import setupOutputFilter + addOutputOptions + printCommandEnd', () => {
|
|
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
|
|
expect(src).toMatch(/addOutputOptions/)
|
|
expect(src).toMatch(/printCommandEnd/)
|
|
})
|
|
|
|
it('调用 setupOutputFilter(opts) + addOutputOptions(cmd)', () => {
|
|
expect(src).toMatch(/setupOutputFilter\(opts\)/)
|
|
expect(src).toMatch(/addOutputOptions\(cmd\)/)
|
|
})
|
|
|
|
it('使用 try/catch/finally 包裹 (错误路径也走 CommandLog)', () => {
|
|
expect(src).toMatch(/\}\s*catch\s*\(\s*e\s*\)\s*\{/)
|
|
expect(src).toMatch(/\}\s*finally\s*\{/)
|
|
// finally 里调用 printCommandEnd
|
|
const finallyIdx = src.indexOf('} finally {')
|
|
expect(finallyIdx).toBeGreaterThan(-1)
|
|
const finallyBlock = src.slice(finallyIdx)
|
|
expect(finallyBlock).toMatch(/printCommandEnd\(/)
|
|
expect(finallyBlock).toMatch(/command:\s*['"]report list['"]/)
|
|
})
|
|
|
|
it('错误路径捕获到 cmdError 后 process.exit(1)', () => {
|
|
// finally 块里的 process.exit(1) 兜底
|
|
const finallyIdx = src.lastIndexOf('} finally {')
|
|
const finallyBlock = src.slice(finallyIdx)
|
|
expect(finallyBlock).toMatch(/if\s*\(\s*cmdError\s*\)\s*process\.exit\(1\)/)
|
|
})
|
|
|
|
it('interfase ListOpts 含 verbose 字段', () => {
|
|
expect(src).toMatch(/interface\s+ListOpts\s*\{[^}]*verbose\??:\s*boolean[^}]*\}/)
|
|
})
|
|
})
|
|
|
|
// ============================================================
|
|
// report/info.ts: info 类接入 (跟 list 同一模式)
|
|
// ============================================================
|
|
describe('report/info.ts 三层接入契约', () => {
|
|
const src = readReportFile('info.ts')
|
|
|
|
it('import setupOutputFilter + addOutputOptions + printCommandEnd', () => {
|
|
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
|
|
expect(src).toMatch(/addOutputOptions/)
|
|
expect(src).toMatch(/printCommandEnd/)
|
|
})
|
|
|
|
it('调用 setupOutputFilter(opts) + addOutputOptions(cmd)', () => {
|
|
expect(src).toMatch(/setupOutputFilter\(opts\)/)
|
|
expect(src).toMatch(/addOutputOptions\(cmd\)/)
|
|
})
|
|
|
|
it('使用 try/catch/finally 包裹', () => {
|
|
expect(src).toMatch(/\}\s*catch\s*\(\s*e\s*\)\s*\{/)
|
|
expect(src).toMatch(/\}\s*finally\s*\{/)
|
|
})
|
|
|
|
it('finally 里 printCommandEnd 含正确 command 名 + 错误退出兜底', () => {
|
|
const finallyIdx = src.lastIndexOf('} finally {')
|
|
const finallyBlock = src.slice(finallyIdx)
|
|
expect(finallyBlock).toMatch(/command:\s*['"]report info['"]/)
|
|
expect(finallyBlock).toMatch(/if\s*\(\s*cmdError\s*\)\s*process\.exit\(1\)/)
|
|
})
|
|
|
|
it('interfase InfoOpts 含 verbose 字段', () => {
|
|
expect(src).toMatch(/interface\s+InfoOpts\s*\{[^}]*verbose\??:\s*boolean[^}]*\}/)
|
|
})
|
|
})
|
|
|
|
// ============================================================
|
|
// runtime smoke: --help 验证 --verbose / --json 选项注册
|
|
// ============================================================
|
|
describe('runtime smoke: --verbose / --json 已注册', () => {
|
|
function buildSubCommand(builder: () => Command): Command {
|
|
return builder()
|
|
}
|
|
|
|
it('report create 注册 --verbose 和 --json', () => {
|
|
const cmd = buildSubCommand(buildReportCreateCommand)
|
|
const help = cmd.helpInformation()
|
|
expect(help).toMatch(/--verbose/)
|
|
expect(help).toMatch(/--json/)
|
|
})
|
|
|
|
it('report list 注册 --verbose 和 --json', () => {
|
|
const cmd = buildSubCommand(buildReportListCommand)
|
|
const help = cmd.helpInformation()
|
|
expect(help).toMatch(/--verbose/)
|
|
expect(help).toMatch(/--json/)
|
|
})
|
|
|
|
it('report info 注册 --verbose 和 --json', () => {
|
|
const cmd = buildSubCommand(buildReportInfoCommand)
|
|
const help = cmd.helpInformation()
|
|
expect(help).toMatch(/--verbose/)
|
|
expect(help).toMatch(/--json/)
|
|
})
|
|
})
|