Add automated testing infrastructure (Vitest + Playwright E2E) #110

Merged
litruv merged 12 commits from issue-109-automated-testing into main 2026-11-03 13:51:15 +11:00
10 changed files with 1420 additions and 6 deletions
Showing only changes of commit 348e47dbd8 - Show all commits
+82
View File
@@ -0,0 +1,82 @@
name: Tests
on:
pull_request:
jobs:
unit:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install dependencies
run: npm ci
- name: Run unit tests
run: npm run test:unit
e2e:
runs-on: ubuntu-latest
env:
# Dedicated test account on the homeserver, not a personal one.
E2E_HOMESERVER: ruv.wtf
E2E_USER: giteaci
# Repo secret: E2E_PASSWORD. The test fails with a clear message when it is missing.
E2E_PASSWORD: ${{ secrets.E2E_PASSWORD }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: false
- name: Checkout submodules manually
run: |
set -euo pipefail
# Use the same host/scheme the runner already used for origin (not public HTTPS).
ORIGIN_URL=$(git remote get-url origin)
ORIGIN_URL=${ORIGIN_URL%/}
ORIGIN_URL=${ORIGIN_URL%.git}
BASE_URL=${ORIGIN_URL%/*}
CINNY_URL="${BASE_URL}/cinny.git"
echo "Rewriting submodule URL to ${CINNY_URL}"
git config submodule.cinny.url "${CINNY_URL}"
git submodule sync --recursive
git submodule update --init --recursive
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: |
package-lock.json
cinny/package-lock.json
- name: Install dependencies
run: npm ci
# The E2E run drives the vite dev server, so cinny's dependencies are needed too.
- name: Install dependencies (cinny)
working-directory: ./cinny
run: npm ci --prefer-offline
- name: Ensure a display for Electron
run: command -v xvfb-run >/dev/null || sudo apt-get install -y xvfb
- name: Run login E2E
run: xvfb-run -a npm run test:e2e
- name: Upload test artifacts
if: failure()
uses: actions/upload-artifact@v4
with:
name: e2e-artifacts
path: |
test-results
playwright-report
+2
View File
@@ -5,3 +5,5 @@ devAssets
.DS_Store
dist-electron/
playwright-report/
test-results/
+21
View File
@@ -30,6 +30,27 @@ To build the app locally, run:
To start local dev server, run:
* `npm run dev`
## Testing
* `npm run test` - unit and E2E
* `npm run test:unit` - Vitest
* `npm run test:e2e` - Playwright driving Electron
The E2E test drives the real Electron app against the vite dev server, so it needs the
cinny dependencies installed and an X display. On a headless machine run it under xvfb:
`xvfb-run -a npm run test:e2e`.
The login test uses a dedicated test account, never a personal one:
Variable | Default
---|---
`E2E_HOMESERVER` | `ruv.wtf`
`E2E_USER` | `giteaci`
`E2E_PASSWORD` | required, read from the environment, never committed
A failed E2E run attaches a screenshot and the main and renderer console logs to the
report. CI uploads `test-results/` and `playwright-report/` as artifacts.
## Plugin System
Paarrot includes a plugin system for extending and customising the app with JavaScript modules.
+1120 -5
View File
File diff suppressed because it is too large Load Diff
+6 -1
View File
@@ -20,7 +20,10 @@
"build:linux": "npm run build -- --linux",
"build:win": "npm run build -- --win",
"postman:generate": "node scripts/generate-postman-collection.js",
"generate:update-manifest": "node scripts/generate-update-manifest.mjs"
"generate:update-manifest": "node scripts/generate-update-manifest.mjs",
"test": "npm run test:unit && npm run test:e2e",
"test:unit": "vitest run",
"test:e2e": "playwright test"
},
"keywords": [],
"author": {
@@ -40,6 +43,7 @@
"devDependencies": {
"@actions/github": "9.1.1",
"@electron/rebuild": "4.2.0",
"@playwright/test": "1.64.1",
"concurrently": "10.0.3",
"cross-env": "10.1.0",
"electron": "43.1.0",
@@ -47,6 +51,7 @@
"node-fetch": "3.3.2",
"png2icons": "2.0.1",
"sharp": "0.35.3",
"vitest": "5.1.0",
"wait-on": "9.0.10"
},
"allowScripts": {
+13
View File
@@ -0,0 +1,13 @@
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests/e2e',
// Electron launch plus first paint is far slower than a page load.
timeout: 120_000,
// One Electron process at a time: the app takes a single-instance lock and
// every test shares the same vite dev server.
workers: 1,
outputDir: 'test-results',
reporter: [['list'], ['html', { open: false }]],
globalSetup: './tests/e2e/dev-server.mjs',
});
+36
View File
@@ -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}`);
}
+99
View File
@@ -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();
});
+33
View File
@@ -0,0 +1,33 @@
import { afterAll, beforeAll, expect, test } from 'vitest';
import PaarrotAPIServer from '../../electron/api-server.js';
const api = new PaarrotAPIServer(null);
let base;
beforeAll(async () => {
// Port 0 lets the OS pick a free port, so the test never collides with a running Paarrot.
await api.start(0);
base = `http://127.0.0.1:${api.server.address().port}`;
});
afterAll(async () => {
await api.stop();
});
test('GET /health reports the API server is up', async () => {
const res = await fetch(`${base}/health`);
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ status: 'ok', app: 'Paarrot API', version: '1.0.0' });
});
test('unknown endpoint answers 404 with the endpoint list', async () => {
const res = await fetch(`${base}/not-an-endpoint`);
expect(res.status).toBe(404);
const body = await res.json();
expect(body.error).toBe('Endpoint not found');
expect(body.availableEndpoints).toContain('GET /health');
});
+8
View File
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['tests/unit/**/*.test.js'],
environment: 'node',
},
});