Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): drop a chat state the table refused
Yudhi Armyndharis4ce5cb01629a
diff --git a/src/engine/adapters/baileys-chat-state-persist-failure.spec.ts b/src/engine/adapters/baileys-chat-state-persist-failure.spec.tsnew file mode 100644index 000000000..ef279fc68--- /dev/null+++ b/src/engine/adapters/baileys-chat-state-persist-failure.spec.ts@@ -0,0 +1,71 @@+import { DataSource, Repository } from 'typeorm';+import { ChatState } from './baileys-chat-state.entity';+import { ChatStateStoreService } from './baileys-chat-state-store.service';++const SID = 'session-a';+const PHONE = '[email protected]';+const LID = '11111@lid';++describe('Chat state after a failed write', () => {+ let db: DataSource;+ let repo: Repository<ChatState>;+ let states: ChatStateStoreService;++ beforeEach(async () => {+ db = new DataSource({ type: 'better-sqlite3', database: ':memory:', entities: [ChatState], synchronize: true });+ await db.initialize();+ repo = db.getRepository(ChatState);+ states = new ChatStateStoreService(repo);+ });++ afterEach(async () => {+ jest.restoreAllMocks();+ await db.destroy();+ });++ const seed = (chatId: string) =>+ repo.upsert(+ { sessionId: SID, chatId, muteEndTime: -1, pinned: true, archived: false, observed: 'muteEndTime,pinned' },+ ['sessionId', 'chatId'],+ );+ const cold = async () => {+ const store = new ChatStateStoreService(repo);+ await store.refreshSession(SID);+ return store;+ };++ it('writes an identical fold again when the first write failed', async () => {+ await seed(LID);+ jest.spyOn(repo, 'upsert').mockRejectedValueOnce(new Error('SQLITE_BUSY'));+ await states.fold(SID, PHONE, [LID], { archived: true });+ expect(await repo.findOneBy({ sessionId: SID, chatId: LID })).not.toBeNull();++ await states.fold(SID, PHONE, [LID], { archived: true });++ expect((await cold()).get(SID, PHONE)).toMatchObject({ muteEndTime: -1, pinned: true, archived: true });+ expect(await repo.findOneBy({ sessionId: SID, chatId: LID })).toBeNull();+ });++ it('keeps the twin row while the repeated fold keeps failing', async () => {+ await seed(LID);+ jest+ .spyOn(repo, 'upsert')+ .mockRejectedValueOnce(new Error('SQLITE_BUSY'))+ .mockRejectedValueOnce(new Error('SQLITE_BUSY'));+ await states.fold(SID, PHONE, [LID], { archived: true });+ await states.fold(SID, PHONE, [LID], { archived: true });++ const store = await cold();+ expect(store.get(SID, LID)).toMatchObject({ muteEndTime: -1, pinned: true, archived: false });+ expect(store.get(SID, PHONE)).toBeUndefined();+ });++ it('writes an identical patch again when the first write failed', async () => {+ await seed(PHONE);+ jest.spyOn(repo, 'upsert').mockRejectedValueOnce(new Error('SQLITE_BUSY'));+ await states.remember(SID, PHONE, { archived: true });+ await states.remember(SID, PHONE, { archived: true });++ expect((await cold()).get(SID, PHONE)).toMatchObject({ muteEndTime: -1, pinned: true, archived: true });+ });+});diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.tsindex fc210a9c6..b9e45baef 100644--- a/src/engine/adapters/baileys-chat-state-store.service.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.ts@@ -486,7 +486,14 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {next.updatedAt = at.getTime();this.bumpWriteSeq(sessionId);this.index(k, next);- return this.persist(sessionId, chatId, next, at);+ const persisted = await this.persist(sessionId, chatId, next, at);+ if (!persisted) {+ // The value was never stored, so a later patch equal to it must not be read as a no-op, nor a fold+ // delete its twins on the strength of it: the next write reads the table again.+ this.states.delete(k);+ this.completeSessions.delete(sessionId);+ }+ return persisted;}private async persist(