接入私聊消息通知和联系人免打扰

This commit is contained in:
xhgit committed 2026-11-15 22:27:58 +08:00
1 parent 5c882ac855
commit 7e165ba9b4
23 files changed
+286 -97

No files matched your search

+47 -19
View File
@@ -52,8 +52,8 @@ async function watchRealtime(actor, t) {
}
// Opaque fixture with the MLS header. Crypto interoperability is tested separately in crypto/.
function frame() {
return Buffer.concat([Buffer.from([0, 1, 0, 2]), randomBytes(96)]).toString('base64')
function frame(kind = 2) {
return Buffer.concat([Buffer.from([0, 1, 0, kind]), randomBytes(96)]).toString('base64')
}
test('群组邀请、授权、成员移除、版本变更与实时提示', { timeout: 25_000 }, async (t) => {
@@ -333,12 +333,14 @@ test('群密文和实时提示只投递到名单设备,移除或撤销阻断
assert.deepEqual(outsiderEvents, [])
})
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
test('设备密文投递、幂等、离线补拉、免打扰与撤销', { timeout: 25000 }, async (t) => {
const sender = await register()
const recipient = await register()
const other = await register()
const payload = { messageId: randomUUID(), recipientDevice: recipient.device.id, ciphertext: frame() }
const preferences = '/contacts/' + sender.user.id
await json('/inbox', 'POST', sender.token, payload, 404)
await json(preferences, 'POST', recipient.token, { action: 'mute' }, 409)
await contact(sender, recipient, 'request')
await contact(recipient, sender, 'accept')
await json('/inbox', 'POST', sender.token, { ...payload, ciphertext: Buffer.from('plaintext').toString('base64') }, 400)
@@ -355,23 +357,49 @@ test('设备密文投递、幂等、离线补拉、实时提示和撤销', { tim
assert.deepEqual(await json('/inbox', 'GET', sender.token), [])
assert.deepEqual(await json('/inbox', 'GET', other.token), [])
const { ticket } = await json('/realtime/ticket', 'POST', recipient.token)
const ws = new WebSocket(base.replace(/^http/, 'ws') + '/api/v1/realtime', ['xhchat.v1', 'ticket.' + ticket], { origin: base })
try {
assert.equal(JSON.parse((await once(ws, 'message'))[0]).type, 'ready')
const changed = once(ws, 'message')
const second = { ...payload, messageId: randomUUID(), ciphertext: frame() }
await json('/inbox', 'POST', sender.token, second)
assert.equal(JSON.parse((await changed)[0]).type, 'inbox_changed')
const pending = await json('/inbox?after=' + first.sequence, 'GET', recipient.token)
assert.equal(pending.length, 1)
assert.equal(pending[0].ciphertext, second.ciphertext)
assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204)
await json('/inbox', 'GET', recipient.token, undefined, 401)
await json('/inbox', 'POST', sender.token, { ...payload, messageId: randomUUID() }, 404)
} finally {
ws.terminate()
const [socket, senderSocket] = await Promise.all([recipient, sender].map((actor) => watchRealtime(actor, t)))
const senderEvents = []
senderSocket.on('message', (data) => senderEvents.push(JSON.parse(data)))
async function expectEvent(type, act) {
const arrival = once(socket, 'message', { signal: AbortSignal.timeout(4000) })
const [[data], result] = await Promise.all([arrival, act()])
assert.deepEqual(JSON.parse(data), type)
return result
}
const syncOnly = { type: 'inbox_changed' }
const alert = { ...syncOnly, contactId: sender.user.id, notify: true }
const contactsChanged = { type: 'contacts_changed' }
const newPayload = (ciphertext = frame()) => ({ ...payload, messageId: randomUUID(), ciphertext })
const deliver = (expected, packet) => expectEvent(expected, () => json('/inbox', 'POST', sender.token, packet))
const second = newPayload()
const result = await deliver(alert, second)
assert.deepEqual(await deliver(syncOnly, second), result)
await deliver(syncOnly, newPayload(frame(3)))
await deliver(syncOnly, { ...newPayload(), silent: true })
await expectEvent(contactsChanged, () => contact(recipient, sender, 'mute'))
await expectEvent(contactsChanged, async () => {
const reply = await call(preferences, 'POST', recipient.token, { action: 'preferences', alias: '朋友', category: '测试' })
assert.equal(reply.status, 204, await reply.text())
})
assert.deepEqual((await json('/contacts', 'GET', recipient.token))[0], {
id: sender.user.id, username: sender.user.username, state: 'friend', alias: '朋友', category: '测试', muted: true,
})
assert.equal((await json('/contacts', 'GET', sender.token))[0].muted, false)
const muted = newPayload()
await deliver(syncOnly, muted)
const pending = await json('/inbox?after=' + first.sequence, 'GET', recipient.token)
assert.equal(pending.length, 4)
assert.equal(pending[0].ciphertext, second.ciphertext)
assert.equal(pending.at(-1).ciphertext, muted.ciphertext)
await expectEvent(contactsChanged, () => contact(recipient, sender, 'unmute'))
const unmuted = (await json('/contacts', 'GET', recipient.token))[0]
assert.deepEqual([unmuted.muted, unmuted.alias, unmuted.category], [false, '朋友', '测试'])
await deliver(alert, newPayload())
assert.deepEqual(senderEvents, [])
assert.equal((await call('/devices/' + recipient.device.id, 'DELETE', recipient.token)).status, 204)
await json('/inbox', 'GET', recipient.token, undefined, 401)
await json('/inbox', 'POST', sender.token, newPayload(), 404)
})
test('设备密钥包验证签名并只领取一次', { timeout: 25000 }, async () => {
+77
View File
@@ -123,6 +123,7 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
await windows.getByLabel('发送已读回执').check()
await sentLine.getByText('已读', { exact: true }).waitFor({ timeout: 15_000 })
assert.ok(receiptPackets.length > 0)
assert.ok(receiptPackets.every((item) => item.silent === true))
assert.ok(receiptPackets.every((item) => !JSON.stringify(item).includes(packet.messageId)))
const reply = 'Windows 到 Web ' + randomBytes(8).toString('hex')
await windows.getByRole('listitem').filter({ hasText: text }).getByRole('button', { name: '引用', exact: true }).click()
@@ -235,6 +236,82 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
} finally { db.close() }
})
assert.deepEqual(stored, [{ wrapped: true, encrypted: true, fields: ['iv', 'key', 'sealed'] }])
const systemToggle = (page) => page.getByRole('button', { name: '消息系统通知', exact: true })
const privateNotice = (page) => page.getByRole('link', { name: '有新的私聊消息', exact: true })
const muteToggle = (page) => page.getByRole('button', { name: '消息免打扰', exact: true })
await desktop.evaluate(({ Notification }) => {
const nativeShow = Notification.prototype.show
globalThis.privateNotificationLog = []
Notification.prototype.show = function () {
globalThis.privateNotificationLog.push({ title: this.title, body: this.body })
return nativeShow.call(this)
}
})
await systemToggle(windows).click()
await windows.getByText('已在本设备启用消息系统通知', { exact: true }).waitFor()
await work(windows).getByRole('link', { name: '设备管理' }).click()
const desktopNoticeText = 'Windows 后台私聊 ' + randomBytes(8).toString('hex')
await desktop.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].minimize())
try {
await send(web, desktopNoticeText)
const toast = await desktop.evaluate(async () => {
for (let attempt = 0; attempt < 30 && !globalThis.privateNotificationLog.length; attempt++) {
await new Promise((resolve) => setTimeout(resolve, 150))
}
return globalThis.privateNotificationLog[0]
})
assert.deepEqual(toast, { title: 'XHChat', body: '有新的私聊消息' })
} finally {
await desktop.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].restore())
}
await privateNotice(windows).click()
await windows.waitForURL(windowsChat)
await enable(windows)
await windows.getByText(desktopNoticeText, { exact: true }).waitFor()
assert.equal(await systemToggle(web).getAttribute('aria-pressed'), 'false')
await web.context().grantPermissions(['notifications'], { origin: base })
await web.evaluate(() => {
const BrowserNotification = window.Notification
window.privateNotificationLog = []
window.Notification = class extends BrowserNotification {
constructor(title, options) {
super(title, options)
window.privateNotificationLog.push({ title, body: options.body, tag: options.tag })
}
}
document.hasFocus = () => false
})
await systemToggle(web).click()
await web.getByText('已在本设备启用消息系统通知', { exact: true }).waitFor()
const webChat = web.url()
await work(web).getByRole('link', { name: '设备管理' }).click()
const webNoticeText = 'Edge 后台私聊 ' + randomBytes(8).toString('hex')
await send(windows, webNoticeText)
await privateNotice(web).waitFor()
await web.waitForFunction(() => window.privateNotificationLog.length === 1)
assert.deepEqual(await web.evaluate(() => window.privateNotificationLog[0]),
{ title: 'XHChat', body: '有新的私聊消息', tag: 'xhchat-private' })
await privateNotice(web).click()
await web.waitForURL(webChat)
await enable(web)
await web.getByText(webNoticeText, { exact: true }).waitFor()
await muteToggle(web).click()
await web.getByRole('button', { name: '消息免打扰', pressed: true }).waitFor()
assert.equal(await muteToggle(windows).getAttribute('aria-pressed'), 'false')
await work(web).getByRole('link', { name: '联系人' }).click()
await web.getByRole('button', { name: new RegExp(b) }).click()
await web.getByRole('button', { name: '消息免打扰', pressed: true }).waitFor()
const mutedText = '免打扰仍能同步 ' + randomBytes(8).toString('hex')
await send(windows, mutedText)
await web.getByRole('link', { name: '打开加密聊天' }).click()
await enable(web)
await web.getByText(mutedText, { exact: true }).waitFor()
assert.equal(await privateNotice(web).count(), 0)
assert.equal(await web.evaluate(() => window.privateNotificationLog.length), 1)
const link = base + '/healthz'
await send(web, link)
const webLink = web.getByRole('link', { name: link, exact: true })
+6 -6
View File
@@ -176,10 +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 })
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()
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()
@@ -691,10 +691,10 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
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 })
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 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())
@@ -709,8 +709,8 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
} finally {
await restoreWindow()
}
await windows.getByRole('button', { name: '群消息系统通知', pressed: true }).click()
await windows.getByText('已在本设备关闭群消息系统通知', { exact: true }).waitFor()
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 })