增加私聊置顶和按设备统计未读
This commit is contained in:
1 parent
7e165ba9b4
commit
ec2679b991
19 files changed
+312
-52
No files matched your search
+42
-10
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
@@ -99,6 +99,11 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
assert.equal(await web.getByRole('button', { name: '发送', exact: true }).count(), 0)
|
||||
await trust(web, desktopCode)
|
||||
await trust(windows, webCode)
|
||||
const pin = web.getByRole('button', { name: '会话置顶', exact: true })
|
||||
await pin.click()
|
||||
await conversation.getByText('已置顶', { exact: true }).waitFor()
|
||||
assert.equal(await pin.getAttribute('aria-pressed'), 'true')
|
||||
assert.equal(await windows.getByRole('button', { name: '会话置顶' }).getAttribute('aria-pressed'), 'false')
|
||||
|
||||
const text = 'Web 到 Windows ' + randomBytes(8).toString('hex')
|
||||
const packets = []
|
||||
@@ -297,6 +302,12 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
await web.waitForURL(webChat)
|
||||
await enable(web)
|
||||
await web.getByText(webNoticeText, { exact: true }).waitFor()
|
||||
await conversation.getByText('已置顶', { exact: true }).waitFor()
|
||||
await pin.click()
|
||||
await conversation.getByText('已置顶', { exact: true }).waitFor({ state: 'detached' })
|
||||
await conversation.locator('.unread-badge').waitFor()
|
||||
await web.evaluate(() => { document.hasFocus = () => true; window.dispatchEvent(new Event('focus')) })
|
||||
await conversation.locator('.unread-badge').waitFor({ state: 'detached' })
|
||||
|
||||
await muteToggle(web).click()
|
||||
await web.getByRole('button', { name: '消息免打扰', pressed: true }).waitFor()
|
||||
@@ -306,9 +317,16 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
await web.getByRole('button', { name: '消息免打扰', pressed: true }).waitFor()
|
||||
const mutedText = '免打扰仍能同步 ' + randomBytes(8).toString('hex')
|
||||
await send(windows, mutedText)
|
||||
await work(web).getByRole('link', { name: '消息', exact: true }).click()
|
||||
const badge = conversation.getByRole('link', { name: new RegExp(b) }).locator('.unread-badge.muted')
|
||||
await badge.waitFor()
|
||||
assert.equal(await badge.locator('.unread-label').textContent(), '1 条未读')
|
||||
await work(web).getByRole('link', { name: '联系人' }).click()
|
||||
await web.getByRole('button', { name: new RegExp(b) }).click()
|
||||
await web.getByRole('link', { name: '打开加密聊天' }).click()
|
||||
await enable(web)
|
||||
await web.getByText(mutedText, { exact: true }).waitFor()
|
||||
await badge.waitFor({ state: 'detached' })
|
||||
assert.equal(await privateNotice(web).count(), 0)
|
||||
assert.equal(await web.evaluate(() => window.privateNotificationLog.length), 1)
|
||||
|
||||
|
||||
Reference in new issue
Block a user