const API = 'http://localhost:3000/api/v1'; const MOCK_SMS_CODE = process.env.MOCK_SMS_CODE ?? '123456'; async function req(clientApp, path, options = {}) { const headers = { 'Content-Type': 'application/json', 'X-Client-App': clientApp, ...(options.token ? { Authorization: `Bearer ${options.token}` } : {}), }; const res = await fetch(`${API}${path}`, { ...options, headers, body: options.body }); const json = await res.json(); if (json.code !== 0) throw new Error(`${path}: ${json.message}`); return json.data; } async function sendSms(clientApp, phone, scene, sendPath = '/auth/sms/send') { await req(clientApp, sendPath, { method: 'POST', body: JSON.stringify({ phone, scene }), }); } async function loginSms(clientApp, phone, scene, loginPath = '/auth/login/sms', sendPath = '/auth/sms/send') { await sendSms(clientApp, phone, scene, sendPath); return req(clientApp, loginPath, { method: 'POST', body: JSON.stringify({ phone, code: MOCK_SMS_CODE }), }); } async function main() { console.log('1. Health'); await req('USER_H5', '/health'); console.log('2. User login'); const userLogin = await loginSms('USER_H5', '13800000001', 'USER_LOGIN'); const userToken = userLogin.accessToken; console.log('3. Catalog'); const products = await req('USER_H5', '/catalog/products'); if (products.length < 4) throw new Error('Expected 4 products'); console.log('4. Create address + order + mock pay'); const addr = await req('USER_H5', '/user/addresses', { method: 'POST', token: userToken, body: JSON.stringify({ receiverName: '测试', phone: '13800000001', province: '河南省', city: '郑州市', district: '金水区', detail: '冒烟测试地址1号', isDefault: true, }), }); const order = await req('USER_H5', '/trade/orders', { method: 'POST', token: userToken, body: JSON.stringify({ productId: products[0].id, quantity: 2, addressId: addr.id }), }); await req('USER_H5', `/trade/orders/${order.id}/pay`, { method: 'POST', token: userToken }); console.log('5. Shop login + redeem preview'); const shopLogin = await loginSms( 'SHOP_H5', '13910000001', 'STORE_LOGIN', '/shop/auth/login/sms', '/shop/auth/sms/send', ); const coupons = await req('USER_H5', '/benefit/coupons', { token: userToken }); if (!coupons.length) throw new Error('No coupons'); const tokenRes = await req('USER_H5', '/redeem/tokens', { method: 'POST', token: userToken, body: JSON.stringify({ amount: 50 }), }); await req('SHOP_H5', '/shop/redeem/preview', { method: 'POST', token: shopLogin.accessToken, body: JSON.stringify({ token: tokenRes.token }), }); console.log('6. Partner login'); await loginSms( 'PARTNER_H5', '13700000001', 'PARTNER_LOGIN', '/partner/auth/login/sms', '/partner/auth/sms/send', ); console.log('preV1 smoke OK'); } main().catch((e) => { console.error(e); process.exit(1); });