A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!
TypeScript agent-skillsaidsh-pluginhacktoberfestjavascriptmcp-serverreactresume-builderself-hostedtypescript
import type { E2EAccount } from "./data";
import type { APIRequestContext, Browser, BrowserContext, Page } from "@playwright/test";
import { request } from "@playwright/test";
async function assertAuthResponse(response: Awaited<ReturnType<APIRequestContext["post"]>>) {
if (response.ok()) return;
throw new Error(`Authentication request failed with ${response.status()}: ${await response.text()}`);
}
export async function registerViaUi(page: Page, account: E2EAccount, callbackURL?: string) {
await page.goto(`/auth/register${callbackURL ? `?callbackURL=${encodeURIComponent(callbackURL)}` : ""}`);
await page.getByRole("textbox", { name: "Name", exact: true }).fill(account.name);
// The username is optional; an empty one is made from the email address.
if (account.username) await page.getByLabel("Username").fill(account.username);
await page.getByLabel("Email address", { exact: true }).fill(account.email);
await page.getByLabel("Password", { exact: true }).fill(account.password);
await page.getByRole("button", { name: "Sign up" }).click();
await page.waitForURL(/\/dashboard/);
}
export async function loginViaUi(page: Page, account: E2EAccount, path = "/auth/login") {
await page.goto(path);
await page.getByLabel("Email or username", { exact: true }).fill(account.email);
await page.getByLabel("Password", { exact: true }).fill(account.password);
await page.getByRole("button", { name: "Sign in" }).click();
await page.waitForURL(/\/dashboard/);
}
export async function logoutViaUi(page: Page, account: E2EAccount) {
// The avatar row at the foot of the sidebar opens the account menu.
await page
.getByRole("complementary")
.getByRole("button", { name: new RegExp(account.name) })
.click();
await page.getByRole("menuitem", { name: "Sign out" }).click();
await page.waitForURL((url) => url.pathname === "/");
}
async function registerViaApi(request: APIRequestContext, account: E2EAccount, baseURL: string) {
const response = await request.post("/api/auth/sign-up/email", {
headers: {
origin: baseURL,
referer: `${baseURL}/auth/register`,
},
data: {
name: account.name,
email: account.email,
password: account.password,
username: account.username,
displayUsername: account.username,
callbackURL: "/dashboard",
},
});
await assertAuthResponse(response);
}
export async function createAuthenticatedContext(
browser: Browser,
request: APIRequestContext,
account: E2EAccount,
baseURL: string,
): Promise<BrowserContext> {
await registerViaApi(request, account, baseURL);
return browser.newContext({
baseURL,
storageState: await request.storageState(),
});
}
/** A fresh authenticated reader keeps long UI flows independent of an expired idle API socket. */
export async function readAuthenticatedJson(page: Page, path: string) {
const reader = await request.newContext({
baseURL: new URL(page.url()).origin,
storageState: await page.context().storageState(),
});
try {
const response = await reader.get(path);
if (!response.ok()) throw new Error(`Read failed with ${response.status()}: ${path}`);
return await response.json();
} finally {
await reader.dispose();
}
}