feat: tests & bootstrap

This commit is contained in:
Anatoly Antonov 2026-04-22 02:26:43 +09:00
parent 4bd927bb4e
commit 79e20ca37c
19 changed files with 706 additions and 23 deletions

27
tests/e2e/auth.spec.ts Normal file
View file

@ -0,0 +1,27 @@
import { test, expect, login, logout } from './fixtures';
test('anonymous users are redirected to /login from /predict', async ({ page, context }) => {
await logout(context);
await page.goto('/predict');
await page.waitForURL('**/login', { timeout: 10_000 });
await expect(page.getByRole('heading', { level: 5 }).first()).toContainText(
/Вход|Sign in/,
);
});
test('submitting the login form authenticates and redirects', async ({ page, context }) => {
await logout(context);
await page.goto('/login');
await page.getByLabel(/Имя пользователя|Username/).fill('demo');
await page.getByLabel(/Пароль|Password/).fill('demo');
await page.getByRole('button', { name: /^(Войти|Sign in)$/ }).click();
await page.waitForURL('**/predict', { timeout: 15_000 });
});
test('already-authenticated visit to / lands on /predict', async ({ page, context }) => {
await login(context);
await page.goto('/');
await page.waitForURL('**/predict', { timeout: 15_000 });
});

104
tests/e2e/fake_tawhiri.py Normal file
View file

