2026-05-01 21:27:39 +08:00
|
|
|
import test from 'node:test'
|
|
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
import { buildDbName, sortConversations, sortMessages } from '../src/runtime'
|
|
|
|
|
|
|
|
|
|
test('buildDbName derives a stable per-user database name', () => {
|
|
|
|
|
assert.equal(buildDbName('ak_demo_chat', 'demo_alice'), 'xuqm_im_ak_demo_chat_demo_alice')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('sortConversations keeps pinned conversations first and then sorts by time', () => {
|
|
|
|
|
const conversations = sortConversations([
|
|
|
|
|
{ targetId: 'c', chatType: 'SINGLE', lastMsgTime: 100, unreadCount: 0, isMuted: false, isPinned: false },
|
|
|
|
|
{ targetId: 'a', chatType: 'SINGLE', lastMsgTime: 80, unreadCount: 0, isMuted: false, isPinned: true },
|
|
|
|
|
{ targetId: 'b', chatType: 'GROUP', lastMsgTime: 120, unreadCount: 0, isMuted: false, isPinned: false },
|
|
|
|
|
{ targetId: 'd', chatType: 'GROUP', lastMsgTime: 120, unreadCount: 0, isMuted: false, isPinned: false },
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
conversations.map((item) => `${item.chatType}:${item.targetId}`),
|
|
|
|
|
['SINGLE:a', 'GROUP:b', 'GROUP:d', 'SINGLE:c'],
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('sortMessages deduplicates by message id and sorts newest first', () => {
|
|
|
|
|
const messages = sortMessages([
|
2026-05-07 19:39:41 +08:00
|
|
|
{ id: 'm2', appKey: 'ak', fromUserId: 'u1', toId: 'peer', chatType: 'SINGLE', msgType: 'TEXT', content: 'old', status: 'SENT', createdAt: 100 },
|
|
|
|
|
{ id: 'm1', appKey: 'ak', fromUserId: 'u1', toId: 'peer', chatType: 'SINGLE', msgType: 'TEXT', content: 'latest', status: 'SENT', createdAt: 200 },
|
|
|
|
|
{ id: 'm1', appKey: 'ak', fromUserId: 'u1', toId: 'peer', chatType: 'SINGLE', msgType: 'TEXT', content: 'duplicate', status: 'READ', createdAt: 180 },
|
2026-05-01 21:27:39 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
messages.map((item) => `${item.id}:${item.createdAt}:${item.content}`),
|
|
|
|
|
['m1:200:latest', 'm2:100:old'],
|
|
|
|
|
)
|
|
|
|
|
})
|