Files
dacli/tests/unit/report_wired.test.ts
T
ssw 11f94a3dd0 feat(dacli): P6 sample 续 7 文件 → wrapCommand + 测试同步
P6 sample 续迁移 7 文件 (累计 14 文件):
  - form/list.ts (--summary + --json + 表格 三路径)
  - report/list.ts (webapi 化 + DacliError 已有)
  - report/info.ts (同上)
  - storage/list.ts (无 appCode, wrapCommand 仍能收口 stdout 5c)
  - integration/emailtest.ts (DacliError 已有 + 新 wrapCommand)
  - integration/smstest.ts (同上)
  - integration/webhooktest.ts (DacliError 已有 + 新 wrapCommand)

P5 全量 (commit c6b7fe8):
  - portal/create.ts:76,93 (2 throws businessType 字典 + 业务分类)
  - bigscreen/construct.ts:269 (1 throw validProps empty)

测试 fix-up:
  - tests/unit/report_wired.test.ts 更新 6 个老 pattern 静态契约
  - 老 pattern (printCommandEnd + try/finally + cmdError + process.exit(1))
  - 新 pattern (wrapCommand + ctx.success/fail + no process.exit)

验证:
  - TS compile 56 baseline 不变
  - 68/68 单元测试 PASS (output_filter + auto_fix + report_wired + workflow)
  - 端到端: form list --summary / report list / storage list / workflow list 都正常
2026-07-21 18:01:00 +08:00

198 lines
8.3 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 + 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 类接入 (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/)
})
})