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 = []; let profile; // ServerPicker gives its input no name or test id, so position is the only handle on it; // this breaks if the form ever gains a field before the ones below. const serverInput = () => page.locator('input').first(); const usernameInput = () => page.locator('input[name="usernameInput"]'); const passwordInput = () => page.locator('input[name="passwordInput"]'); const submitButton = () => page.locator('button[type="submit"]'); async function expectLoginForm() { await expect(serverInput()).toBeVisible(); await expect(usernameInput()).toBeVisible(); await expect(passwordInput()).toBeVisible(); await expect(submitButton()).toBeVisible(); } 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 expectLoginForm(); } async function submitLogin(username, password) { await usernameInput().fill(username); await passwordInput().fill(password); await submitButton().click(); } test.beforeEach(async () => { logs = []; if (!PASSWORD) { throw new Error('E2E_PASSWORD is not set. Export it, or add it as the CI secret.'); } // 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. 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(); // main.js opens DevTools in dev mode, and that window can be the first one registered. while (page.url().startsWith('devtools://')) { page = app.windows().find((w) => !w.url().startsWith('devtools://')) ?? await new Promise((resolve) => app.once('window', resolve)); } page.on('console', (msg) => logs.push(`[renderer] ${msg.type()}: ${msg.text()}`)); await chooseServer(HOMESERVER); }); // Config-based screenshots and traces do not fire for a page taken from app.firstWindow(), // and console capture is not covered by config at all, so this hook stays manual. test.afterEach(async ({}, testInfo) => { if (!app) return; 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')); fs.rmSync(profile, { recursive: true, force: true }); }); async function expectAuthenticated() { // Login complete reloads into the client shell, which lands on the last active path // (state/lastActivePath.ts) or the inbox, so assert a shell route rather than any URL. await expect(page).toHaveURL(/#\/(inbox|home|room|space)/); await expect .poll(() => page.evaluate(() => JSON.parse(localStorage.getItem('currentSessionUserId') ?? 'null'))) .toBe(`@${USER}:${HOMESERVER}`); // A truthy accessToken only proves a string exists, so prove it authenticates. Only the // status is asserted, so a failure cannot put a live token into the CI log or the report. await expect .poll(async () => { const token = await page.evaluate(() => JSON.parse(localStorage.getItem('matrixSessions') ?? '[]')[0]?.accessToken); if (!token) return 0; // /versions and /account 404 on ruv.wtf; /capabilities exists and requires a valid token. const res = await fetch(`https://${HOMESERVER}/_matrix/client/v3/capabilities`, { headers: { Authorization: `Bearer ${token}` } }); return res.status; }) .toBe(200); // 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(); // The renderer is still alive and the form is still usable. No second login here: every // run costs Synapse rc_login budget, and the re-login was the main source of flakes. await expect(page.evaluate(() => document.body.innerText.trim())).not.toBe(''); await expect(usernameInput()).toBeVisible(); });