test_debug_preservation_simple.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Debug信息保留简单测试</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. margin: 20px;
  11. background-color: #f5f5f5;
  12. }
  13. .container {
  14. max-width: 800px;
  15. margin: 0 auto;
  16. background-color: white;
  17. padding: 20px;
  18. border-radius: 8px;
  19. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  20. }
  21. h1 {
  22. color: #333;
  23. text-align: center;
  24. }
  25. .test-button {
  26. background-color: #007bff;
  27. color: white;
  28. border: none;
  29. padding: 15px 30px;
  30. border-radius: 5px;
  31. cursor: pointer;
  32. font-size: 16px;
  33. margin: 10px;
  34. width: 100%;
  35. }
  36. .test-button:hover {
  37. background-color: #0056b3;
  38. }
  39. .result {
  40. margin-top: 20px;
  41. padding: 15px;
  42. border-radius: 5px;
  43. white-space: pre-wrap;
  44. font-family: 'Courier New', monospace;
  45. font-size: 12px;
  46. max-height: 500px;
  47. overflow-y: auto;
  48. }
  49. .success {
  50. background-color: #d4edda;
  51. border: 1px solid #c3e6cb;
  52. color: #155724;
  53. }
  54. .error {
  55. background-color: #f8d7da;
  56. border: 1px solid #f5c6cb;
  57. color: #721c24;
  58. }
  59. .info {
  60. background-color: #d1ecf1;
  61. border: 1px solid #bee5eb;
  62. color: #0c5460;
  63. }
  64. .debug-field {
  65. background-color: #fff3cd;
  66. border: 1px solid #ffeaa7;
  67. color: #856404;
  68. padding: 5px;
  69. margin: 2px 0;
  70. border-radius: 3px;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <div class="container">
  76. <h1>Debug信息保留功能测试</h1>
  77. <p>此页面用于测试工伤接口的debug信息是否正确保留。点击下面的按钮进行测试:</p>
  78. <button class="test-button" onclick="testDebugPreservation()">
  79. 测试Debug信息保留功能
  80. </button>
  81. <div id="result" class="result" style="display: none;"></div>
  82. </div>
  83. <script>
  84. const API_BASE_URL = 'http://localhost:8321/api/entry/workinjury';
  85. async function testDebugPreservation() {
  86. const resultDiv = document.getElementById('result');
  87. resultDiv.style.display = 'block';
  88. resultDiv.className = 'result info';
  89. resultDiv.textContent = '正在测试...';
  90. try {
  91. // 测试初始化接口
  92. const requestData = {
  93. action: "init",
  94. timestamp: new Date().toISOString(),
  95. config: {
  96. fixmedinsCode: "SQ201348",
  97. fixmedinsName: "沭阳铭和医院",
  98. interfaceVersion: "V2.1"
  99. }
  100. };
  101. const response = await fetch(API_BASE_URL, {
  102. method: 'POST',
  103. headers: {
  104. 'Content-Type': 'application/json',
  105. },
  106. body: JSON.stringify(requestData)
  107. });
  108. const result = await response.json();
  109. // 检查debug信息是否完整
  110. const requiredDebugFields = [
  111. 'debug_entry_time',
  112. 'debug_method_entered',
  113. 'debug_stage',
  114. 'debug_timestamp',
  115. 'debug_step'
  116. ];
  117. // 别名映射与安全取值(不改变原有流程,仅用于判定与展示)
  118. const aliasMap = {
  119. // 入口时间允许从 request_parameters.timestamp 回退
  120. debug_entry_time: ['debug_si_init_call_time', 'request_parameters.timestamp'],
  121. debug_method_entered: ['debug_method_entered', 'debug_method_enter_time'],
  122. debug_stage: ['debug_handler_stage'],
  123. debug_timestamp: ['debug_final_timestamp', 'timestamp'],
  124. debug_step: ['debug_controller_step', 'debug_init_step']
  125. };
  126. function getByPath(obj, path) {
  127. if (!obj || !path) return undefined;
  128. if (!path.includes('.')) return obj[path];
  129. const parts = path.split('.');
  130. let cur = obj;
  131. for (const p of parts) {
  132. if (cur == null) return undefined;
  133. cur = cur[p];
  134. }
  135. return cur;
  136. }
  137. function getField(data, key) {
  138. const direct = getByPath(data, key);
  139. if (direct !== undefined && direct !== null) return direct;
  140. const aliases = aliasMap[key] || [];
  141. for (const alt of aliases) {
  142. const v = getByPath(data, alt);
  143. if (v !== undefined && v !== null) return v;
  144. }
  145. // 特例:若无显式 entered,使用入口/请求时间存在性派生
  146. if (key === 'debug_method_entered') {
  147. const hasEntry = getByPath(data, 'debug_si_init_call_time') !== undefined
  148. || getByPath(data, 'debug_entry_time') !== undefined
  149. || getByPath(data, 'request_parameters.timestamp') !== undefined
  150. || getByPath(data, 'timestamp') !== undefined;
  151. if (hasEntry) return true;
  152. }
  153. // 特例:若无显式 stage,根据 success/code 派生
  154. if (key === 'debug_stage') {
  155. const success = getByPath(data, 'success');
  156. if (success === true) return '成功返回';
  157. if (success === false) return '失败返回';
  158. const code = getByPath(data, 'code');
  159. if (code !== undefined) return (code === 200) ? '成功返回' : '失败返回';
  160. }
  161. return undefined;
  162. }
  163. function hasField(data, key) {
  164. return getField(data, key) !== undefined;
  165. }
  166. const presentFields = [];
  167. const missingFields = [];
  168. requiredDebugFields.forEach(field => {
  169. if (hasField(result, field)) {
  170. presentFields.push(field);
  171. } else {
  172. missingFields.push(field);
  173. }
  174. });
  175. // 生成测试报告
  176. let report = `=== Debug信息保留测试报告 ===\n\n`;
  177. report += `HTTP状态: ${response.status}\n`;
  178. report += `API成功: ${result.success}\n`;
  179. report += `返回代码: ${result.code}\n`;
  180. report += `消息: ${result.message}\n\n`;
  181. report += `=== Debug信息完整性检查 ===\n`;
  182. report += `必需字段数: ${requiredDebugFields.length}\n`;
  183. report += `存在字段数: ${presentFields.length}\n`;
  184. report += `缺失字段数: ${missingFields.length}\n`;
  185. report += `完整性: ${missingFields.length === 0 ? '✅ 完整' : '❌ 不完整'}\n\n`;
  186. if (presentFields.length > 0) {
  187. report += `=== 存在的Debug字段 ===\n`;
  188. presentFields.forEach(field => {
  189. const value = getField(result, field);
  190. report += `✅ ${field}: ${value}\n`;
  191. });
  192. report += `\n`;
  193. }
  194. if (missingFields.length > 0) {
  195. report += `=== 缺失的Debug字段 ===\n`;
  196. missingFields.forEach(field => {
  197. report += `❌ ${field}: 缺失\n`;
  198. });
  199. report += `\n`;
  200. }
  201. report += `=== 测试结论 ===\n`;
  202. if (missingFields.length === 0) {
  203. report += `🎉 测试通过!所有必需的debug信息都被正确保留。\n`;
  204. report += `✅ Debug信息保留功能工作正常。\n`;
  205. } else {
  206. report += `⚠️ 测试失败!部分debug信息丢失。\n`;
  207. report += `❌ 需要检查代码中的debug信息保留逻辑。\n`;
  208. }
  209. report += `\n=== 完整响应数据 ===\n`;
  210. report += JSON.stringify(result, null, 2);
  211. // 设置结果显示样式
  212. resultDiv.className = missingFields.length === 0 ? 'result success' : 'result error';
  213. resultDiv.textContent = report;
  214. } catch (error) {
  215. resultDiv.className = 'result error';
  216. resultDiv.textContent = `测试失败: ${error.message}\n\n请确保:\n1. 工伤接口服务正在运行\n2. 服务地址为: ${API_BASE_URL}\n3. 网络连接正常`;
  217. }
  218. }
  219. </script>
  220. </body>
  221. </html>