import assert from 'node:assert/strict' import { generateKeyPairSync, randomBytes, randomUUID, sign } from 'node:crypto' import { once } from 'node:events' import test from 'node:test' import { WebSocket } from 'ws' const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080' function call(path, method = 'GET', token, body) { return fetch(base + '/api/v1' + path, { method, headers: { ...(token && { Authorization: 'Bearer ' + token }), ...(body && { 'Content-Type': 'application/json' }), }, body: body && JSON.stringify(body), signal: AbortSignal.timeout(10_000), }) } async function json(path, method, token, body, status = 200) { const response = await call(path, method, token, body) assert.equal(response.status, status, await response.clone().text()) return response.json() } async function register() { const { challenge } = await json('/auth/challenge', 'POST') const { publicKey, privateKey } = generateKeyPairSync('ed25519') return json('/auth/register', 'POST', null, { username: 'qa_' + randomBytes(6).toString('hex'), password: randomBytes(24).toString('hex'), 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) } async function contact(actor, target, action) { const reply = await call('/contacts/' + target.user.id, 'POST', actor.token, { action }) assert.equal(reply.status, 204, await reply.text()) } // 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') } test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => { const sender = await register() const recipient = await register() const other = await register() const payload = { messageId: randomUUID(), recipientDevice: recipient.device.id, ciphertext: frame() } await json('/inbox', 'POST', sender.token, payload, 404) await contact(sender, recipient, 'request') await contact(recipient, sender, 'accept') await json('/inbox', 'POST', sender.token, { ...payload, ciphertext: Buffer.from('plaintext').toString('base64') }, 400) await json('/inbox', 'POST', sender.token, { ...payload, text: 'plaintext' }, 400) const first = await json('/inbox', 'POST', sender.token, payload) assert.deepEqual(await json('/inbox', 'POST', sender.token, payload), first) await json('/inbox', 'POST', sender.token, { ...payload, ciphertext: frame() }, 409) const [item] = await json('/inbox', 'GET', recipient.token) assert.equal(item.sequence, first.sequence) assert.equal(item.ciphertext, payload.ciphertext) assert.equal(item.senderDevice, sender.device.id) 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), []) 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() } })