v11_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const BASE = 'cfc.iwintrue.com';
  5. const PORT = 80;
  6. const RESULT_FILE = path.join(__dirname, 'v11_result.txt');
  7. let out = '';
  8. let adminToken = null;
  9. function post(path, body, headers) {
  10. return new Promise(resolve => {
  11. const data = JSON.stringify(body);
  12. const req = http.request({ hostname: BASE, port: PORT, path, method: 'POST', headers: { 'Content-Type': 'application/json', ...headers, 'Content-Length': Buffer.byteLength(data) } }, res => {
  13. let d = '';
  14. res.on('data', c => d += c);
  15. res.on('end', () => {
  16. try { resolve({ status: res.statusCode, data: JSON.parse(d) }); }
  17. catch { resolve({ status: res.statusCode, raw: d.slice(0, 300) }); }
  18. });
  19. });
  20. req.on('error', e => resolve({ error: e.message }));
  21. req.write(data); req.end();
  22. });
  23. }
  24. async function main() {
  25. out += '=== CFC API v11 Results ===\n';
  26. out += 'Time: ' + new Date().toISOString() + '\n\n';
  27. // ===== Admin Login =====
  28. await post('/api/admin-auth/send-code', { phone: '13800138000' });
  29. const lr = await post('/api/admin-auth/login', { phone: '13800138000', code: '123456' });
  30. if (lr.data && lr.data.data && lr.data.data.token) {
  31. adminToken = lr.data.data.token;
  32. out += '[PASS] Admin login userId=' + (lr.data.data.adminId || 7) + '\n';
  33. } else {
  34. out += '[FAIL] Admin login: status=' + lr.status + ' raw=' + (lr.raw || JSON.stringify(lr.data)) + '\n';
  35. fs.writeFileSync(RESULT_FILE, out);
  36. return;
  37. }
  38. function check(name, resp) {
  39. if (resp.status === 200 && resp.data && resp.data.code === 200) {
  40. out += '[PASS] ' + name + '\n';
  41. } else {
  42. out += '[FAIL] ' + name + ': code=' + resp.status + ' msg=' + (resp.data ? (resp.data.message || JSON.stringify(resp.data)) : '') + '\n';
  43. }
  44. }
  45. function info(name, details) {
  46. out += '[INFO] ' + name + ': ' + details + '\n';
  47. }
  48. // ===================================================================
  49. // REGRESSION TESTS (from v9)
  50. // ===================================================================
  51. // NEW-01: Energy rule create
  52. const rc = 'CHK_' + Date.now();
  53. const er = await post('/api/admin/energy-rule/create', {
  54. ruleName: 'Test_' + Date.now(), dimensionCode: 'body', eventType: 'checkin',
  55. amount: 5, status: 1, action: 'increase', priority: 0, ruleCode: rc,
  56. }, { Authorization: 'Bearer ' + adminToken });
  57. check('REG Energy rule create', er);
  58. // NEW-02: SKU list
  59. const sku = await post('/api/admin/product/sku/list', { productId: 7 }, { Authorization: 'Bearer ' + adminToken });
  60. check('REG SKU list', sku);
  61. // NEW-03: Activity create + publish
  62. const act = await post('/api/activity/create', {
  63. title: 'Test_' + Date.now(), dimensionCode: 'body',
  64. startTime: new Date(Date.now() + 86400000 * 7).toISOString(),
  65. endTime: new Date(Date.now() + 86400000 * 8).toISOString(),
  66. location: 'test', maxParticipants: 10,
  67. }, { Authorization: 'Bearer ' + adminToken });
  68. const actId = act.data && act.data.data && (act.data.data.id || act.data.data);
  69. if (actId) {
  70. const pub = await post('/api/activity/publish', { id: actId }, { Authorization: 'Bearer ' + adminToken });
  71. check('REG Activity publish', pub);
  72. } else {
  73. info('REG Activity create', 'no id: ' + JSON.stringify(act.data));
  74. }
  75. // Public endpoints
  76. check('REG Articles list (public)', await post('/api/articles/list', { page: 1, size: 5 }));
  77. check('REG Activity list (public)', await post('/api/activity/list', { page: 1, size: 5 }));
  78. // Admin management
  79. check('REG User list', await post('/api/admin/users', { page: 1, size: 10 }, { Authorization: 'Bearer ' + adminToken }));
  80. check('REG Energy dims', await post('/api/admin/energy-rule/dimensions', {}, { Authorization: 'Bearer ' + adminToken }));
  81. check('REG Vendor list', await post('/api/admin/vendor/list', {}, { Authorization: 'Bearer ' + adminToken }));
  82. check('REG Admin articles', await post('/api/admin/articles/list', { page: 1, size: 5 }, { Authorization: 'Bearer ' + adminToken }));
  83. // Points & Energy
  84. check('REG Points balance', await post('/api/points/balance', { childId: 1 }, { Authorization: 'Bearer ' + adminToken }));
  85. check('REG Energy overview', await post('/api/energy/overview', { childId: 1 }, { Authorization: 'Bearer ' + adminToken }));
  86. // Article publish (specific ID)
  87. const ap = await post('/api/admin/articles/publish', { id: 13, status: 'published' }, { Authorization: 'Bearer ' + adminToken });
  88. check('REG Article publish', ap);
  89. // Create user
  90. const phone = '139' + String(Math.floor(Math.random() * 1e9)).padStart(9, '0');
  91. check('REG Create user', await post('/api/admin/users/create', { phone, password: 'Test123456', name: 'AutoTest', role: 'parent' }, { Authorization: 'Bearer ' + adminToken }));
  92. // Guide activity list
  93. const gaList = await post('/api/guide/activities/list', { status: '' }, { Authorization: 'Bearer ' + adminToken });
  94. if (gaList.status === 200 && gaList.data && gaList.data.code === 200) {
  95. out += '[PASS] REG Guide activity list (endpoint OK)\n';
  96. } else {
  97. info('REG Guide activity list', gaList.status + ' ' + (gaList.data ? (gaList.data.message || '') : ''));
  98. }
  99. // ===================================================================
  100. // ISSUE-001: Vendor login (with test-mode code bypass)
  101. // ===================================================================
  102. await post('/api/auth/send-code', { phone: '13800138001' });
  103. const vl = await post('/api/auth/phone-login', { phone: '13800138001', code: '123456' });
  104. if (vl.data && vl.data.data && vl.data.data.token) {
  105. out += '[PASS] ISSUE-001 Vendor login (with test-mode bypass)\n';
  106. const vs = await post('/api/product/shelve', { productId: 7, action: 'unshelve' }, { Authorization: 'Bearer ' + vl.data.data.token });
  107. check('ISSUE-001 Vendor unshelve', vs);
  108. } else {
  109. info('ISSUE-001 Vendor login', vl.status + ' ' + JSON.stringify(vl.data));
  110. }
  111. // ===================================================================
  112. // NEW FEATURE: Food Management (AdminFoodController)
  113. // ===================================================================
  114. // FOOD-01: Food list
  115. const foodList = await post('/api/admin/foods/list', { page: 1, size: 10 }, { Authorization: 'Bearer ' + adminToken });
  116. check('FOOD-01 Food list', foodList);
  117. // FOOD-02: Food create
  118. const foodCreate = await post('/api/admin/foods/create', {
  119. name: 'TestFood_' + Date.now(),
  120. category: 'vegetable',
  121. unit: '斤',
  122. nutrition: '{"calories":25,"protein":1.5}',
  123. }, { Authorization: 'Bearer ' + adminToken });
  124. check('FOOD-02 Food create', foodCreate);
  125. // FOOD-03: Food detail with months
  126. const foodId = foodCreate.data && foodCreate.data.data;
  127. if (foodId && typeof foodId !== 'string') {
  128. const foodDetail = await post('/api/admin/foods/detail', { id: foodId }, { Authorization: 'Bearer ' + adminToken });
  129. check('FOOD-03 Food detail', foodDetail);
  130. // FOOD-04: Save seasonal months
  131. const saveMonths = await post('/api/admin/foods/saveMonths', { id: foodId, months: [6, 7, 8] }, { Authorization: 'Bearer ' + adminToken });
  132. check('FOOD-04 Save months', saveMonths);
  133. // FOOD-05: Verify months after save
  134. const detail2 = await post('/api/admin/foods/detail', { id: foodId }, { Authorization: 'Bearer ' + adminToken });
  135. if (detail2.data && detail2.data.data && detail2.data.data.months) {
  136. const months = detail2.data.data.months;
  137. if (months.includes(6) && months.includes(7) && months.includes(8)) {
  138. out += '[PASS] FOOD-05 Verify months saved (6,7,8)\n';
  139. } else {
  140. info('FOOD-05 Verify months', 'expected 6,7,8 got ' + JSON.stringify(months));
  141. }
  142. } else {
  143. info('FOOD-05 Verify months', 'months not in response: ' + JSON.stringify(detail2.data));
  144. }
  145. // FOOD-06: Food delete
  146. check('FOOD-06 Food delete', await post('/api/admin/foods/delete', { id: foodId }, { Authorization: 'Bearer ' + adminToken }));
  147. } else {
  148. info('FOOD-03~06', 'food create no id: ' + JSON.stringify(foodCreate.data));
  149. }
  150. // ===================================================================
  151. // NEW FEATURE: Article Auto-Tagging
  152. // ===================================================================
  153. const articleCreate = await post('/api/admin/articles/create', {
  154. title: 'AutoTagTest_' + Date.now(),
  155. content: '今天天气很好,适合户外跑步锻炼身体。多吃水果蔬菜对健康有益。',
  156. categoryId: 1,
  157. status: 'draft',
  158. coverImage: '',
  159. summary: 'Auto tag test article',
  160. }, { Authorization: 'Bearer ' + adminToken });
  161. if (articleCreate.data && articleCreate.data.data) {
  162. const articleId = articleCreate.data.data.id || articleCreate.data.data;
  163. out += '[PASS] AUTO-TAG-01 Article create\n';
  164. if (typeof articleId === 'number') {
  165. // Check article detail to see auto-generated tags
  166. const articleDetail = await post('/api/admin/articles/detail', { id: articleId }, { Authorization: 'Bearer ' + adminToken });
  167. if (articleDetail.data && articleDetail.data.code === 200) {
  168. const article = articleDetail.data.data;
  169. if (article.tags && article.tags.length > 0) {
  170. out += '[PASS] AUTO-TAG-02 Tags auto-generated: ' + article.tags + '\n';
  171. } else {
  172. info('AUTO-TAG-02 Tags', 'no tags field in response: ' + JSON.stringify(article).slice(0, 200));
  173. }
  174. } else {
  175. info('AUTO-TAG-02 Tags detail', JSON.stringify(articleDetail.data));
  176. }
  177. // Cleanup
  178. await post('/api/admin/articles/delete', { id: articleId }, { Authorization: 'Bearer ' + adminToken });
  179. }
  180. } else {
  181. info('AUTO-TAG-01', 'create failed: ' + JSON.stringify(articleCreate.data));
  182. }
  183. // ===================================================================
  184. // SUMMARY
  185. // ===================================================================
  186. const lines = out.split('\n').filter(l => l.startsWith('[PASS]') || l.startsWith('[FAIL]') || l.startsWith('[SKIP]') || l.startsWith('[INFO]'));
  187. const passCount = lines.filter(l => l.startsWith('[PASS]')).length;
  188. const failCount = lines.filter(l => l.startsWith('[FAIL]')).length;
  189. const skipCount = lines.filter(l => l.startsWith('[SKIP]')).length;
  190. const infoCount = lines.filter(l => l.startsWith('[INFO]')).length;
  191. out += '\n=== Summary: ' + passCount + ' PASS / ' + failCount + ' FAIL / ' + skipCount + ' SKIP / ' + infoCount + ' INFO / ' + lines.length + ' Total ===\n';
  192. fs.writeFileSync(RESULT_FILE, out);
  193. console.log('V11_DONE - ' + RESULT_FILE);
  194. }
  195. main().catch(e => {
  196. fs.writeFileSync(RESULT_FILE, 'ERROR: ' + e.message);
  197. console.error('V11_ERROR: ' + e.message);
  198. });