@ -0,0 +1,104 @@
"""
Tiny stand-in for the Tawhiri prediction service. Responds to Tawhiri v2's
GET query-string endpoint with a synthetic trajectory so stratoflights has
something to forward back to leaflet_svelte during e2e tests.
Start:
python3 /tmp/fake_tawhiri.py
Then tell stratoflights to use it:
TAWHIRI_BASE_URL=http://localhost:8001/api/v2/ ... python3 manage.py runserver
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qs, urlparse
from datetime import datetime, timedelta, timezone
import json
def _iso(dt: datetime) -> str:
return dt.isoformat().replace("+00:00", "Z")
def build_prediction(params):
try:
launch_dt = datetime.fromisoformat(
params.get("launch_datetime", "2026-05-01T12:00:00Z").replace("Z", "+00:00"),
)
except Exception:
launch_dt = datetime.now(timezone.utc)
launch_lat = float(params.get("launch_latitude", 62.0))
launch_lng = float(params.get("launch_longitude", 129.0))
launch_alt = float(params.get("launch_altitude", 0.0))
burst_alt = float(params.get("burst_altitude", 30000.0))
ascent_rate = float(params.get("ascent_rate", 5.0))
descent_rate = float(params.get("descent_rate", 5.0))
ascent_duration_s = int((burst_alt - launch_alt) / max(0.1, ascent_rate))
descent_duration_s = int(burst_alt / max(0.1, descent_rate))
step = 30
ascent = []
for t in range(0, ascent_duration_s + 1, step):
ascent.append({
"altitude": launch_alt + t * ascent_rate,
"datetime": _iso(launch_dt + timedelta(seconds=t)),
"latitude": launch_lat + t * 0.00002,
"longitude": launch_lng + t * 0.00005,
})
descent = []
burst_dt = launch_dt + timedelta(seconds=ascent_duration_s)
burst_lat = launch_lat + ascent_duration_s * 0.00002
burst_lng = launch_lng + ascent_duration_s * 0.00005
for t in range(0, descent_duration_s + 1, step):
alt = max(0.0, burst_alt - t * descent_rate)
descent.append({
"altitude": alt,
"datetime": _iso(burst_dt + timedelta(seconds=t)),
"latitude": burst_lat + t * 0.00001,
"longitude": burst_lng + t * 0.00003,
})
return {
"metadata": {
"start_datetime": _iso(launch_dt - timedelta(hours=1)),
"complete_datetime": _iso(burst_dt + timedelta(seconds=descent_duration_s)),
},
"prediction": [
{"stage": "ascent", "trajectory": ascent},
{"stage": "descent", "trajectory": descent},
],
"request": {
"dataset": "fake",
"launch_latitude": launch_lat,
"launch_longitude": launch_lng,
"launch_altitude": launch_alt,
},
}
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
parsed = urlparse(self.path)
if not parsed.path.rstrip("/").endswith("/api/v2"):
self.send_error(404)
return
params = {k: v[0] for k, v in parse_qs(parsed.query).items()}
body = json.dumps(build_prediction(params)).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(body)
def log_message(self, format, *args):
# Quiet default access log; keep stderr clean.
pass
if __name__ == "__main__":
port = 8001
server = HTTPServer(("127.0.0.1", port), Handler)
print(f"fake-tawhiri listening on http://127.0.0.1:{port}/api/v2/")
server.serve_forever()

74
tests/e2e/fixtures.ts Normal file
View file

@ -0,0 +1,74 @@
import {
test as base,
expect,
type Page,
type BrowserContext,
} from '@playwright/test';
/**
* Shared fixtures and helpers.
*
* Works against either the mock plugin (VITE_USE_MOCK_API=true) or the real
* Django backend (stratoflights). Mock accepts any credentials; Django
* expects demo/demo and enforces CSRF.
*
* The `TEST_USERNAME` / `TEST_PASSWORD` envs let CI override the default.
*
* Critical detail: login/logout must go through `page.context().request` so
* cookies are shared between API calls and the page. Using the plain
* `request` fixture gives you a separate cookie jar.
*/
const USERNAME = process.env.TEST_USERNAME ?? 'demo';
const PASSWORD = process.env.TEST_PASSWORD ?? 'demo';
export async function login(context: BrowserContext): Promise<void> {
const request = context.request;
await request.get('/api/csrf/');
const state = await context.storageState();
const csrf = state.cookies.find((c) => c.name === 'csrftoken')?.value ?? '';
const res = await request.post('/api/login/', {
data: { username: USERNAME, password: PASSWORD },
headers: { 'X-CSRFToken': csrf, 'Content-Type': 'application/json' },
});
if (!res.ok()) {
throw new Error(
`Login failed: ${res.status()} ${await res.text()}. Is stratoflights running on :8000 with a user "${USERNAME}" / "${PASSWORD}"?`,
);
}
}
export async function logout(context: BrowserContext): Promise<void> {
const state = await context.storageState();
const csrf = state.cookies.find((c) => c.name === 'csrftoken')?.value ?? '';
await context.request.post('/api/logout/', {
headers: { 'X-CSRFToken': csrf, 'Content-Type': 'application/json' },
});
// Clear cookies so the page navigator is fully anonymous next goto.
await context.clearCookies();
}
export const test = base.extend({
page: async ({ page }, use) => {
const hardErrors: string[] = [];
page.on('pageerror', (e) => hardErrors.push(`[pageerror] ${e.message}`));
page.on('console', (msg) => {
if (msg.type() === 'error') {
// eslint-disable-next-line no-console
console.log(' [page console.error]', msg.text());
}
});
await use(page);
if (hardErrors.length > 0) throw new Error('Page errors:\n' + hardErrors.join('\n'));
},
});
export { expect };
export async function openPredict(page: Page) {
await page.goto('/predict');
await page
.locator('.map-container canvas')
.first()
.waitFor({ state: 'attached', timeout: 15_000 });
}

View file

@ -0,0 +1,26 @@
import { test, expect, openPredict, login } from './fixtures';
test.beforeEach(async ({ context, page }) => {
await login(context);
await page.goto('/');
await page.evaluate(() => localStorage.removeItem('workspaces'));
});
test('can open the points library from the conditions panel', async ({ page }) => {
await openPredict(page);
await page
.locator('.panel-container-left')
.getByRole('button', { name: /Условия|Conditions/ })
.first()
.click();
const openBookBtn = page
.locator('.panel-container-left')
.getByRole('button')
.filter({ has: page.locator('i.bi-journal-bookmark-fill') })
.first();
await openBookBtn.click();
await expect(page.getByRole('dialog')).toBeVisible();
});

View file

@ -0,0 +1,24 @@
import { test, expect, openPredict, login } from './fixtures';
test.beforeEach(async ({ context }) => {
await login(context);
});
test('switching locale to English updates UI text', async ({ page }) => {
await openPredict(page);
await page
.locator('.panel-container-right')
.getByRole('button', { name: /Настройки|Settings/ })
.first()
.click();
const langSelect = page.locator('.panel-container-right select').first();
await langSelect.selectOption('en');
await expect(
page.locator('.panel-container-right').getByRole('button', { name: /Workspaces/ }).first(),
).toBeVisible({ timeout: 5_000 });
await langSelect.selectOption('ru');
});

31
tests/e2e/smoke.spec.ts Normal file
View file

@ -0,0 +1,31 @@
import { test, expect, login } from './fixtures';
test.beforeEach(async ({ context }) => {
await login(context);
});
test('predict page loads and mounts the MapLibre canvas', async ({ page }) => {
await page.goto('/predict');
await expect(page.locator('.map-container canvas').first()).toBeAttached({
timeout: 15_000,
});
});
test('left and right panels are visible on predict', async ({ page }) => {
await page.goto('/predict');
await expect(page.locator('.panel-container-left')).toBeVisible();
await expect(page.locator('.panel-container-right')).toBeVisible();
});
test('navbar shows current username dropdown when authenticated', async ({ page }) => {
await page.goto('/predict');
await expect(page.getByRole('link', { name: /Прогноз|Predict/ }).first()).toBeVisible();
await expect(page.getByText('demo').first()).toBeVisible();
});
test('tracking page mounts its own map', async ({ page }) => {
await page.goto('/track');
await expect(page.locator('.map-container canvas').first()).toBeAttached({
timeout: 15_000,
});
});

View file

@ -0,0 +1,68 @@
import { test, expect, openPredict, login } from './fixtures';
test.beforeEach(async ({ context, page }) => {
await login(context);
await page.goto('/');
await page.evaluate(() => localStorage.removeItem('workspaces'));
});
function workspacesPanel(page: import('@playwright/test').Page) {
return page.locator('.panel-container-right');
}
test('a workspace is auto-created on first visit to /predict', async ({ page }) => {
await openPredict(page);
const panel = workspacesPanel(page);
await expect(panel.locator('.workspace-row').first()).toBeVisible({ timeout: 10_000 });
await expect(panel.getByRole('button', { name: /Рассчитать|Run/ }).first()).toBeVisible();
});
test('can add and remove workspaces', async ({ page }) => {
await openPredict(page);
const panel = workspacesPanel(page);
await expect(panel.locator('.workspace-row')).toHaveCount(1, { timeout: 10_000 });
const addBtn = panel.getByRole('button', { name: /Добавить|Add/ }).first();
await addBtn.click();
await expect(panel.locator('.workspace-row')).toHaveCount(2);
await addBtn.click();
await expect(panel.locator('.workspace-row')).toHaveCount(3);
// Delete the last workspace via its row's delete button.
const deleteBtn = panel.locator('.workspace-row').last().locator('button.btn-danger');
await deleteBtn.click();
// Confirm in the modal.
await page
.getByRole('dialog')
.getByRole('button', { name: /^(Удалить|Delete)$/ })
.click();
await expect(panel.locator('.workspace-row')).toHaveCount(2);
});
test('workspace render pipeline adds a map scene after a run', async ({ page }) => {
test.setTimeout(90_000);
await openPredict(page);
// Wait for the Run button to be enabled before clicking.
const runBtn = workspacesPanel(page)
.locator('.workspace-row')
.first()
.getByRole('button', { name: /Рассчитать|Run/ });
await runBtn.click();
// Wait for the prediction request to complete and layers to be added.
await expect
.poll(
async () =>
page.evaluate(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const map: any = (window as any)._lsvMap;
if (!map) return 0;
return map
.getStyle()
.layers.filter((l: { id: string }) => l.id.startsWith('ws/')).length;
}),
{ timeout: 75_000, intervals: [1000, 2000, 3000] },
)
.toBeGreaterThan(0);
});