test(e2e): cover chat send token refresh retry
This commit is contained in:
@@ -18,12 +18,15 @@ import {
|
||||
paidVoiceHistoryResponse,
|
||||
paidPaymentOrderStatusResponse,
|
||||
paymentPlansResponse,
|
||||
refreshedEmailLoginResponse,
|
||||
refreshedGuestLoginResponse,
|
||||
userEntitlementsResponse,
|
||||
vipStatusResponse,
|
||||
} from "./test-data";
|
||||
|
||||
interface MockCoreApisOptions {
|
||||
chatLimitTriggerAt?: number;
|
||||
chatSendTokenRefreshFlow?: boolean;
|
||||
paidImageFlow?: boolean;
|
||||
paidImageInsufficientCreditsFlow?: boolean;
|
||||
paidVoiceInsufficientCreditsFlow?: boolean;
|
||||
@@ -35,16 +38,22 @@ export async function mockCoreApis(
|
||||
): Promise<void> {
|
||||
const {
|
||||
chatLimitTriggerAt,
|
||||
chatSendTokenRefreshFlow = false,
|
||||
paidImageFlow = false,
|
||||
paidImageInsufficientCreditsFlow = false,
|
||||
paidVoiceInsufficientCreditsFlow = false,
|
||||
} = options;
|
||||
let sendCount = 0;
|
||||
let hasExpiredChatSend = false;
|
||||
let paidImageRequested = false;
|
||||
let paidImageUnlocked = false;
|
||||
|
||||
await page.route("**/api/auth/guest", async (route) => {
|
||||
await route.fulfill({ json: apiEnvelope(guestLoginResponse) });
|
||||
const response =
|
||||
chatSendTokenRefreshFlow && hasExpiredChatSend
|
||||
? refreshedGuestLoginResponse
|
||||
: guestLoginResponse;
|
||||
await route.fulfill({ json: apiEnvelope(response) });
|
||||
});
|
||||
|
||||
await page.route("**/api/auth/login", async (route) => {
|
||||
@@ -55,6 +64,10 @@ export async function mockCoreApis(
|
||||
await route.fulfill({ json: apiEnvelope(emailLoginResponse) });
|
||||
});
|
||||
|
||||
await page.route("**/api/auth/refresh", async (route) => {
|
||||
await route.fulfill({ json: apiEnvelope(refreshedEmailLoginResponse) });
|
||||
});
|
||||
|
||||
await page.route("**/api/auth/logout", async (route) => {
|
||||
await route.fulfill({ json: apiEnvelope(null) });
|
||||
});
|
||||
@@ -88,6 +101,17 @@ export async function mockCoreApis(
|
||||
const message =
|
||||
typeof requestBody?.message === "string" ? requestBody.message : "";
|
||||
|
||||
if (chatSendTokenRefreshFlow && !hasExpiredChatSend) {
|
||||
hasExpiredChatSend = true;
|
||||
await route.fulfill({
|
||||
status: 401,
|
||||
json: {
|
||||
message: "expired",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const response =
|
||||
paidImageFlow && message.includes("给我发图片")
|
||||
? paidImageChatSendResponse
|
||||
|
||||
@@ -43,12 +43,22 @@ export const guestLoginResponse = {
|
||||
user: e2eUser,
|
||||
};
|
||||
|
||||
export const refreshedGuestLoginResponse = {
|
||||
...guestLoginResponse,
|
||||
token: "e2e-refreshed-guest-token",
|
||||
};
|
||||
|
||||
export const emailLoginResponse = {
|
||||
token: "e2e-email-token",
|
||||
refreshToken: "e2e-email-refresh-token",
|
||||
user: e2eEmailUser,
|
||||
};
|
||||
|
||||
export const refreshedEmailLoginResponse = {
|
||||
token: "e2e-refreshed-email-token",
|
||||
refreshToken: "e2e-refreshed-email-refresh-token",
|
||||
};
|
||||
|
||||
export const userEntitlementsResponse = {
|
||||
userId: e2eEmailUser.id,
|
||||
isGuest: false,
|
||||
|
||||
@@ -72,10 +72,32 @@ export async function dismissChatInterruptions(page: Page) {
|
||||
}
|
||||
|
||||
export async function switchToEmailSignIn(page: Page) {
|
||||
await page.getByRole("button", { name: /Other Sign In Options/i }).click();
|
||||
await expect(
|
||||
page.getByRole("dialog", { name: /Other sign-in options/i }),
|
||||
).toBeVisible();
|
||||
const otherOptionsButton = page.getByRole("button", {
|
||||
name: /Other Sign In Options/i,
|
||||
});
|
||||
const otherOptionsDialog = page.getByRole("dialog", {
|
||||
name: /Other sign-in options/i,
|
||||
});
|
||||
|
||||
await expect(otherOptionsButton).toBeVisible({ timeout: 10_000 });
|
||||
await expect(otherOptionsButton).toBeEnabled();
|
||||
|
||||
for (let attempt = 0; attempt < 3; attempt += 1) {
|
||||
await otherOptionsButton.click();
|
||||
try {
|
||||
await expect(otherOptionsDialog).toBeVisible({ timeout: 2_000 });
|
||||
break;
|
||||
} catch (error) {
|
||||
if (attempt === 2) throw error;
|
||||
await otherOptionsButton.evaluate((button) =>
|
||||
(button as HTMLButtonElement).click(),
|
||||
);
|
||||
if (await otherOptionsDialog.isVisible().catch(() => false)) {
|
||||
break;
|
||||
}
|
||||
await page.waitForTimeout(500);
|
||||
}
|
||||
}
|
||||
|
||||
await page.getByRole("button", { name: "Email Sign In" }).click();
|
||||
await expect(page.getByPlaceholder("Email")).toHaveValue(
|
||||
@@ -176,3 +198,21 @@ export async function expectInsufficientCreditsDialog(page: Page) {
|
||||
|
||||
return insufficientCreditsDialog;
|
||||
}
|
||||
|
||||
export async function expectVipChatSubscriptionUrl(page: Page) {
|
||||
await expect(page).toHaveURL(/\/subscription\?/);
|
||||
const url = new URL(page.url());
|
||||
expect(url.pathname).toBe("/subscription");
|
||||
expect(url.searchParams.get("type")).toBe("vip");
|
||||
expect(url.searchParams.get("returnTo")).toBe("chat");
|
||||
}
|
||||
|
||||
export function expectAuthRedirectToVipChatSubscription(page: Page) {
|
||||
const redirect = new URL(page.url()).searchParams.get("redirect");
|
||||
expect(redirect).not.toBeNull();
|
||||
|
||||
const redirectUrl = new URL(redirect ?? "", "http://e2e.local");
|
||||
expect(redirectUrl.pathname).toBe("/subscription");
|
||||
expect(redirectUrl.searchParams.get("type")).toBe("vip");
|
||||
expect(redirectUrl.searchParams.get("returnTo")).toBe("chat");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user