import { test, expect } from "@playwright/test" const servers = [1, 2].map(id => ({ id, name: "Роутер " + id, host: "192.0.2." + id, country: "RU", enabled: true, status: null })) const host = (serverId: number) => ({ serverId, serverName: "Роутер " + serverId, serverSite: "", serverCountry: "RU" }) const neighbor = (serverId: number, address = "192.0.2." + serverId) => ({ ...host(serverId), id: "*1", address, routerId: address, instance: "default", area: "backbone", areaId: "0.0.0.0", interface: "ether1", state: "Full", uptime: "1h", stateChanges: 0, priority: 1 }) const all = { neighbors: [neighbor(1), neighbor(2)], interfaces: [], instances: [], bfdSessions: [], routes: [], failures: [] } for (const broken of ["http", "payload", "duplicate", "conflict", "foreign", "inventory"]) { test("OSPF: неизвестное начало, кэш и восстановление " + broken, async ({ page }) => { let phase = 0 await page.route("**/health", r => r.fulfill({ json: { ok: true } })) await page.route("**/api/**", r => { const path = new URL(r.request().url()).pathname if (path === "/api/servers") return r.fulfill({ json: phase === 2 && broken === "inventory" ? [servers[0], servers[0]] : servers }) if (path === "/api/ospf/catalog") { if (phase === 0 || phase === 2 && broken === "http") return r.fulfill({ status: 503, json: { error: "OSPF недоступен тест" } }) return r.fulfill({ json: phase !== 2 ? all : broken === "payload" ? { ...all, neighbors: [{}] } : broken === "duplicate" ? { ...all, neighbors: [neighbor(1), neighbor(1)] } : broken === "conflict" ? { ...all, failures: [{ serverId: 2, serverName: "Роутер 2", error: "Таймаут" }] } : broken === "foreign" ? { ...all, neighbors: [neighbor(3)] } : all }) } return r.fulfill({ json: path === "/api/auth/config" ? { required: false } : {} }) }) await page.goto("/ospf") const alert = page.getByRole("alert").filter({ hasText: "Не удалось загрузить OSPF" }) await expect(alert).toContainText("Данные пока неизвестны") await expect(page.getByLabel("Сводка OSPF", { exact: true })).toContainText("неизвестно") await expect(page.getByText("OSPF интерфейсы не настроены ни на одном сервере", { exact: true })).toHaveCount(0) phase = 1 await alert.getByRole("button", { name: "Повторить загрузку OSPF" }).click() await page.getByRole("button", { name: "Соседи", exact: true }).click() const rows = page.locator("tr[data-row-id]") await expect(rows).toHaveCount(2) phase = 2 await page.getByRole("button", { name: "Обновить", exact: true }).click() await expect(alert).toContainText("Показаны последние полученные данные") await expect(rows.filter({ hasText: "Неизвестно" })).toHaveCount(2) await expect(page.getByRole("status").filter({ hasText: "Топология OSPF неизвестна" })).toBeVisible() phase = 3 await alert.getByRole("button", { name: "Повторить загрузку OSPF" }).click() await expect(alert).toHaveCount(0) await expect(rows.filter({ hasText: "Неизвестно" })).toHaveCount(0) }) } test("OSPF: частичный отказ сохраняет устройство, успешный пустой опрос очищает", async ({ page }) => { let phase = 0 await page.route("**/health", r => r.fulfill({ json: { ok: true } })) await page.route("**/api/**", r => { const path = new URL(r.request().url()).pathname return r.fulfill({ json: path === "/api/servers" ? servers : path === "/api/ospf/catalog" ? phase === 0 ? all : { ...all, neighbors: [neighbor(1, "198.51.100.1")], failures: phase === 1 ? [{ serverId: 2, serverName: "Роутер 2", error: "Таймаут BFD тест" }] : [] } : path === "/api/auth/config" ? { required: false } : {} }) }) await page.goto("/ospf") await page.getByRole("button", { name: "Соседи", exact: true }).click() const rows = page.locator("tr[data-row-id]") await expect(rows).toHaveCount(2) phase = 1 await page.getByRole("button", { name: "Обновить", exact: true }).click() const alert = page.getByRole("alert").filter({ hasText: "Не удалось опросить часть роутеров OSPF" }) await expect(alert).toContainText("Роутер 2: Таймаут BFD тест") await expect(rows.filter({ hasText: "Роутер 2" })).toContainText("Неизвестно") await expect(rows.filter({ hasText: "198.51.100.1" })).toContainText("Full") await page.getByRole("option").filter({ hasText: "Роутер 1" }).click() await expect(page.getByLabel("Сводка соседей OSPF", { exact: true })).not.toContainText("—") await page.getByRole("option").filter({ hasText: "Роутер 2" }).click() await expect(page.getByLabel("Сводка соседей OSPF", { exact: true })).toContainText("—") phase = 2 await alert.getByRole("button", { name: "Повторить загрузку OSPF" }).click() await expect(rows).toHaveCount(0) await expect(page.getByText("Нет OSPF-соседей", { exact: true })).toBeVisible() })