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

23 lines
3.4 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 row=(name:string)=>({id:1,name,host:"192.0.2.1",country:"RU",enabled:true,status:"online",os:null})
for(const change of ["backend","mode"])for(const part of ["catalog","exec"])for(const fails of [false,true])test("Терминал: поздний "+part+"/"+change+"/"+fails,async({page})=>{
let started=false,completed=false,release!:()=>void;const gate=new Promise<void>(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;return r.fulfill({headers,json:p==="/api/auth/config"?{required:false}:p==="/api/servers"?[row(u.port==="8101"?"Новый источник":"Прежний источник")]:p.endsWith("/exec")?{output:"новый вывод"}:{}})})
await page.goto("/settings");await page.getByRole("link",{name:/^Терминал/}).click();await page.getByRole("option",{name:/Прежний источник/}).click()
await expect(page.getByRole("textbox",{name:"Команда · Прежний источник"})).toBeEnabled()
const path=part==="catalog"?"/api/servers":"/api/servers/1/exec"
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==="catalog"?[row("Поздний каталог")]:{output:"Поздний вывод терминала"}})}catch{}finally{completed=true}})
if(part==="catalog")await page.getByRole("button",{name:"Обновить устройства",exact:true}).click()
else{await page.getByRole("textbox",{name:/^Команда ·/}).fill("interface print");await page.getByRole("button",{name:"Выполнить",exact:true}).click()}
await expect.poll(()=>started).toBe(true);await page.getByRole("link",{name:/^Настройки/}).click()
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 page.getByRole("option",{name:/Новый источник/}).click();else await page.getByRole("option").first().click()
await expect(page.getByRole("textbox",{name:/^Команда ·/})).toBeEnabled()
release();await expect.poll(()=>completed).toBe(true);await page.evaluate(()=>new Promise<void>(resolve=>requestAnimationFrame(()=>requestAnimationFrame(()=>resolve()))))
await expect(page.getByText("Поздний каталог",{exact:false})).toHaveCount(0);await expect(page.getByText("Поздний вывод терминала",{exact:false})).toHaveCount(0);await expect(page.getByText("Прежняя ошибка терминала",{exact:false})).toHaveCount(0)
if(change==="backend")await expect(page.getByRole("textbox",{name:"Команда · Новый источник"})).toBeEnabled();else await expect(page.getByRole("status").filter({hasText:"Демо: команды не отправляются"})).toBeVisible()
})