接入群消息系统通知和本机开关

This commit is contained in:
xhgit committed 2026-11-15 22:11:23 +08:00
1 parent fef421bd4b
commit 5c882ac855
7 files changed
+179 -6

No files matched your search

+52
View File
@@ -133,6 +133,18 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
route.connectToServer()
})
await owner.context().grantPermissions(['microphone'], { origin: base })
await removed.context().grantPermissions(['notifications'], { origin: base })
await removed.addInitScript(() => {
const BrowserNotification = window.Notification
window.groupNotificationLog = []
window.Notification = class extends BrowserNotification {
constructor(title, options) {
super(title, options)
window.groupNotificationLog.push({ title, body: options.body, tag: options.tag })
}
}
document.hasFocus = () => false
})
await Promise.all([owner.goto(base), removed.goto(base)])
desktop = await _electron.launch({
executablePath: electron,
@@ -140,6 +152,14 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
env: { ...process.env, XHCHAT_SERVER_URL: base },
})
const windows = await desktop.firstWindow()
await desktop.evaluate(({ Notification }) => {
const show = Notification.prototype.show
globalThis.groupNotificationLog = []
Notification.prototype.show = function () {
globalThis.groupNotificationLog.push({ title: this.title, body: this.body })
return show.call(this)
}
})
pages.push(owner, windows, removed)
assert.equal(await windows.evaluate(async () => {
try { await window.xhchat.allowVoiceRecording(); return true } catch { return false }
@@ -156,6 +176,10 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await owner.getByRole('heading', { name: group }).waitFor()
await invite(owner, windows, names[1], group)
await invite(owner, removed, names[2], group)
const webToggle = removed.getByRole('button', { name: '群消息系统通知', exact: true })
assert.equal(await webToggle.getAttribute('aria-pressed'), 'false')
await webToggle.click()
await removed.getByText('已在本设备启用群消息系统通知', { exact: true }).waitFor()
const settings = (page, name) => page.getByRole('button', { name, exact: true })
await settings(removed, '群聊免打扰').click()
await removed.getByRole('button', { name: '群聊免打扰', pressed: true }).waitFor()
@@ -201,6 +225,7 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await owner.getByText('实时连接已建立', { exact: true }).waitFor()
await Promise.all([owner, windows].map((page) => chat(page).getByText(first, { exact: true }).waitFor({ timeout: 3500 })))
assert.equal(await removed.getByRole('link', { name: '有新的群消息' }).count(), 0)
assert.equal(await removed.evaluate(() => window.groupNotificationLog.length), 0)
await work(removed).getByRole('link', { name: '群组' }).click()
const unreadGroup = removed.getByRole('link', { name: new RegExp(group + '.*1 条未读') })
await unreadGroup.click()
@@ -242,6 +267,9 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
assert.equal(await send(windows, edited, '保存编辑'), epoch)
const notice = removed.getByRole('link', { name: '有新的群消息' })
await notice.waitFor({ timeout: 4500 })
await removed.waitForFunction(() => window.groupNotificationLog.length === 1)
assert.deepEqual(await removed.evaluate(() => window.groupNotificationLog[0]),
{ title: 'XHChat', body: '有新的群消息', tag: 'xhchat-group' })
const incomingEdit = chat(owner).getByRole('listitem').filter({ has: owner.getByText(edited, { exact: true }) })
await incomingEdit.getByText('已编辑', { exact: true }).waitFor()
await notice.click()
@@ -659,6 +687,30 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await successorAnnouncement.getByLabel('公告内容').fill('新群主公告')
await successorAnnouncement.getByRole('button', { name: '发布公告' }).click()
await successorAnnouncement.getByText('新群主公告', { exact: true }).waitFor()
await owner.getByRole('link', { name: new RegExp(group) }).click()
await chat(owner).getByRole('button', { name: '启用本机群聊' }).click()
await chat(owner).getByText('群加密已建立', { exact: true }).waitFor()
const windowsToggle = windows.getByRole('button', { name: '群消息系统通知', exact: true })
assert.equal(await windowsToggle.getAttribute('aria-pressed'), 'false')
await windowsToggle.click()
await windows.getByText('已在本设备启用群消息系统通知', { exact: true }).waitFor()
await desktop.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].minimize())
await send(owner, 'Windows 后台群消息 ' + id())
const restoreWindow = async () => desktop.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].restore())
try {
const toast = await desktop.evaluate(async () => {
for (let attempt = 0; attempt < 24 && !globalThis.groupNotificationLog.length; attempt++) {
await new Promise((resolve) => setTimeout(resolve, 150))
}
return globalThis.groupNotificationLog[0]
})
assert.deepEqual(toast, { title: 'XHChat', body: '有新的群消息' })
} finally {
await restoreWindow()
}
await windows.getByRole('button', { name: '群消息系统通知', pressed: true }).click()
await windows.getByText('已在本设备关闭群消息系统通知', { exact: true }).waitFor()
} catch (error) {
const status = await Promise.all(pages.map((page) => page.locator('[role=alert], .group-chat [role=status]').allTextContents()))
throw new Error(JSON.stringify(status), { cause: error })