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

This commit is contained in:
Denozordec committed 2026-09-30 11:17:32 +07:00
1 parent 4b01f20558
commit 1cf7297d64
6 files changed
+162 -36

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
import { test, expect } from "@playwright/test"
import type { FilterRule } from "../../lib/data"
for (const editing of [false, true]) {
test(`Filters: ${editing ? "редактирование" : "создание"} ждёт подтверждения и сохраняет ввод`, async ({ page }) => {
let attempts = 0
let release: (() => void) | undefined
const gate = new Promise<void>(resolve => { release = resolve })
let rules: FilterRule[] = [{ id: "rule-1", community: "65001:101", gateway: "", gatewayTunnelId: "", action: "blackhole", description: "Исходное правило" }]
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("1")
expect(body.source).toBe("apply")
expect(body.rules).toHaveLength(editing ? 1 : 2)
expect(body.rules.find((r: FilterRule) => r.description === "Сохранить ввод")).toMatchObject({ community: editing ? "65001:101" : "65001:999", action: "blackhole", gateway: "", gatewayTunnelId: "" })
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 = body.rules.map((r: FilterRule) => ({ ...r, description: r.description === "Сохранить ввод" ? "Подтверждено API" : r.description }))
return route.fulfill({ json: { ok: true, rules } })
}
return route.fulfill({ json: path === "/api/filters/rules" ? { rulesets: [{ serverId: "1", rules }], greTunnels: [], live: true }
: path === "/api/servers" ? [{ id: 1, name: "Тестовый роутер", host: "192.0.2.1", 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(1)
const open = async () => {
if (editing) {
await rows.first().hover()
await rows.first().getByRole("button", { name: "Редактировать правило", exact: true }).click()
} else await page.getByRole("button", { name: "Новое правило", exact: true }).click()
}
await open()
const dialog = page.getByRole("dialog")
if (!editing) {
await dialog.getByPlaceholder("65001:100").fill("65001:999")
await dialog.getByRole("button", { name: /⊘ Blackhole/ }).click()
}
const description = dialog.getByRole("textbox", { name: "Описание правила" })
await description.fill("Сохранить ввод")
const submit = dialog.getByRole("button", { name: editing ? "Сохранить" : "Добавить", 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("Ожидание подтверждения сервера")
await expect(description).toBeDisabled()
await expect(dialog.getByPlaceholder("65001:100")).toBeDisabled()
await expect(dialog.getByRole("button", { name: /⊘ Blackhole/ })).toBeDisabled()
await expect(dialog.getByRole("button", { name: "Отмена", exact: true })).toBeDisabled()
await expect(dialog.getByRole("button", { name: "Закрыть форму", exact: true })).toBeDisabled()
await page.keyboard.press("Escape")
await expect(dialog).toBeVisible()
await expect(rows).toHaveCount(1)
await expect(rows.first()).toContainText("Исходное правило")
release!()
await expect(dialog.getByRole("alert")).toContainText("Сохранение запрещено тест")
await expect(description).toHaveValue("Сохранить ввод")
for (let i = 2; i <= 3; i++) {
await submit.click()
await expect.poll(() => attempts).toBe(i)
await expect(dialog.getByRole("alert")).toContainText("Сервер не подтвердил применение правил")
await expect(description).toHaveValue("Сохранить ввод")
await expect(rows).toHaveCount(1)
await expect(page.getByText("Правила применены на роутер", { exact: true })).toHaveCount(0)
}
await submit.click()
await expect(dialog).toHaveCount(0)
await expect(rows).toHaveCount(editing ? 1 : 2)
await expect(page.getByText("Подтверждено API", { exact: true })).toBeVisible()
expect(attempts).toBe(4)
await open()
await expect(dialog.getByRole("alert")).toHaveCount(0)
await expect(description).toHaveValue(editing ? "Подтверждено API" : "")
})
}