48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
async function globalSetup() {
|
|
// Read env vars written by scripts/test-env.sh (make test-up calls it first)
|
|
const envFile = path.resolve(__dirname, '../../../data/test/.env');
|
|
if (!fs.existsSync(envFile)) {
|
|
throw new Error('data/test/.env not found — run "make test-up" first');
|
|
}
|
|
for (const line of fs.readFileSync(envFile, 'utf-8').split('\n')) {
|
|
const eq = line.indexOf('=');
|
|
if (eq > 0) process.env[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
|
|
}
|
|
|
|
const baseURL = process.env.TT_BASE_URL!;
|
|
|
|
// Obtain admin JWT via the login API
|
|
const res = await fetch(`${baseURL}/api/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email: 'admin@tutortool.com', password: 'admin' }),
|
|
});
|
|
if (!res.ok) throw new Error(`Login failed: ${res.status} ${await res.text()}`);
|
|
const { token, is_superadmin } = await res.json() as { token: string; is_superadmin: boolean };
|
|
|
|
// Write Playwright storage state with localStorage pre-populated
|
|
const authDir = path.resolve(__dirname, '.auth');
|
|
fs.mkdirSync(authDir, { recursive: true });
|
|
const storageState = {
|
|
cookies: [],
|
|
origins: [
|
|
{
|
|
origin: baseURL,
|
|
localStorage: [
|
|
{ name: 'token', value: token },
|
|
{ name: 'is_superadmin', value: String(is_superadmin) },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
fs.writeFileSync(path.join(authDir, 'admin.json'), JSON.stringify(storageState, null, 2));
|
|
}
|
|
|
|
export default globalSetup;
|