Skip to content

Commit b7d9daa

Browse files
test: normalize Playwright sign links from email
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 841e8e6 commit b7d9daa

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

playwright/support/mailpit.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export type { MailpitClient }
1010

1111
type Message = Awaited<ReturnType<MailpitClient['getMessageSummary']>>
1212

13+
function resolveAgainstPlaywrightBaseUrl(url: string): string {
14+
const baseUrl = process.env.PLAYWRIGHT_BASE_URL
15+
if (!baseUrl) {
16+
return url
17+
}
18+
19+
const parsedUrl = new URL(url)
20+
const targetUrl = new URL(`${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`, baseUrl)
21+
return targetUrl.toString()
22+
}
23+
1324
/** Creates a MailpitClient using MAILPIT_URL (default: http://localhost:8025). */
1425
export function createMailpitClient(): MailpitClient {
1526
const defaultUrl = existsSync('/.dockerenv')
@@ -65,7 +76,12 @@ export async function waitForEmailTo(
6576
/** Extracts a LibreSign sign link from an email body matching /p/sign/{uuid}. */
6677
export function extractSignLink(body: string): string | null {
6778
const match = body.match(/\S+\/p\/sign\/[\w-]+/)
68-
return match ? match[0] : null
79+
if (!match) {
80+
return null
81+
}
82+
83+
const parsedUrl = new URL(match[0])
84+
return `${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`
6985
}
7086

7187
/** Extracts a numeric token from an email body. Default pattern: 4-8 digit sequence. */
@@ -80,5 +96,5 @@ export function extractTokenFromEmail(
8096
/** Extracts the first URL from an email body (email.Text). */
8197
export function extractLinkFromEmail(body: string): string | null {
8298
const match = body.match(/https?:\/\/\S+/)
83-
return match ? match[0] : null
99+
return match ? resolveAgainstPlaywrightBaseUrl(match[0]) : null
84100
}

0 commit comments

Comments
 (0)