File size: 4,976 Bytes
e3fefe8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | /*
Realtime end-to-end flow validator for current booking lifecycle.
Usage:
BASE_URL=http://192.168.1.10:3000 \
CUSTOMER_TOKEN=ey... \
PROF_TOKEN=ey... \
PROF_UID=professional_uid \
node scripts/e2e_test.js
Flow:
1) Customer creates request (no price required)
2) Professional proposes price
3) Customer accepts price (deal confirmed)
4) Professional starts job (in_progress)
5) Customer confirms completion (customer_confirmed)
6) Professional completes (completed + commission deduction)
*/
const BASE = process.env.BASE_URL || 'http://127.0.0.1:3000';
const CUSTOMER_TOKEN = process.env.CUSTOMER_TOKEN;
const PROF_TOKEN = process.env.PROF_TOKEN;
const PROF_UID = process.env.PROF_UID;
if (!CUSTOMER_TOKEN || !PROF_TOKEN || !PROF_UID) {
console.error('Missing env vars. Set CUSTOMER_TOKEN, PROF_TOKEN, PROF_UID.');
process.exit(1);
}
const headers = (token) => ({
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
});
async function call(path, opts = {}) {
const url = `${BASE}${path}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 10000);
const res = await fetch(url, { ...opts, signal: controller.signal });
clearTimeout(timer);
const text = await res.text();
let body = text;
try { body = JSON.parse(text); } catch (e) {}
return { status: res.status, body };
}
(async () => {
try {
console.log('1) Create request as customer...');
const createPayload = {
professionalId: PROF_UID,
serviceType: 'plumber',
description: 'Kitchen sink leakage test case',
address: 'Test address from e2e script',
scheduledTime: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
};
const createRes = await call('/api/bookings', {
method: 'POST',
headers: headers(CUSTOMER_TOKEN),
body: JSON.stringify(createPayload),
});
console.log('Create status:', createRes.status);
console.log(JSON.stringify(createRes.body, null, 2));
if (!createRes.body || !createRes.body.success) {
console.error('Create booking failed. Aborting.');
process.exit(2);
}
const bookingId = createRes.body.data.bookingId;
console.log('\n2) Professional fetches bookings...');
const myRes = await call('/api/bookings/my', { headers: headers(PROF_TOKEN) });
console.log('GET /api/bookings/my status:', myRes.status);
console.log(JSON.stringify(myRes.body, null, 2));
console.log('\n3) Professional proposes price...');
const proposeRes = await call(`/api/bookings/${bookingId}/propose-price`, {
method: 'POST',
headers: headers(PROF_TOKEN),
body: JSON.stringify({ price: 4500 }),
});
console.log('Propose status:', proposeRes.status);
console.log(JSON.stringify(proposeRes.body, null, 2));
if (!proposeRes.body || !proposeRes.body.success) {
console.error('Propose price failed. Aborting.');
process.exit(3);
}
console.log('\n4) Customer accepts price...');
const acceptRes = await call(`/api/bookings/${bookingId}/accept-price`, {
method: 'POST',
headers: headers(CUSTOMER_TOKEN),
body: JSON.stringify({ price: 4500 }),
});
console.log('Accept price status:', acceptRes.status);
console.log(JSON.stringify(acceptRes.body, null, 2));
if (!acceptRes.body || !acceptRes.body.success) {
console.error('Accept price failed.');
process.exit(4);
}
console.log('\n5) Professional starts job...');
const startRes = await call(`/api/bookings/${bookingId}/start`, {
method: 'POST',
headers: headers(PROF_TOKEN),
});
console.log('Start status:', startRes.status);
console.log(JSON.stringify(startRes.body, null, 2));
if (!startRes.body || !startRes.body.success) {
console.error('Start job failed.');
process.exit(5);
}
console.log('\n6) Customer confirms completion...');
const custDoneRes = await call(`/api/bookings/${bookingId}/customer-complete`, {
method: 'POST',
headers: headers(CUSTOMER_TOKEN),
});
console.log('Customer complete status:', custDoneRes.status);
console.log(JSON.stringify(custDoneRes.body, null, 2));
if (!custDoneRes.body || !custDoneRes.body.success) {
console.error('Customer completion failed.');
process.exit(6);
}
console.log('\n7) Professional completes booking...');
const doneRes = await call(`/api/bookings/${bookingId}/complete`, {
method: 'POST',
headers: headers(PROF_TOKEN),
});
console.log('Complete status:', doneRes.status);
console.log(JSON.stringify(doneRes.body, null, 2));
if (!doneRes.body || !doneRes.body.success) {
console.error('Professional completion failed.');
process.exit(7);
}
console.log('\nE2E test completed successfully.');
process.exit(0);
} catch (err) {
console.error('E2E script error:', err);
process.exit(10);
}
})();
|