import { expect, test } from "@playwright/test"; import { mockCoreApis } from "../../fixtures/api-mocks"; import { paidImageMessageId } from "../../fixtures/test-data"; test.beforeEach(async ({ baseURL, context, page }) => { await context.clearCookies(); const client = await context.newCDPSession(page); const origins = new Set([ new URL(baseURL ?? "http://127.0.0.1:3000").origin, "http://127.0.0.1:3000", "http://localhost:3000", ]); for (const origin of origins) { await client .send("Storage.clearDataForOrigin", { origin, storageTypes: "all", }) .catch(() => {}); } await mockCoreApis(page, { paidImageFlow: true, }); }); test("guest can unlock a paid image after email login and subscription payment", async ({ page, }) => { await enterChatFromSplash(page); await dismissChatInterruptions(page); const messageInput = page.getByRole("textbox", { name: "Message" }); await expect(messageInput).toBeVisible({ timeout: 10_000 }); await messageInput.fill("给我发图片"); const sendRequestPromise = page.waitForRequest("**/api/chat/send"); await messageInput.press("Enter"); const sendRequest = await sendRequestPromise; expect(sendRequest.postDataJSON()).toMatchObject({ message: "给我发图片", }); const paidImageButton = page.getByRole("button", { name: "Open image in fullscreen", }).last(); await expect(paidImageButton).toBeVisible(); await paidImageButton.click(); await expect(page).toHaveURL(new RegExp(`/chat/image/${paidImageMessageId}$`)); const lockedImageDialog = page.getByRole("dialog", { name: "Locked fullscreen image", }); await expect(lockedImageDialog).toBeVisible(); await lockedImageDialog .getByRole("button", { name: "Unlock high-definition large image" }) .click(); await expect(page).toHaveURL(/\/auth\?redirect=/); expect(new URL(page.url()).searchParams.get("redirect")).toBe( "/subscription?type=vip&returnTo=chat", ); await page.getByRole("button", { name: /Other Sign In Options/i }).click(); await expect( page.getByRole("dialog", { name: /Other sign-in options/i }), ).toBeVisible(); await page.getByRole("button", { name: "Email Sign In" }).click(); await expect(page.getByPlaceholder("Email")).toHaveValue( "chanwillian0@gmail.com", ); await expect(page.getByPlaceholder("Password")).toHaveValue("12345678"); const loginRequestPromise = page.waitForRequest("**/api/auth/login"); await page.getByRole("button", { name: "Login" }).click(); const loginRequest = await loginRequestPromise; expect(loginRequest.postDataJSON()).toMatchObject({ email: "chanwillian0@gmail.com", password: "12345678", isTestAccount: true, }); await expect(page).toHaveURL(/\/subscription\?type=vip&returnTo=chat$/); const createOrderRequestPromise = page.waitForRequest( "**/api/payment/create-order", ); const orderStatusRequestPromise = page.waitForRequest( "**/api/payment/order-status**", ); await page.getByRole("button", { name: /Pay and Activ/i }).click(); const createOrderRequest = await createOrderRequestPromise; expect(createOrderRequest.postDataJSON()).toMatchObject({ planId: "vip_monthly", payChannel: "stripe", }); await orderStatusRequestPromise; const paymentSuccessDialog = page.getByRole("alertdialog", { name: "Payment successful", }); await expect(paymentSuccessDialog).toBeVisible(); await paymentSuccessDialog.getByRole("button", { name: "OK" }).click(); await expect(page).toHaveURL(new RegExp(`/chat/image/${paidImageMessageId}$`)); }); async function enterChatFromSplash(page: import("@playwright/test").Page) { await page.goto("/splash"); const skipButton = page.getByRole("button", { name: "Skip" }); await expect(skipButton).toBeVisible({ timeout: 10_000 }); await expect(skipButton).toBeEnabled(); for (let attempt = 0; attempt < 3; attempt += 1) { await skipButton.click(); try { await expect(page).toHaveURL(/\/chat$/, { timeout: 3_000 }); return; } catch { await page.waitForTimeout(500); } } await expect(page).toHaveURL(/\/chat$/); } async function dismissChatInterruptions(page: import("@playwright/test").Page) { const laterButton = page.getByRole("button", { name: "Later" }); if (await laterButton.isVisible().catch(() => false)) { await laterButton.click(); } const cancelButton = page.getByRole("button", { name: "Cancel" }); if (await cancelButton.isVisible().catch(() => false)) { await cancelButton.click(); } const okButton = page.getByRole("button", { name: "OK" }); if (await okButton.isVisible().catch(() => false)) { await okButton.click(); } }