import { test, expect } from "@playwright/test" for (const section of ["wireguard", "ipsec", "gre", "firewall", "filters", "recursive-routes"]) { test(`История ${section}: ошибка сохраняет подтверждение, повтор закрывает`, async ({ page }) => { const errors: string[] = [] page.on("pageerror", error => errors.push(error.message)) page.on("console", message => { if (message.type() === "error") errors.push(message.text()) }) let attempts = 0 let release: (() => void) | undefined const gate = new Promise(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.endsWith("/restore")) { expect(decodeURIComponent(path)).toBe(`/api/${section}/revisions/rev/1/restore`) expect(route.request().postDataJSON()).toEqual({ serverId: "1" }) attempts++ if (attempts === 1) { await gate return route.fulfill({ status: 403, json: { error: "Восстановление запрещено тест" } }) } if (attempts === 2) return route.fulfill({ json: { ok: false, routes: [], rules: [] } }) if (attempts === 3) return route.fulfill({ json: {} }) return route.fulfill({ json: { ok: true, serverId: 1, pushedRoutes: 0, routes: [], rules: [] } }) } const json = path.endsWith("/revisions") ? { revisions: [{ id: "rev/1", serverId: "1", section, source: "apply", fingerprint: "fixture", createdAt: "2026-09-29T00:00:00Z", note: null, itemCount: 1 }] } : path === "/api/auth/config" ? { required: false } : path === "/api/servers" ? [{ id: 1, name: "История роутер", host: "192.0.2.1", type: "exit-node", enabled: true, country: "RU", site: "TEST", asn: "", status: "online", latency: 1 }] : path === "/api/wireguard" ? { interfaces: [] } : path === "/api/ipsec" ? { servers: [], clients: [] } : path === "/api/gre/tunnels" || path === "/api/filters/gre-tunnels" ? { tunnels: [] } : path === "/api/firewall/all" ? { rules: [], addressLists: [] } : path === "/api/filters/rules" ? { rulesets: [{ serverId: "1", rules: [] }], greTunnels: [], live: true } : path === "/api/recursive-routes/catalog" ? { serverId: 1, routes: [], live: true, stale: false } : path === "/api/recursive-routes/gateways/catalog" ? { serverId: 1, gateways: [] } : path === "/api/evobgp/settings" ? { enabled: false, baseUrl: "", secretConfigured: false } : {} return route.fulfill({ json }) }) await page.goto(`/${section}?server=1`) if (section === "ipsec") await page.getByRole("option").filter({ hasText: "История роутер" }).click() await page.getByRole("button", { name: "История", exact: true }).click() const sheet = page.getByRole("dialog") await sheet.getByRole("button", { name: "Откатить", exact: true }).click() const dialog = page.getByRole("alertdialog") await dialog.getByRole("button", { name: "Откатить", exact: true }).evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() }) await expect.poll(() => attempts).toBe(1) await expect(dialog.getByRole("status")).toContainText("Ожидание результата") await expect(dialog.getByRole("button", { name: "Отмена" })).toBeDisabled() await page.keyboard.press("Escape") await expect(dialog).toBeVisible() release!() await expect(dialog.getByRole("alert").filter({ hasText: "Восстановление запрещено тест" })).toBeVisible() await expect(dialog.getByRole("button", { name: "Откатить", exact: true })).toBeEnabled() for (let i = 0; i < 2; i++) { await dialog.getByRole("button", { name: "Откатить", exact: true }).click() await expect.poll(() => attempts).toBe(i + 2) await expect(dialog.getByRole("alert").filter({ hasText: "Сервер не подтвердил восстановление" })).toBeVisible() await expect(page.getByText("Версия применена на роутер", { exact: true })).toHaveCount(0) } await dialog.getByRole("button", { name: "Откатить", exact: true }).click() await expect(dialog).toHaveCount(0) expect(attempts).toBe(4) await sheet.getByRole("button", { name: "Откатить", exact: true }).click() await expect(dialog.getByText("Восстановление запрещено тест")).toHaveCount(0) await page.keyboard.press("Escape") await expect(dialog).toHaveCount(0) expect(errors.filter(error => !error.includes("403 (Forbidden)"))).toEqual([]) }) } for (const section of ["ipsec", "gre", "firewall", "filters", "recursive-routes"]) { test(`История ${section}: ошибка, повтор и поздний ответ закрытой панели`, async ({ page }) => { let attempts = 0 let release: (() => void) | undefined const gate = new Promise(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/${section}/revisions`) { attempts++ if (attempts === 1) return route.fulfill({ status: 403, json: { error: "История недоступна тест" } }) if (attempts === 2) { await gate return route.fulfill({ status: 500, json: { error: "Устаревшая ошибка тест" } }) } if (attempts === 3) return route.fulfill({ json: {} }) const revision = { id: "rev/1", serverId: "1", section, source: "apply", fingerprint: "fixture", createdAt: "2026-09-29T00:00:00Z", note: null, itemCount: 1 } if (attempts === 4) return route.fulfill({ json: { revisions: [{ ...revision, serverId: "2" }] } }) if (attempts === 5) return route.fulfill({ json: { revisions: [{ ...revision, section: "wireguard" }] } }) if (attempts === 6) return route.fulfill({ json: { revisions: [{ ...revision, createdAt: "invalid" }] } }) return route.fulfill({ json: { revisions: [] } }) } return route.fulfill({ json: path === "/api/auth/config" ? { required: false } : path === "/api/ipsec" ? { servers: [], clients: [] } : path === "/api/gre/tunnels" || path === "/api/filters/gre-tunnels" ? { tunnels: [] } : path === "/api/firewall/all" ? { rules: [], addressLists: [] } : path === "/api/filters/rules" ? { rulesets: [{ serverId: "1", rules: [] }], greTunnels: [], live: true } : path === "/api/recursive-routes/catalog" ? { serverId: 1, routes: [], live: true, stale: false } : path === "/api/recursive-routes/gateways/catalog" ? { serverId: 1, gateways: [] } : path === "/api/evobgp/settings" ? { enabled: false, baseUrl: "", secretConfigured: false } : path === "/api/servers" ? [{ id: 1, name: "История роутер", host: "192.0.2.1", enabled: true, type: "exit-node", country: "RU", status: "online" }] : {} }) }) await page.goto(`/${section}?server=1`) if (section === "ipsec") await page.getByRole("option").filter({ hasText: "История роутер" }).click() const open = page.getByRole("button", { name: "История", exact: true }) const sheet = page.getByRole("dialog") await open.click() await expect(sheet.getByRole("alert")).toContainText("История недоступна тест") await expect(sheet.getByText("Истории пока нет", { exact: true })).toHaveCount(0) await sheet.getByRole("button", { name: "Повторить загрузку" }).click() await expect.poll(() => attempts).toBe(2) await expect(sheet.getByRole("status")).toContainText("Загрузка истории") await page.keyboard.press("Escape") await expect(sheet).toHaveCount(0) await open.click() await expect(sheet.getByRole("alert")).toContainText("Некорректный ответ истории") for (let attempt = 4; attempt <= 6; attempt++) { await sheet.getByRole("button", { name: "Повторить загрузку" }).click() await expect.poll(() => attempts).toBe(attempt) await expect(sheet.getByRole("alert")).toContainText("Некорректный ответ истории") await expect(sheet.getByRole("button", { name: "Откатить", exact: true })).toHaveCount(0) } await sheet.getByRole("button", { name: "Повторить загрузку" }).click() await expect(sheet.getByText("Истории пока нет", { exact: true })).toBeVisible() const response = page.waitForResponse(r => r.url().includes(`/api/${section}/revisions?`)) release!() await (await response).finished() await page.waitForTimeout(150) await expect(sheet.getByRole("alert")).toHaveCount(0) await expect(sheet.getByText("Истории пока нет", { exact: true })).toBeVisible() }) }