import { test, expect } from "@playwright/test" import type { FirewallRule } from "../../lib/data" for (const direction of ["down", "up"] as const) { test(`Firewall: перемещение ${direction} требует подтверждения и блокирует конфликты`, async ({ page }) => { let rules: FirewallRule[] = Array.from({ length: 3 }, (_, i) => ({ id: `rule-${i + 1}`, rosId: `*${i + 1}`, serverId: "1", serverName: "Роутер", family: "ip", table: "filter", chain: "forward", action: "accept", proto: "tcp", src: "", dst: "", port: "443", iface: "", comment: `Правило ${i + 1}`, enabled: true, hits: 1 })) let attempts = 0 let otherWrites = 0 let release: (() => void) | undefined let releaseRead: (() => void) | undefined const gate = new Promise(resolve => { release = resolve }) const readGate = new Promise(resolve => { releaseRead = resolve }) const id = direction === "down" ? "rule-1" : "rule-3" await page.route("**/health", route => route.fulfill({ json: { ok: true } })) await page.route("**/api/**", async route => { const path = new URL(route.request().url()).pathname if (path === "/api/firewall/rules/move") { attempts++ expect(route.request().postDataJSON()).toEqual({ serverId: "1", family: "ip", table: "filter", rosId: direction === "down" ? "*1" : "*3", destinationRosId: direction === "down" ? "*3" : "*2" }) if (attempts === 1) { await gate; return route.fulfill({ status: 403, json: { error: "Перемещение запрещено тест" } }) } if (attempts === 2) return route.fulfill({ json: {} }) if (attempts === 3) return route.fulfill({ json: { ok: false } }) rules = direction === "down" ? [rules[1], rules[0], rules[2]] : [rules[0], rules[2], rules[1]] return route.fulfill({ json: { ok: true } }) } if (route.request().method() !== "GET") otherWrites++ if (path === "/api/firewall/all" && attempts === 4) await readGate return route.fulfill({ json: path === "/api/auth/config" ? { required: false } : path === "/api/firewall/all" ? { rules, addressLists: [] } : path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", type: "exit-node", enabled: true, country: "RU", status: "online" }] : {} }) }) await page.goto("/firewall?server=1") const rows = page.locator("tr[data-row-id]") await expect(rows).toHaveCount(3) const move = async () => { const handle = page.locator(`tr[data-row-id="${id}"]`).getByRole("button", { name: "Перетащите для изменения порядка строк", exact: true }) await handle.focus() await page.keyboard.press("Space") await expect(handle).toHaveAttribute("aria-pressed", "true") await page.keyboard.press(direction === "down" ? "ArrowDown" : "ArrowUp") await expect(page.getByRole("status").filter({ hasText: "over droppable area rule-2" })).toBeVisible() await page.keyboard.press("Space") } const originalOrder = ["rule-1", "rule-2", "rule-3"] const order = () => rows.evaluateAll(elements => elements.map(e => e.getAttribute("data-row-id"))) await move() await expect.poll(() => attempts).toBe(1) const waiting = page.getByRole("status").filter({ hasText: "Ожидание перемещения правила" }) await expect(waiting).toBeVisible() await expect(page.getByRole("button", { name: "История", exact: true })).toBeDisabled() await expect(page.getByRole("button", { name: "Обновить", exact: true })).toBeDisabled() await expect(page.getByRole("button", { name: "Перетащите для изменения порядка строк", exact: true })).toHaveCount(0) for (const toggle of await rows.getByRole("switch").all()) await expect(toggle).toBeDisabled() await rows.first().getByRole("button", { name: "Действия: forward accept", exact: true }).click() await expect(page.getByRole("menuitem", { name: "Удалить правило", exact: true })).toBeDisabled() await expect(page.getByRole("menuitem", { name: "Отключить", exact: true })).toBeDisabled() await page.keyboard.press("Escape") await page.getByRole("button", { name: "Новое правило", exact: true }).click() await expect(page.getByRole("dialog")).toHaveCount(0) expect(attempts).toBe(1) expect(otherWrites).toBe(0) release!() await expect(page.getByRole("alert").filter({ hasText: "Перемещение запрещено тест" })).toBeVisible() await expect.poll(order).toEqual(originalOrder) for (let i = 2; i <= 3; i++) { await move() await expect.poll(() => attempts).toBe(i) await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил перемещение правила" })).toBeVisible() await expect.poll(order).toEqual(originalOrder) } await move() await expect.poll(() => attempts).toBe(4) await expect(waiting).toBeVisible() for (const toggle of await rows.getByRole("switch").all()) await expect(toggle).toBeDisabled() releaseRead!() await expect(waiting).toHaveCount(0) await expect.poll(order).toEqual(direction === "down" ? ["rule-2", "rule-1", "rule-3"] : ["rule-1", "rule-3", "rule-2"]) await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил перемещение" })).toHaveCount(0) expect(otherWrites).toBe(0) }) }