import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
import { chromium } from "playwright";
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 context = await browser.newContext({ acceptDownloads: true });
const page = await context.newPage();
page.setDefaultTimeout(8000);
const errors = [];
let passed = 0;
page.on("pageerror", (e) => errors.push(e.message));
await mkdir("test-results/content", { recursive: true });
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=statistics&panel=admin&theme=${theme}&locale=${locale}`,
);
await page.locator(".statistics-panel").last().waitFor();
assert.equal(await page.locator(".statistics-panel").count(), 8);
const layout = await page
.locator(".statistics-panel")
.evaluateAll((nodes) =>
nodes.map((node) => {
const plot = node.querySelector(".chart-plot"),
rect = plot.getBoundingClientRect(),
data = node
.querySelector(".chart-data")
.getBoundingClientRect();
return {
height: rect.height,
bottom: rect.bottom,
dataTop: data.top,
colors: [...node.querySelectorAll(".chart-line")].map(
(n) => getComputedStyle(n).stroke,
),
};
}),
);
assert.ok(
layout.every(
(x) => Math.abs(x.height - 180) < 1 && x.dataTop >= x.bottom,
),
JSON.stringify(layout),
);
assert.notEqual(layout[2].colors[0], layout[2].colors[1]);
assert.equal(
await page.evaluate(
() => document.documentElement.scrollWidth > innerWidth,
),
false,
);
const chart = page.locator('[data-stat-chart="network"]');
await chart
.locator(".chart-plot")
.hover({ position: { x: 70, y: 80 } });
assert.equal(await chart.locator(".chart-tooltip").isVisible(), true);
await chart.locator(".chart-plot").focus();
await page.keyboard.press("End");
assert.ok(
(await chart.locator(".chart-tooltip").innerText()).includes(
locale === "ru" ? "Исходящий" : "Outbound",
),
);
await page.keyboard.press("Escape");
assert.equal(await chart.locator(".chart-tooltip").isVisible(), false);
await chart.locator("summary").click();
assert.equal(await chart.locator("tbody tr").count(), 24);
await chart.locator("summary").click();
await page.locator("[data-statistics-range]").selectOption("24h");
assert.equal(
await page.locator("[data-statistics-range]").inputValue(),
"24h",
);
await page.evaluate(() => {
document.activeElement?.blur();
scrollTo(0, 0);
});
await page.mouse.move(width - 2, 2);
if (locale === "ru")
await page.screenshot({
path: `test-results/content/statistics-${theme}-${width}.png`,
fullPage: true,
});
passed++;
}
for (const theme of ["light", "dark"])
for (const width of [1440, 768, 390]) {
await page.setViewportSize({ width, height: 844 });
await page.goto(
`${base}/?page=documents&path=/server-guide.md&theme=${theme}`,
);
await page.locator(".markdown-body table").waitFor();
assert.equal(
await page.evaluate(
() => document.documentElement.scrollWidth > innerWidth,
),
false,
`Markdown overflow ${theme}/${width}`,
);
await page.screenshot({
path: `test-results/content/markdown-${theme}-${width}.png`,
fullPage: true,
});
passed++;
}
await page.setViewportSize({ width: 1440, height: 950 });
await page.goto(
base + "/?page=documents&path=/some/fee/foo/README.md&theme=dark",
);
await page.locator(".markdown-body h1").waitFor();
assert.ok(await page.locator(".code-editor-gutter").innerText());
await page
.locator(".code-editor textarea")
.fill('# Новый заголовок\n\n```json\n{"port":8080}\n```');
await page
.locator(".markdown-body h1")
.filter({ hasText: "Новый заголовок" })
.waitFor();
await page.locator(".markdown-body .hljs-attr").waitFor();
const before = await page.locator(".code-editor textarea").inputValue();
await page.locator(".code-editor textarea").press("Control+End");
await page.keyboard.press("Enter");
await page.keyboard.press("Tab");
await page.keyboard.insertText("abc");
assert.ok(
(await page.locator(".code-editor textarea").inputValue()).endsWith(
"\n abc",
),
);
await page.keyboard.press("Control+z");
const download = page.waitForEvent("download");
await page.locator("[data-document-download]").click();
await download;
await page.locator(".breadcrumbs a").filter({ hasText: /^fee$/ }).click();
assert.equal(new URL(page.url()).searchParams.get("path"), "/some/fee");
await page.goBack();
await page
.locator(".markdown-body h1")
.filter({ hasText: "Новый заголовок" })
.waitFor();
await page.screenshot({
path: "test-results/content/documents-dark.png",
fullPage: true,
});
passed++;
await page.evaluate(async () => {
const {
mountMarkdown,
mountChart,
mountMiniLog,
mountStatistics,
statisticsMarkup,
mountCodeEditor,
} = await import("/runtime/index.js");
const fixture = document.createElement("section");
fixture.id = "content-fixture";
fixture.style = "position:relative;margin:30px 120px;width:500px;";
document.body.append(fixture);
const markdown = document.createElement("div");
fixture.append(markdown);
window.__unsafe = 0;
const md = mountMarkdown(markdown, {
source:
'# Safe\n\n
bad\n\n- [x] done',
});
await md.ready;
if (
markdown.querySelector(
'script,iframe,[onerror],a[href^="javascript:"]',
) ||
window.__unsafe
)
throw Error("Unsafe Markdown");
if (!markdown.querySelector("input[disabled]"))
throw Error("Task is editable");
await md.setSource("## Replacement");
if (!markdown.querySelector("h2")) throw Error("Markdown update");
md.dispose();
const el = document.createElement("div");
fixture.append(el);
const c = mountChart(el, {
type: "bar",
title: "Zero",
unit: "%",
labels: ["a", "b"],
series: [{ name: "CPU", values: [0, 0] }],
});
if (
[...el.querySelectorAll(".chart-bar")].some(
(n) => Number(n.getAttribute("height")) !== 0,
)
)
throw Error("Zero bar drawn");
c.update({ series: [{ name: "CPU", values: [50, 100] }] });
if (!el.querySelector(".chart-scale").textContent.includes("100"))
throw Error("Stale chart scale");
c.update({ series: [] });
if (!el.querySelector(".chart-empty")) throw Error("Empty chart");
c.update({
type: "line",
series: [{ name: "CPU", values: [10, null, 20] }],
});
if (el.querySelectorAll("circle.chart-bar").length !== 2)
throw Error("Isolated points missing");
c.dispose();
el.querySelector(".chart-plot").dispatchEvent(
new KeyboardEvent("keydown", { key: "End", bubbles: true }),
);
if (!el.querySelector(".chart-tooltip").hidden)
throw Error("Chart not disposed");
const logs = document.createElement("div");
fixture.append(logs);
const l = mountMiniLog(logs, {
entries: [{ level: "WARN", message: "" }],
limit: 2,
});
l.append({ level: "ERROR", message: "Failure" });
l.append({ level: "INFO", message: "Recovered" });
if (
logs.querySelectorAll(".mini-log-line").length !== 2 ||
logs.querySelector("script")
)
throw Error("Mini-log limit/text");
l.dispose();
const stats = document.createElement("section");
fixture.append(stats);
stats.innerHTML = statisticsMarkup();
const st = mountStatistics(stats.querySelector(".server-statistics"), {
data: null,
});
st.update({ labels: ["a"], cpu: [10], memory: [2] });
if (stats.querySelectorAll(".statistics-panel .chart-plot").length < 2)
throw Error("Stats empty recovery");
st.dispose();
const edit = document.createElement("div");
fixture.append(edit);
const editor = mountCodeEditor(edit, { value: "readonly", readOnly: true });
if (!edit.querySelector("textarea").readOnly) throw Error("Readonly");
editor.dispose();
fixture.remove();
});
passed++;
assert.deepEqual(errors, []);
await writeFile(
"test-results/content/qa.json",
JSON.stringify({ passed, errors }, null, 2),
);
console.log(`Content workspaces: ${passed} checks passed, no runtime errors`);
} finally {
await browser.close();
}