Files
loft/frontend/e2e/helpers.ts
2026-06-04 21:55:10 +03:00

27 lines
1.1 KiB
TypeScript

import { expect, type Page } from '@playwright/test';
/** A fresh, unique account per call so tests never collide in the shared test DB. */
export function uniqueUser() {
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
return { username: `e2e_${id}`, password: 'password123' };
}
/**
* Registers a brand-new user and logs in via the real /api/auth flow.
* `page.request` shares the browser context's cookie jar, so the session
* cookie it sets makes subsequent page navigations authenticated.
*/
export async function registerAndLogin(page: Page) {
const user = uniqueUser();
const register = await page.request.post('/api/auth/register', { data: user });
expect(register.ok(), 'register should succeed').toBeTruthy();
const login = await page.request.post('/api/auth/login', { data: user });
expect(login.ok(), 'login should succeed').toBeTruthy();
return user;
}
/** A small in-memory upload fixture (no files on disk needed). */
export function textFile(name: string) {
return { name, mimeType: 'text/plain', buffer: Buffer.from('e2e test content') };
}