Files
MikrotikManager/tests/e2e/ospf-context.spec.ts
T

54 lines
4.1 KiB
TypeScript

import { test, expect } from "@playwright/test"
const headers = { "access-control-allow-origin": "*", "access-control-allow-methods": "GET,POST,OPTIONS", "access-control-allow-headers": "Content-Type,Authorization" }
const device = { id: 1, name: "Роутер 1", host: "192.0.2.1", enabled: true, country: "RU", status: null }
const iface = { id: "*1", serverId: 1, serverName: "Роутер 1", serverSite: "", serverCountry: "RU", instance: "default", area: "backbone", areaId: "0.0.0.0", interface: "ether1", cost: 20, type: "broadcast", disabled: false, inactive: false, priority: 1, helloInterval: 10, deadInterval: 40, useBfd: false }
const catalog = { neighbors: [], interfaces: [iface], instances: [], bfdSessions: [], routes: [], failures: [] }
const result = { serverId: 1, serverName: "Роутер 1", pingWeight: 60, optimizedCount: 1, applied: [{ interface: "ether1", from: 20, to: 10 }], interfaces: [{ id: "*1", interface: "ether1", currentCost: 20, optimalCost: 10, score: 1, pingMs: 10, dlMbps: 100, ulMbps: 100 }] }
for (const change of ["backend", "mode"]) for (const operation of ["read", "write"]) for (const fails of [false, true]) {
test('OSPF: поздний ' + operation + ' после смены ' + change + '/' + fails, async ({ page }) => {
let newReads = 0
await page.route("**/health", r => r.fulfill({ headers, json: { ok: true } }))
await page.route("**/api/**", r => {
const url = new URL(r.request().url()), path = url.pathname
if (path === "/api/ospf/catalog" && url.port === "8101") newReads++
return r.fulfill({ headers, json: path === "/api/ospf/catalog" ? { ...catalog, interfaces: [{ ...iface, interface: url.port === "8101" ? "Новый интерфейс" : "Старый интерфейс" }] } : path === "/api/servers" ? [device] : path === "/api/auth/config" ? { required: false } : {} })
})
await page.goto("/settings")
await page.getByRole("link", { name: /^OSPF/ }).click()
await expect(page.getByText("Старый интерфейс", { exact: true })).toBeVisible()
await page.getByRole("option").filter({ hasText: "Роутер 1" }).click()
let release: (() => void) | undefined, started = false
const gate = new Promise<void>(resolve => { release = resolve })
const endpoint = operation === "read" ? "/api/ospf/catalog" : "/api/servers/1/ospf/optimize"
await page.route("**" + endpoint, async r => {
if (new URL(r.request().url()).port !== "8000") return r.fallback()
started = true
await gate
return r.fulfill(fails ? { headers, status: 503, json: { error: "Старая ошибка OSPF" } } : { headers, json: operation === "read" ? { ...catalog, interfaces: [] } : result })
})
await page.getByRole("button", { name: operation === "read" ? "Обновить" : "Оптимизировать OSPF", exact: true }).click()
await expect.poll(() => started).toBe(true)
await page.goBack()
await page.goBack()
await expect(page).toHaveURL(/settings/)
if (change === "backend") {
const input = page.getByPlaceholder("http://localhost:8000")
await input.fill("http://localhost:8101")
await input.press("Enter")
} else await page.getByRole("button", { name: "Моковые", exact: true }).click()
await page.getByRole("link", { name: /^OSPF/ }).click()
if (change === "backend") await expect(page.getByText("Новый интерфейс", { exact: true })).toBeVisible()
else await expect(page.getByText("Моковые данные", { exact: true })).toBeVisible()
const before = newReads
const response = page.waitForResponse(r => r.url().includes(":8000" + endpoint))
release!()
await (await response).finished()
await page.evaluate(() => new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))))
if (change === "backend") await expect(page.getByText("Новый интерфейс", { exact: true })).toBeVisible()
await expect(page.getByText("Старая ошибка OSPF", { exact: false })).toHaveCount(0)
await expect(page.getByText("API подтвердил оптимизацию OSPF", { exact: false })).toHaveCount(0)
expect(newReads).toBe(before)
})
}