import { test, expect } from "@playwright/test" for (const active of [true, false]) { test(`Firewall: переключение адреса ${active} требует подтверждения`, async ({ page }) => { let attempts = 0 let release: (() => void) | undefined const gate = new Promise(resolve => { release = resolve }) let currentEnabled = active const entry = { id: "address-1", rosId: "*A/1", serverId: "1", serverName: "Роутер", family: "ip", list: "trusted", address: "192.0.2.10", comment: "Адрес тест", disabled: !active } 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 (route.request().method() === "PATCH") { attempts++ expect(path).toBe("/api/firewall/address-lists") expect(route.request().postDataJSON()).toMatchObject({ serverId: "1", rosId: "*A/1", family: "ip", disabled: active }) 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 } }) currentEnabled = !active return route.fulfill({ json: { ok: true } }) } return route.fulfill({ json: path === "/api/auth/config" ? { required: false } : path === "/api/firewall/all" ? { rules: [], addressLists: [{ ...entry, disabled: !currentEnabled }] } : 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") await page.getByRole("button", { name: /Адр. листы/ }).click() const toggle = page.getByRole("switch") await expect(toggle).toHaveCount(1) await toggle.evaluate(element => { (element as HTMLElement).click(); (element as HTMLElement).click() }) await expect.poll(() => attempts).toBe(1) await expect(toggle).toBeDisabled() await expect(page.getByRole("status").filter({ hasText: "Ожидание изменения адреса" })).toBeVisible() release!() await expect(page.getByRole("alert").filter({ hasText: "Изменение запрещено тест" })).toBeVisible() for (let i = 2; i <= 3; i++) { await toggle.click() await expect.poll(() => attempts).toBe(i) await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил изменение адреса" })).toBeVisible() await expect(toggle).toHaveAttribute("aria-checked", String(active)) } await toggle.click() await expect(toggle).toHaveAttribute("aria-checked", String(!active)) expect(attempts).toBe(4) await expect(page.getByRole("alert").filter({ hasText: "Сервер не подтвердил" })).toHaveCount(0) }) }