fix(api): проверить каталог speed-проб и изолировать источник

This commit is contained in:
Denozordec committed 2026-10-04 10:31:02 +07:00
1 parent f30319ac55
commit d4f2961f76
13 files changed
+123 -51

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
import { test, expect } from "@playwright/test"
import { server } from "./fixtures/users"
const headers = { "access-control-allow-origin":"*", "access-control-allow-methods":"GET,PUT,POST,OPTIONS", "access-control-allow-headers":"Content-Type,Authorization" }
const probe = { id:"probe-1", srcServerId:"1", dstServerId:"2", srcInterface:"ether1", dstInterface:"ether1", protocol:"tcp", direction:"both", durationSec:"10", enabled:true,lastRunAt:null,lastTxAvgMbps:null,lastRxAvgMbps:0,lastStatus:null,lastError:null }
const run = {id:"run-1",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"ether1",protocol:"tcp",direction:"both",durationSec:10,txAvgMbps:42,rxAvgMbps:0,status:"done",error:null,createdAt:"2026-10-04T00:00:00Z"}
for (const change of ["backend","mode"] as const) for (const resource of ["probes","history"] as const) for (const failure of [false,true]) test("Uptime speed context/"+change+"/"+resource+"/"+failure,async({page})=>{
let hold=false,started=false,completed=false;let release!:()=>void;const gate=new Promise<void>(resolve=>release=resolve)
const target=resource === "probes" ? "/api/uptime/speed-probes" : "/api/uptime/speed-test/runs"
await page.route("**/health",r=>r.fulfill({headers,json:{ok:true}}))
await page.route("**/api/**",async r=>{
const url=new URL(r.request().url()),path=url.pathname,old=url.port === "8000"
const body=path === "/api/auth/config" ? {required:false} : path === "/api/servers" ? [{...server,name:old ? "Старый источник" : "Новый источник"}] : path === "/api/uptime/overview" ? {probes:[],resources:[]} : path === "/api/uptime/speed-probes" ? {probes:[{...probe,id:old ? "old-probe" : "new-probe"}]} : path === "/api/uptime/speed-test/runs" ? {runs:[{...run,id:old ? "old-run" : "new-run"}]} : {}
if(path === target && old && hold){started=true;await gate;try{await r.fulfill(failure ? {headers,status:503,json:{error:"СТАРАЯ ОШИБКА СКОРОСТИ"}} : {headers,json:body})}catch{}finally{completed=true};return}
return r.fulfill({headers,json:body})
})
await page.goto("/uptime?tab=speed");await expect(page.locator('tr[data-row-id="old-run"]')).toBeVisible();await page.getByRole("button",{name:"Пауза",exact:true}).click();hold=true;await page.getByRole("button",{name:"Обновить",exact:true}).click();await expect.poll(()=>started).toBe(true)
await page.goto("/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.goto("/uptime?tab=speed")
if(change === "backend"){await expect(page.locator('tr[data-row-id="new-run"]')).toBeVisible();await expect(page.getByText("Новый источник",{exact:true}).first()).toBeVisible()}else await expect(page.getByRole("button",{name:"Сбросить",exact:true})).toBeVisible()
release();await expect.poll(()=>completed).toBe(true);await expect(page.locator('tr[data-row-id="old-run"]')).toHaveCount(0);await expect(page.getByText("СТАРАЯ ОШИБКА СКОРОСТИ",{exact:true})).toHaveCount(0)
if(change === "mode"){await expect(page.getByText(/^Speed-пробы: получено/)).toHaveCount(0);await expect(page.getByRole("alert",{name:"Чтение скорости: probes",exact:true})).toHaveCount(0)}
})
+15 -1
View File
@@ -1,4 +1,5 @@
import { test, expect } from "@playwright/test"
import axe from "axe-core"
import { server } from "./fixtures/users"
const run = { id:"history-1", srcServerId:"1", dstServerId:"2", srcInterface:"ether1", dstInterface:"ether1", protocol:"tcp", direction:"both", durationSec:10, txAvgMbps:42, rxAvgMbps:0, status:"done", error:null, createdAt:"2026-10-04T00:00:00Z" }
for (const resource of ["probes", "history"] as const) test("Скорость: initial unknown и независимое recovery/" + resource, async ({ page }) => {
@@ -30,7 +31,7 @@ test("Скорость: поздняя история не задерживае
test("Скорость: null не становится нулём, malformed DTO сохраняет последний результат", async ({page}) => {
let entries:unknown[]=[{...run,txAvgMbps:null,rxAvgMbps:0}]
await page.route("**/health",r=>r.fulfill({json:{ok:true}}));await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;return r.fulfill({json:p === "/api/auth/config" ? {required:false} : p === "/api/servers" ? [server] : p === "/api/uptime/overview" ? {probes:[],resources:[]} : p === "/api/uptime/speed-probes" ? {probes:[{id:"probe-1",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"ether1",protocol:"tcp",direction:"both",durationSec:"10",enabled:true,lastStatus:"done",lastRunAt:"2026-10-03T00:00:00Z",lastTxAvgMbps:999,lastRxAvgMbps:0}]} : p === "/api/uptime/speed-test/runs" ? {runs:entries} : {}})})
await page.route("**/health",r=>r.fulfill({json:{ok:true}}));await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;return r.fulfill({json:p === "/api/auth/config" ? {required:false} : p === "/api/servers" ? [server] : p === "/api/uptime/overview" ? {probes:[],resources:[]} : p === "/api/uptime/speed-probes" ? {probes:[{id:"probe-1",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"ether1",protocol:"tcp",direction:"both",durationSec:"10",enabled:true,lastStatus:"done",lastRunAt:"2026-10-03T00:00:00Z",lastTxAvgMbps:999,lastRxAvgMbps:0,lastError:null}]} : p === "/api/uptime/speed-test/runs" ? {runs:entries} : {}})})
await page.goto("/uptime?tab=speed");await page.getByRole("button",{name:"Пауза",exact:true}).click();const row=page.locator('tr[data-row-id="history-1"]');await expect(row).toContainText("Неизвестно");await expect(row).toContainText("0 Мбит/с");await expect(page.getByText("999 Мбит/с",{exact:true})).toHaveCount(0)
await page.setViewportSize({width:320,height:1000});expect(await page.evaluate(()=>document.documentElement.scrollWidth <= innerWidth)).toBe(true)
await page.addStyleTag({content:"nextjs-portal{display:none!important}"});await page.screenshot({path:"docs/ux-ui/screens/speed-history-null-320.png",fullPage:true,animations:"disabled"})
@@ -40,3 +41,16 @@ test("Скорость: null не становится нулём, malformed DTO
}
entries=[{...run,txAvgMbps:0}];await page.getByRole("button",{name:"Обновить",exact:true}).click();await expect(page.getByRole("alert",{name:"Чтение скорости: history",exact:true})).toHaveCount(0);await expect(row).not.toContainText("Неизвестно");await expect(row).toContainText("0 Мбит/с")
})
test("Speed-пробы: malformed каталог, cache/freshness и блокировка замены до recovery", async ({page}) => {
const probe={id:"probe-1",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"ether1",protocol:"tcp",direction:"both",durationSec:"10",enabled:true,lastRunAt:null,lastTxAvgMbps:null,lastRxAvgMbps:0,lastStatus:null,lastError:null}
let raw:unknown={probes:[{...probe,enabled:"yes"}]};const writes:string[]=[]
await page.route("**/health",r=>r.fulfill({json:{ok:true}}));await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;if(r.request().method() !== "GET")writes.push(p);return r.fulfill({json:p === "/api/auth/config" ? {required:false} : p === "/api/servers" ? [server] : p === "/api/uptime/overview" ? {probes:[],resources:[]} : p === "/api/uptime/speed-probes" ? raw : p === "/api/uptime/speed-test/runs" ? {runs:[]} : {}})})
await page.goto("/uptime?tab=speed");await page.getByRole("button",{name:"Пауза",exact:true}).click();const alert=page.getByRole("alert",{name:"Чтение скорости: probes",exact:true});await expect(alert).toContainText("Данные пока неизвестны");await expect(page.getByText("Нет speed-проб",{exact:true})).toHaveCount(0);await expect(page.getByRole("button",{name:"Новая speed-проба",exact:true})).toBeDisabled()
const refresh=page.getByRole("button",{name:"Обновить",exact:true});raw={probes:[probe]};await refresh.click();await expect(alert).toHaveCount(0);const toggle=page.getByRole("switch");await expect(toggle).toBeChecked();await expect(toggle).toBeEnabled();const freshness=page.getByRole("status").filter({hasText:/^Speed-пробы: получено/});const before=await freshness.textContent()
for(const invalid of [{probes:[probe,probe]},{probes:[{...probe,lastRxAvgMbps:-1}]},{probes:[{...probe,lastStatus:"other"}]},{probes:[{...probe,lastRunAt:"bad"}]}]) {raw=invalid;await refresh.click();await expect(alert).toContainText("Последние данные сохранены");await expect(freshness).toHaveText(before!);await expect(toggle).toBeChecked();await expect(toggle).toBeDisabled();await expect(page.getByRole("button",{name:"Новая speed-проба",exact:true})).toBeDisabled()}
await page.setViewportSize({width:320,height:1000});expect(await page.evaluate(()=>document.documentElement.scrollWidth <= innerWidth)).toBe(true);await expect(toggle).toHaveAccessibleName(/Включить speed-пробу/)
await page.addScriptTag({content:axe.source});const audit=await page.evaluate(async()=> (window as unknown as {axe:typeof axe}).axe.run(document.querySelector('[aria-label="Чтение скорости: probes"]')!,{runOnly:{type:"tag",values:["wcag2a","wcag2aa","wcag21a","wcag21aa"]}}));expect(audit.violations).toEqual([])
await page.addStyleTag({content:"nextjs-portal{display:none!important}"});await page.screenshot({path:"docs/ux-ui/screens/speed-probes-error-320.png",fullPage:true,animations:"disabled"})
raw={probes:[probe]};await refresh.click();await expect(alert).toHaveCount(0);await expect(toggle).toBeEnabled();expect(writes).toEqual([])
})