freshyo/tests/web/specs/e2e-setup.spec.ts
2026-10-05 11:46:47 +05:30

71 lines
2.8 KiB
TypeScript

import * as fs from 'node:fs';
import * as path from 'node:path';
import { test, expect } from '@playwright/test';
import { loginAsSuperadmin, superadminUrl, stamp } from '../lib/helpers';
import { staffLogin } from '../lib/trpc';
/**
* Full-suite setup (tests/e2e-suite.txt steps 1-3).
* Reuses region 1 (Mahabubnagar): asserts it is present, creates a fresh
* region-1 admin, verifies the admin can log in, then writes the hand-off
* env file that tests/e2e/full-suite/run.sh sources for the Maestro legs.
*
* Run standalone: npx playwright test --config tests/web/playwright.config.ts e2e-setup
*/
test('e2e setup: region 1 present + fresh region-1 admin + hand-off env (suite steps 1-3)', async ({ page }) => {
const runStamp = stamp();
const adminName = `e2e-admin-${runStamp}`;
const adminPassword = `E2ePass${runStamp.slice(-4)}!x9`;
const regionId = 1;
// Step 1: superadmin login (lands on /dashboard).
await loginAsSuperadmin(page);
// Step 2: region 1 must exist — assert Mahabubnagar on the regions list
// (list renders newest-first, so scroll it into view).
await page.goto(`${superadminUrl()}/regions`, { waitUntil: 'networkidle' });
const mbnr = page.getByText('Mahabubnagar').first();
await mbnr.scrollIntoViewIfNeeded();
await expect(mbnr).toBeVisible({ timeout: 20_000 });
// Step 3: create an admin account scoped to Mahabubnagar (region 1).
await page.goto(`${superadminUrl()}/admins`, { waitUntil: 'networkidle' });
await page.waitForTimeout(1500);
await page.fill('input[placeholder^="e.g. ravi"]', adminName);
await page.fill('input[type="password"]', adminPassword);
const selects = await page.$$('select');
expect(selects).toHaveLength(2);
await selects[0].selectOption({ label: 'admin' });
await selects[1].selectOption({ label: 'Mahabubnagar' });
const dialogText = await new Promise<string>((resolve) => {
page.once('dialog', async (d) => {
const msg = d.message();
await d.dismiss().catch(() => {});
resolve(msg);
});
page.click('button:has-text("Create Admin")').catch(() => {});
});
expect(dialogText).toMatch(/Admin created successfully/);
// New admin authenticates and belongs to region 1.
const { staff } = await staffLogin(adminName, adminPassword);
expect(staff.regionId).toBe(regionId);
expect(staff.roleName).toBe('admin');
// Hand-off for the Maestro legs.
const outDir = path.resolve(__dirname, '../../e2e/full-suite');
fs.mkdirSync(outDir, { recursive: true });
const envFile = path.join(outDir, '.env.e2e');
fs.writeFileSync(
envFile,
[
'# Generated by tests/web e2e-setup spec — sourced by tests/e2e/full-suite/run.sh. Do not edit.',
`STAMP=${runStamp}`,
`ADMIN_NAME=${adminName}`,
`ADMIN_PASSWORD=${adminPassword}`,
`REGION_ID=${regionId}`,
'',
].join('\n'),
);
});