回应用户"都去完善" — 落地 4 项 tdd8 smoke test 暴露的痛点:
[1/4] non-JSON 容错 (src/core/client.ts)
- 修 schedule list / monitor flow 等命令报"unexpected response shape"崩
- parseJson(): HTTP 2xx + 空 body → 返 envelope-style {code:200, data:null} (JNPF 端点 bug 已知)
- HTTP 2xx + 非 JSON body → graceful degrade 同上, 不 throw
- 实战: schedule list 之前 100% 崩, 现在返 200 + 空 data (语义清晰)
[2/4] dbPing 真实实现 (src/commands/app/status.ts)
- 替换 /system/DataSource/TestConnection (404) → /system/DataModel/{dbLinkId}/Tables
- 端点真实, dm list 端点, 走通即 DB 连得上
- 4xx/5xx 都报真错误, 不再 skip
- 实战: dbPing 75ms 真检查 (之前永远 skip, 防不住 BS-E14)
[3/4] menuIntegrity --fix 跟进 (src/commands/app/status.ts)
- 新选项: --fix-menu-url 自动重建空 urlAddress 菜单
- 策略: 拉 form list → enCode 匹配 → PUT /system/Menu/{id} urlAddress=model/{enCode}
- 非 form 菜单 (enCode 不在 formList) → skip + log "需手填"
- tdd8 实测: 4 项空 urlAddress 可自动修 (需你确认执行)
- DacliHelp 同步更新 notes
[4/4] --reuse-if-exists 通用化 (src/commands/_reusable_create.ts)
- 新增通用 wrapper: reusableCreate({cmdName, enCode, findExisting, runCreate})
- form/dict/menu create 全部接入 (各 ~20 行)
- 6 个新单元测试 (复用场景, 异常容错, 空 enCode 等)
- 4 个命令统一 shape: ctx.success({enCode, id, fullName, state, reused: true, mode: 'reuse-if-exists'})
- dm/workflow 跳过 (enCode 概念不直接适用, tableName/flowId 走不同路径)
验证:
- typecheck 0 errors
- vitest 39 文件 / 389 tests pass (含 6 个新 reusable_create 测试)
- doc:check 0 drift (扫描 271 文件)
- quality gate 4/4 (tsc + vitest + doc:check + smoke)
意外: dict/typecreate.ts 文件在 case-insensitive fs 上 cp 时被误删, git restore 恢复 — 提醒后续 case-sensitivity 风险
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { reusableCreate, reuseSuccessPayload } from '../../src/commands/_reusable_create.ts'
|
|
|
|
describe('reusableCreate wrapper', () => {
|
|
it('runs create when reuseIfExists=false', async () => {
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: false,
|
|
findExisting: vi.fn(),
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('runs create when enCode is empty', async () => {
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: '',
|
|
reuseIfExists: true,
|
|
findExisting: vi.fn(),
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('returns existing when found and reuseIfExists=true', async () => {
|
|
const findExisting = vi.fn().mockResolvedValue({ id: '123', enCode: 'x', state: 1 })
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(true)
|
|
if (result.reused) {
|
|
expect(result.existing.id).toBe('123')
|
|
expect(result.existing.state).toBe(1)
|
|
}
|
|
expect(runCreate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('runs create when findExisting returns null (enCode不存在)', async () => {
|
|
const findExisting = vi.fn().mockResolvedValue(null)
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('runs create when findExisting throws (容错不阻断)', async () => {
|
|
const findExisting = vi.fn().mockRejectedValue(new Error('network down'))
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
})
|
|
|
|
describe('reuseSuccessPayload', () => {
|
|
it('returns uniform shape for ctx.success', () => {
|
|
const out = reuseSuccessPayload({ id: '42', enCode: 'foo', state: 1, fullName: 'Foo' }, 'reuse-if-exists')
|
|
expect(out).toEqual({
|
|
enCode: 'foo',
|
|
id: '42',
|
|
fullName: 'Foo',
|
|
state: 1,
|
|
reused: true,
|
|
mode: 'reuse-if-exists',
|
|
})
|
|
})
|
|
}) |