Files
XHChat/tests/clients.test.mjs

76 lines
3.5 KiB
JavaScript

import assert from 'node:assert/strict'
import { randomBytes } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import { _electron, chromium } from 'playwright'
import electron from 'electron'
const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080'
async function form(page, username, password) {
await page.getByLabel('用户名').fill(username)
await page.getByLabel('密码', { exact: true }).fill(password)
}
test('Web 注册,Windows 登录,Web 撤销 Windows 设备', { timeout: 45000 }, async () => {
const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' })
const profile = await mkdtemp(join(tmpdir(), 'xhchat-client-test-'))
let desktop
try {
const web = await browser.newPage({ viewport: { width: 390, height: 844 } })
await web.goto(base)
await web.waitForFunction(() => document.querySelector('.brand img')?.naturalWidth === 512)
await web.getByRole('button', { name: '注册', exact: true }).click()
const username = 'qa_' + randomBytes(6).toString('hex')
const password = randomBytes(24).toString('base64url')
await form(web, username, password)
await web.getByRole('button', { name: '创建账号' }).click()
await web.getByText('实时连接已建立').waitFor()
assert.equal(await web.evaluate(() => document.documentElement.scrollWidth), 390)
const privateKeyExtractable = await web.evaluate(async (username) => {
const db = await new Promise((resolve, reject) => {
const request = indexedDB.open('xhchat-device', 1)
request.onsuccess = () => resolve(request.result)
request.onerror = () => reject(request.error)
})
try {
return await new Promise((resolve, reject) => {
const request = db.transaction('keys').objectStore('keys').get(username)
request.onsuccess = () => resolve(request.result.privateKey.extractable)
request.onerror = () => reject(request.error)
})
} finally {
db.close()
}
}, username)
assert.equal(privateKeyExtractable, false)
desktop = await _electron.launch({
executablePath: electron,
args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile],
env: { ...process.env, XHCHAT_SERVER_URL: base },
})
const windows = await desktop.firstWindow()
await windows.waitForFunction(() => document.querySelector('.brand img')?.naturalWidth === 512)
await form(windows, username, password)
await windows.locator('button.primary').click()
await windows.getByText('实时连接已建立').waitFor()
await windows.getByText('Windows 电脑').waitFor()
assert.equal(await windows.evaluate(() => typeof window.require), 'undefined')
await web.getByRole('button', { name: '刷新' }).click()
await web.getByText('Windows 电脑').waitFor()
await web.getByRole('button', { name: '撤销', exact: true }).click()
await web.getByRole('button', { name: '确认撤销' }).click()
await web.getByText('已撤销').waitFor()
await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor()
await web.getByRole('button', { name: '退出登录' }).click()
await web.getByRole('heading', { name: '登录 XHChat' }).waitFor()
} finally {
await desktop?.close()
await browser.close()
await rm(profile, { recursive: true, maxRetries: 5, retryDelay: 200, force: true })
}
})