import { test, expect } from "@playwright/test" for (const mode of ["certificate", "pre-shared-key", "edit"] as const) { test(`IPsec: форма клиента ${mode} сохраняет ввод и требует подтверждения`, async ({ page }) => { const editing = mode === "edit" const authMethod = mode === "certificate" ? "certificate" : "pre-shared-key" const client = { id: "client-1", rosId: "*P/1", serverId: "1", serverName: "Роутер", name: "alice", authMethod, online: false, disabled: false, managed: true } 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 (route.request().method() === (editing ? "PATCH" : "POST") && path.startsWith("/api/ipsec/users")) { attempts++ expect(route.request().postDataJSON()).toMatchObject({ name: "bob", staticIp: "10.77.0.10" }) if (editing) expect(decodeURIComponent(path)).toBe("/api/ipsec/users/1/*P/1") else expect(route.request().postDataJSON()).toMatchObject({ serverId: "1", authMethod }) if (authMethod === "pre-shared-key") expect(route.request().postDataJSON().psk).toBe("test-secret-123") 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: editing ? { ok: false } : { client: null } }) if (attempts === 4 && !editing) return route.fulfill({ json: { client: { ...client, name: "bob", serverId: "2" } } }) return route.fulfill({ json: editing ? { ok: true } : { client: { ...client, name: "bob" } } }) } return route.fulfill({ json: path === "/api/auth/config" ? { required: false } : path === "/api/ipsec" ? { servers: [], clients: editing ? [client] : [] } : path === "/api/servers" ? [{ id: 1, name: "Роутер", host: "192.0.2.1", enabled: true, type: "exit-node", country: "RU", status: "online" }] : {} }) }) await page.goto("/ipsec") await page.getByRole("button", { name: editing ? "alice" : "Клиент", exact: true }).click() const sheet = page.getByRole("dialog") if (!editing) { await sheet.getByLabel("Сервер", { exact: true }).selectOption("1") if (mode === "pre-shared-key") await sheet.getByRole("button", { name: "PSK", exact: true }).click() } await sheet.getByLabel("Имя клиента").fill("bob") const submit = sheet.getByRole("button", { name: editing ? "Сохранить" : "Создать клиента", exact: true }) if (authMethod === "pre-shared-key") { await sheet.getByLabel("Secret (PSK)").fill("short") await expect(submit).toBeDisabled() await sheet.getByLabel("Secret (PSK)").fill("test-secret-123") } await sheet.getByRole("switch", { name: "Статический IP" }).click() await sheet.getByLabel("IP клиента", { exact: true }).fill("10.77.0.10") await submit.evaluate(button => { (button as HTMLButtonElement).click(); (button as HTMLButtonElement).click() }) await expect.poll(() => attempts).toBe(1) await expect(sheet.getByRole("status")).toContainText("Ожидание результата") await expect(sheet.getByLabel("Имя клиента")).toBeDisabled() await expect(sheet.getByRole("switch")).toBeDisabled() await expect(sheet.getByRole("button", { name: "Отмена" })).toBeDisabled() await page.keyboard.press("Escape") await page.mouse.click(10, 10) await expect(sheet).toBeVisible() release!() await expect(sheet.getByRole("alert")).toContainText("Клиент запрещён тест") await expect(sheet.getByLabel("Имя клиента")).toHaveValue("bob") await expect(sheet.getByLabel("IP клиента", { exact: true })).toHaveValue("10.77.0.10") for (let attempt = 2; attempt <= (editing ? 3 : 4); attempt++) { await submit.click() await expect.poll(() => attempts).toBe(attempt) await expect(sheet.getByRole("alert")).toContainText("Сервер не подтвердил") } await submit.click() await expect(sheet).toHaveCount(0) expect(attempts).toBe(editing ? 4 : 5) }) }