Files
anidev-design-system/tests/system-pages.mjs
T

136 lines
5.1 KiB
JavaScript

import { chromium } from "playwright";
import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
import { errorStatuses } from "../components/error-page.js";
const base = process.env.BASE_URL || "http://127.0.0.1:4173";
const browser = await chromium.launch({
executablePath:
process.env.PLAYWRIGHT_EXECUTABLE_PATH ||
"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
headless: true,
});
const page = await browser.newPage();
const errors = [];
page.on("pageerror", (error) => errors.push(error.message));
await mkdir("references/system-pages", { recursive: true });
let passed = 0;
try {
for (const theme of ["light", "dark"])
for (const locale of ["ru", "en"])
for (const width of [1440, 768, 390]) {
await page.setViewportSize({ width, height: 900 });
await page.goto(
`${base}/?page=errors&status=404&theme=${theme}&locale=${locale}`,
);
for (const status of errorStatuses) {
await page
.locator("#error-preview-status")
.selectOption(String(status));
const card = page.locator(`[data-error-status="${status}"]`);
await card.waitFor();
assert.equal(await page.locator("#content h1").count(), 1);
assert.ok(await card.locator("h1").innerText());
if (typeof status === "number")
assert.equal(
await card.locator(".error-page-code").innerText(),
String(status),
);
if (status === "banned")
assert.ok(await card.locator(".error-page-reason p").innerText());
assert.equal(
await page.evaluate(
() => document.documentElement.scrollWidth <= innerWidth,
),
true,
);
const rect = await card.boundingBox();
assert.ok(rect.x >= 0 && rect.x + rect.width <= width);
if (
locale === "ru" &&
width !== 768 &&
[404, 500, 502, "banned"].includes(status)
)
await card.screenshot({
path: `references/system-pages/${status}-${theme}-${width}.png`,
});
passed++;
}
}
await page.goto(`${base}/?page=errors&status=599&locale=ru`);
await page.locator('[data-error-status="500"]').waitFor();
await page.getByRole("button", { name: "Повторить", exact: true }).click();
await page.locator("#toast:popover-open").waitFor();
await page.locator("[data-toast-dismiss]").click();
await page
.getByRole("button", { name: "К доступным разделам", exact: true })
.click();
await page
.getByRole("button", { name: "Страницы ошибок", exact: true })
.click();
await page.locator("#error-preview-status").waitFor();
passed++;
await page.evaluate(async () => {
const { errorPageMarkup, mountErrorPage } =
await import("/components/error-page.js");
const root = document.createElement("div");
const numeric = document.createElement("div");
numeric.innerHTML = errorPageMarkup({ status: "502", id: "numeric-code" });
if (numeric.querySelector(".error-page-code").textContent !== "502")
throw Error("Numeric string status lost");
root.innerHTML = errorPageMarkup({
id: "safety-example",
status: "banned",
reason: "<img src=x onerror=alert(1)>\nПричина из API",
requestId: "<script>bad</script>",
});
document.body.append(root);
if (root.querySelector("img,script"))
throw Error("Error page did not escape server strings");
if (
!root.querySelector(".error-page-reason p").textContent.includes("<img")
)
throw Error("Reason text lost");
let clicked = 0;
const cleanup = mountErrorPage(root, { onAccount: () => clicked++ });
root.querySelector("button").click();
cleanup();
root.querySelector("button").click();
if (clicked !== 1) throw Error("Error page handlers survived cleanup");
root.remove();
const missing = document.createElement("div");
missing.innerHTML = errorPageMarkup({
id: "missing-reason",
status: "banned",
});
if (
!missing
.querySelector(".error-page-reason")
.textContent.includes("Причина не указана")
)
throw Error("Missing reason fallback");
});
passed++;
await page.emulateMedia({ reducedMotion: "reduce" });
await page.goto(`${base}/?page=errors&status=banned&theme=dark&locale=ru`);
await page.locator('[data-error-action="account"]').click();
assert.equal(new URL(page.url()).searchParams.get("page"), "profile");
passed++;
assert.deepEqual(errors, []);
await writeFile(
"references/system-pages/qa.json",
JSON.stringify(
{
passed,
errors,
checks:
"All status pages in both themes/RU/EN/three widths; preview actions/fallback; escaped reason/reference, missing reason, cleanup, account access",
},
null,
2,
) + "\n",
);
console.log(`System pages: ${passed} checks passed, no runtime errors`);
} finally {
await browser.close();
}