acupedia-store-ui/tests/e2e/fixtures/apiMocks.ts

244 lines
6.7 KiB
TypeScript

import type { Page } from '@playwright/test';
export const MOCK_PLANS = [
{
code: 'LIFETIME_ALL',
name: 'Lifetime All-Access Plan',
description: '10 Years access to all modules',
plan_type: 'bundle',
billing_period: 'lifetime',
price_inr: 15000,
price_usd: 350,
price_eur: 350,
is_active: true,
module_slugs: ['acupuncture-points', 'scalp-acupuncture', 'ear-acupuncture', 'nutrition', 'auriculotherapy', 'gynecological-disorders'],
modules: [],
is_featured: true,
},
{
code: 'POINTS_PACK',
name: 'Points Complete Pack (1 Yr)',
plan_type: 'bundle',
billing_period: 'yearly',
price_inr: 900,
price_usd: 15,
price_eur: 15,
is_active: true,
module_slugs: ['acupuncture-points', 'scalp-acupuncture', 'ear-acupuncture'],
},
{
code: 'POINTS_ACU',
name: 'Acu Points Module (1 Yr)',
plan_type: 'one_time',
billing_period: 'yearly',
price_inr: 300,
price_usd: 5,
price_eur: 5,
is_active: true,
module_slugs: ['acupuncture-points'],
},
{
code: 'POINTS_SCALP',
name: 'Scalp Points Module (1 Yr)',
plan_type: 'one_time',
billing_period: 'yearly',
price_inr: 100,
price_usd: 2,
price_eur: 2,
is_active: true,
module_slugs: ['scalp-acupuncture'],
},
{
code: 'POINTS_EAR',
name: 'Ear Points Module (1 Yr)',
plan_type: 'one_time',
billing_period: 'yearly',
price_inr: 200,
price_usd: 3,
price_eur: 3,
is_active: true,
module_slugs: ['ear-acupuncture'],
},
{
code: 'PATTERN_CORE',
name: 'Pattern Module Access (1 Yr)',
plan_type: 'bundle',
billing_period: 'yearly',
price_inr: 500,
price_usd: 10,
price_eur: 10,
is_active: true,
module_slugs: ['eight-principles'],
},
{
code: 'NUTRITION_CORE',
name: 'Nutrition Module Access (1 Yr)',
plan_type: 'bundle',
billing_period: 'yearly',
price_inr: 600,
price_usd: 15,
price_eur: 15,
is_active: true,
module_slugs: ['nutrition'],
},
{
code: 'THERAPEUTICS_GYNAE',
name: 'Therapeutics: Gynecology (1 Yr)',
plan_type: 'one_time',
billing_period: 'yearly',
price_inr: 600,
price_usd: 15,
price_eur: 15,
is_active: true,
module_slugs: ['gynecological-disorders'],
},
{
code: 'THERAPEUTICS_EAR',
name: 'Therapeutics: Ear reflexology (1 Yr)',
plan_type: 'one_time',
billing_period: 'yearly',
price_inr: 600,
price_usd: 15,
price_eur: 15,
is_active: true,
module_slugs: ['auriculotherapy'],
},
{
code: 'THERAPEUTICS_CORE',
name: 'Therapeutics Complete Bundle (1 Yr)',
plan_type: 'bundle',
billing_period: 'yearly',
price_inr: 1200,
price_usd: 30,
price_eur: 30,
is_active: true,
module_slugs: ['gynecological-disorders', 'auriculotherapy'],
},
];
export async function mockPublicStoreApis(page: Page) {
await page.route('**/api/checkout/config', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ razorpay_key_id: 'rzp_test_mock_key' }),
});
});
await page.route('**/api/plans', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(MOCK_PLANS),
});
});
await page.route('**/api/contents/modules', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ modules: [] }),
});
});
await page.route('**/api/cart/calculate', async (route) => {
const reqBody = route.request().postDataJSON() || {};
const currency = (reqBody.currency || 'INR').toUpperCase();
const coupon = reqBody.coupon_code ? String(reqBody.coupon_code).trim().toUpperCase() : null;
const rawItems = reqBody.cart_item_codes || reqBody.items || [];
const itemCodes: string[] = Array.isArray(rawItems)
? rawItems.map((i: any) => (typeof i === 'string' ? i : i.code))
: [];
const priceKey = currency === 'INR' ? 'price_inr' : currency === 'EUR' ? 'price_eur' : 'price_usd';
const cartItems = itemCodes.map((code) => {
const found = MOCK_PLANS.find((p) => p.code === code);
return {
code,
name: found?.name || code,
price: found ? (found as any)[priceKey] : 100,
};
});
const baseSubtotal = cartItems.reduce((sum, item) => sum + item.price, 0);
let couponDiscount = 0;
let couponApplied = 'None';
let couponError: string | null = null;
if (coupon === 'SAVE20') {
couponDiscount = Math.round(baseSubtotal * 0.2 * 100) / 100;
couponApplied = 'Code: SAVE20 - 20% Off';
} else if (coupon === 'BADCODE') {
couponError = 'Invalid or expired coupon code.';
}
const totalDiscounts = couponDiscount;
const grandTotal = Math.round(Math.max(0, baseSubtotal - totalDiscounts) * 100) / 100;
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
currency,
cart_items: cartItems,
base_subtotal: baseSubtotal,
bundle_discount_amount: 0,
renewal_discount_amount: 0,
coupon_discount_amount: couponDiscount,
total_discounts: totalDiscounts,
convenience_fee_rate: 0,
convenience_fee: 0,
taxable_amount: 0,
gst_amount: 0,
grand_total: grandTotal,
discount_policy: 'None',
coupon_applied: couponApplied,
coupon_error: couponError,
}),
});
});
}
export async function mockAuthenticatedUser(page: Page, overrides: Record<string, any> = {}) {
const userPayload = {
id: 'user-1',
email: 'practitioner@acupedia.in',
display_name: 'Dr. Test',
phone_number: '+919876543210',
country: 'India',
...overrides,
};
const accessPayload = {
user_id: userPayload.id,
active_subscriptions: overrides.active_subscriptions || [],
active_entitlements: overrides.active_entitlements || [],
unlocked_module_codes: overrides.unlocked_module_codes || [],
unlocked_module_slugs: overrides.unlocked_module_slugs || [],
};
await page.addInitScript(() => {
localStorage.setItem('access_token', 'mock-jwt-token');
});
await page.route('**/api/auth/profile', async (route) => {
if (route.request().method() === 'GET') {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(userPayload),
});
} else {
await route.continue();
}
});
await page.route('**/api/access/me', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(accessPayload),
});
});
}