fix(web): prevent negative unread counts
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { createJiti } from 'jiti';
|
||||
|
||||
globalThis.__APP_VERSION__ = 'test';
|
||||
globalThis.window = { addEventListener() {}, removeEventListener() {} };
|
||||
|
||||
const jiti = createJiti(import.meta.url, { alias: { '@': fileURLToPath(new URL('../src', import.meta.url)) } });
|
||||
const { getConfirmedRead, markConfirmedRead, repairContactReadState } = await jiti.import('../src/lib/tinode/topics.ts');
|
||||
const LOCAL_SEQID = 0xfffffff;
|
||||
|
||||
test('optimistic local sequence never advances the confirmed read cursor', () => {
|
||||
markConfirmedRead('unread-cursor-test', 260);
|
||||
markConfirmedRead('unread-cursor-test', LOCAL_SEQID);
|
||||
assert.equal(getConfirmedRead('unread-cursor-test'), 260);
|
||||
markConfirmedRead('unread-cursor-test', 267);
|
||||
assert.equal(getConfirmedRead('unread-cursor-test'), 267);
|
||||
});
|
||||
|
||||
test('repairs existing negative unread counts caused by local sequence IDs', () => {
|
||||
for (const seq of [260, 267]) {
|
||||
const contact = { seq, read: LOCAL_SEQID, recv: LOCAL_SEQID, unread: seq - LOCAL_SEQID };
|
||||
assert.equal(contact.unread, seq === 260 ? -268435195 : -268435188);
|
||||
repairContactReadState(contact);
|
||||
assert.deepEqual(contact, { seq, read: seq, recv: seq, unread: 0 });
|
||||
}
|
||||
});
|
||||
|
||||
test('keeps valid read and unread state unchanged', () => {
|
||||
const contact = { seq: 267, read: 260, recv: 265, unread: 7 };
|
||||
repairContactReadState(contact);
|
||||
assert.deepEqual(contact, { seq: 267, read: 260, recv: 265, unread: 7 });
|
||||
});
|
||||
Reference in New Issue
Block a user