verify-dms-field-prefix.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * 验证 DMS 内网双 c_ 字段编解码(public/config.js 的 fieldDoubleCPrefix)。
  3. *
  4. * node harness/tools/verify-dms-field-prefix.mjs
  5. */
  6. import { spawnSync } from 'node:child_process';
  7. const build = spawnSync(
  8. 'npx',
  9. [
  10. 'esbuild',
  11. 'harness/tools/_entry-dms.ts',
  12. '--bundle',
  13. '--format=esm',
  14. '--outfile=harness/tools/_dms-client.mjs',
  15. '--alias:@=./src',
  16. "--define:import.meta.env={}",
  17. ],
  18. { encoding: 'utf8', shell: true }
  19. );
  20. if (build.status !== 0) {
  21. console.error(build.stderr || build.stdout);
  22. process.exit(1);
  23. }
  24. let pass = 0;
  25. let fail = 0;
  26. const check = (label, ok, extra = '') => {
  27. if (ok) {
  28. pass += 1;
  29. console.log(` ok ${label}${extra ? ` —— ${extra}` : ''}`);
  30. } else {
  31. fail += 1;
  32. console.log(` FAIL ${label}${extra ? ` —— ${extra}` : ''}`);
  33. }
  34. };
  35. const loadClient = async (doublePrefix, columns) => {
  36. globalThis.__ZHAOSHANG_CONFIG__ = {
  37. dms: {
  38. columns: columns || { honor: 1, session: 2, enterprise: 3, record: 4 },
  39. models: { honor: 11, session: 22, enterprise: 33, record: 44 },
  40. fieldDoubleCPrefix: doublePrefix,
  41. },
  42. };
  43. // ESM 有模块缓存:用 query 强制新实例,且须在 import 前写好 globalThis(栏目常量在加载时固化)
  44. return import(`./_dms-client.mjs?t=${Date.now()}-${doublePrefix}-${Math.random()}`);
  45. };
  46. console.log('\n【1】fieldDoubleCPrefix = false(外网)');
  47. {
  48. const m = await loadClient(false);
  49. check('encode 保持 c_question', m.encodeDmsFieldName('c_question') === 'c_question');
  50. check('encode 不动 title', m.encodeDmsFieldName('title') === 'title');
  51. check('decode 保持 c_c_question', m.decodeDmsFieldName('c_c_question') === 'c_c_question');
  52. const row = m.encodeDmsRow({ c_question: 'q', title: 't', id: '1' });
  53. check('encodeRow 键不变', row.c_question === 'q' && row.title === 't' && row.id === '1');
  54. check('isDmsFieldDoubleCPrefix=false', m.isDmsFieldDoubleCPrefix() === false);
  55. }
  56. console.log('\n【2】fieldDoubleCPrefix = true(内网)');
  57. {
  58. const m = await loadClient(true);
  59. check('isDmsFieldDoubleCPrefix=true', m.isDmsFieldDoubleCPrefix() === true);
  60. check('encode c_question → c_c_question', m.encodeDmsFieldName('c_question') === 'c_c_question');
  61. check('encode 已是 c_c_ 不再叠', m.encodeDmsFieldName('c_c_question') === 'c_c_question');
  62. check('encode 不动 title / id', m.encodeDmsFieldName('title') === 'title' && m.encodeDmsFieldName('id') === 'id');
  63. check('decode c_c_question → c_question', m.decodeDmsFieldName('c_c_question') === 'c_question');
  64. const encoded = m.encodeDmsRow({ c_question: 'q', c_session_id: 's', title: 't', id: '1' });
  65. check(
  66. 'encodeRow',
  67. encoded.c_c_question === 'q' &&
  68. encoded.c_c_session_id === 's' &&
  69. encoded.title === 't' &&
  70. encoded.id === '1' &&
  71. encoded.c_question === undefined
  72. );
  73. const decoded = m.decodeDmsRow({ c_c_question: 'q', c_c_record_id: 'r', title: 't', id: '1' });
  74. check(
  75. 'decodeRow',
  76. decoded.c_question === 'q' &&
  77. decoded.c_record_id === 'r' &&
  78. decoded.title === 't' &&
  79. decoded.id === '1' &&
  80. decoded.c_c_question === undefined
  81. );
  82. }
  83. console.log('\n【3】栏目 id 读 config');
  84. {
  85. const m = await loadClient(true, { honor: 101, session: 102, enterprise: 103, record: 104 });
  86. check('DMS_COLUMN_HONOR 来自 config', m.DMS_COLUMN_HONOR === 101);
  87. check('DMS_COLUMN_SESSION 来自 config', m.DMS_COLUMN_SESSION === 102);
  88. check('DMS_COLUMN_ENTERPRISE 来自 config', m.DMS_COLUMN_ENTERPRISE === 103);
  89. check('DMS_COLUMN_RECORD 来自 config', m.DMS_COLUMN_RECORD === 104);
  90. check('DMS_MODEL_RECORD 来自 config', m.DMS_MODEL_RECORD === 44);
  91. }
  92. console.log(`\n结果:${pass} 通过,${fail} 失败`);
  93. process.exit(fail ? 1 : 0);