import assert from 'node:assert/strict' import { randomBytes } from 'node:crypto' import { mkdtemp, readFile, 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'] }) if (request.url === '/download') { response.setHeader('Content-Type', 'application/octet-stream') response.setHeader('Content-Disposition', 'attachment; filename=download-check.txt') response.end('download-check') return } response.setHeader('Content-Type', 'text/html; charset=utf-8') response.end('隔离测试站

OK

') }) 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: '添加书签', exact: true }).click() await toolbar.getByRole('button', { name: '取消书签', exact: true }).waitFor() await toolbar.getByRole('button', { name: '书签与历史', exact: true }).click() const savedFile = join(profile, 'download-check.txt') await app.evaluate(({ session, webContents }, { url, savedFile }) => { const sites = session.fromPartition('persist:xhchat-sites') sites.once('will-download', (_event, item) => item.setSavePath(savedFile)) const page = webContents.getAllWebContents().find((entry) => entry.getURL() === url + '/first') if (!page) throw new Error('找不到下载来源标签页') page.downloadURL(url + '/download') }, { url, savedFile }) await toolbar.getByRole('button', { name: '下载', exact: true }).click() await toolbar.getByText('download-check.txt', { exact: true }).waitFor() await toolbar.getByText('已完成', { exact: true }).waitFor() assert.equal(await readFile(savedFile, 'utf8'), 'download-check') await toolbar.getByRole('button', { name: '清除下载记录' }).click() await toolbar.getByText('暂无下载记录').waitFor() await toolbar.getByRole('button', { name: '书签与历史', exact: true }).click() await toolbar.locator('#bookmarks').getByText(url + '/first', { exact: true }).waitFor() await toolbar.locator('#history').getByText(url + '/first', { exact: true }).waitFor() const openedPrivate = app.waitForEvent('window') await toolbar.getByRole('button', { name: '隐私窗口', exact: true }).click() const privateToolbar = await openedPrivate const privateAddress = privateToolbar.getByRole('textbox', { name: '网址或搜索关键词' }) await privateAddress.fill(url + '/private') await privateAddress.press('Enter') await privateToolbar.getByRole('button', { name: '隔离测试站', exact: true }).waitFor() assert.equal(await privateToolbar.getByRole('button', { name: '书签与历史' }).count(), 0) assert.equal(await toolbar.locator('#history').getByText(url + '/private', { exact: true }).count(), 0) await privateToolbar.close() await toolbar.locator('#bookmarks').getByRole('button', { name: '移除' }).click() await toolbar.getByText('暂无书签').waitFor() await toolbar.getByRole('button', { name: '书签与历史', exact: true }).click() 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 }) } })