Files
MikrotikManager/tests/e2e/settings-context.spec.ts

17 lines
6.2 KiB
TypeScript

import {test,expect,type Page} from "@playwright/test"
import {evoSettings,evoCatalog} from "./fixtures/evobgp"
const headers={"access-control-allow-origin":"*","access-control-allow-methods":"GET,POST,PUT,OPTIONS","access-control-allow-headers":"Content-Type,Authorization"}
async function setup(page:Page){await page.route("**/health",r=>r.fulfill({headers,json:{ok:true}}));await page.route("**/api/**",r=>{const path=new URL(r.request().url()).pathname;return r.fulfill(path==="/api/auth/config"?{headers,json:{required:false}}:path==="/api/evobgp/settings"?{headers,json:evoSettings}:path==="/api/evobgp/catalog"?{headers,json:evoCatalog}:{headers,status:503,json:{error:"outside source fixture"}})});await page.goto("/settings");await expect(page.getByRole("textbox",{name:"URL бекенда",exact:true})).toHaveValue("http://localhost:8000")}
test("Settings source: invalid backend URL stays draft without changing active source",async({page})=>{await setup(page);const input=page.getByRole("textbox",{name:"URL бекенда",exact:true});for(const value of ["bad","ftp://host.test","https://user:[email protected]","https://host.test?token=secret"]){await input.fill(value);await input.press("Enter");await expect(page.locator("#main-content").getByRole("alert")).toContainText("Укажите HTTP(S) адрес");expect(await page.evaluate(()=>localStorage.getItem("routerlists:backend-url"))).toBeNull();await expect(input).toHaveValue(value)}})
test("Settings source: URL canonicalizes and persists after Enter",async({page})=>{await setup(page);const input=page.getByRole("textbox",{name:"URL бекенда",exact:true});await input.fill(" http://LOCALHOST:8101/ ");await input.press("Enter");await expect(input).toHaveValue("http://localhost:8101");expect(await page.evaluate(()=>localStorage.getItem("routerlists:backend-url"))).toBe("http://localhost:8101");await page.reload();await expect(input).toHaveValue("http://localhost:8101")})
test("Settings source: explicit demo/live mode persists",async({page})=>{await setup(page);await page.getByRole("button",{name:"Моковые",exact:true}).click();expect(await page.evaluate(()=>localStorage.getItem("routerlists:data-source"))).toBe("mock");await page.reload();await expect(page.getByRole("button",{name:"Моковые",exact:true})).toBeVisible();await page.getByRole("button",{name:"Живые",exact:true}).click();expect(await page.evaluate(()=>localStorage.getItem("routerlists:data-source"))).toBe("live")})
for(const failed of [false,true])test("Settings health: old backend result ignored/"+failed,async({page})=>{
await page.route("**/health",r=>r.fulfill({headers,json:{status:"ok"}}));let started=false,completed=false,release!:()=>void;const gate=new Promise<void>(r=>release=r)
await page.route("http://localhost:8000/health",async r=>{started=true;await gate;try{await r.fulfill(failed?{headers,status:503,json:{error:"old health"}}:{headers,json:{status:"ok"}})}catch{}finally{completed=true}})
try{await page.route("**/api/**",r=>r.fulfill({headers,json:{required:false}}));await page.goto("/settings");await expect.poll(()=>started).toBe(true);const input=page.getByRole("textbox",{name:"URL бекенда",exact:true});await input.fill("http://localhost:8101");await input.press("Enter");await expect(page.getByText("Доступен",{exact:true})).toBeVisible();release();await expect.poll(()=>completed).toBe(true);await expect(page.getByText("Доступен",{exact:true})).toBeVisible();await expect(page.getByText("Недоступен",{exact:true})).toHaveCount(0)}finally{release()}
})
test("Settings health: source switch does not inherit available status",async({page})=>{await setup(page);await expect(page.getByText("Доступен",{exact:true})).toBeVisible();let started=false,release!:()=>void;const gate=new Promise<void>(r=>release=r);await page.route("http://localhost:8101/health",async r=>{started=true;await gate;try{await r.fulfill({headers,json:{ok:false}})}catch{}});try{const input=page.getByRole("textbox",{name:"URL бекенда",exact:true});await input.fill("http://localhost:8101");await input.press("Enter");await expect.poll(()=>started).toBe(true);await expect(page.getByText("Проверка…",{exact:true})).toBeVisible();await expect(page.getByText("Доступен",{exact:true})).toHaveCount(0);release();await expect(page.getByText("Недоступен",{exact:true})).toBeVisible()}finally{release()}})
test("Settings health: returning to source A rejects its old failed request",async({page})=>{await page.route("**/health",r=>r.fulfill({headers,json:{status:"ok"}}));await page.route("**/api/**",r=>r.fulfill({headers,json:{required:false}}));let count=0,freshA=false,completed=false,release!:()=>void;const gate=new Promise<void>(r=>release=r);await page.route("http://localhost:8000/health",async r=>{count++;if(freshA)return r.fulfill({headers,json:{status:"ok"}});await gate;try{await r.fulfill({headers,status:503,json:{error:"first A failed"}})}catch{}finally{completed=true}});try{await page.goto("/settings");await expect.poll(()=>count).toBeGreaterThan(0);const input=page.getByRole("textbox",{name:"URL бекенда",exact:true});await input.fill("http://localhost:8101");await input.press("Enter");await expect(page.getByText("Доступен",{exact:true})).toBeVisible();const previousARequests=count;freshA=true;await input.fill("http://localhost:8000");await input.press("Enter");await expect.poll(()=>count).toBeGreaterThan(previousARequests);await expect(page.getByText("Доступен",{exact:true})).toBeVisible();release();await expect.poll(()=>completed).toBe(true);await expect(page.getByText("Доступен",{exact:true})).toBeVisible()}finally{release()}})
for(const payload of [{},{ok:false},"proxy html"])test("Settings health: HTTP 200 with invalid body is unavailable/"+JSON.stringify(payload),async({page})=>{await setup(page);await expect(page.getByText("Доступен",{exact:true})).toBeVisible();await page.route("**/health",r=>r.fulfill({headers,json:payload}));await page.getByRole("button",{name:"Проверить",exact:true}).click();await expect(page.getByText("Недоступен",{exact:true})).toBeVisible();await expect(page.getByText("Доступен",{exact:true})).toHaveCount(0)})