Add the Vitest and Playwright harness with the login E2E test. (#109)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
// electron/main.js loads this URL whenever the app is not packaged, so the E2E
|
||||
// run needs the vite dev server up. It is the same server `npm run dev` uses.
|
||||
export const DEV_URL = 'http://localhost:38347';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
|
||||
const reachable = async () => {
|
||||
try {
|
||||
const res = await fetch(DEV_URL, { signal: AbortSignal.timeout(3000) });
|
||||
return res.ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export default async function globalSetup() {
|
||||
// A dev server the developer already started is reused rather than restarted.
|
||||
if (await reachable()) return () => {};
|
||||
|
||||
const child = spawn('npm', ['run', 'dev:vite'], { cwd: ROOT, detached: true, stdio: 'inherit' });
|
||||
|
||||
for (let attempt = 0; attempt < 60; attempt++) {
|
||||
if (await reachable()) {
|
||||
// detached puts npm and its sh -c child in their own process group.
|
||||
return () => process.kill(-child.pid, 'SIGTERM');
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
|
||||
process.kill(-child.pid, 'SIGTERM');
|
||||
throw new Error(`Vite dev server did not come up at ${DEV_URL}`);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { test, expect, _electron } from '@playwright/test';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
|
||||
const HOMESERVER = process.env.E2E_HOMESERVER ?? 'ruv.wtf';
|
||||
const USER = process.env.E2E_USER ?? 'giteaci';
|
||||
const PASSWORD = process.env.E2E_PASSWORD;
|
||||
|
||||
let app;
|
||||
let page;
|
||||
let logs;
|
||||
|
||||
const serverInput = () => page.locator('input').first();
|
||||
const usernameInput = () => page.locator('input[name="usernameInput"]');
|
||||
const passwordInput = () => page.locator('input[name="passwordInput"]');
|
||||
|
||||
async function chooseServer(server) {
|
||||
await serverInput().fill(server);
|
||||
// The server field commits on Enter, which re-runs discovery and reloads the auth flows.
|
||||
await serverInput().press('Enter');
|
||||
await expect(usernameInput()).toBeVisible();
|
||||
}
|
||||
|
||||
async function submitLogin(username, password) {
|
||||
await usernameInput().fill(username);
|
||||
await passwordInput().fill(password);
|
||||
await page.locator('button[type="submit"]').click();
|
||||
}
|
||||
|
||||
test.beforeEach(async () => {
|
||||
if (!PASSWORD) {
|
||||
throw new Error('E2E_PASSWORD is not set. Export it, or add it as the CI secret.');
|
||||
}
|
||||
|
||||
logs = [];
|
||||
|
||||
// Fresh profile per run. electron/main.js takes a single-instance lock keyed to
|
||||
// the user data dir, so a shared profile collides with a running Paarrot.
|
||||
const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'paarrot-e2e-'));
|
||||
app = await _electron.launch({ args: ['.', `--user-data-dir=${profile}`] });
|
||||
|
||||
app.on('console', (msg) => logs.push(`[main] ${msg.type()}: ${msg.text()}`));
|
||||
|
||||
page = await app.firstWindow();
|
||||
page.on('console', (msg) => logs.push(`[renderer] ${msg.type()}: ${msg.text()}`));
|
||||
|
||||
await page.waitForSelector('input');
|
||||
await chooseServer(HOMESERVER);
|
||||
});
|
||||
|
||||
test.afterEach(async ({}, testInfo) => {
|
||||
if (testInfo.status !== testInfo.expectedStatus) {
|
||||
if (page && !page.isClosed()) {
|
||||
await testInfo.attach('failure.png', { body: await page.screenshot(), contentType: 'image/png' });
|
||||
}
|
||||
await testInfo.attach('console.log', { body: logs.join('\n'), contentType: 'text/plain' });
|
||||
}
|
||||
|
||||
await app
|
||||
.close()
|
||||
// main.js preventDefault()s window close to minimise to tray, so close can hang.
|
||||
.catch(() => app.process().kill('SIGTERM'));
|
||||
});
|
||||
|
||||
async function expectAuthenticated() {
|
||||
// Login complete reloads into the client shell, which lands on the last active
|
||||
// path or the inbox, so assert on the shell rather than one route.
|
||||
await expect(page).not.toHaveURL(/#\/login\//, { timeout: 30_000 });
|
||||
|
||||
const session = await page.evaluate(() => ({
|
||||
userId: JSON.parse(localStorage.getItem('currentSessionUserId') ?? 'null'),
|
||||
sessions: JSON.parse(localStorage.getItem('matrixSessions') ?? '[]'),
|
||||
}));
|
||||
|
||||
expect(session.userId).toBe(`@${USER}:${HOMESERVER}`);
|
||||
expect(session.sessions[0].accessToken).toBeTruthy();
|
||||
|
||||
// The login form is gone, so the app is on the authenticated shell.
|
||||
await expect(usernameInput()).toHaveCount(0);
|
||||
}
|
||||
|
||||
test('test account can log in and reaches the authenticated app', async () => {
|
||||
await submitLogin(USER, PASSWORD);
|
||||
await expectAuthenticated();
|
||||
});
|
||||
|
||||
test('failed login reports an error without crashing the app', async () => {
|
||||
await submitLogin(USER, 'not-the-password');
|
||||
|
||||
await expect(page.getByText('Invalid Username or Password.')).toBeVisible({ timeout: 30_000 });
|
||||
|
||||
// The main process is still running and the form is still usable.
|
||||
expect(app.process().exitCode).toBeNull();
|
||||
await expect(usernameInput()).toBeVisible();
|
||||
|
||||
await submitLogin(USER, PASSWORD);
|
||||
await expectAuthenticated();
|
||||
});
|
||||
Reference in New Issue
Block a user