import { test, expect } from "@playwright/test" import type { FirewallRule } from "../../lib/data" import axe from "axe-core" test("Firewall1000: страницы, точный destination, DnD, ошибки и320px", async ({ page }, info) => { test.setTimeout(90000) let rules: FirewallRule[] = Array.from({ length: 1000 }, (_, i) => ({ id: `r${i + 1}`, rosId: `*${i + 1}`, serverId: "1", serverName: "Роутер", family: "ip", table: "filter", chain: "forward", action: "accept", proto: "tcp", src: `192.0.2.${i % 250 + 1}`, dst: "", port: "443", iface: "", comment: `rule-${String(i + 1).padStart(4, "0")}`, enabled: true, hits: i + 1, })) const moves: Record[] = [] let failed = false, writeFail = false, reads = 0 await page.route("**/health", r => r.fulfill({ json: { ok: true } })) await page.route("**/api/**", r => { const p = new URL(r.request().url()).pathname if (p === "/api/firewall/rules/move") { const body = r.request().postDataJSON(); moves.push(body) if (writeFail) return r.fulfill({ status: 403, json: { error: "Перемещение запрещено1000" } }) const active = rules.find(rule => rule.rosId === body.rosId)! rules = rules.filter(rule => rule !== active) const destination = rules.findIndex(rule => rule.rosId === body.destinationRosId) rules.splice(destination < 0 ? rules.length : destination, 0, active) return r.fulfill({ json: { ok: true } }) } if (p === "/api/firewall/all") { reads++ return r.fulfill(failed ? { status: 503, json: { error: "Firewall недоступен1000" } } : { json: { rules, addressLists: [] } }) } return r.fulfill({ json: p === "/api/auth/config" ? { required: false } : p === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", type: "exit-node", enabled: true, country: "RU", status: "online" }] : {} }) }) const start = Date.now() await page.goto("/firewall?server=1") const rows = page.locator("tr[data-row-id]") await expect(rows).toHaveCount(50) const profile = { count: 1000, bytes: Buffer.byteLength(JSON.stringify(rules)), rows: await rows.count(), nodes: await page.locator("*").count(), readyMs: Date.now() - start } console.log("Firewall profile", JSON.stringify(profile)) await info.attach("profile", { body: JSON.stringify(profile), contentType: "application/json" }) await expect(rows.first().getByRole("button", { name: "Поднять правило *1", exact: true })).toBeDisabled() await rows.last().getByRole("button", { name: "Опустить правило *50", exact: true }).click() await expect.poll(() => moves.length).toBe(1) expect(moves[0]).toEqual({ serverId: "1", family: "ip", table: "filter", rosId: "*50", destinationRosId: "*52" }) await expect(rows.last()).toHaveAttribute("data-row-id", "r51") const next = page.getByRole("button", { name: "Следующая страница", exact: true }) await next.click(); await expect(rows.first()).toHaveAttribute("data-row-id", "r50") await rows.first().getByRole("button", { name: "Поднять правило *50", exact: true }).focus() await page.keyboard.press("Enter") await expect.poll(() => moves.length).toBe(2) expect(moves[1]).toEqual({ serverId: "1", family: "ip", table: "filter", rosId: "*50", destinationRosId: "*51" }) await expect(rows.first()).toHaveAttribute("data-row-id", "r51") const handle = rows.first().getByRole("button", { name: "Перетащите для изменения порядка строк", exact: true }) await handle.focus(); await page.keyboard.press("Space"); await expect(handle).toHaveAttribute("aria-pressed", "true") await page.keyboard.press("ArrowDown"); await expect(page.getByRole("status").filter({ hasText: "over droppable area r52" })).toBeVisible(); await page.keyboard.press("Space") await expect.poll(() => moves.length).toBe(3) expect(moves[2]).toEqual({ serverId: "1", family: "ip", table: "filter", rosId: "*51", destinationRosId: "*53" }) await expect(rows.first()).toHaveAttribute("data-row-id", "r52") writeFail = true await rows.first().getByRole("button", { name: "Поднять правило *52", exact: true }).click() await expect(page.getByRole("alert").filter({ hasText: "Перемещение запрещено1000" })).toBeVisible() await expect(rows.first()).toHaveAttribute("data-row-id", "r52"); writeFail = false const read = reads await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect.poll(() => reads).toBeGreaterThan(read); await expect(rows.first()).toHaveAttribute("data-row-id", "r52") const search = page.getByPlaceholder("Поиск по адресу, действию…") await search.fill("rule-1000"); await expect(rows).toHaveCount(1) await expect(rows.first()).toHaveAttribute("data-row-id", "r1000") await expect(rows.first().getByRole("button", { name: "Опустить правило *1000", exact: true })).toHaveCount(0) await search.fill(""); await expect(rows).toHaveCount(50) await next.click(); await expect(rows.first()).toHaveAttribute("data-row-id", "r52") failed = true await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(page.getByRole("alert").filter({ hasText: "Firewall недоступен1000" })).toBeVisible() await expect(rows.first()).toHaveAttribute("data-row-id", "r52") await expect(page.getByText("Показаны последние успешно загруженные данные.", { exact: false })).toBeVisible() await page.setViewportSize({ width: 320, height: 1000 }) const action = rows.first().getByRole("button", { name: "Действия: forward accept", exact: true }) await action.scrollIntoViewIfNeeded() await page.locator('[data-slot="data-grid"] [data-slot="scroll-area-viewport"]').evaluate(el => { el.scrollLeft = el.scrollWidth }) await expect(action).toBeInViewport() expect((await action.boundingBox())?.height).toBeGreaterThanOrEqual(44) expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(320) await page.addScriptTag({ content: axe.source }) expect((await page.evaluate(async () => (window as unknown as { axe: typeof axe }).axe.run(document.querySelector('[data-slot="data-grid"]')!, { runOnly: { type: "tag", values: ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"] } }))).violations).toEqual([]) await page.addStyleTag({ content: "nextjs-portal{display:none!important}" }) await page.screenshot({ path: info.outputPath("firewall-320.png"), animations: "disabled" }) failed = false; rules = [rules[999]] await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(rows).toHaveCount(1); await expect(rows.first()).toHaveAttribute("data-row-id", "r1000") expect(moves).toHaveLength(4) })