58 lines
2.2 KiB
TypeScript
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)
|
|
}
|
|
})
|
|
})
|