- tests/test-import-buildformdata.ts: 证明 dacli 可 import web 端 buildFormData (替换 #/ alias + @jnpf/utils → uuid.ts + lodash-es → structuredClone) - tests/test-direct-ai-call.ts: 验证直接调 AI API 可用 - tests/test-direct-ai-jnpf-prompt.ts: JNPF 原版 GEN_MODEL_QUETION prompt + Anthropic 格式 + 简单/主子表 2 个测试 - data/buildformdata-contract.md: buildFormData 契约清单(16+50+23 keys) 验证结论: - buildFormData 100% 注入 formId/renderKey/isSubTable (BS-51 根因) - buildFormData 自动为 select/options 注入 (BS-54 根因) - JNPF 原版 prompt 可直接用于直连 AI, 不需 JNPF backend - 后续 _ai.ts 改造为双路径 (env 直连 / JNPF backend fallback)
93 lines
4.2 KiB
TypeScript
93 lines
4.2 KiB
TypeScript
// 预处理 formUtils.ts:把 #/ 路径替换成绝对路径,然后 import
|
||
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
|
||
import { dirname, resolve } from 'path';
|
||
import { pathToFileURL } from 'url';
|
||
import { tmpdir } from 'os';
|
||
|
||
const WEB_SRC = '/home/ubuntu/ccWorkDir/web/apps/jnpf-web-apps-main/src';
|
||
const SRC = `${WEB_SRC}/components/FormGenerator/src/helper/formUtils.ts`;
|
||
|
||
// 1. 读源
|
||
let src = readFileSync(SRC, 'utf-8');
|
||
|
||
// 2. 替换 #/ 为绝对路径,@jnpf/utils 替换成 web 端本地 uuid.ts(避开 Vite env 依赖)
|
||
const JNPF_UTILS_UUID = '/home/ubuntu/ccWorkDir/web/packages/jnpf/utils/src/helpers/uuid.ts';
|
||
src = src.replace(/from\s+['"]#\/(.+?)['"]/g, (_, p) => `from '${WEB_SRC}/${p}'`);
|
||
src = src.replace(/from\s+['"]@jnpf\/utils['"]/g, `from '${JNPF_UTILS_UUID}'`);
|
||
// 3. lodash-es 替换成 Node 内置 structuredClone(formUtils.ts 只用 cloneDeep)
|
||
src = src.replace(/import\s*\{\s*cloneDeep\s*\}\s*from\s+['"]lodash-es['"];?/g,
|
||
`const cloneDeep = (x) => structuredClone(x);`);
|
||
|
||
console.log('=== 替换后的 formUtils.ts 头 10 行 ===');
|
||
console.log(src.split('\n').slice(0, 15).join('\n'));
|
||
console.log('...\n');
|
||
|
||
// 3. 写到 /tmp
|
||
const tmpDir = `${tmpdir()}/dacli-test-${Date.now()}`;
|
||
mkdirSync(tmpDir, { recursive: true });
|
||
const tmpFile = `${tmpDir}/formUtils-fixed.ts`;
|
||
writeFileSync(tmpFile, src);
|
||
console.log('=== 写到:', tmpFile, '===\n');
|
||
|
||
// 4. 动态 import
|
||
try {
|
||
const mod = await import(pathToFileURL(tmpFile).href);
|
||
console.log('✅ Imported! Exports:', Object.keys(mod));
|
||
|
||
const aiModelList = [
|
||
{
|
||
tableTitle: '客户表',
|
||
tableName: 'customer',
|
||
isMain: true,
|
||
fields: [
|
||
{ fieldTitle: '姓名', fieldName: 'f_name', fieldComponent: 'input' },
|
||
{ fieldTitle: '电话', fieldName: 'f_phone', fieldComponent: 'input' },
|
||
{ fieldTitle: '状态', fieldName: 'f_status', fieldComponent: 'select',
|
||
fieldOptions: [{id: '1', fullName: '启用'}, {id: '0', fullName: '禁用'}] },
|
||
],
|
||
},
|
||
];
|
||
|
||
const result = mod.buildFormData(aiModelList);
|
||
const formData = JSON.parse(result.formData);
|
||
|
||
console.log('\n✅ buildFormData 调用成功!');
|
||
console.log(' defaultDataForm 顶层 keys:', Object.keys(result).filter(k => k !== 'formData'));
|
||
console.log(' formData.fields 数:', formData.fields.length);
|
||
console.log(' formConf 顶层 keys (前 10):', Object.keys(formData).slice(0, 10));
|
||
|
||
console.log('\n=== 第一个 field 详细结构 ===');
|
||
const f0 = formData.fields[0];
|
||
console.log(' __vModel__:', f0.__vModel__);
|
||
console.log(' __config__ 键数:', Object.keys(f0.__config__).length);
|
||
console.log(' __config__ keys:', Object.keys(f0.__config__).join(', '));
|
||
console.log(' ⭐ formId:', f0.__config__.formId);
|
||
console.log(' ⭐ renderKey:', f0.__config__.renderKey);
|
||
console.log(' ⭐ isSubTable:', f0.__config__.isSubTable);
|
||
console.log(' ⭐ tag:', f0.__config__.tag);
|
||
console.log(' ⭐ visibility:', f0.__config__.visibility);
|
||
|
||
console.log('\n=== select 字段的 options ===');
|
||
const statusField = formData.fields.find(f => f.__vModel__ === 'f_status');
|
||
console.log(' status field options:', JSON.stringify(statusField?.options));
|
||
console.log(' status field dataType:', statusField?.__config__?.dataType);
|
||
|
||
console.log('\n=== 顶层 defaultDataForm 详情 ===');
|
||
console.log(' webType:', result.webType, '(=2 表单+列表)');
|
||
console.log(' state:', result.state, '(=1 已发布)');
|
||
console.log(' type:', result.type, '(=1 自定义)');
|
||
console.log(' dbLinkId:', result.dbLinkId, '(="0" 主库)');
|
||
console.log(' tables:', result.tables);
|
||
console.log(' columnData:', result.columnData);
|
||
|
||
console.log('\n=== 验证 BS-51/BS-54 修复 ===');
|
||
console.log(' ✅ formId 存在:', !!f0.__config__.formId);
|
||
console.log(' ✅ renderKey 存在:', !!f0.__config__.renderKey);
|
||
console.log(' ✅ isSubTable 存在:', f0.__config__.isSubTable === false);
|
||
console.log(' ✅ select 组件 options 存在:', Array.isArray(statusField?.options));
|
||
console.log(' ✅ options 不为空:', (statusField?.options?.length ?? 0) > 0);
|
||
} catch (e) {
|
||
console.error('❌ Failed:', e.message);
|
||
console.error('Stack:', e.stack?.split('\n').slice(0, 8).join('\n'));
|
||
}
|