import { test, expect, type Page } from "@playwright/test" import { wgListResponseSchema } from "../../packages/contracts/src/wireguard" const fixture = wgListResponseSchema.parse({ interfaces: Array.from({ length: 12 }, (_, i) => ({ id: `wg-${i + 1}`, rosId: `*${i + 1}`, name: `wg-test-${i + 1}`, serverId: "1", serverName: "Тестовый роутер", serverCountry: "RU", listenPort: 13231 + i, mtu: 1420, comment: "Тест", enabled: true, status: "up", peers: [{ id: `peer-${i + 1}`, rosId: `*${i + 20}`, publicKey: `fixture-public-key-${i + 1}`, name: `Пир ${i + 1}`, allowedIps: [`10.0.0.${i + 1}/32`], transferRx: i + 1, transferTx: i + 1 }], })) }) async function setup(page: Page, empty = false) { await page.route("**/health", route => route.fulfill({ json: { ok: true } })) await page.route("**/api/**", route => { const path = new URL(route.request().url()).pathname return route.fulfill({ json: path === "/api/wireguard" ? (empty ? { interfaces: [] } : fixture) : path === "/api/auth/config" ? { required: false } : path === "/api/servers" ? [] : {} }) }) await page.goto("/wireguard") } test("WireGuard: 12 интерфейсов, сортировка порта, раскрытие и список пиров", async ({ page }) => { const errors: string[] = [] page.on("pageerror", e => errors.push(e.message)) await setup(page) const rows = page.locator("tr[data-row-id]") await expect(rows).toHaveCount(12) await page.getByRole("button", { name: "Порт", exact: true }).click() await expect(rows.first()).toContainText("wg-test-12") await rows.first().click() await expect(page.getByText("10.0.0.12/32", { exact: true })).toBeVisible() await rows.first().click() await expect(page.getByText("10.0.0.12/32", { exact: true })).toBeHidden() await page.getByRole("tab", { name: /Пиры/ }).click() await expect(rows).toHaveCount(12) await expect(rows.last()).toContainText("10.0.0.12/32") await page.getByRole("button", { name: "Пир", exact: true }).click() await expect(rows.first()).toContainText("Пир 1") expect(errors).toEqual([]) }) test("WireGuard: пустые интерфейсы и пиры", async ({ page }) => { await setup(page, true) await expect(page.getByText("Нет WireGuard интерфейсов", { exact: true })).toBeVisible() await page.getByRole("tab", { name: /Пиры/ }).click() await expect(page.getByText("Нет пиров", { exact: true })).toBeVisible() })