test: add playwright e2e tests for chat, auth, model selector, and api (#1355)

This commit is contained in:
josh 2025-12-15 14:43:39 +00:00 committed by GitHub
parent b1da86062e
commit 5f9e231788
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 260 additions and 416 deletions

View file

@ -1,70 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import {
type APIRequestContext,
type Browser,
type BrowserContext,
expect,
type Page,
} from "@playwright/test";
import { generateId } from "ai";
import { getUnixTime } from "date-fns";
export type UserContext = {
context: BrowserContext;
page: Page;
request: APIRequestContext;
};
export async function createAuthenticatedContext({
browser,
name,
}: {
browser: Browser;
name: string;
}): Promise<UserContext> {
const directory = path.join(__dirname, "../playwright/.sessions");
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
const storageFile = path.join(directory, `${name}.json`);
const context = await browser.newContext();
const page = await context.newPage();
const email = `test-${name}@playwright.com`;
const password = generateId();
await page.goto("http://localhost:3000/register");
await page.getByPlaceholder("user@acme.com").click();
await page.getByPlaceholder("user@acme.com").fill(email);
await page.getByLabel("Password").click();
await page.getByLabel("Password").fill(password);
await page.getByRole("button", { name: "Sign Up" }).click();
await expect(page.getByTestId("toast")).toContainText(
"Account created successfully!"
);
// Wait for redirect to home page
await page.waitForURL("/");
await page.waitForTimeout(1000);
await context.storageState({ path: storageFile });
await page.close();
const newContext = await browser.newContext({ storageState: storageFile });
const newPage = await newContext.newPage();
return {
context: newContext,
page: newPage,
request: newContext.request,
};
}
export function generateRandomTestUser() {
const email = `test-${getUnixTime(new Date())}@playwright.com`;
const password = generateId();
@ -74,3 +10,7 @@ export function generateRandomTestUser() {
password,
};
}
export function generateTestMessage() {
return `Test message ${Date.now()}`;
}