const http = require('http'); const fs = require('fs'); const BASE = 'cfc.iwintrue.com'; const PORT = 80; let adminToken = null; let out = ''; function post(path, body, headers) { return new Promise((resolve) => { const data = JSON.stringify(body); const h = { 'Content-Type': 'application/json', ...headers, 'Content-Length': Buffer.byteLength(data) }; const req = http.request({ hostname: BASE, port: PORT, path, method: 'POST', headers: h }, (res) => { let d = ''; res.on('data', c => d += c); res.on('end', () => { try { resolve({ ok: res.statusCode === 200, status: res.statusCode, data: JSON.parse(d) }); } catch { resolve({ ok: false, status: res.statusCode, raw: d.slice(0, 100) }); } }); }); req.on('error', () => resolve({ ok: false, error: 'network' })); req.write(data); req.end(); }); } async function admin(path, body) { if (!adminToken) return { ok: false, error: 'notoken' }; return post(path, body, { Authorization: 'Bearer ' + adminToken }); } async function run() { out += '=== CFC API v3 Results ===\n\n'; // Login await post('/api/admin-auth/send-code', { phone: '13800138000' }); const lr = await post('/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'; fs.writeFileSync('C:/code/cfc/tests/v3_out.txt', out); return; } // 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.ok) out += '[PASS] NEW-01 energy-rule create id=' + (er.data?.data?.id || er.data?.id) + '\n'; else out += '[FAIL] NEW-01 energy-rule create code=' + er.status + ' msg=' + (er.data?.message || '') + '\n'; // NEW-02: SKU list const sku = await admin('/api/admin/product/sku/list', { productId: 1 }); if (sku.ok) out += '[PASS] NEW-02 sku/list count=' + (Array.isArray(sku.data?.data) ? sku.data.data.length : 0) + '\n'; else out += '[FAIL] NEW-02 sku/list code=' + sku.status + ' msg=' + (sku.data?.message || '') + '\n'; // NEW-03: Activity create + 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.ok) 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'; } // ISSUE-001: Vendor unshelve const vs = await post('/api/product/shelve', { productId: 7, action: 'unshelve' }, { 'X-User-Id': '81069' }); if (vs.ok) out += '[PASS] ISSUE-001 vendor unshelve product 7\n'; else out += '[FAIL] ISSUE-001 vendor unshelve code=' + vs.status + ' msg=' + (vs.data?.message || '') + '\n'; // 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.ok) 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'; // New API: grantEnergy const ge = await admin('/api/energy/grantEnergy', { childId: 1, taskId: 1, amount: 10, dimensionCode: 'body' }); if (ge.ok) out += '[PASS] energy.grantEnergy\n'; else out += '[FAIL] energy.grantEnergy code=' + ge.status + ' msg=' + (ge.data?.message || '') + '\n'; // New API: points.checkAndGrantReward const pr = await admin('/api/points/checkAndGrantReward', { childId: 1, amount: 5 }); if (pr.ok || pr.data?.data === -1) out += '[PASS] points.checkAndGrantReward result=' + pr.data?.data + '\n'; else out += '[FAIL] points.checkAndGrantReward code=' + pr.status + '\n'; // New API: getRecordsByChild const gr = await admin('/api/growth/record/child/1', {}); if (gr.ok) out += '[PASS] growth.getRecordsByChild count=' + (Array.isArray(gr.data?.data) ? gr.data.data.length : 0) + '\n'; else out += '[FAIL] growth.getRecordsByChild code=' + gr.status + '\n'; fs.writeFileSync('C:/code/cfc/tests/v3_out.txt', out); console.log('DONE'); // won't be seen but whatever } run().catch(e => { fs.writeFileSync('C:/code/cfc/tests/v3_out.txt', 'ERROR: ' + e.message); });