Files
dacli/tests/unit/form_ai_field_json.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

58 lines
2.2 KiB
TypeScript

/**
* tests/unit/form_ai_field_json.test.ts
*
* 覆盖 src/commands/form/_ai.ts renderAiFieldResult
* (2026-07-22 eventhub 阻断修复 — Task 6 / 设计 §3.6)
*
* 修复前 root cause:
* form create --ai-field --json 分支旧逻辑:
* printAiFormResult(data) // 人类可读 (console.log 多行中文说明)
* if (opts.json) printJson(data) // 又追加 JSON
* 两者同时输出 → stdout 混入人类说明文本, JSON.parse(stdout) 失败.
*
* 修复后契约 (mutual exclusive):
* - opts.json === true → 仅 printJson(data), 不调用 printAiFormResult
* - 否则 → 仅 printAiFormResult(data), 不调用 printJson
*/
import { describe, it, expect, vi } from 'vitest'
import { renderAiFieldResult } from '../../src/commands/form/_ai.ts'
describe('renderAiFieldResult — json / 人类格式 互斥', () => {
const sample = { enCode: 'x', fullName: 'X', aiModelList: [] }
it('json=true → 只调 printJson, 不调 printHuman', () => {
const printJson = vi.fn()
const printHuman = vi.fn()
renderAiFieldResult(sample, { json: true }, { printJson, printHuman })
expect(printJson).toHaveBeenCalledTimes(1)
expect(printJson).toHaveBeenCalledWith(sample)
expect(printHuman).not.toHaveBeenCalled()
})
it('json=false → 只调 printHuman, 不调 printJson', () => {
const printJson = vi.fn()
const printHuman = vi.fn()
renderAiFieldResult(sample, { json: false }, { printJson, printHuman })
expect(printHuman).toHaveBeenCalledTimes(1)
expect(printHuman).toHaveBeenCalledWith(sample)
expect(printJson).not.toHaveBeenCalled()
})
it('json undefined → 默认走人类格式', () => {
const printJson = vi.fn()
const printHuman = vi.fn()
renderAiFieldResult(sample, {}, { printJson, printHuman })
expect(printHuman).toHaveBeenCalledTimes(1)
expect(printJson).not.toHaveBeenCalled()
})
it('两个分支永不同时触发 (互斥不变量)', () => {
for (const json of [true, false, undefined]) {
const printJson = vi.fn()
const printHuman = vi.fn()
renderAiFieldResult(sample, { json }, { printJson, printHuman })
expect(printJson.mock.calls.length + printHuman.mock.calls.length).toBe(1)
}
})
})