fix(ui): подтверждать копирование правил Filters

This commit is contained in:
Denozordec committed 2026-09-30 11:24:48 +07:00
1 parent 1cf7297d64
commit 8412ba9bbe
5 files changed
+141 -33

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
import { test, expect } from "@playwright/test"
import type { FilterRule } from "../../lib/data"
for (const mode of ["append", "replace"] as const) {
test(`Filters: копирование ${mode} подтверждает API и сохраняет выбор при ошибке`, async ({ page }) => {
const makeRule = (id: string, community: string, description: string): FilterRule => ({ id, community, description, gateway: "", gatewayTunnelId: "", action: "blackhole" })
const sourceRules = [makeRule("source-1", "65001:101", "Выбранное правило"), makeRule("source-2", "65001:102", "Исключённое правило")]
let targetRules = [makeRule("target-1", "65002:201", "Правило цели")]
let attempts = 0
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
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/filters/rules" && route.request().method() === "PUT") {
attempts++
const body = route.request().postDataJSON()
expect(body.serverId).toBe("2")
expect(body.source).toBe("copy")
expect(body.rules).toHaveLength(mode === "append" ? 2 : 1)
const copied = body.rules.find((r: FilterRule) => r.community === "65001:101")
expect(copied).toMatchObject({ description: "Выбранное правило", action: "blackhole" })
expect(copied.id).not.toBe("source-1")
expect(body.rules.some((r: FilterRule) => r.community === "65001:102")).toBe(false)
if (mode === "append") expect(body.rules[0]).toEqual(targetRules[0])
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 } })
targetRules = body.rules
return route.fulfill({ json: { ok: true, rules: targetRules } })
}
const requested = new URL(route.request().url()).searchParams.get("serverId")
const rulesets = [{ serverId: "1", rules: sourceRules }, { serverId: "2", rules: targetRules }]
return route.fulfill({ json: path === "/api/filters/rules" ? { rulesets: requested ? rulesets.filter(r => r.serverId === requested) : rulesets, greTunnels: [], live: true }
: path === "/api/servers" ? [1, 2].map(id => ({ id, name: id === 1 ? "Исходный роутер" : "Целевой роутер", host: `192.0.2.${id}`, type: "exit-node", site: "TEST", country: "RU", asn: "", enabled: true, status: "online", latency: 1 }))
: path === "/api/auth/config" ? { required: false }
: path === "/api/filters/gre-tunnels" ? { tunnels: [] }
: path === "/api/recursive-routes" ? { routes: [] } : {} })
})
await page.goto("/filters")
const rows = page.locator("tr[data-row-id]")
await expect(rows).toHaveCount(2)
await page.getByRole("button", { name: "Копировать", exact: true }).click()
const dialog = page.getByRole("dialog")
const excluded = dialog.getByRole("button", { name: /65001:102/ })
const chosen = dialog.getByRole("button", { name: /65001:101/ })
await excluded.click()
const target = dialog.getByRole("button", { name: /Целевой роутер/ })
await target.click()
const copyMode = dialog.getByRole("button", { name: mode === "append" ? /^Добавить/ : /^Заменить/ })
await copyMode.click()
const submit = dialog.getByRole("button", { name: "Копировать 1 правил", exact: true })
await submit.evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() })
await expect.poll(() => attempts).toBe(1)
await expect(dialog.getByRole("status")).toContainText("Ожидание подтверждения копирования")
for (const control of [excluded, chosen, target, copyMode, dialog.getByRole("button", { name: "Отмена", exact: true }), dialog.getByRole("button", { name: "Закрыть копирование", exact: true })]) await expect(control).toBeDisabled()
await page.keyboard.press("Escape")
await expect(dialog).toBeVisible()
await expect(page.getByText("Скопировано!", { exact: true })).toHaveCount(0)
release!()
await expect(dialog.getByRole("alert")).toContainText("Копирование запрещено тест")
for (let attempt = 1; attempt <= 3; attempt++) {
await expect(target).toHaveAttribute("aria-pressed", "true")
await expect(copyMode).toHaveAttribute("aria-pressed", "true")
await expect(chosen).toHaveAttribute("aria-pressed", "true")
await expect(excluded).toHaveAttribute("aria-pressed", "false")
await expect(rows).toHaveCount(2)
await expect(page.getByText("Правила применены на роутер", { exact: true })).toHaveCount(0)
await submit.click()
if (attempt < 3) {
await expect.poll(() => attempts).toBe(attempt + 1)
await expect(dialog.getByRole("alert")).toContainText("Сервер не подтвердил применение правил")
}
}
await expect(dialog).toHaveCount(0)
expect(attempts).toBe(4)
await expect(rows).toHaveCount(2)
await page.getByRole("button", { name: "Копировать", exact: true }).click()
await expect(dialog.getByRole("alert")).toHaveCount(0)
await expect(excluded).toHaveAttribute("aria-pressed", "true")
await expect(dialog.getByRole("button", { name: /Целевой роутер/ })).toHaveAttribute("aria-pressed", "false")
await dialog.getByRole("button", { name: "Отмена", exact: true }).click()
await page.getByRole("option", { name: /Целевой роутер/ }).click()
await expect(rows).toHaveCount(mode === "append" ? 2 : 1)
await expect(rows.filter({ hasText: "Выбранное правило" })).toHaveCount(1)
await expect(rows.filter({ hasText: "Исключённое правило" })).toHaveCount(0)
})
}