|
| 1 | +import { expect, type Page, test } from "playwright/test"; |
| 2 | + |
| 3 | +async function installCheckoutMocks(page: Page) { |
| 4 | + const requests: Array<{ url: string; body: unknown }> = []; |
| 5 | + |
| 6 | + await page.route("https://api.elizacloud.ai/**", async (route) => { |
| 7 | + const request = route.request(); |
| 8 | + const url = new URL(request.url()); |
| 9 | + let body: unknown = null; |
| 10 | + try { |
| 11 | + body = request.postDataJSON(); |
| 12 | + } catch { |
| 13 | + body = null; |
| 14 | + } |
| 15 | + requests.push({ url: request.url(), body }); |
| 16 | + |
| 17 | + if (url.pathname === "/api/auth/steward-session") { |
| 18 | + return route.fulfill({ |
| 19 | + json: { success: true, user: { id: "steward-user-1" } }, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + if (url.pathname === "/api/stripe/create-checkout-session") { |
| 24 | + return route.fulfill({ |
| 25 | + json: { |
| 26 | + url: "http://127.0.0.1:4455/checkout/success?sku=elizaos-phone", |
| 27 | + }, |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + return route.fulfill({ |
| 32 | + json: { success: true }, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + return requests; |
| 37 | +} |
| 38 | + |
| 39 | +test("checkout product picker, color swatches, email login, and Stripe handoff are wired", async ({ |
| 40 | + page, |
| 41 | +}) => { |
| 42 | + const requests = await installCheckoutMocks(page); |
| 43 | + |
| 44 | + await page.goto("/checkout?sku=elizaos-usb"); |
| 45 | + await expect(page.getByRole("heading", { name: "ElizaOS USB" })).toBeVisible(); |
| 46 | + |
| 47 | + await page.getByRole("button", { name: /ElizaOS Phone/i }).click(); |
| 48 | + await expect(page).toHaveURL(/\/checkout\?sku=elizaos-phone$/); |
| 49 | + await expect(page.getByRole("heading", { name: "ElizaOS Phone" })).toBeVisible(); |
| 50 | + |
| 51 | + await page.getByRole("button", { name: "Select Black" }).click(); |
| 52 | + |
| 53 | + await page.getByRole("button", { name: "Email link" }).click(); |
| 54 | + await expect(page.getByText("Enter your email first.")).toBeVisible(); |
| 55 | + |
| 56 | + await page |
| 57 | + .getByPlaceholder("you@example.com") |
| 58 | + .fill("checkout-controls@example.com"); |
| 59 | + await page.getByRole("button", { name: "Email link" }).click(); |
| 60 | + await expect(page.getByText("Check your inbox.")).toBeVisible(); |
| 61 | + |
| 62 | + await page.goto( |
| 63 | + "/checkout?sku=elizaos-phone#token=steward-token-1&refreshToken=refresh-token-1", |
| 64 | + ); |
| 65 | + await expect(page.getByRole("button", { name: "Pay deposit" })).toBeVisible(); |
| 66 | + await page.getByRole("button", { name: "Pay deposit" }).click(); |
| 67 | + await expect(page).toHaveURL(/\/checkout\/success\?sku=elizaos-phone$/); |
| 68 | + |
| 69 | + const sessionRequest = requests.find( |
| 70 | + (request) => new URL(request.url).pathname === "/api/auth/steward-session", |
| 71 | + ); |
| 72 | + expect(sessionRequest?.body).toMatchObject({ |
| 73 | + token: "steward-token-1", |
| 74 | + refreshToken: "refresh-token-1", |
| 75 | + }); |
| 76 | + |
| 77 | + const checkoutRequest = requests.find( |
| 78 | + (request) => |
| 79 | + new URL(request.url).pathname === "/api/stripe/create-checkout-session", |
| 80 | + ); |
| 81 | + expect(checkoutRequest?.body).toMatchObject({ |
| 82 | + hardwareSku: "elizaos-phone", |
| 83 | + hardwareColor: "Black", |
| 84 | + returnUrl: "billing", |
| 85 | + }); |
| 86 | +}); |
0 commit comments