run_v3_http.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const fs = require('fs');
  2. const http = require('http');
  3. const BASE = 'cfc.iwintrue.com';
  4. let out = 'CFC API v3 Results\n==================\n\n';
  5. let adminToken = null;
  6. function api(path, body, headers) {
  7. return new Promise((resolve) => {
  8. const data = body ? JSON.stringify(body) : '';
  9. const options = {
  10. hostname: BASE,
  11. port: 80,
  12. path: path,
  13. method: 'POST',
  14. headers: { 'Content-Type': 'application/json', ...headers, 'Content-Length': Buffer.byteLength(data) },
  15. };
  16. const req = http.request(options, (res) => {
  17. let d = '';
  18. res.on('data', c => d += c);
  19. res.on('end', () => {
  20. try { resolve({ status: res.statusCode, data: JSON.parse(d) }); }
  21. catch { resolve({ status: res.statusCode, data: null, raw: d.slice(0, 100) }); }
  22. });
  23. });
  24. req.on('error', () => resolve({ error: 'network' }));
  25. if (data) req.write(data);
  26. req.end();
  27. });
  28. }
  29. async function admin(path, body) {
  30. if (!adminToken) return { error: 'notoken' };
  31. return api(path, body, { Authorization: 'Bearer ' + adminToken });
  32. }
  33. async function run() {
  34. // 1. Login
  35. await api('/api/admin-auth/send-code', { phone: '13800138000' });
  36. const lr = await api('/api/admin-auth/login', { phone: '13800138000', code: '123456' });
  37. if (lr.data?.data?.token) {
  38. adminToken = lr.data.data.token;
  39. out += '[PASS] Admin login userId=' + (lr.data.data.adminId || 7) + '\n';
  40. } else {
  41. out += '[FAIL] Admin login: ' + (lr.raw || JSON.stringify(lr.data)) + '\n';
  42. }
  43. // 2. NEW-01 Energy rule create
  44. const er = await admin('/api/admin/energy-rule/create', {
  45. ruleName: 'Auto_NEW01_' + Date.now(), dimensionCode: 'body', eventType: 'checkin', energyValue: 5, status: 1,
  46. });
  47. if (er.error) {
  48. out += '[FAIL] NEW-01 energy-rule create: ' + er.error + '\n';
  49. } else if (er.status === 200 || er.data?.code === 200) {
  50. const id = er.data?.data?.id || er.data?.id;
  51. out += '[PASS] NEW-01 energy-rule create id=' + id + '\n';
  52. } else {
  53. out += '[FAIL] NEW-01 energy-rule create code=' + er.status + ' msg=' + (er.data?.message || '') + '\n';
  54. }
  55. // 3. NEW-02 SKU list
  56. const sku = await admin('/api/admin/product/sku/list', { productId: 1 });
  57. if (sku.error) {
  58. out += '[FAIL] NEW-02 sku/list: ' + sku.error + '\n';
  59. } else if (sku.status === 200 || sku.data?.code === 200) {
  60. out += '[PASS] NEW-02 sku/list count=' + (Array.isArray(sku.data?.data) ? sku.data.data.length : '?') + '\n';
  61. } else {
  62. out += '[FAIL] NEW-02 sku/list code=' + sku.status + ' msg=' + (sku.data?.message || '') + '\n';
  63. }
  64. // 4. NEW-03 Activity publish
  65. const act = await admin('/api/activity/create', {
  66. title: 'Auto_NEW03_' + Date.now(), dimensionCode: 'body',
  67. startTime: '2026-07-15 10:00:00', endTime: '2026-07-15 12:00:00',
  68. location: 'test', maxParticipants: 10,
  69. });
  70. const actId = act.data?.data?.id || act.data?.id;
  71. if (actId) {
  72. out += '[INFO] Activity created id=' + actId + '\n';
  73. const pub = await admin('/api/activity/publish', { id: actId });
  74. if (pub.status === 200 || pub.data?.code === 200) {
  75. out += '[PASS] NEW-03 activity publish id=' + actId + '\n';
  76. } else {
  77. out += '[FAIL] NEW-03 activity publish code=' + pub.status + ' msg=' + (pub.data?.message || '') + '\n';
  78. }
  79. } else {
  80. out += '[FAIL] NEW-03 activity create code=' + act.status + '\n';
  81. }
  82. // 5. ISSUE-001 vendor unshelve
  83. const vs = await api('/api/product/shelve', { productId: 7, action: 'unshelve' }, { 'X-User-Id': '81069' });
  84. if (vs.status === 200 || vs.data?.code === 200) {
  85. out += '[PASS] ISSUE-001 vendor unshelve product 7\n';
  86. } else {
  87. out += '[FAIL] ISSUE-001 vendor unshelve code=' + vs.status + ' msg=' + (vs.data?.message || '') + '\n';
  88. }
  89. // 6. ISSUE-003 create user
  90. const phone = '139' + String(Math.floor(Math.random() * 1e9)).padStart(9, '0');
  91. const cu = await admin('/api/admin/users/create', { phone, password: 'Test123456', name: 'AutoTest', role: 'parent' });
  92. if (cu.error) {
  93. out += '[FAIL] ISSUE-003 create user: ' + cu.error + '\n';
  94. } else if (cu.status === 200 || cu.data?.code === 200) {
  95. out += '[PASS] ISSUE-003 create user id=' + (cu.data?.data?.id || cu.data?.id) + '\n';
  96. } else {
  97. out += '[FAIL] ISSUE-003 create user code=' + cu.status + '\n';
  98. }
  99. // 7. New API grantEnergy
  100. const ge = await admin('/api/energy/grantEnergy', { childId: 1, taskId: 1, amount: 10, dimensionCode: 'body' });
  101. if (ge.error) {
  102. out += '[FAIL] energy.grantEnergy: ' + ge.error + '\n';
  103. } else if (ge.status === 200 || ge.data?.code === 200) {
  104. out += '[PASS] energy.grantEnergy\n';
  105. } else {
  106. out += '[FAIL] energy.grantEnergy code=' + ge.status + ' msg=' + (ge.data?.message || '') + '\n';
  107. }
  108. // 8. New API growth.getRecordsByChild
  109. const gr = await admin('/api/growth/record/child/1', {});
  110. if (gr.error) {
  111. out += '[FAIL] growth.getRecordsByChild: ' + gr.error + '\n';
  112. } else if (gr.status === 200 || gr.data?.code === 200) {
  113. out += '[PASS] growth.getRecordsByChild count=' + (Array.isArray(gr.data?.data) ? gr.data.data.length : '?') + '\n';
  114. } else {
  115. out += '[FAIL] growth.getRecordsByChild code=' + gr.status + '\n';
  116. }
  117. fs.writeFileSync('C:/code/cfc/tests/v3_http_result.txt', out, 'utf8');
  118. }
  119. run().catch(e => {
  120. fs.writeFileSync('C:/code/cfc/tests/v3_http_result.txt', 'ERROR: ' + e.message, 'utf8');
  121. });