import type { Page } from "playwright"; import { resolveRpaQuoteCaptureMs } from "@/lib/rpa/env"; import { RpaError } from "@/modules/rpa/errors"; /** submit 后期望落点(匿名流 sign-up-quote 亦为合法结果页) */ export const POST_SUBMIT_URL_PATTERN = /sign-up-quote|\/quote(?:\/|$|\?)/i; export function isPostSubmitDestination(url: string): boolean { return POST_SUBMIT_URL_PATTERN.test(url); } const POST_SUBMIT_POLL_MS = 250; export async function waitForPostSubmitDestination( page: Page, timeoutMs = 20_000, ): Promise { const effectiveTimeoutMs = resolveRpaQuoteCaptureMs(timeoutMs); const deadline = Date.now() + effectiveTimeoutMs; while (Date.now() < deadline) { const url = page.url(); if (isPostSubmitDestination(url)) { return url; } await page.waitForTimeout(POST_SUBMIT_POLL_MS); } throw new RpaError( "QUOTE_ENTRY_UNAVAILABLE", `submit 后未到达报价结果页(url=${page.url()})`, { retryable: true }, ); }