Files
XHChat/tests/contacts-browser.test.mjs
T

105 lines
5.5 KiB
JavaScript

import assert from 'node:assert/strict'
import { randomBytes } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { createServer } from 'node:http'
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 register(page, desktop = false) {
if (!desktop) await page.goto(base)
const username = 'qa_' + randomBytes(6).toString('hex')
await page.getByRole('button', { name: '注册', exact: true }).click()
await page.getByLabel('用户名').fill(username)
await page.getByLabel('密码', { exact: true }).fill(randomBytes(24).toString('hex'))
await page.getByRole('button', { name: '创建账号' }).click()
await page.getByText('实时连接已建立').waitFor()
await page.getByRole('navigation', { name: '工作区' }).getByRole('link', { name: '联系人' }).click()
return username
}
test('双端联系人实时同步,桌面浏览器独立分区与 UA', { timeout: 60000 }, async () => {
const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' })
const profile = await mkdtemp(join(tmpdir(), 'xhchat-browser-test-'))
const requests = []
const local = createServer((request, response) => {
requests.push({ path: request.url, userAgent: request.headers['user-agent'] })
response.setHeader('Content-Type', 'text/html; charset=utf-8')
response.end('<title>隔离测试站</title><h1>OK</h1>')
})
let app
try {
await new Promise((resolve) => local.listen(0, '127.0.0.1', resolve))
const url = 'http://127.0.0.1:' + local.address().port
const web = await browser.newPage()
const webName = await register(web)
app = await _electron.launch({
executablePath: electron,
args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile],
env: { ...process.env, XHCHAT_SERVER_URL: base },
})
const desktop = await app.firstWindow()
const desktopName = await register(desktop, true)
await web.getByLabel('按用户名查找').fill(desktopName)
await web.getByRole('button', { name: '查找' }).click()
await web.getByRole('button', { name: '添加好友' }).click()
await desktop.getByRole('button', { name: new RegExp(webName) }).click()
await desktop.getByRole('button', { name: '接受申请' }).click()
await web.getByLabel('备注').fill('跨端好友')
await web.getByLabel('分组').fill('验收')
await web.getByRole('button', { name: '保存' }).click()
await web.getByRole('heading', { name: '跨端好友' }).waitFor()
await desktop.getByRole('heading', { name: webName }).waitFor()
await web.getByRole('button', { name: '拉黑', exact: true }).click()
await web.getByRole('button', { name: '确认', exact: true }).click()
await web.getByText('已拉黑').waitFor()
await desktop.getByText('还没有联系人').waitFor()
await web.getByRole('button', { name: '解除拉黑' }).click()
await web.getByText('还没有联系人').waitFor()
const opened = app.waitForEvent('window')
await desktop.getByRole('navigation', { name: '主导航' }).getByRole('button', { name: '浏览器' }).click()
const toolbar = await opened
await toolbar.getByRole('button', { name: '新标签页', exact: true }).waitFor()
const address = toolbar.getByRole('textbox', { name: '网址或搜索关键词' })
await address.fill(url + '/first')
await address.press('Enter')
try {
await toolbar.getByRole('button', { name: '隔离测试站', exact: true }).waitFor({ timeout: 8000 })
} catch (error) {
const pages = await app.evaluate(({ webContents }) => webContents.getAllWebContents().map((entry) => ({ url: entry.getURL(), title: entry.getTitle() })))
throw new Error(JSON.stringify({ pages, requests, status: await toolbar.locator('#status').textContent() }), { cause: error })
}
const site = await app.evaluate(async ({ webContents }, url) => {
const view = webContents.getAllWebContents().find((contents) => contents.getURL() === url + '/first')
if (!view) throw new Error('找不到网页视图')
return {
version: process.versions.chrome,
isolated: await view.executeJavaScript('typeof window.xhchat + ":" + typeof window.xhchatBrowser + ":" + typeof window.require'),
sandbox: view.getLastWebPreferences().sandbox,
separateSession: view.session !== webContents.getAllWebContents().find((contents) => contents.getURL().startsWith('xhchat://app/#'))?.session,
}
}, url)
assert.equal(site.isolated, 'undefined:undefined:undefined')
assert.equal(site.sandbox, true)
assert.equal(site.separateSession, true)
const userAgent = requests.find((entry) => entry.path === '/first').userAgent
assert.ok(userAgent.includes('Chrome/' + site.version))
assert.match(userAgent, /XHChat\/0\.1\.0/)
await toolbar.getByRole('button', { name: '新建标签页' }).click()
await toolbar.getByRole('button', { name: '新标签页', exact: true }).waitFor()
await address.fill('file:///C:/Windows/win.ini')
await address.press('Enter')
await toolbar.getByText('请输入 HTTPS 地址或搜索关键词。').waitFor()
} finally {
await app?.close()
await browser.close()
await new Promise((resolve) => local.close(resolve))
await rm(profile, { recursive: true, maxRetries: 5, retryDelay: 200, force: true })
}
})