Files
cinny-desktop/tests/e2e/login.spec.mjs
T

100 lines
3.5 KiB
JavaScript

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();
});