接入群消息实时提醒和重连补拉

This commit is contained in:
xhgit committed 2026-11-14 14:59:24 +08:00
1 parent 5ace82b0d7
commit b2e04e8174
10 files changed
+122 -33

No files matched your search

+19 -1
View File
@@ -42,6 +42,15 @@ async function contact(actor, target, action) {
assert.equal(reply.status, 204, await reply.text())
}
async function watchRealtime(actor, t) {
const { ticket } = await json('/realtime/ticket', 'POST', actor.token)
const ws = new WebSocket(base.replace(/^http/, 'ws') + '/api/v1/realtime', ['xhchat.v1', 'ticket.' + ticket], { origin: base })
t.after(() => ws.terminate())
assert.deepEqual(JSON.parse((await once(ws, 'message', { signal: AbortSignal.timeout(4000) }))[0]),
{ type: 'ready', deviceId: actor.device.id })
return ws
}
// 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')
@@ -89,7 +98,7 @@ test('群组邀请、授权、成员移除与版本变更', { timeout: 25_000 },
await json(path, 'GET', owner.token, undefined, 404)
})
test('群密文只投递到当前名单中的设备,离开或撤销后阻断旧世代', { timeout: 25_000 }, async () => {
test('群密文和实时提示只投递到名单设备,移除或撤销阻断旧世代', { timeout: 25_000 }, async (t) => {
const enroll = async () => {
const keys = generateKeyPairSync('ed25519')
const actor = await register(keys)
@@ -145,7 +154,13 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
] }
await publish(owner, { ...second, devices: [second.devices[0], { device: outsider.device.id, wire: welcome }] }, 409)
await publish(member, { ...second, devices: [{ device: member.device.id }, { device: owner.device.id, wire: welcome }] }, 409)
const [ownerSocket, memberSocket, outsiderSocket] = await Promise.all([owner, member, outsider].map((actor) => watchRealtime(actor, t)))
const outsiderEvents = []
outsiderSocket.on('message', (data) => outsiderEvents.push(JSON.parse(data)))
const hint = { type: 'group_changed', groupId: group.id }
const epochHint = once(memberSocket, 'message', { signal: AbortSignal.timeout(4000) })
await publish(owner, second)
assert.deepEqual(JSON.parse((await epochHint)[0]), hint)
await publish(owner, second)
await publish(owner, { ...second, id: randomUUID() }, 409)
const epochs = await json(path + '/epochs', 'GET', member.token)
@@ -153,7 +168,9 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
roster: [owner.device.id, member.device.id].sort() }])
await json(path + '/messages', 'GET', outsider.token, undefined, 404)
const message = { id: randomUUID(), epoch: second.id, wire: frame() }
const messageHint = once(ownerSocket, 'message', { signal: AbortSignal.timeout(4000) })
const sequence = await json(path + '/messages', 'POST', member.token, message)
assert.deepEqual(JSON.parse((await messageHint)[0]), hint)
assert.deepEqual(await json(path + '/messages', 'POST', member.token, message), sequence)
const received = await json(path + '/messages', 'GET', member.token)
assert.deepEqual(received.map((item) => item.wire), [message.wire])
@@ -203,6 +220,7 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
assert.equal((await json(path + '/epochs', 'GET', owner.token)).every((epoch) => !epoch.current), true)
await change(owner, 'disband')
await json(attachment, 'GET', owner.token, undefined, 404)
assert.deepEqual(outsiderEvents, [])
})
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
+14 -2
View File
@@ -121,6 +121,13 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
const pages = []
try {
const [owner, removed] = await Promise.all([browser.newPage(), browser.newPage()])
let ownerSocket
let holdConnection = false
await owner.routeWebSocket('**/api/v1/realtime', (route) => {
if (holdConnection) { void route.close({ code: 1012 }); return }
ownerSocket = route
route.connectToServer()
})
await owner.context().grantPermissions(['microphone'], { origin: base })
await Promise.all([owner.goto(base), removed.goto(base)])
desktop = await _electron.launch({
@@ -163,8 +170,13 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await Promise.all(pages.map((page) => chat(page).getByText('群加密已建立', { exact: true }).waitFor()))
const first = 'Windows 群消息 ' + id()
await work(removed).getByRole('link', { name: '联系人' }).click()
holdConnection = true
await ownerSocket.close({ code: 1012 })
await owner.getByText('正在重连', { exact: true }).waitFor()
const epoch = await send(windows, first)
await Promise.all([owner, windows].map((page) => chat(page).getByText(first, { exact: true }).waitFor()))
holdConnection = false
await owner.getByText('实时连接已建立', { exact: true }).waitFor()
await Promise.all([owner, windows].map((page) => chat(page).getByText(first, { exact: true }).waitFor({ timeout: 3500 })))
await work(removed).getByRole('link', { name: '群组' }).click()
await removed.getByRole('link', { name: new RegExp(group) }).click()
await chat(removed).getByRole('button', { name: '启用本机群聊' }).click()
@@ -177,7 +189,7 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
const reply = '加密引用回复 ' + id()
assert.equal(await send(owner, reply), epoch)
const receivedReply = chat(windows).getByRole('listitem').filter({ hasText: reply })
await receivedReply.locator('blockquote').getByText(first, { exact: true }).waitFor()
await receivedReply.locator('blockquote').getByText(first, { exact: true }).waitFor({ timeout: 3500 })
await work(removed).getByRole('link', { name: '联系人' }).click()
const own = chat(windows).getByRole('listitem').filter({ has: windows.getByText(first, { exact: true }) }).first()
await own.getByRole('button', { name: '编辑', exact: true }).click()