增加私聊置顶和按设备统计未读

This commit is contained in:
xhgit committed 2026-11-15 22:35:45 +08:00
1 parent 7e165ba9b4
commit ec2679b991
19 files changed
+312 -52

No files matched your search

+42 -10
View File
@@ -24,17 +24,21 @@ async function json(path, method, token, body, status = 200) {
return response.json()
}
async function register({ publicKey, privateKey } = generateKeyPairSync('ed25519')) {
async function authenticate(mode, username, password, { publicKey, privateKey } = generateKeyPairSync('ed25519')) {
const { challenge } = await json('/auth/challenge', 'POST')
return json('/auth/register', 'POST', null, {
username: 'qa_' + randomBytes(6).toString('hex'),
password: randomBytes(24).toString('hex'),
return json('/auth/' + mode, 'POST', null, {
username,
password,
deviceName: '投递验收设备',
platform: 'web',
challenge,
publicKey: publicKey.export({ format: 'jwk' }).x,
signature: sign(null, Buffer.from('XHChat device authentication v1\n' + challenge), privateKey).toString('base64url'),
}, 201)
}, mode === 'register' ? 201 : 200)
}
function register(keys, password = randomBytes(24).toString('hex')) {
return authenticate('register', 'qa_' + randomBytes(6).toString('hex'), password, keys)
}
async function contact(actor, target, action) {
@@ -335,14 +339,21 @@ test('群密文和实时提示只投递到名单设备,移除或撤销阻断
test('设备密文投递、幂等、离线补拉、免打扰与撤销', { timeout: 25000 }, async (t) => {
const sender = await register()
const recipient = await register()
const recipientPassword = randomBytes(24).toString('hex')
const recipient = await register(undefined, recipientPassword)
const other = await register()
const payload = { messageId: randomUUID(), recipientDevice: recipient.device.id, ciphertext: frame() }
const preferences = '/contacts/' + sender.user.id
const unread = (actor) => json('/inbox/unread', 'GET', actor.token)
const markSeen = (actor, sequence, senderDevice = sender.device.id) =>
call('/inbox/seen', 'POST', actor.token, { senderDevice, sequence })
await json('/inbox', 'POST', sender.token, payload, 404)
await json(preferences, 'POST', recipient.token, { action: 'mute' }, 409)
await json(preferences, 'POST', recipient.token, { action: 'pin' }, 409)
await contact(sender, recipient, 'request')
await contact(recipient, sender, 'accept')
const sibling = await authenticate('login', recipient.user.username, recipientPassword)
assert.notEqual(sibling.device.id, recipient.device.id)
await json('/inbox', 'POST', sender.token, { ...payload, ciphertext: Buffer.from('plaintext').toString('base64') }, 400)
await json('/inbox', 'POST', sender.token, { ...payload, text: 'plaintext' }, 400)
@@ -356,6 +367,13 @@ test('设备密文投递、幂等、离线补拉、免打扰与撤销', { timeou
assert.deepEqual(await json('/inbox?after=' + first.sequence, 'GET', recipient.token), [])
assert.deepEqual(await json('/inbox', 'GET', sender.token), [])
assert.deepEqual(await json('/inbox', 'GET', other.token), [])
assert.deepEqual(await unread(recipient), { [sender.user.id]: 1 })
assert.deepEqual(await unread(other), {})
assert.deepEqual(await unread(sibling), {})
await json('/inbox', 'POST', sender.token, { ...payload, recipientDevice: sibling.device.id })
assert.deepEqual(await unread(sibling), { [sender.user.id]: 1 })
assert.equal((await markSeen(other, first.sequence)).status, 404)
assert.equal((await markSeen(recipient, first.sequence, other.device.id)).status, 404)
const [socket, senderSocket] = await Promise.all([recipient, sender].map((actor) => watchRealtime(actor, t)))
const senderEvents = []
@@ -378,27 +396,41 @@ test('设备密文投递、幂等、离线补拉、免打扰与撤销', { timeou
await deliver(syncOnly, { ...newPayload(), silent: true })
await expectEvent(contactsChanged, () => contact(recipient, sender, 'mute'))
await expectEvent(contactsChanged, () => contact(recipient, sender, 'pin'))
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,
id: sender.user.id, username: sender.user.username, state: 'friend', alias: '朋友', category: '测试', muted: true, pinned: true,
})
assert.equal((await json('/contacts', 'GET', sender.token))[0].muted, false)
const senderView = (await json('/contacts', 'GET', sender.token))[0]
assert.deepEqual([senderView.muted, senderView.pinned], [false, false])
const siblingView = (await json('/contacts', 'GET', sibling.token))[0]
assert.deepEqual([siblingView.muted, siblingView.pinned], [true, true])
const muted = newPayload()
await deliver(syncOnly, muted)
const mutedResult = await deliver(syncOnly, muted)
assert.deepEqual(await unread(recipient), { [sender.user.id]: 3 })
assert.equal((await markSeen(recipient, result.sequence)).status, 204)
assert.equal((await markSeen(recipient, first.sequence)).status, 204)
assert.deepEqual(await unread(recipient), { [sender.user.id]: 1 })
assert.equal((await markSeen(recipient, mutedResult.sequence)).status, 204)
assert.deepEqual(await unread(recipient), {})
assert.deepEqual(await unread(sibling), { [sender.user.id]: 1 })
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, '朋友', '测试'])
assert.deepEqual([unmuted.muted, unmuted.pinned, unmuted.alias, unmuted.category], [false, true, '朋友', '测试'])
await expectEvent(contactsChanged, () => contact(recipient, sender, 'unpin'))
assert.equal((await json('/contacts', 'GET', recipient.token))[0].pinned, 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)
assert.equal((await markSeen(recipient, mutedResult.sequence)).status, 401)
await json('/inbox', 'POST', sender.token, newPayload(), 404)
})