本仓 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,未来以本仓历史为主
169 lines
6.6 KiB
TypeScript
169 lines
6.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
closeFormContract,
|
|
FormSchemaInvalidError,
|
|
FORM_SCHEMA_INVALID_TAG,
|
|
buildGetFormAdapter,
|
|
} from '../../src/commands/form/_schema_normalize.ts'
|
|
|
|
describe('form create/update 闭环 (Task 3)', () => {
|
|
it('POST 后 GET schema 非法 → 不 release + 抛 FormSchemaInvalidError', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '999', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '999', state: 0,
|
|
fields: [{ __vModel__: 'f_x', __config__: { jnpfKey: 'relationForm' /* 缺 modelId */ } }],
|
|
}),
|
|
releaseForm: vi.fn(),
|
|
putForm: vi.fn(),
|
|
}
|
|
await expect(closeFormContract(webapi, {
|
|
enCode: 'tdd5_x', appCode: 'tdd5',
|
|
autoRelease: true, op: 'create',
|
|
})).rejects.toBeInstanceOf(FormSchemaInvalidError)
|
|
expect(webapi.releaseForm).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('POST → GET → release 全 OK → 返回 {id, state: 1, schema}', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '999', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '999', state: 0,
|
|
fields: [{ __vModel__: 'f_x', __config__: { jnpfKey: 'select', label: 'X' } }],
|
|
}),
|
|
releaseForm: vi.fn().mockResolvedValue({ code: 200 }),
|
|
}
|
|
const r = await closeFormContract(webapi, {
|
|
enCode: 'tdd5_x', appCode: 'tdd5',
|
|
autoRelease: true, op: 'create',
|
|
})
|
|
expect(r.id).toBe('999')
|
|
expect(r.state).toBe(1)
|
|
expect(r.schema).toHaveLength(1)
|
|
expect(webapi.postForm).toHaveBeenCalledOnce()
|
|
expect(webapi.getForm).toHaveBeenCalledOnce()
|
|
expect(webapi.releaseForm).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('op=update 时调 putForm 而非 postForm', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn(),
|
|
putForm: vi.fn().mockResolvedValue({ id: '888', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '888', state: 0,
|
|
fields: [{ __vModel__: 'f_y', __config__: { jnpfKey: 'input' } }],
|
|
}),
|
|
releaseForm: vi.fn(),
|
|
}
|
|
const r = await closeFormContract(webapi, {
|
|
enCode: 'tdd5_y', appCode: 'tdd5',
|
|
autoRelease: true, op: 'update',
|
|
})
|
|
expect(webapi.putForm).toHaveBeenCalledOnce()
|
|
expect(webapi.postForm).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('autoRelease=false → 不调 releaseForm 但仍验证 schema', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '777', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '777', state: 0,
|
|
fields: [{ __vModel__: 'f_z', __config__: { jnpfKey: 'divider', required: true } }], // divider+required conflict
|
|
}),
|
|
}
|
|
await expect(closeFormContract(webapi, {
|
|
enCode: 'tdd5_z', appCode: 'tdd5',
|
|
autoRelease: false, op: 'create',
|
|
})).rejects.toBeInstanceOf(FormSchemaInvalidError)
|
|
})
|
|
|
|
// ─── T3 review: 4 negative tests ───
|
|
|
|
it('Test 5: getForm 返回空 fields → 当前 silently PASS (validateStrictSchema 对空数组 ok=true), 明确契约', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '555', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({ id: '555', state: 0, fields: [] }),
|
|
releaseForm: vi.fn().mockResolvedValue({ code: 200 }),
|
|
}
|
|
// 当前契约: validateStrictSchema 对空数组返回 ok=true (无字段则无错)
|
|
// 这是显式契约: "空 form" 在 dacli 视角视为合法 (允许 0 字段的极简表单)
|
|
// 若未来要"拒绝空 schema", 应改 validateStrictSchema 加 `if (fields.length === 0) errors.push('empty schema')`
|
|
const r = await closeFormContract(webapi, {
|
|
enCode: 'tdd5_empty', appCode: 'tdd5', autoRelease: true, op: 'create',
|
|
})
|
|
expect(r.state).toBe(1)
|
|
expect(r.schema).toHaveLength(0)
|
|
})
|
|
|
|
it('Test 6: op=update 但 webapi 无 putForm → 抛 typed error', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn(),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '444', state: 0, fields: [{ __vModel__: 'f_a', __config__: { jnpfKey: 'input' } }],
|
|
}),
|
|
releaseForm: vi.fn(),
|
|
// 无 putForm
|
|
}
|
|
await expect(closeFormContract(webapi, {
|
|
enCode: 'tdd5_noput', appCode: 'tdd5', autoRelease: true, op: 'update',
|
|
})).rejects.toThrow(/PUT operation requires putForm/)
|
|
})
|
|
|
|
it('Test 7: autoRelease=true 但 webapi 无 releaseForm → 抛 typed error', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '333', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '333', state: 0, fields: [{ __vModel__: 'f_b', __config__: { jnpfKey: 'input' } }],
|
|
}),
|
|
// 无 releaseForm
|
|
}
|
|
await expect(closeFormContract(webapi, {
|
|
enCode: 'tdd5_norel', appCode: 'tdd5', autoRelease: true, op: 'create',
|
|
})).rejects.toThrow(/autoRelease requires releaseForm/)
|
|
})
|
|
|
|
it('Test 8: autoRelease=false + schema invalid → releaseForm 不被调用', async () => {
|
|
const webapi = {
|
|
postForm: vi.fn().mockResolvedValue({ id: '222', code: 200 }),
|
|
getForm: vi.fn().mockResolvedValue({
|
|
id: '222', state: 0,
|
|
fields: [{ __vModel__: 'f_c', __config__: { jnpfKey: 'divider', required: true } }],
|
|
}),
|
|
releaseForm: vi.fn(),
|
|
}
|
|
await expect(closeFormContract(webapi, {
|
|
enCode: 'tdd5_inv', appCode: 'tdd5', autoRelease: false, op: 'create',
|
|
})).rejects.toBeInstanceOf(FormSchemaInvalidError)
|
|
expect(webapi.releaseForm).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('buildGetFormAdapter: getInfo 返 formData string → 解析 fields', async () => {
|
|
const getInfo = vi.fn().mockResolvedValue({
|
|
id: '111', state: '1',
|
|
formData: JSON.stringify({ fields: [{ __vModel__: 'f_d', __config__: { jnpfKey: 'input' } }] }),
|
|
})
|
|
const r = await buildGetFormAdapter(getInfo as any)('any-enCode')
|
|
expect(r.id).toBe('111')
|
|
expect(r.state).toBe(1)
|
|
expect(r.fields).toHaveLength(1)
|
|
})
|
|
|
|
it('buildGetFormAdapter: getInfo 返 formData 对象 (已解析) → 直接取 fields', async () => {
|
|
const getInfo = vi.fn().mockResolvedValue({
|
|
id: '111', state: 1,
|
|
formData: { fields: [{ __vModel__: 'f_e', __config__: { jnpfKey: 'select' } }] },
|
|
})
|
|
const r = await buildGetFormAdapter(getInfo as any)('any-enCode')
|
|
expect(r.id).toBe('111')
|
|
expect(r.state).toBe(1)
|
|
expect(r.fields).toHaveLength(1)
|
|
})
|
|
|
|
it('FORM_SCHEMA_INVALID_TAG magic const 与 FormSchemaInvalidError.tag 一致', () => {
|
|
const e = new FormSchemaInvalidError(['x'])
|
|
expect(e.tag).toBe(FORM_SCHEMA_INVALID_TAG)
|
|
expect(e.message).toContain(FORM_SCHEMA_INVALID_TAG)
|
|
expect(e.name).toBe('FormSchemaInvalidError')
|
|
expect(e.errors).toEqual(['x'])
|
|
})
|
|
}) |