const fs = require('fs'); const http = require('http'); const BASE = 'cfc.iwintrue.com'; let out = 'CFC API v3 Results\n==================\n\n'; let adminToken = null; function api(path, body, headers) { return new Promise((resolve) => { const data = body ? JSON.stringify(body) : ''; const options = { hostname: BASE, port: 80, path: path, method: 'POST', headers: { 'Content-Type': 'application/json', ...headers, 'Content-Length': Buffer.byteLength(data) }, }; const req = http.request(options, (res) => { let d = ''; res.on('data', c => d += c); res.on('end', () => { try { resolve({ status: res.statusCode, data: JSON.parse(d) }); } catch { resolve({ status: res.statusCode, data: null, raw: d.slice(0, 100) }); } }); }); req.on('error', () => resolve({ error: 'network' })); if (data) req.write(data); req.end(); }); } async function admin(path, body) { if (!adminToken) return { error: 'notoken' }; return api(path, body, { Authorization: 'Bearer ' + adminToken }); } async function run() { // 1. Login await api('/api/admin-auth/send-code', { phone: '13800138000' }); const lr = await api('/api/admin-auth/login', { phone: '13800138000', code: '123456' }); if (lr.data?.data?.token) { adminToken = lr.data.data.token; out += '[PASS] Admin login userId=' + (lr.data.data.adminId || 7) + '\n'; } else { out += '[FAIL] Admin login: ' + (lr.raw || JSON.stringify(lr.data)) + '\n'; } // 2. NEW-01 Energy rule create const er = await admin('/api/admin/energy-rule/create', { ruleName: 'Auto_NEW01_' + Date.now(), dimensionCode: 'body', eventType: 'checkin', energyValue: 5, status: 1, }); if (er.error) { out += '[FAIL] NEW-01 energy-rule create: ' + er.error + '\n'; } else if (er.status === 200 || er.data?.code === 200) { const id = er.data?.data?.id || er.data?.id; out += '[PASS] NEW-01 energy-rule create id=' + id + '\n'; } else { out += '[FAIL] NEW-01 energy-rule create code=' + er.status + ' msg=' + (er.data?.message || '') + '\n'; } // 3. NEW-02 SKU list const sku = await admin('/api/admin/product/sku/list', { productId: 1 }); if (sku.error) { out += '[FAIL] NEW-02 sku/list: ' + sku.error + '\n'; } else if (sku.status === 200 || sku.data?.code === 200) { out += '[PASS] NEW-02 sku/list count=' + (Array.isArray(sku.data?.data) ? sku.data.data.length : '?') + '\n'; } else { out += '[FAIL] NEW-02 sku/list code=' + sku.status + ' msg=' + (sku.data?.message || '') + '\n'; } // 4. NEW-03 Activity publish const act = await admin('/api/activity/create', { title: 'Auto_NEW03_' + Date.now(), dimensionCode: 'body', startTime: '2026-07-15 10:00:00', endTime: '2026-07-15 12:00:00', location: 'test', maxParticipants: 10, }); const actId = act.data?.data?.id || act.data?.id; if (actId) { out += '[INFO] Activity created id=' + actId + '\n'; const pub = await admin('/api/activity/publish', { id: actId }); if (pub.status === 200 || pub.data?.code === 200) { out += '[PASS] NEW-03 activity publish id=' + actId + '\n'; } else { out += '[FAIL] NEW-03 activity publish code=' + pub.status + ' msg=' + (pub.data?.message || '') + '\n'; } } else { out += '[FAIL] NEW-03 activity create code=' + act.status + '\n'; } // 5. ISSUE-001 vendor unshelve const vs = await api('/api/product/shelve', { productId: 7, action: 'unshelve' }, { 'X-User-Id': '81069' }); if (vs.status === 200 || vs.data?.code === 200) { out += '[PASS] ISSUE-001 vendor unshelve product 7\n'; } else { out += '[FAIL] ISSUE-001 vendor unshelve code=' + vs.status + ' msg=' + (vs.data?.message || '') + '\n'; } // 6. ISSUE-003 create user const phone = '139' + String(Math.floor(Math.random() * 1e9)).padStart(9, '0'); const cu = await admin('/api/admin/users/create', { phone, password: 'Test123456', name: 'AutoTest', role: 'parent' }); if (cu.error) { out += '[FAIL] ISSUE-003 create user: ' + cu.error + '\n'; } else if (cu.status === 200 || cu.data?.code === 200) { out += '[PASS] ISSUE-003 create user id=' + (cu.data?.data?.id || cu.data?.id) + '\n'; } else { out += '[FAIL] ISSUE-003 create user code=' + cu.status + '\n'; } // 7. New API grantEnergy const ge = await admin('/api/energy/grantEnergy', { childId: 1, taskId: 1, amount: 10, dimensionCode: 'body' }); if (ge.error) { out += '[FAIL] energy.grantEnergy: ' + ge.error + '\n'; } else if (ge.status === 200 || ge.data?.code === 200) { out += '[PASS] energy.grantEnergy\n'; } else { out += '[FAIL] energy.grantEnergy code=' + ge.status + ' msg=' + (ge.data?.message || '') + '\n'; } // 8. New API growth.getRecordsByChild const gr = await admin('/api/growth/record/child/1', {}); if (gr.error) { out += '[FAIL] growth.getRecordsByChild: ' + gr.error + '\n'; } else if (gr.status === 200 || gr.data?.code === 200) { out += '[PASS] growth.getRecordsByChild count=' + (Array.isArray(gr.data?.data) ? gr.data.data.length : '?') + '\n'; } else { out += '[FAIL] growth.getRecordsByChild code=' + gr.status + '\n'; } fs.writeFileSync('C:/code/cfc/tests/v3_http_result.txt', out, 'utf8'); } run().catch(e => { fs.writeFileSync('C:/code/cfc/tests/v3_http_result.txt', 'ERROR: ' + e.message, 'utf8'); });