Files
dacli/tests/unit/report_wired.test.ts
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

196 lines
8.2 KiB
TypeScript

// dacli/tests/unit/report_wired.test.ts
/**
* report/{create,list,info}.ts 三层接入契约测试 (2026-07-21 固化, 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 检测).
*
* v2.2+ (2026-07-21): P6 sample 续把 report/{list,info}.ts 从 printCommandEnd
* 老 pattern 升级到 wrapCommand(ctx.success/fail) 新 pattern. 测试同步更新:
* - 老检查: `printCommandEnd` + `try/finally` + `if (cmdError) process.exit(1)`
* - 新检查: `wrapCommand` + `ctx.success` + `ctx.fail`
*
* 覆盖:
* - report/create.ts: 静态检查包含 runCommandAutoFix + setupOutputFilter + wrapCommand
* + addOutputOptions 调用 (老 printCommandEnd pattern; 后续将同样迁移到 wrapCommand)
* - report/list.ts: 静态检查包含 setupOutputFilter + wrapCommand + ctx.success/fail
* - report/info.ts: 同 list
* - wrapCommand 调用次数: create 至少 1 次 (P6 sample 续后), 后续对齐
* - 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 + wrapCommand (输出过滤 + v2.2 收口)', () => {
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
expect(src).toMatch(/addOutputOptions/)
expect(src).toMatch(/wrapCommand/)
// v2.2+ (2026-07-21): wrapCommand 替代 printCommandEnd 散布, 不应再显式调用
expect(src).not.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('wrapCommand 收口 5 处 ctx.fail (替代 5 处 process.exit + 5 处 printCommandEnd)', () => {
const failMatches = src.match(/ctx\.fail\(/g) || []
expect(failMatches.length).toBeGreaterThanOrEqual(5)
})
it('成功路径 ctx.success (默认 success, 替代旧版 success printCommandEnd)', () => {
expect(src).toMatch(/ctx\.success\(/)
// 成功路径不应在 ctx.success 前调用 ctx.fail
const lastSuccess = src.lastIndexOf('ctx.success(')
expect(lastSuccess).toBeGreaterThan(-1)
})
it('interfase CreateOpts 含 verbose 字段', () => {
expect(src).toMatch(/interface\s+CreateOpts\s*\{[^}]*verbose\??:\s*boolean[^}]*\}/)
})
})
// ============================================================
// report/list.ts: read 类接入 (v2.2+ wrapCommand 收口 5c 样板)
// ============================================================
describe('report/list.ts 三层接入契约', () => {
const src = readReportFile('list.ts')
it('import setupOutputFilter + addOutputOptions + wrapCommand (v2.2+ 替代 printCommandEnd 老 pattern)', () => {
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
expect(src).toMatch(/addOutputOptions/)
expect(src).toMatch(/wrapCommand/)
expect(src).not.toMatch(/printCommandEnd/) // v2.2+ 已替换
})
it('调用 setupOutputFilter(opts) + addOutputOptions(cmd)', () => {
expect(src).toMatch(/setupOutputFilter\(opts\)/)
expect(src).toMatch(/addOutputOptions\(cmd\)/)
})
it('wrapCommand 内含 try/catch (失败走 ctx.fail)', () => {
expect(src).toMatch(/wrapCommand\(\{[^}]*command:\s*['"]report list['"]/)
expect(src).toMatch(/ctx\.fail\(/)
expect(src).toMatch(/ctx\.success\(/)
})
it('未用裸 process.exit / 老 try/finally cmdError pattern', () => {
// wrapCommand 已经收口 process.exit, 老 pattern 应不在文件里
expect(src).not.toMatch(/if\s*\(\s*cmdError\s*\)\s*process\.exit/)
expect(src).not.toMatch(/cmdSummary\s*=\s*\{/)
})
it('interfase ListOpts 含 verbose 字段', () => {
expect(src).toMatch(/interface\s+ListOpts\s*\{[^}]*verbose\??:\s*boolean[^}]*\}/)
})
})
// ============================================================
// report/info.ts: info 类接入 (v2.2+ wrapCommand 收口)
// ============================================================
describe('report/info.ts 三层接入契约', () => {
const src = readReportFile('info.ts')
it('import setupOutputFilter + addOutputOptions + wrapCommand (v2.2+)', () => {
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
expect(src).toMatch(/addOutputOptions/)
expect(src).toMatch(/wrapCommand/)
expect(src).not.toMatch(/printCommandEnd/) // v2.2+ 已替换
})
it('调用 setupOutputFilter(opts) + addOutputOptions(cmd)', () => {
expect(src).toMatch(/setupOutputFilter\(opts\)/)
expect(src).toMatch(/addOutputOptions\(cmd\)/)
})
it('wrapCommand 内含 try/catch (ctx.success + ctx.fail)', () => {
expect(src).toMatch(/wrapCommand\(\{[^}]*command:\s*['"]report info['"]/)
expect(src).toMatch(/ctx\.fail\(/)
expect(src).toMatch(/ctx\.success\(/)
})
it('未用裸 process.exit / 老 try/finally cmdError pattern', () => {
expect(src).not.toMatch(/if\s*\(\s*cmdError\s*\)\s*process\.exit/)
expect(src).not.toMatch(/cmdSummary\s*=\s*\{/)
})
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/)
})
})