import type { Page } from '@playwright/test'; // Teaching fixture only. No real retailer, secret store, or purchase endpoint. // Run against the local website preview, e.g. http://127.0.0.1:4333. export async function prepareDemoCart(page: Page, baseUrl: string) { const base = new URL(baseUrl); if (base.protocol !== 'http:' || !['127.0.0.1', 'localhost', '[::1]'].includes(base.hostname)) { throw new Error('This example only operates on a local mock portal.'); } await page.goto(new URL('/resources/supplier-portal-demo.html', base).href); await page.locator("xpath=//*[@id='mock-login']//input[@name='email']") .fill('demo@example.invalid'); await page.getByLabel('Demo passphrase').fill('not-a-secret'); await page.getByRole('button', { name: 'Sign in to demo', exact: true }).click(); await page.locator('#demo-catalog').waitFor({ state: 'visible' }); await page.locator("xpath=//*[@id='mock-search']//input[@name='partNumber']") .fill('2756'); await page.getByRole('button', { name: 'Search demo parts', exact: true }).click(); const product = page.locator("[data-mpn='BKR6E-11']"); await product.waitFor({ state: 'visible' }); if (await product.count() !== 1) throw new Error('Ambiguous part match'); const available = Number(await product.getAttribute('data-stock')); const quantity = 16; if (!Number.isSafeInteger(available) || available < quantity) { throw new Error('The allocated quantity is not confirmed available'); } await product.getByLabel('Quantity', { exact: true }).fill(String(quantity)); await page.locator("xpath=//*[@data-mpn='BKR6E-11']//button[@data-action='add-to-cart']") .click(); const cart = page.locator('#demo-cart'); await cart.waitFor({ state: 'visible' }); const actualPart = await cart.locator('[data-cart-mpn]').textContent(); const actualQty = Number(await cart.locator('[data-cart-quantity]').textContent()); if (actualPart !== 'BKR6E-11' || actualQty !== quantity) { throw new Error('Prepared cart does not match the allocation'); } return { exampleOnly: true, manufacturerPart: actualPart, quantity: actualQty, unit: 'each', merchandiseCents: actualQty * 320, submitted: false }; }