const http = require('http'); const PORT = process.env.PORT || 9898; function request(options, data = null) { return new Promise((resolve, reject) => { const req = http.request({ port: PORT, ...options }, (res) => { let body = ''; res.on('data', chunk => body += chunk); res.on('end', () => { try { resolve({ status: res.statusCode, data: JSON.parse(body) }); } catch (e) { resolve({ status: res.statusCode, text: body }); } }); }); req.on('error', reject); if (data) { req.write(typeof data === 'string' ? data : JSON.stringify(data)); } req.end(); }); } async function runTests() { console.log('--- Starting API End-to-End Verification ---'); // 1. Get Babies const babiesRes = await request({ hostname: 'localhost', path: '/api/babies', method: 'GET' }); console.log('1. GET /api/babies:', babiesRes.status, 'Babies found:', babiesRes.data.data.length); const baby = babiesRes.data.data[0]; console.log(' Active baby:', baby.name, 'ID:', baby.baby_id, 'Age:', baby.ageText); // 2. Test Baby Update (PUT /api/babies/:babyId) const updateRes = await request({ hostname: 'localhost', path: `/api/babies/${baby.baby_id}`, method: 'PUT', headers: { 'Content-Type': 'application/json' } }, { name: baby.name, gender: baby.gender, birthDate: baby.birth_date, themeColor: baby.theme_color }); console.log('2. PUT /api/babies/:babyId:', updateRes.status, 'Success:', updateRes.data.success); // 3. Test Custom Metric Creation & Query const customRes = await request({ hostname: 'localhost', path: '/api/custom-metrics', method: 'POST', headers: { 'Content-Type': 'application/json' } }, { babyId: baby.baby_id, name: '补充维D3滴剂', icon: '💊', unit: '滴', dailyTarget: 1, type: 'count' }); console.log('3. POST /api/custom-metrics:', customRes.status, 'Success:', customRes.data.success); const listCustomRes = await request({ hostname: 'localhost', path: `/api/custom-metrics?babyId=${baby.baby_id}`, method: 'GET' }); console.log(' GET /api/custom-metrics:', listCustomRes.status, 'Metrics count:', listCustomRes.data.data.length); // 4. Test Quick Logging: Bottle Record const bottleRes = await request({ hostname: 'localhost', path: '/api/records', method: 'POST', headers: { 'Content-Type': 'application/json' } }, { babyId: baby.baby_id, operatorName: '宝妈', activityType: 'bottle', durationMinutes: 15, payload: { amountMl: 120, milkType: 'formula', note: '测试120ml配方奶' } }); console.log('4. POST /api/records (bottle 120ml):', bottleRes.status, 'Success:', bottleRes.data.success); // 5. Test Quick Logging: Custom Metric const customLogRes = await request({ hostname: 'localhost', path: '/api/records', method: 'POST', headers: { 'Content-Type': 'application/json' } }, { babyId: baby.baby_id, operatorName: '宝爸', activityType: 'custom', durationMinutes: 1, payload: { name: '补充维D3滴剂', icon: '💊', unit: '滴', value: 1, note: '餐后按时服用' } }); console.log('5. POST /api/records (custom goal):', customLogRes.status, 'Success:', customLogRes.data.success); // 6. Test Summary (KPI, 4-Period Slots, Custom Goals) const summaryRes = await request({ hostname: 'localhost', path: `/api/summary?babyId=${baby.baby_id}`, method: 'GET' }); console.log('6. GET /api/summary:', summaryRes.status, 'Success:', summaryRes.data.success); const s = summaryRes.data.data; console.log(' KPI Total Milk:', s.kpi.feeding.totalBottleMl, 'ml'); console.log(' KPI Total Sleep:', s.kpi.sleep.totalSleepText); console.log(' KPI Diaper:', s.kpi.diaper.diaperCount, 'times'); console.log(' Period Slots available:', Object.keys(s.periodSlots)); console.log(' Period Dawn items:', s.periodSlots.dawn.items.length); console.log(' Period Morning items:', s.periodSlots.morning.items.length); console.log(' Period Afternoon items:', s.periodSlots.afternoon.items.length); console.log(' Period Evening items:', s.periodSlots.evening.items.length); // 7. Test Care Guides const guidesRes = await request({ hostname: 'localhost', path: '/api/care-guides', method: 'GET' }); console.log('7. GET /api/care-guides:', guidesRes.status, 'Guides count:', guidesRes.data.data.length); console.log('--- All API Tests Passed Successfully! ---'); } runTests().catch(console.error);