/** * tests/unit/form_update_read_only.test.ts — sswflow M1 Task 9 * * 纯函数测试 makeFormReadOnly (不依赖 commander, 不依赖 webapi). * 命令执行通过 webapi 真镜像 (visualDev.update + release) 已在 manual 流程验证. */ import { describe, it, expect } from 'vitest' import { makeFormReadOnly } from '../../src/commands/form/_readonly.ts' const ORIG_FORM = { id: 'sswflowFlowRun_formId', enCode: 'sswflowFlowRun', fullName: 'SSWFlow运行实例', formData: JSON.stringify({ fields: [ { field: 'f_id', label: 'id' }, { field: 'f_status', label: '状态' }, ], columnList: [ { field: 'f_status', label: '状态', children: [{ field: 'f_id', label: 'id' }], }, ], btnsList: [{ value: 'add', label: '新增' }, { value: 'edit', label: '编辑' }], tableBtns: [{ value: 'delete', label: '删除' }], }), // 故意没传 tables (物理表零变更是契约) } describe('form update --read-only — makeFormReadOnly (sswflow M1 Task 9)', () => { it('不携带 tables 字段 → 物理表零变更', () => { const r = makeFormReadOnly({ ...ORIG_FORM, tables: { foo: 'bar' } }) expect(r.schema).not.toHaveProperty('tables') }) it('清空 btnsList 与 tableBtns (新增/编辑/删除按钮消失)', () => { const r = makeFormReadOnly(ORIG_FORM) const fd = JSON.parse(r.schema.formData as string) expect(fd.btnsList).toEqual([]) expect(fd.tableBtns).toEqual([]) }) it('字段 disabled + readOnly 锁定', () => { const r = makeFormReadOnly(ORIG_FORM) const fd = JSON.parse(r.schema.formData as string) for (const f of fd.fields) { expect(f.disabled).toBe(true) expect(f.readOnly).toBe(true) } }) it('columnList disabled (含 children 嵌套)', () => { const r = makeFormReadOnly(ORIG_FORM) const fd = JSON.parse(r.schema.formData as string) for (const c of fd.columnList) { expect(c.disabled).toBe(true) for (const cc of c.children) { expect(cc.disabled).toBe(true) } } }) it('formData 必须是字符串 (web 后端契约)', () => { const r = makeFormReadOnly(ORIG_FORM) expect(typeof r.schema.formData).toBe('string') }) it('不修改原对象 (返回新 schema)', () => { const orig = JSON.parse(JSON.stringify(ORIG_FORM)) const r = makeFormReadOnly(orig) expect(orig.formData).toBe(ORIG_FORM.formData) // 原对象 formData 字符串未变 expect(r.schema).not.toBe(orig) }) it('空字段/空列也安全 (无报错)', () => { const r = makeFormReadOnly({ formData: JSON.stringify({ fields: [], columnList: [] }) }) expect(r.modified.fieldsLocked).toBe(0) expect(r.modified.columnsLocked).toBe(0) }) it('formData 是 object 也能处理 (web 端兼容)', () => { const fd = { fields: [{ field: 'f_a' }], btnsList: [{ value: 'add' }] } const r = makeFormReadOnly({ formData: fd }) expect(typeof r.schema.formData).toBe('string') const parsed = JSON.parse(r.schema.formData as string) expect(parsed.btnsList).toEqual([]) expect(parsed.fields[0].disabled).toBe(true) }) })