2025-09-21 12:03:29 +01:00
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
2025-04-14 10:34:11 -07:00
|
|
|
import {
|
|
|
|
|
type APIRequestContext,
|
|
|
|
|
type Browser,
|
|
|
|
|
type BrowserContext,
|
|
|
|
|
expect,
|
|
|
|
|
type Page,
|
2025-09-21 12:03:29 +01:00
|
|
|
} from '@playwright/test';
|
|
|
|
|
import { generateId } from 'ai';
|
|
|
|
|
import { ChatPage } from './pages/chat';
|
|
|
|
|
import { getUnixTime } from 'date-fns';
|
2025-04-14 10:34:11 -07:00
|
|
|
|
|
|
|
|
export type UserContext = {
|
|
|
|
|
context: BrowserContext;
|
|
|
|
|
page: Page;
|
|
|
|
|
request: APIRequestContext;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function createAuthenticatedContext({
|
|
|
|
|
browser,
|
|
|
|
|
name,
|
2025-09-21 12:03:29 +01:00
|
|
|
chatModel = 'chat-model',
|
2025-04-14 10:34:11 -07:00
|
|
|
}: {
|
|
|
|
|
browser: Browser;
|
|
|
|
|
name: string;
|
2025-09-21 12:03:29 +01:00
|
|
|
chatModel?: 'chat-model' | 'chat-model-reasoning';
|
2025-04-14 10:34:11 -07:00
|
|
|
}): Promise<UserContext> {
|
2025-09-21 12:03:29 +01:00
|
|
|
const directory = path.join(__dirname, '../playwright/.sessions');
|
2025-04-14 10:34:11 -07:00
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
if (!fs.existsSync(directory)) {
|
|
|
|
|
fs.mkdirSync(directory, { recursive: true });
|
2025-04-14 10:34:11 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
const storageFile = path.join(directory, `${name}.json`);
|
2025-04-14 10:34:11 -07:00
|
|
|
|
|
|
|
|
const context = await browser.newContext();
|
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
const email = `test-${name}@playwright.com`;
|
2025-07-03 02:26:34 -07:00
|
|
|
const password = generateId();
|
2025-04-14 10:34:11 -07:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
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();
|
2025-04-14 10:34:11 -07:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
await expect(page.getByTestId('toast')).toContainText(
|
|
|
|
|
'Account created successfully!',
|
2025-04-14 10:34:11 -07:00
|
|
|
);
|
|
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
const chatPage = new ChatPage(page);
|
|
|
|
|
await chatPage.createNewChat();
|
2025-09-21 12:03:29 +01:00
|
|
|
await chatPage.chooseModelFromSelector('chat-model-reasoning');
|
|
|
|
|
await expect(chatPage.getSelectedModel()).resolves.toEqual('Reasoning model');
|
2025-04-25 23:40:15 -07:00
|
|
|
|
|
|
|
|
await page.waitForTimeout(1000);
|
2025-04-14 10:34:11 -07:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-04-25 23:40:15 -07:00
|
|
|
|
|
|
|
|
export function generateRandomTestUser() {
|
|
|
|
|
const email = `test-${getUnixTime(new Date())}@playwright.com`;
|
2025-07-03 02:26:34 -07:00
|
|
|
const password = generateId();
|
2025-04-25 23:40:15 -07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
};
|
|
|
|
|
}
|