import { openSettings } from "./fixtures/open-settings" import {test,expect} from "@playwright/test" import { emptyMapFlow } from "./fixtures/map-flow" const headers={"access-control-allow-origin":"*","access-control-allow-methods":"GET,POST,OPTIONS","access-control-allow-headers":"Content-Type,Authorization"} const wan={id:"w",name:"WAN",iface:"ether1",isp:"ISP",ip:"router.example",maxDl:100,maxUl:100} const row=(name:string)=>({id:1,name,host:"192.0.2.1",type:"home-router",country:"RU",enabled:true,status:"online",wanUplinks:[wan]}) for(const change of ["backend","mode"])for(const part of ["snapshot","flow","dns"])for(const fails of [false,true])test("Карта: поздний "+part+"/"+change+"/"+fails,async({page})=>{ let newReads=0,started=false,completed=false,release!:()=>void;const gate=new Promise(resolve=>{release=resolve}) await page.route("**/health",r=>r.fulfill({headers,json:{ok:true}})) await page.route("**/api/**",r=>{ const u=new URL(r.request().url()),p=u.pathname;if(u.port==="8101")newReads++ return r.fulfill({headers,json:p==="/api/auth/config"?{required:false}:p==="/api/servers"?[row(u.port==="8101"?"Новая карта":"Прежняя карта")]:p==="/api/filters/gre-tunnels"?{tunnels:[],failures:[]}:p==="/api/uptime/speed-probes"?{probes:[]}:p==="/api/traffic/flow/map-hops"?emptyMapFlow(u):p==="/api/network/resolve-hosts"?{results:{"router.example":"192.0.2.2"}}:{}}) }) await page.goto("/settings");await page.getByRole("link",{name:/^Карта сети/}).click() await expect(page.getByRole("button",{name:"Устройство Прежняя карта",exact:true})).toBeVisible({timeout:20_000}) await expect(page.getByText(/IPFIX · среднее за 1 мин 30 с/)).toBeVisible() const path=part==="snapshot"?"/api/servers":part==="flow"?"/api/traffic/flow/map-hops":"/api/network/resolve-hosts" await page.route("**/api/**",async r=>{ const u=new URL(r.request().url());if(u.port!=="8000"||u.pathname!==path||started)return r.fallback() started=true;await gate try{await r.fulfill(fails?{headers,status:503,json:{error:"Старая ошибка карты"}}:{headers,json:part==="snapshot"?[row("Поздняя карта")]:part==="flow"?{...emptyMapFlow(u),services:[{id:"svc:late",label:"Поздний сервис",category:"Web",bytes:1,bps:1,share:1}]}:{results:{"router.example":null}}})}catch{}finally{completed=true} }) await page.getByRole("button",{name:"Обновить",exact:true}).click() await expect.poll(()=>started).toBe(true);await openSettings(page) 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:/^Карта сети/}).click() if(change==="backend"){await expect(page.getByRole("button",{name:"Устройство Новая карта",exact:true})).toBeVisible();await expect(page.getByText(/IPFIX · среднее за 1 мин 30 с/)).toBeVisible()} else await expect(page.getByText("Демонстрационные данные",{exact:true})).toBeVisible() const before=newReads;release();await expect.poll(()=>completed).toBe(true) await page.evaluate(()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(()=>resolve())))) await expect(page.getByText("Старая ошибка карты",{exact:false})).toHaveCount(0) await expect(page.getByRole("button",{name:"Устройство Поздняя карта",exact:true})).toHaveCount(0) await expect(page.getByText("Поздний сервис",{exact:false})).toHaveCount(0) await expect(page.getByRole("alert").filter({hasText:"DNS не разрешён"})).toHaveCount(0) if(change==="backend")await expect(page.getByRole("button",{name:"Устройство Новая карта",exact:true})).toBeVisible() expect(newReads).toBe(before) }) test("Карта: плотные группы WAN и узлов раскрывают отказ и найденный объект", async ({page}) => { const uplinks = Array.from({length: 12}, (_, i) => ({ ...wan, id: "wan-" + i, name: "Uplink-" + (i + 1), iface: "ether" + (i + 1), ip: "198.51.100." + (i + 1), })) const routers = Array.from({length: 12}, (_, i) => ({ id: i + 2, name: "NSK-JH-" + String(i + 1).padStart(2, "0"), host: "192.0.2." + (i + 2), type: "jump-host", site: "Новосибирск", country: "RU", enabled: true, status: "online", })) const exit = {id: 20, name: "ARN-EXIT", host: "192.0.2.20", type: "exit-node", site: "Стокгольм", country: "SE", enabled: true, status: "online"} const tunnels = ["up", "down", "up"].map((status, i) => ({ id: "gre-" + i, name: "gre-arn-" + i, serverId: String(routers[0].id), localAddress: routers[0].host, remoteAddress: exit.host, localInnerIp: `10.60.${i}.1/30`, remoteInnerIp: `10.60.${i}.2/30`, poolId: "pool", ipsec: null, mtu: 1476, keepaliveInterval: 10, keepaliveRetries: 3, dscp: "inherit", clampTcpMss: true, allowFastPath: true, comment: "", enabled: true, status, })) await page.route("**/health", route => route.fulfill({headers, json: {ok: true}})) await page.route("**/api/**", route => { const url = new URL(route.request().url()) const replies: Record = { "/api/auth/config": {required: false}, "/api/servers": [{...row("NSK-HOME"), site: "Новосибирск", wanUplinks: uplinks}, ...routers, exit], "/api/filters/gre-tunnels": {tunnels, failures: []}, "/api/uptime/speed-probes": {probes: []}, "/api/traffic/flow/map-hops": emptyMapFlow(url), } return route.fulfill({headers, json: replies[url.pathname] ?? {}}) }) await page.goto("/network-map") const roleGroup = page.locator("[data-map-group]").filter({hasText: "JH ×12"}) const wanGroup = page.locator("[data-map-group]").filter({hasText: "WAN ×12"}) const hiddenServer = page.getByRole("button", {name: "Устройство NSK-JH-12", exact: true}) const hiddenWan = page.getByRole("button", {name: "Аплинк Uplink-12", exact: true}) await expect(roleGroup).toHaveAttribute("aria-expanded", "false") await expect(roleGroup).toContainText("Проблемы · 1") await expect(wanGroup).toHaveAttribute("aria-expanded", "false") await expect(hiddenServer).toHaveCount(0) await expect(hiddenWan).toHaveCount(0) const line = page.locator('[data-map-group-link="gre"]') await expect(line).toHaveAttribute("data-channel-count", "3") await expect(line).toHaveAttribute("data-channel-down", "1") const marker = page.locator('[data-map-group-link-label="gre"]') await expect(marker).toBeVisible() await marker.click() await expect(roleGroup).toHaveAttribute("aria-expanded", "true") await expect(page.getByRole("button", {name: /^Устройство NSK-JH-/})).toHaveCount(12) await expect(wanGroup).toHaveAttribute("aria-expanded", "false") await roleGroup.press("Enter") await expect(hiddenServer).toHaveCount(0) const search = page.getByRole("textbox", {name: "Поиск узла или назначения"}) const results = page.getByRole("group", {name: "Подсказки поиска"}) await search.fill("NSK-JH-12") await results.getByRole("button", {name: /NSK-JH-12/}).click() await expect(hiddenServer).toHaveAttribute("aria-pressed", "true") await expect(roleGroup).toHaveAttribute("aria-expanded", "true") await search.fill("Uplink-12") await results.getByRole("button", {name: /Uplink-12/}).click() await expect(hiddenWan).toHaveAttribute("aria-pressed", "true") await expect(wanGroup).toHaveAttribute("aria-expanded", "true") })