diff --git a/src/amis/scriptCommands.test.ts b/src/amis/scriptCommands.test.ts index 2ab2409..744c8d2 100644 --- a/src/amis/scriptCommands.test.ts +++ b/src/amis/scriptCommands.test.ts @@ -1,5 +1,10 @@ -import { describe, expect, it } from 'vitest'; -import { translateCommand, translateCommands, type ScriptCommand } from './scriptCommands'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { + translateCommand, + translateCommands, + __resetUnsupportedWarningsForTest, + type ScriptCommand, +} from './scriptCommands'; // P4-5:脚本指令 → amis 动作翻译(29 UI 动词覆盖核心分支)。 describe('translateCommand', () => { @@ -24,22 +29,11 @@ describe('translateCommand', () => { expect(a).toEqual({ actionType: 'setValue', componentId: 'OLD_STORE_ID', args: { value: 'S-99' } }); }); - it('清空选项 setOptions → setValue options 置空', () => { - const a = translateCommand({ actionType: 'setOptions', componentId: 'ELIGIBILITY', args: { options: [] } }); - expect(a?.actionType).toBe('setValue'); - expect((a?.args as Record).options).toEqual([]); - }); - it('追加选项 addOption 透传 value/label', () => { const a = translateCommand({ actionType: 'addOption', componentId: 'ELIGIBILITY', args: { value: 4, label: '合格' } }); expect(a).toEqual({ actionType: 'addOption', componentId: 'ELIGIBILITY', args: { value: 4, label: '合格' } }); }); - it('数据过滤 setFilter 透传 filter map', () => { - const a = translateCommand({ actionType: 'setFilter', componentId: 'BUILDCOMPANY', args: { filter: { UNITTYPE: 7 } } }); - expect((a?.args as Record).filter).toEqual({ UNITTYPE: 7 }); - }); - it('子表按钮 addSubTableButton 透传 args', () => { const a = translateCommand({ actionType: 'addSubTableButton', componentId: 'GOODSDETAIL', args: { target: 'SELECT', index: 0 } }); expect(a?.actionType).toBe('addSubTableButton'); @@ -96,6 +90,45 @@ describe('translateCommand', () => { }); }); +// P1 修复(§10.2):amis 运行时无对应机制的 6 个动词,从「误转 setValue 静默哑火/清值」改为显式跳过 + 告警。 +describe('amis 运行时无机制动词 → 显式跳过(不再误转 setValue)', () => { + beforeEach(() => __resetUnsupportedWarningsForTest()); + afterEach(() => vi.restoreAllMocks()); + + const UNSUPPORTED: Array<[string, Record]> = [ + ['setLabel', { label: '新标题' }], + ['setRequired', { required: true, errorMessage: '必填' }], + ['setFilter', { filter: { UNITTYPE: 7 } }], + ['setOptions', { options: [] }], + ['setNullSelection', { value: true }], + ['setStyle', { width: 120 }], + ]; + + it.each(UNSUPPORTED)('%s 返回 null(跳过,不再产出 setValue)', (actionType, args) => { + vi.spyOn(console, 'warn').mockImplementation(() => {}); + expect(translateCommand({ actionType, componentId: 'X', args })).toBeNull(); + }); + + it('translateCommands 把这些动词整体过滤掉,不混入 setValue', () => { + vi.spyOn(console, 'warn').mockImplementation(() => {}); + const out = translateCommands([ + { actionType: 'hidden', componentId: 'A' }, + { actionType: 'setRequired', componentId: 'B', args: { required: true } }, + { actionType: 'setOptions', componentId: 'C', args: { options: [] } }, + { actionType: 'toast', args: { msg: 'x' } }, + ]); + expect(out.map((a) => a.actionType)).toEqual(['hidden', 'toast']); + }); + + it('同一动词只告警一次(去重)', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + translateCommand({ actionType: 'setRequired', componentId: 'X', args: { required: true } }); + translateCommand({ actionType: 'setRequired', componentId: 'Y', args: { required: false } }); + expect(warn).toHaveBeenCalledTimes(1); + expect(String(warn.mock.calls[0][0])).toContain('setRequired'); + }); +}); + describe('translateCommands', () => { it('数组翻译并过滤无法翻译项', () => { const cmds: ScriptCommand[] = [ diff --git a/src/amis/scriptCommands.ts b/src/amis/scriptCommands.ts index 18fed6f..c4dfef8 100644 --- a/src/amis/scriptCommands.ts +++ b/src/amis/scriptCommands.ts @@ -28,6 +28,34 @@ export interface AmisAction { [key: string]: unknown; } +// amis 运行时【无对应机制】的语义动词。历史实现把它们误翻成 amis `setValue`,但 amis 的 setValue 动作 +// (CmptAction)只读 args.value、丢弃 required/filter/options/label/style 等语义 args → 运行时静默哑火 +// (且 args.value 为 undefined,经 onChange 路径的控件还可能把值清成 undefined)。 +// 现改为【显式跳过 + 一次性告警】:不再误转 setValue(消除清值风险 + 不再伪装「已处理」)。 +// 真正支持「动态必填/选项/过滤/标题/样式」需转换器侧生成数据绑定 schema(requiredOn/source 绑数据变量)、 +// 由重放改数据变量——属跨切面改造,见进度总览 §10.2 follow-up。 +const AMIS_RUNTIME_UNSUPPORTED = new Set([ + 'setLabel', 'setRequired', 'setFilter', 'setOptions', 'setNullSelection', 'setStyle', +]); +const warnedUnsupported = new Set(); + +function skipUnsupported(actionType: string): null { + if (!warnedUnsupported.has(actionType)) { + warnedUnsupported.add(actionType); + // eslint-disable-next-line no-console + console.warn( + `[scriptCommands] 指令 "${actionType}" 在 amis 运行时无对应机制,已跳过(不再误转 setValue)。` + + '「动态必填/选项/过滤/标题/样式」需转换器侧数据绑定支持(总览 §10.2 follow-up)。', + ); + } + return null; +} + +/** 测试可见:清空告警去重集,避免跨用例污染。 */ +export function __resetUnsupportedWarningsForTest(): void { + warnedUnsupported.clear(); +} + /** * 单条脚本指令 → amis 动作对象。 * 返回 null 表示该指令无对应 amis 动作(跳过,不报错)。 @@ -36,6 +64,11 @@ export function translateCommand(cmd: ScriptCommand): AmisAction | null { if (!cmd || !cmd.actionType) return null; const { actionType, componentId, args = {} } = cmd; + // amis 运行时无机制的动词:显式跳过 + 一次性告警(不再误转 setValue 致哑火/清值)。 + if (AMIS_RUNTIME_UNSUPPORTED.has(actionType)) { + return skipUnsupported(actionType); + } + switch (actionType) { // 显隐 —— amis 内置 show/hidden 动作(componentId 定位组件) case 'show': @@ -56,29 +89,12 @@ export function translateCommand(cmd: ScriptCommand): AmisAction | null { case 'setValue': return { actionType: 'setValue', componentId, args: { value: args.value } }; - // 标题 —— 走 setValue 到组件 label(amis 无独立 setLabel,用组件 schema 更新) - case 'setLabel': - return { actionType: 'setValue', componentId, args: { label: args.label } }; - - // 必填 —— amis 表单项 required(用 setValue 更新组件 required 态) - case 'setRequired': - return { actionType: 'setValue', componentId, args: { required: args.required, errorMessage: args.errorMessage } }; - - // 数据过滤 —— 透传 filter(联动数据源;componentId 为目标选择组件) - case 'setFilter': - return { actionType: 'setValue', componentId, args: { filter: args.filter } }; - - // 清空选项 —— amis setOptions 置空 - case 'setOptions': - return { actionType: 'setValue', componentId, args: { options: args.options ?? [] } }; + // setLabel / setRequired / setFilter / setOptions:amis 运行时无对应机制,已在上方 AMIS_RUNTIME_UNSUPPORTED 跳过。 // 追加选项 —— 透传 addOption(前端合并进组件 options) case 'addOption': return { actionType: 'addOption', componentId, args: { value: args.value, label: args.label } }; - case 'setNullSelection': - return { actionType: 'setValue', componentId, args: { nullSelection: args.value } }; - case 'removeOption': return { actionType: 'removeOption', componentId, args: { value: args.value } }; @@ -112,9 +128,7 @@ export function translateCommand(cmd: ScriptCommand): AmisAction | null { case 'focus': return { actionType: 'focus', componentId }; - // 样式 —— 透传 width/height - case 'setStyle': - return { actionType: 'setValue', componentId, args: { style: args } }; + // setStyle:amis 运行时无样式动作,已在上方 AMIS_RUNTIME_UNSUPPORTED 跳过。 // 重载数据源 —— amis reload case 'reload':