35 خطوط
5.3 KiB
JavaScript
35 خطوط
5.3 KiB
JavaScript
import { writeFile } from 'node:fs/promises'
|
|
|
|
const port = process.argv[2] ?? '9223'
|
|
const base = `http://127.0.0.1:${port}`
|
|
const target = await fetch(`${base}/json/new?http://127.0.0.1:5173/login`, { method: 'PUT' }).then((response) => response.json())
|
|
const socket = new WebSocket(target.webSocketDebuggerUrl)
|
|
await new Promise((resolve, reject) => { socket.addEventListener('open', resolve, { once: true }); socket.addEventListener('error', reject, { once: true }) })
|
|
let id = 0
|
|
const pending = new Map()
|
|
socket.addEventListener('message', (event) => { const message = JSON.parse(event.data); if (message.id && pending.has(message.id)) { const { resolve, reject } = pending.get(message.id); pending.delete(message.id); message.error ? reject(new Error(message.error.message)) : resolve(message.result) } })
|
|
const call = (method, params = {}) => new Promise((resolve, reject) => { const requestId = ++id; pending.set(requestId, { resolve, reject }); socket.send(JSON.stringify({ id: requestId, method, params })) })
|
|
const evaluate = async (expression) => (await call('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true })).result.value
|
|
const waitFor = async (predicate, message) => { for (let attempt = 0; attempt < 60; attempt += 1) { if (await evaluate(predicate)) return; await new Promise((resolve) => setTimeout(resolve, 250)) } throw new Error(message) }
|
|
const screenshot = async (name) => { const result = await call('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }); await writeFile(new URL(`../${name}`, import.meta.url), Buffer.from(result.data, 'base64')) }
|
|
|
|
await call('Page.enable'); await call('Runtime.enable'); await call('Emulation.setDeviceMetricsOverride', { width: 1440, height: 1000, deviceScaleFactor: 1, mobile: false })
|
|
await waitFor(`document.querySelector('input[type="email"]') !== null`, 'Login page did not render')
|
|
const loggedIn = await evaluate(`fetch('/api/v1/auth/login',{method:'POST',headers:{'Content-Type':'application/json','Accept':'application/json'},body:JSON.stringify({email:'designer@microlearn.test',password:'password',device_name:'phase12-browser-smoke'})}).then(r=>r.json()).then(x=>{localStorage.setItem('microlearn.access_token',x.data.token);location.href='/app/dashboard';return true})`)
|
|
if (!loggedIn) throw new Error('Designer login failed')
|
|
await waitFor(`location.pathname === '/app/dashboard' && document.querySelector('[aria-label="بازکردن حساب کاربری"]') !== null`, 'Designer dashboard did not render')
|
|
await evaluate(`document.querySelector('[aria-label="بازکردن حساب کاربری"]').click()`)
|
|
await waitFor(`document.querySelector('[role="menu"]') !== null`, 'Account menu did not open')
|
|
await screenshot('phase12-account-header.png')
|
|
await evaluate(`fetch('/api/v1/courses?perPage=1',{headers:{Accept:'application/json',Authorization:'Bearer '+localStorage.getItem('microlearn.access_token')}}).then(r=>r.json()).then(async list=>{const id=list.data[0].id;const workspace=await fetch('/api/v1/courses/'+id,{headers:{Accept:'application/json',Authorization:'Bearer '+localStorage.getItem('microlearn.access_token')}}).then(r=>r.json());const lesson=workspace.data.modules[0].lessons[0];location.href='/app/courses/'+id+'/versions/'+workspace.data.version.id+'/lessons/'+lesson.id+'/builder';return true})`)
|
|
await waitFor(`document.querySelector('.course-builder') !== null && document.querySelector('[aria-label="دیدگاهها"]') !== null`, 'Builder did not render'); await evaluate(`document.querySelector('[aria-label="دیدگاهها"]').click()`); await waitFor(`document.querySelector('.collaboration-panel') !== null && !document.body.textContent.includes('در حال همگامسازی')`, 'Builder collaboration panel did not settle')
|
|
await screenshot('phase12-builder-collaboration.png')
|
|
await evaluate(`location.href='/app/reviews'`); await waitFor(`location.pathname === '/app/reviews' && document.querySelector('.review-toolbar') !== null`, 'Review Center did not render'); await waitFor(`!document.body.textContent.includes('در حال دریافت بازبینیها')`, 'Review Center data did not settle')
|
|
await screenshot('phase12-review-center.png')
|
|
await call('Emulation.setDeviceMetricsOverride', { width: 375, height: 812, deviceScaleFactor: 1, mobile: true }); await evaluate(`location.reload()`); await waitFor(`document.querySelector('.review-toolbar') !== null && !document.body.textContent.includes('در حال دریافت بازبینیها')`, 'Mobile Review Center did not render')
|
|
await screenshot('phase12-review-center-mobile.png')
|
|
await evaluate(`localStorage.setItem('microlearn.theme','dark');location.reload()`); await waitFor(`document.documentElement.dataset.theme === 'dark' && document.querySelector('[aria-label="تغییر زبان"]') !== null`, 'Dark mode did not render'); await evaluate(`document.querySelector('[aria-label="تغییر زبان"]').click()`); await waitFor(`document.documentElement.lang === 'en' && document.querySelector('.review-toolbar')?.textContent.includes('Open') && !document.body.textContent.includes('Loading reviews')`, 'English mode did not render')
|
|
await screenshot('phase12-review-center-mobile-en-dark.png')
|
|
console.log(JSON.stringify({ accountHeader: true, builderCollaboration: true, reviewCenter: true, mobile375: true, englishDark: true }))
|
|
socket.close()
|