Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): evict the oldest chat state first after a preload
Yudhi Armyndharis0916c3c0cf4e
diff --git a/src/engine/adapters/baileys-chat-state-store.service.spec.ts b/src/engine/adapters/baileys-chat-state-store.service.spec.tsindex b8f27203f..7bce53c26 100644--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts@@ -17,9 +17,13 @@ function makeRepo(initial: Partial<ChatState>[] = []) {}const repo = {rows,- find: jest.fn((opts?: { where?: { sessionId: string } }) =>- Promise.resolve([...rows.values()].filter(r => !opts?.where || r.sessionId === opts.where.sessionId)),- ),+ // Honors the DESC order and the take the service asks for; the sort is stable for equal stamps.+ find: jest.fn((opts?: { where?: { sessionId: string }; order?: { updatedAt?: 'DESC' }; take?: number }) => {+ const found = [...rows.values()].filter(r => !opts?.where || r.sessionId === opts.where.sessionId);+ const at = (r: ChatState) => r.updatedAt?.getTime() ?? 0;+ if (opts?.order?.updatedAt === 'DESC') found.sort((a, b) => at(b) - at(a));+ return Promise.resolve(opts?.take ? found.slice(0, opts.take) : found);+ }),findOne: jest.fn(({ where }: { where: { sessionId: string; chatId: string } }) =>Promise.resolve(rows.get(KEY(where.sessionId, where.chatId))),),@@ -99,6 +103,26 @@ describe('ChatStateStoreService', () => {expect(svc.get('s', 'c')).toBeDefined();});+ describe('keeps the most recently changed rows when a preload fills the cap', () => {+ const rows = (): Partial<ChatState>[] => [+ { sessionId: 's', chatId: 'old', pinned: true, updatedAt: new Date(1000) },+ { sessionId: 's', chatId: 'mid', pinned: true, updatedAt: new Date(2000) },+ { sessionId: 's', chatId: 'new', pinned: true, updatedAt: new Date(3000) },+ ];++ it.each([+ ['reload', (svc: ChatStateStoreService) => svc.reload()],+ ['refreshSession', (svc: ChatStateStoreService) => svc.refreshSession('s')],+ ])('evicts the oldest preloaded row first after %s', async (_name, load) => {+ process.env[ENV] = '2';+ const svc = svcWith(makeRepo(rows()));+ await load(svc);+ await svc.remember('s', 'fresh', { pinned: true });+ expect(svc.get('s', 'new')).toEqual(expect.objectContaining({ pinned: true }));+ expect(svc.get('s', 'mid')).toBeUndefined();+ });+ });+it('preserves persisted siblings when a partial patch lands on a cache-missed chat', async () => {// A muted + archived chat whose row is persisted but NOT in the in-memory cache (evicted, or an// old row outside the newest-maxEntries reload window). A partial `chats.update` carrying onlydiff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.tsindex d3bcd4143..c24d23a26 100644--- a/src/engine/adapters/baileys-chat-state-store.service.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.ts@@ -80,7 +80,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {});this.states.clear();this.absent.clear();- for (const row of rows) {+ // Oldest first, so the newest row ends at the most-recent end of the LRU rather than the first+ // one evicted (the query is DESC only so `take` keeps the newest rows).+ for (const row of [...rows].reverse()) {this.index(this.key(row.sessionId, row.chatId), {muteEndTime: row.muteEndTime,archived: row.archived,@@ -227,7 +229,8 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {for (const k of [...this.states.keys()]) {if (k.startsWith(prefix)) this.states.delete(k);}- for (const row of rows) {+ for (const row of [...rows].reverse()) {+ // Oldest first, as in reload.this.index(this.key(sessionId, row.chatId), {muteEndTime: row.muteEndTime,archived: row.archived,