fix: improve calculator accessibility and async result handling
This commit is contained in:
1 parent
ea2f0fbe05
commit
bb21a767d7
9 files changed
+473
-105
No files matched your search
@@ -0,0 +1,201 @@
|
||||
// Run against an isolated Belki server. See AGENTS.md for setup and cleanup.
|
||||
const assert = require('node:assert/strict');
|
||||
const { chromium } = require('playwright');
|
||||
|
||||
const origin = process.env.BELKI_TEST_URL || 'http://127.0.0.1:8876';
|
||||
// This suite writes synthetic data. Refuse to run against a public/production host.
|
||||
assert(['127.0.0.1', 'localhost', '[::1]'].includes(new URL(origin).hostname));
|
||||
|
||||
async function run() {
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
...(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}),
|
||||
});
|
||||
try {
|
||||
for (const width of [1280, 390]) {
|
||||
const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: 'reduce', locale: 'ru-RU' });
|
||||
try {
|
||||
const errors = [];
|
||||
const page = await context.newPage();
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); });
|
||||
await page.route('**/api/weather/ulli', route => route.fulfill({ json: { available: false } }));
|
||||
await page.goto(`${origin}/belki`);
|
||||
assert.equal(await page.title(), 'Отчёты — Беличья нора');
|
||||
assert(await page.getByRole('heading', { level: 1 }).isVisible());
|
||||
assert.deepEqual(await page.locator('input, select, textarea').evaluateAll(fields => fields
|
||||
.filter(field => field.getClientRects().length && !field.labels?.length && !field.getAttribute('aria-label') && !field.getAttribute('aria-labelledby'))
|
||||
.map(field => field.id || field.name)), []);
|
||||
|
||||
// Native radio keyboard behavior works without a custom focusgroup/polyfill.
|
||||
await page.locator('[data-mode="coaching"]').focus();
|
||||
assert.equal(await page.locator('[data-mode="coaching"]').evaluate(node => getComputedStyle(node.closest('label')).outlineStyle), 'solid');
|
||||
await page.keyboard.press('ArrowRight');
|
||||
assert(await page.locator('[data-mode="training"]').isChecked());
|
||||
assert(await page.locator('#training').isVisible());
|
||||
await page.keyboard.press('ArrowLeft');
|
||||
assert(await page.locator('[data-mode="coaching"]').isChecked());
|
||||
await page.locator('#addKitten').click();
|
||||
assert(await page.locator('#extraKittens').getByLabel('Имя котёнка').isVisible());
|
||||
await page.locator('#extraKittens [data-remove]').click();
|
||||
assert(await page.locator('#addKitten').evaluate(node => node === document.activeElement));
|
||||
|
||||
// A real calculation is invalidated by edits, even before another submit.
|
||||
for (const [name, value] of Object.entries({ escort_name: 'Тестовый куратор', escort_id: '101', kitten_name: 'Тестовый котёнок', kitten_id: '102' })) {
|
||||
await page.locator(`#coaching [name="${name}"]`).fill(value);
|
||||
}
|
||||
await page.locator('#reportActions [type="submit"]').click();
|
||||
await page.locator('#result').waitFor({ state: 'visible' });
|
||||
assert(await page.locator('#sendReport').isEnabled());
|
||||
await page.evaluate(() => Object.defineProperty(navigator, 'clipboard', {
|
||||
configurable: true, value: { writeText: async () => { throw new Error('Permission denied'); } },
|
||||
}));
|
||||
await page.locator('#copy').click();
|
||||
assert.match(await page.locator('#reportStatus').textContent(), /Текст выделен/);
|
||||
assert(await page.locator('#message').evaluate(node => node.selectionEnd === node.value.length));
|
||||
await page.locator('#coaching [name="kitten_name"]').fill('Другой котёнок');
|
||||
assert(await page.locator('#result').isHidden());
|
||||
assert(await page.locator('#sendReport').isDisabled());
|
||||
|
||||
// Reject a delayed response after switching modes; prevent duplicate submits.
|
||||
let release;
|
||||
const gate = new Promise(resolve => { release = resolve; });
|
||||
let requests = 0;
|
||||
const delayed = async route => { requests += 1; await gate; await route.fulfill({ json: { message: 'Устаревший отчёт' } }); };
|
||||
await page.route('**/api/calculate', delayed);
|
||||
const request = page.waitForRequest('**/api/calculate');
|
||||
await page.locator('#reportActions [type="submit"]').click();
|
||||
await request;
|
||||
assert(await page.locator('#reportActions [type="submit"]').isDisabled());
|
||||
await page.locator('#form').evaluate(form => form.requestSubmit());
|
||||
await page.locator('[data-mode="points"]').check();
|
||||
release();
|
||||
await page.waitForFunction(() => !document.querySelector('#form').hasAttribute('aria-busy'));
|
||||
assert.equal(requests, 1);
|
||||
assert(await page.locator('#result').isHidden());
|
||||
assert(await page.locator('#sendReport').isDisabled());
|
||||
await page.unroute('**/api/calculate', delayed);
|
||||
|
||||
// Inactive activity fields must not block browser constraint validation.
|
||||
await page.locator('#pointsInspection [name="inspections"]').evaluate(node => { node.value = '-1'; });
|
||||
await page.locator('#pointsActivity').selectOption('training');
|
||||
await page.locator('#pointsActions [type="submit"]').click();
|
||||
await page.locator('#pointsResult').waitFor({ state: 'visible' });
|
||||
assert.match(await page.locator('#pointsSummary').textContent(), /25/);
|
||||
await page.locator('[data-mode="inspection"]').check();
|
||||
assert(await page.locator('#pointsResult').isHidden());
|
||||
await page.locator('#inspection [name="resources"]').fill('+2 вьюнок');
|
||||
await page.locator('#noResourcesBtn').click();
|
||||
assert.equal(await page.locator('#inspection [name="resources"]').inputValue(), '');
|
||||
|
||||
// Native modal contains focus, Escape closes it and returns focus.
|
||||
await page.locator('#inspection [name="character_name"]').fill('Несохранённый кот');
|
||||
await page.locator('#resolveIdentity').click();
|
||||
await page.getByRole('dialog', { name: 'Уточнить имя или ID кота' }).waitFor();
|
||||
assert(await page.locator('#identityMissingValue').evaluate(node => node === document.activeElement));
|
||||
await page.keyboard.press('Escape');
|
||||
assert(await page.locator('#identityDialog').isHidden());
|
||||
assert(await page.locator('#resolveIdentity').evaluate(node => node === document.activeElement));
|
||||
|
||||
// Native links keep URLs, browser Back, and context-menu/new-tab behavior.
|
||||
assert.equal(await page.locator('#applicationsTab').getAttribute('href'), '/belki?section=applications');
|
||||
await page.locator('#applicationsTab').click();
|
||||
assert.equal(new URL(page.url()).searchParams.get('section'), 'applications');
|
||||
await page.goBack();
|
||||
assert(await page.locator('#reportsSection').isVisible());
|
||||
await page.goForward();
|
||||
assert(await page.locator('#applicationsSection').isVisible());
|
||||
|
||||
// Both application entry points expose file inputs and reset hidden fields.
|
||||
for (const standalone of [false, true]) {
|
||||
if (standalone) await page.goto(`${origin}/applications`);
|
||||
const type = value => page.locator(standalone ? `[data-type="${value}"]` : `[data-application-type="${value}"]`);
|
||||
const practiced = page.locator('[name="practiced_before"]');
|
||||
const practiceLink = page.locator('[name="practice_review_link"]');
|
||||
await type('flying_squirrel').click();
|
||||
assert(await practiceLink.isDisabled());
|
||||
await practiced.selectOption('yes');
|
||||
assert(await practiceLink.isVisible());
|
||||
await type('kitten').click();
|
||||
const applicationForm = page.locator(standalone ? '#form' : '#applicationForm');
|
||||
const file = applicationForm.locator('input[type="file"]:not(:disabled)');
|
||||
assert(await file.isVisible());
|
||||
await file.focus();
|
||||
assert(await file.evaluate(node => node === document.activeElement));
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false);
|
||||
if (process.env.BELKI_SCREENSHOT_DIR) {
|
||||
await page.screenshot({ path: `${process.env.BELKI_SCREENSHOT_DIR}/applications-${standalone ? 'standalone' : 'main'}-${width}.png`, fullPage: true });
|
||||
}
|
||||
await type('flying_squirrel').click();
|
||||
await practiced.selectOption('no');
|
||||
assert(await practiceLink.isDisabled());
|
||||
assert(await practiceLink.isHidden());
|
||||
|
||||
// An in-flight submission cannot post a duplicate application.
|
||||
await type('feather_donation').click();
|
||||
await applicationForm.locator('[name="name"]').fill('Тестовая белка');
|
||||
await applicationForm.locator('[name="character_id"]').fill('103');
|
||||
for (const [field, value] of Object.entries({ amount: '1', color: 'синее', source: 'тестовые запасы' })) {
|
||||
await applicationForm.locator(`[data-field="${field}"], [data-application-field="${field}"]`).fill(value);
|
||||
}
|
||||
let finish;
|
||||
const pending = new Promise(resolve => { finish = resolve; });
|
||||
let submissions = 0;
|
||||
const submitApplication = async route => { submissions += 1; await pending; await route.fulfill({ json: { id: 55 } }); };
|
||||
await page.route('**/api/applications', submitApplication);
|
||||
const submitted = page.waitForRequest('**/api/applications');
|
||||
await applicationForm.locator('[type="submit"]').click();
|
||||
await submitted;
|
||||
assert(await applicationForm.locator('[type="submit"]').isDisabled());
|
||||
await applicationForm.evaluate(form => form.requestSubmit());
|
||||
finish();
|
||||
await page.locator(standalone ? '#success' : '#applicationSuccess').waitFor({ state: 'visible' });
|
||||
assert.equal(submissions, 1);
|
||||
await page.unroute('**/api/applications', submitApplication);
|
||||
}
|
||||
|
||||
// Capture useful review artifacts only when explicitly requested outside Git.
|
||||
await page.goto(`${origin}/belki`);
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false);
|
||||
if (process.env.BELKI_SCREENSHOT_DIR) {
|
||||
await page.screenshot({ path: `${process.env.BELKI_SCREENSHOT_DIR}/calculator-${width}.png`, fullPage: true });
|
||||
}
|
||||
await page.goto(`${origin}/admin`);
|
||||
assert(await page.getByLabel('Пароль', { exact: true }).isVisible());
|
||||
|
||||
// Review screens use pressed buttons; native gallery Escape restores focus.
|
||||
await page.route('**/api/admin/session', route => route.fulfill({ json: { authenticated: true } }));
|
||||
const photo = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j8WQAAAAASUVORK5CYII=';
|
||||
await page.route('**/api/admin/calculations?*', route => route.fulfill({ json: { items: [
|
||||
{ id: 1, created_at: '2026-11-14T10:00:00Z', message: 'Тестовый отчёт', input: { mode: 'training', attachments: [{ url: photo }] } },
|
||||
] } }));
|
||||
await page.route('**/api/admin/applications?*', route => route.fulfill({ json: { items: [] } }));
|
||||
await page.routeWebSocket('**/api/admin/updates', () => {});
|
||||
await page.reload();
|
||||
await page.locator('#appView').waitFor({ state: 'visible' });
|
||||
assert.equal(await page.locator('#reportPeriod').inputValue(), 'current_month');
|
||||
assert.equal(await page.locator('#sort').inputValue(), 'date');
|
||||
assert.equal(await page.locator('#direction').inputValue(), 'desc');
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false);
|
||||
await page.locator('[data-category="training"]').click();
|
||||
assert.equal(await page.locator('[data-category="training"]').getAttribute('aria-pressed'), 'true');
|
||||
const thumbnail = page.getByRole('button', { name: 'Открыть фото 1' });
|
||||
await thumbnail.click();
|
||||
await page.getByRole('dialog', { name: 'Скриншоты отчёта' }).waitFor();
|
||||
await page.keyboard.press('Escape');
|
||||
assert(await page.locator('#galleryDialog').isHidden());
|
||||
assert(await thumbnail.evaluate(node => node === document.activeElement));
|
||||
if (process.env.BELKI_SCREENSHOT_DIR) {
|
||||
await page.screenshot({ path: `${process.env.BELKI_SCREENSHOT_DIR}/admin-${width}.png`, fullPage: true });
|
||||
}
|
||||
assert.deepEqual(errors, []);
|
||||
console.log(`PASS ${width}px: labels, keyboard, calculations, races, clipboard, dialogs, links, applications, admin filters`);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
run().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
Reference in new issue
Block a user