import { test, expect, type Page } from "@playwright/test" const first = "2026-09-30T03:00:00.000Z" const second = "2026-09-30T03:05:00.000Z" const third = "2026-09-30T03:10:00.000Z" const rule = { id: "rule-1", rosId: "*1", serverId: "1", serverName: "Роутер", family: "ip", table: "filter", chain: "forward", action: "accept", proto: "tcp", src: "", dst: "", port: "443", iface: "", comment: "Правило тест", enabled: true, hits: 1 } async function setup(page: Page) { const control: { payload: unknown; fail: boolean } = { payload: { rules: [rule], addressLists: [] }, fail: false } await page.clock.setFixedTime(new Date(first)) await page.route("**/health", route => route.fulfill({ json: { ok: true } })) await page.route("**/api/**", route => { const path = new URL(route.request().url()).pathname if (path === "/api/firewall/all" && control.fail) return route.fulfill({ status: 503, json: { error: "Сбой обновления тест" } }) return route.fulfill({ json: path === "/api/auth/config" ? { required: false } : path === "/api/firewall/all" ? control.payload : path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", type: "exit-node", enabled: true, country: "RU", status: "online" }] : {} }) }) return control } test("Firewall: время успешного среза обновляется, недоступного сохраняется", async ({ page }) => { const control = await setup(page) await page.goto("/firewall?server=1") const freshness = page.getByLabel("Свежесть данных Firewall") await expect(freshness.locator("time")).toHaveAttribute("datetime", first) await page.clock.setFixedTime(new Date(second)) control.payload = { rules: [], addressLists: [], failures: [{ serverId: "1", family: "ip", table: "filter", error: "Таблица недоступна тест" }] } await page.getByRole("button", { name: "Обновить", exact: true }).click() const warning = page.getByRole("alert").filter({ hasText: "Не все срезы" }) await expect(warning.locator("time")).toHaveAttribute("datetime", first) await expect(freshness.locator("time").first()).toHaveAttribute("datetime", first) await expect(freshness.locator("time").last()).toHaveAttribute("datetime", second) await page.clock.setFixedTime(new Date(third)) control.fail = true await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(page.getByRole("alert").filter({ hasText: "Сбой обновления тест" })).toBeVisible() await expect(freshness.locator("time").first()).toHaveAttribute("datetime", first) await expect(freshness.locator("time").last()).toHaveAttribute("datetime", second) control.fail = false control.payload = { rules: [], addressLists: [], failures: [{ serverId: "1", error: "Устройство недоступно тест" }] } await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(warning.locator("time").first()).toHaveAttribute("datetime", first) await expect(warning.locator("time").last()).toHaveAttribute("datetime", second) control.payload = { rules: [], addressLists: [] } await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(freshness.locator("time")).toHaveCount(1) await expect(freshness.locator("time")).toHaveAttribute("datetime", third) await expect(warning).toHaveCount(0) await expect(page.locator("tr[data-row-id]")).toHaveCount(0) }) test("Firewall: первично недоступный срез не получает время успешного опроса", async ({ page }) => { const control = await setup(page) control.payload = { rules: [], addressLists: [], failures: [{ serverId: "1", error: "Устройство недоступно тест" }] } await page.goto("/firewall?server=1") const freshness = page.getByLabel("Свежесть данных Firewall") await expect(freshness).toContainText("Время получения срезов неизвестно") await expect(freshness).toContainText("Не получено срезов: 5") await expect(freshness.locator("time")).toHaveCount(0) await expect(page.getByRole("alert").filter({ hasText: "Не все срезы" })).toContainText("Последние данные: ещё не получены") control.payload = { rules: [], addressLists: [] } await page.clock.setFixedTime(new Date(second)) await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(freshness.locator("time")).toHaveAttribute("datetime", second) await expect(freshness).not.toContainText("Не получено срезов") })