| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /**
- * 验证 DMS 内网双 c_ 字段编解码(public/config.js 的 fieldDoubleCPrefix)。
- *
- * node harness/tools/verify-dms-field-prefix.mjs
- */
- import { spawnSync } from 'node:child_process';
- const build = spawnSync(
- 'npx',
- [
- 'esbuild',
- 'harness/tools/_entry-dms.ts',
- '--bundle',
- '--format=esm',
- '--outfile=harness/tools/_dms-client.mjs',
- '--alias:@=./src',
- "--define:import.meta.env={}",
- ],
- { encoding: 'utf8', shell: true }
- );
- if (build.status !== 0) {
- console.error(build.stderr || build.stdout);
- process.exit(1);
- }
- let pass = 0;
- let fail = 0;
- const check = (label, ok, extra = '') => {
- if (ok) {
- pass += 1;
- console.log(` ok ${label}${extra ? ` —— ${extra}` : ''}`);
- } else {
- fail += 1;
- console.log(` FAIL ${label}${extra ? ` —— ${extra}` : ''}`);
- }
- };
- const loadClient = async (doublePrefix, columns) => {
- globalThis.__ZHAOSHANG_CONFIG__ = {
- dms: {
- columns: columns || { honor: 1, session: 2, enterprise: 3, record: 4 },
- models: { honor: 11, session: 22, enterprise: 33, record: 44 },
- fieldDoubleCPrefix: doublePrefix,
- },
- };
- // ESM 有模块缓存:用 query 强制新实例,且须在 import 前写好 globalThis(栏目常量在加载时固化)
- return import(`./_dms-client.mjs?t=${Date.now()}-${doublePrefix}-${Math.random()}`);
- };
- console.log('\n【1】fieldDoubleCPrefix = false(外网)');
- {
- const m = await loadClient(false);
- check('encode 保持 c_question', m.encodeDmsFieldName('c_question') === 'c_question');
- check('encode 不动 title', m.encodeDmsFieldName('title') === 'title');
- check('decode 保持 c_c_question', m.decodeDmsFieldName('c_c_question') === 'c_c_question');
- const row = m.encodeDmsRow({ c_question: 'q', title: 't', id: '1' });
- check('encodeRow 键不变', row.c_question === 'q' && row.title === 't' && row.id === '1');
- check('isDmsFieldDoubleCPrefix=false', m.isDmsFieldDoubleCPrefix() === false);
- }
- console.log('\n【2】fieldDoubleCPrefix = true(内网)');
- {
- const m = await loadClient(true);
- check('isDmsFieldDoubleCPrefix=true', m.isDmsFieldDoubleCPrefix() === true);
- check('encode c_question → c_c_question', m.encodeDmsFieldName('c_question') === 'c_c_question');
- check('encode 已是 c_c_ 不再叠', m.encodeDmsFieldName('c_c_question') === 'c_c_question');
- check('encode 不动 title / id', m.encodeDmsFieldName('title') === 'title' && m.encodeDmsFieldName('id') === 'id');
- check('decode c_c_question → c_question', m.decodeDmsFieldName('c_c_question') === 'c_question');
- const encoded = m.encodeDmsRow({ c_question: 'q', c_session_id: 's', title: 't', id: '1' });
- check(
- 'encodeRow',
- encoded.c_c_question === 'q' &&
- encoded.c_c_session_id === 's' &&
- encoded.title === 't' &&
- encoded.id === '1' &&
- encoded.c_question === undefined
- );
- const decoded = m.decodeDmsRow({ c_c_question: 'q', c_c_record_id: 'r', title: 't', id: '1' });
- check(
- 'decodeRow',
- decoded.c_question === 'q' &&
- decoded.c_record_id === 'r' &&
- decoded.title === 't' &&
- decoded.id === '1' &&
- decoded.c_c_question === undefined
- );
- }
- console.log('\n【3】栏目 id 读 config');
- {
- const m = await loadClient(true, { honor: 101, session: 102, enterprise: 103, record: 104 });
- check('DMS_COLUMN_HONOR 来自 config', m.DMS_COLUMN_HONOR === 101);
- check('DMS_COLUMN_SESSION 来自 config', m.DMS_COLUMN_SESSION === 102);
- check('DMS_COLUMN_ENTERPRISE 来自 config', m.DMS_COLUMN_ENTERPRISE === 103);
- check('DMS_COLUMN_RECORD 来自 config', m.DMS_COLUMN_RECORD === 104);
- check('DMS_MODEL_RECORD 来自 config', m.DMS_MODEL_RECORD === 44);
- }
- console.log(`\n结果:${pass} 通过,${fail} 失败`);
- process.exit(fail ? 1 : 0);
|