实现浏览器下载管理和本地下载记录

This commit is contained in:
xhgit committed 2026-11-13 19:23:20 +08:00
1 parent ae04cc9494
commit 9b8c2026ea
8 files changed
+155 -13

No files matched your search

+22 -1
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { randomBytes } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { createServer } from 'node:http'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -28,6 +28,12 @@ test('双端联系人实时同步,桌面浏览器独立分区与 UA', { timeou
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('<title>隔离测试站</title><h1>OK</h1>')
})
@@ -93,6 +99,21 @@ test('双端联系人实时同步,桌面浏览器独立分区与 UA', { timeou
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')