feat: support guest session (#919)
This commit is contained in:
parent
24cb2ce19b
commit
9279135355
34 changed files with 741 additions and 288 deletions
81
tests/helpers.ts
Normal file
81
tests/helpers.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
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 { ChatPage } from './pages/chat';
|
||||
import { getUnixTime } from 'date-fns';
|
||||
|
||||
export type UserContext = {
|
||||
context: BrowserContext;
|
||||
page: Page;
|
||||
request: APIRequestContext;
|
||||
};
|
||||
|
||||
export async function createAuthenticatedContext({
|
||||
browser,
|
||||
name,
|
||||
chatModel = 'chat-model',
|
||||
}: {
|
||||
browser: Browser;
|
||||
name: string;
|
||||
chatModel?: 'chat-model' | 'chat-model-reasoning';
|
||||
}): 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(16);
|
||||
|
||||
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!',
|
||||
);
|
||||
|
||||
const chatPage = new ChatPage(page);
|
||||
await chatPage.createNewChat();
|
||||
await chatPage.chooseModelFromSelector('chat-model-reasoning');
|
||||
await expect(chatPage.getSelectedModel()).resolves.toEqual('Reasoning model');
|
||||
|
||||
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(16);
|
||||
|
||||
return {
|
||||
email,
|
||||
password,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue