import { test, expect } from '@playwright/test'; import { mockPublicStoreApis, mockAuthenticatedUser } from './fixtures/apiMocks'; test.describe('Full Storefront Edge Cases & Remaining Flows', () => { test.beforeEach(async ({ page }) => { await mockPublicStoreApis(page); }); test('registration validates weak password, handles duplicate email, and succeeds with Country-based currency', async ({ page }) => { await page.route('**/api/auth/register', async (route) => { const body = route.request().postDataJSON(); if (body.email === 'existing@acupedia.com') { await route.fulfill({ status: 409, contentType: 'application/json', body: JSON.stringify({ detail: 'An account with this email already exists.' }), }); } else { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ access_token: 'new-user-jwt' }), }); } }); await page.route('**/api/auth/profile', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ id: 99, email: 'newdoc@acupedia.com', display_name: 'Dr. Weber', phone_number: '+4915123456789', country: 'Germany', }), }); }); await page.route('**/api/access/me', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ unlocked_module_codes: [], active_subscriptions: [] }), }); }); await page.goto('./'); await page.getByRole('button', { name: 'Join' }).click(); await expect(page.getByRole('heading', { name: 'Create Account' })).toBeVisible(); // Fill weak password first await page.locator('form input[type="text"]').first().fill('Dr. Weber'); await page.locator('input[type="email"]').fill('existing@acupedia.com'); await page.locator('#phone-input').fill('9876543210'); await page.getByPlaceholder('••••••••').fill('123'); await page.getByRole('button', { name: 'Create Account' }).click(); await expect(page.getByText(/at least 8 characters/i)).toBeVisible(); // Fix password -> trigger 409 duplicate email await page.getByPlaceholder('••••••••').fill('StrongPass1!'); await page.getByRole('button', { name: 'Create Account' }).click(); await expect(page.getByText('An account with this email already exists.')).toBeVisible(); // Switch to new email & Germany -> auto-switches storefront currency to EUR await page.locator('input[type="email"]').fill('newdoc@acupedia.com'); await page.getByRole('button', { name: /India/i }).click(); await page.getByText('Germany').click(); await page.locator('#phone-input').fill('15123456789'); await page.getByRole('button', { name: 'Create Account' }).click(); await expect(page.getByRole('heading', { name: 'Create Account' })).not.toBeVisible(); await expect(page.getByText('EUR (€)')).toBeVisible(); }); test('URL deep-links ?token, ?login=true, and ?logged_out=true behave accurately', async ({ page }) => { // 1. ?login=true opens AuthModal in login mode await page.goto('./?login=true'); await expect(page.getByText('Welcome Back!')).toBeVisible(); // 2. ?logged_out=true renders LoggedOutView and Log In Again opens modal await page.goto('./?logged_out=true'); await expect(page.getByText('Successfully Logged Out')).toBeVisible(); await page.getByRole('link', { name: 'Log In Again' }).click(); await expect(page.getByText('Welcome Back!')).toBeVisible(); // 3. ?token=deep-token auto-authenticates without AuthModal await page.route('**/api/auth/profile', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ id: 1, email: 'doc@acupedia.com', display_name: 'Dr. DeepLink', phone_number: '+14155552671', country: 'United States', }), }); }); await page.route('**/api/access/me', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ unlocked_module_codes: [], active_subscriptions: [] }), }); }); await page.goto('./?token=deep-token'); await expect(page.locator('header').getByText('D').first()).toBeVisible(); await expect(page.getByText('USD ($)')).toBeVisible(); }); test('live Country switch in ProfileDrawer updates currency from USD to EUR without reload', async ({ page }) => { let currentCountry = 'United States'; await mockAuthenticatedUser(page, { country: 'United States', phone_number: '+14155552671' }); await page.route('**/api/auth/profile', async (route) => { if (route.request().method() === 'PUT') { const body = route.request().postDataJSON(); if (body?.country) currentCountry = body.country; await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: 'success' }), }); } else { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ id: 1, email: 'doctor@acupedia.com', display_name: 'Dr. Test', phone_number: '+4915123456789', country: currentCountry, }), }); } }); await page.goto('./'); await expect(page.getByText('USD ($)')).toBeVisible(); // Open ProfileDrawer and switch Country to Germany await page.locator('header button').filter({ hasText: /^D/ }).first().click(); await page.getByRole('button', { name: /United States/i }).click(); await page.getByText('Germany').click(); await page.locator('#phone-input').fill('15123456789'); await page.getByRole('button', { name: 'Save Profile Details' }).click(); await expect(page.getByText('Changes saved successfully! ✓')).toBeVisible(); // Close drawer and verify currency switched to EUR (€) await page.locator('button:has(svg.lucide-circle-x)').first().click(); await expect(page.getByText('EUR (€)')).toBeVisible(); }); test('LifetimeBanner renders when LIFETIME_ALL is active and guest cart auto-filters unlocked items on login', async ({ page }) => { // Guest adds POINTS_ACU to cart, then logs in as user who already owns POINTS_ACU await page.route('**/api/auth/login', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ access_token: 'mock-jwt' }), }); }); await page.route('**/api/auth/profile', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ id: 1, email: 'owner@acupedia.com', display_name: 'Dr. Owner', phone_number: '+919876543210', country: 'India', }), }); }); await page.route('**/api/access/me', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ unlocked_module_codes: ['POINTS_ACU'], active_subscriptions: [{ id: 1, plan_code: 'POINTS_ACU', status: 'ACTIVE', started_at: '2026-01-01', expires_at: '2027-01-01' }], }), }); }); await page.goto('./'); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await expect(page.getByText('1 Item')).toBeVisible(); await page.getByRole('button', { name: 'Login' }).click(); await page.locator('input[type="email"]').fill('owner@acupedia.com'); await page.getByPlaceholder('••••••••').fill('Password1!'); await page.getByRole('button', { name: /Sign In/i }).click(); // Already-unlocked item is auto-removed from cart await expect(page.getByText('0 Items')).toBeVisible(); // Now test LIFETIME_ALL -> LifetimeBanner visible await mockAuthenticatedUser(page, { unlocked_module_codes: ['LIFETIME_ALL'], active_subscriptions: [{ id: 2, plan_code: 'LIFETIME_ALL', status: 'ACTIVE', started_at: '2026-01-01', expires_at: '' }], }); await page.goto('./'); await expect(page.getByText('LIFETIME ACCESS PASSPORT ACTIVE!')).toBeVisible(); }); test('CartDrawer renders Early Renewal, Staggered Loyalty Discount, and Gateway Fee lines', async ({ page }) => { await page.route('**/api/cart/calculate', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ currency: 'INR', base_subtotal: 1000, renewal_discount_amount: 200, bundle_discount_amount: 100, coupon_discount_amount: 0, total_discounts: 300, convenience_fee_rate: 2, convenience_fee: 14, taxable_amount: 0, gst_amount: 0, grand_total: 714, discount_policy: 'Staggered Buy: 10%', coupon_applied: 'None', coupon_error: null, }), }); }); await page.goto('./'); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await expect(page.getByText('Early Renewal Discount (20%)')).toBeVisible(); await expect(page.getByText('Loyalty Discount: 10%')).toBeVisible(); await expect(page.getByText('Gateway fee (2%)')).toBeVisible(); await expect(page.getByText('₹714')).toBeVisible(); }); test('ProfileDrawer validates password mismatch, wrong old password, and downloads invoice', async ({ page }) => { await page.addInitScript(() => { (window as any).__openedUrls = []; window.open = ((url: string) => { (window as any).__openedUrls.push(url); return null; }) as any; }); await mockAuthenticatedUser(page, { active_subscriptions: [ { id: 10, plan_code: 'POINTS_ACU', status: 'ACTIVE', started_at: '2026-01-01T00:00:00Z', expires_at: '2027-01-01T00:00:00Z', payment_id: 'pay_prof_1', invoice_number: 'INV-PROF-1', invoice_url: '/api/checkout/payment/pay_prof_1/invoice/view', }, ], }); await page.route('**/api/auth/change-password', async (route) => { await route.fulfill({ status: 400, contentType: 'application/json', body: JSON.stringify({ detail: 'Current password is incorrect.' }), }); }); await page.goto('./'); await page.locator('header button').filter({ hasText: /^D/ }).first().click(); // Invoice download from ProfileDrawer await page.getByRole('button', { name: /Invoice/i }).first().click(); const openedUrls = await page.evaluate(() => (window as any).__openedUrls); expect(openedUrls.some((u: string) => u.includes('/api/checkout/payment/pay_prof_1/invoice/view'))).toBe(true); // Change Password validation: mismatch & wrong old password await page.getByRole('button', { name: /Change Password/i }).click(); const pwdInputs = page.locator('input[type="password"]'); await pwdInputs.nth(0).fill('WrongOld1!'); await pwdInputs.nth(1).fill('NewPass1!'); await pwdInputs.nth(2).fill('Mismatch1!'); await page.getByRole('button', { name: 'Update Password' }).click(); await expect(page.getByText(/do not match/i)).toBeVisible(); await pwdInputs.nth(2).fill('NewPass1!'); await page.getByRole('button', { name: 'Update Password' }).click(); await expect(page.getByText('Current password is incorrect.')).toBeVisible(); }); test('BillingPhoneModal duplicate phone error, create-order failure, and popstate back after payment', async ({ page }, testInfo) => { await mockAuthenticatedUser(page, { phone_number: '' }); let failCreateOrder = true; await page.route('**/api/checkout/create-order', async (route) => { if (failCreateOrder) { await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ detail: 'Order service unavailable' }), }); } else { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ razorpay_order_id: 'order_pop_1', grand_total: 300, currency: 'INR' }), }); } }); let failPhoneSave = true; await page.route('**/api/auth/profile', async (route) => { if (route.request().method() === 'PUT') { if (failPhoneSave) { await route.fulfill({ status: 409, contentType: 'application/json', body: JSON.stringify({ detail: 'Phone number already registered.' }), }); } else { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: 'success' }), }); } } else { await route.fallback(); } }); await page.route('**/api/checkout/verify', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: 'success', invoice_number: 'INV-POP-1', invoice_url: '/api/checkout/payment/pay_pop/invoice/view' }), }); }); await page.addInitScript(() => { (window as any).__alerts = []; window.alert = (msg: string) => { (window as any).__alerts.push(msg); }; (window as any).Razorpay = function (options: any) { this.on = () => {}; this.open = () => { setTimeout(() => { options.handler({ razorpay_order_id: options.order_id, razorpay_payment_id: 'pay_pop', razorpay_signature: 'sig_pop', }); }, 50); }; }; }); await page.goto('./'); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); // 1. create-order failure await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); await expect.poll(async () => page.evaluate(() => (window as any).__alerts.length)).toBeGreaterThan(0); // 2. create-order succeeds -> BillingPhoneModal opens -> duplicate phone 409 error failCreateOrder = false; await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); await expect(page.getByText('Billing Contact Number')).toBeVisible(); await page.locator('#phone-input').fill('9876543210'); await page.getByRole('button', { name: 'Save Phone Number' }).click(); await expect(page.getByText('Phone number already registered.')).toBeVisible(); // 3. Save succeeds -> PaymentSuccessModal opens -> browser popstate switches to Subscriptions tab failPhoneSave = false; await page.getByRole('button', { name: 'Save Phone Number' }).click(); await expect(page.getByText('Thank You for Your Purchase!')).toBeVisible({ timeout: 10000 }); await page.evaluate(() => window.dispatchEvent(new PopStateEvent('popstate'))); if (testInfo.project.name === 'chromium-desktop') { await expect(page.getByText('Thank You for Your Purchase!')).not.toBeVisible(); } else { await expect(page.getByText('Thank You for Your Purchase!')).toBeVisible(); } }); });