Andrey Epifancev efa537e008 perf: каркас замеров фронта
Refs #202

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 22:12:36 +04:00

26 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { BrowserContext } from '@playwright/test';
import type { User } from './manifest';
export const API = process.env.PERF_API ?? 'http://localhost:8000';
export const WEB = process.env.PERF_WEB ?? 'http://localhost:8080';
// Ключи localStorage: web/src/api/utils/token.js (saveAuthTokens), их читает AuthProvider.jsx.
export const ACCESS_KEY = 'access_token';
export const REFRESH_KEY = 'refresh_token';
/** Логин через API и запись токенов в localStorage origin'а фронта (init-script, до загрузки страницы). */
export async function loginAs(context: BrowserContext, user: User): Promise<void> {
const r = await context.request.post(`${API}/api/v1/auth/login`, {
data: { username: user.username, password: user.password },
});
if (!r.ok()) throw new Error(`login ${user.username}: ${r.status()} ${await r.text()}`);
const d = await r.json();
await context.addInitScript(
([a, rk, at, rt]) => {
localStorage.setItem(a, at);
if (rt) localStorage.setItem(rk, rt);
},
[ACCESS_KEY, REFRESH_KEY, d.access_token, d.refresh_token ?? ''] as const,
);
}