Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): report a dead page when setting own presence

Yudhi Armyndharis1fcf8a23df53
diff --git a/src/engine/adapters/own-presence.spec.ts b/src/engine/adapters/own-presence.spec.ts
index a54c0f623..e7991f220 100644
--- a/src/engine/adapters/own-presence.spec.ts
+++ b/src/engine/adapters/own-presence.spec.ts
@@ -26,6 +26,7 @@ describe('WwebjsChats.setOnlinePresence', () => {
const host = {
ensureReady: jest.fn(),
getClient: () => client as unknown as Client,
+ isPageTransportError: () => false,
logger,
} as unknown as WwebjsEngineHost;
return { chats: new WwebjsChats(host, {} as unknown as WwebjsMessaging), client };
diff --git a/src/engine/adapters/wwebjs-chat-transport-death.spec.ts b/src/engine/adapters/wwebjs-chat-transport-death.spec.ts
index 2db08c80c..d4f584eb7 100644
--- a/src/engine/adapters/wwebjs-chat-transport-death.spec.ts
+++ b/src/engine/adapters/wwebjs-chat-transport-death.spec.ts
@@ -78,9 +78,10 @@ describe('boolean chat operations distinguish a dead page from a refusal', () =>
/**
* Mute and pin resolve the chat first and then write. The write leg is where a renderer that dies
* mid-call rejects, so it needs the same split: a 503 with the death reported, while any other page
- * rejection still reaches the caller unchanged.
+ * rejection still reaches the caller unchanged. Setting the account's own presence is the same kind
+ * of write and makes the same split.
*/
-describe('mute and pin classify a dead page on the write leg', () => {
+describe('mute, pin and presence classify a dead page on the write leg', () => {
const transportError = new Error('Protocol error (Runtime.callFunctionOn): Target closed');
function makeChats(reject: unknown): { chats: WwebjsChats; reportIfPageTransportError: jest.Mock } {
@@ -90,6 +91,8 @@ describe('mute and pin classify a dead page on the write leg', () => {
unmuteChat: jest.fn().mockRejectedValue(reject),
pinChat: jest.fn().mockRejectedValue(reject),
unpinChat: jest.fn().mockRejectedValue(reject),
+ sendPresenceAvailable: jest.fn().mockRejectedValue(reject),
+ sendPresenceUnavailable: jest.fn().mockRejectedValue(reject),
};
const reportIfPageTransportError = jest.fn();
const host = {
@@ -107,6 +110,8 @@ describe('mute and pin classify a dead page on the write leg', () => {
['unmuteChat', chats => chats.muteChat('[email protected]', null)],
['pinChat', chats => chats.pinChat('[email protected]', true)],
['unpinChat', chats => chats.pinChat('[email protected]', false)],
+ ['setOnlinePresence(true)', chats => chats.setOnlinePresence(true)],
+ ['setOnlinePresence(false)', chats => chats.setOnlinePresence(false)],
];
it.each(WRITES)('%s reports a dead page as a transport failure', async (_op, call) => {
@@ -116,6 +121,19 @@ describe('mute and pin classify a dead page on the write leg', () => {
expect(reportIfPageTransportError).toHaveBeenCalledWith(transportError, expect.any(String));
});
+ it.each(WRITES)(
+ '%s answers a protocol timeout as a transport failure without reporting a death',
+ async (_op, call) => {
+ const timeout = new Error(
+ "Runtime.callFunctionOn timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.",
+ );
+ const { chats, reportIfPageTransportError } = makeChats(timeout);
+
+ await expect(call(chats)).rejects.toThrow(EngineTransportError);
+ expect(reportIfPageTransportError).not.toHaveBeenCalled();
+ },
+ );
+
it.each(WRITES)('%s rethrows any other page rejection unchanged', async (_op, call) => {
const refusal = new Error('Evaluation failed: TypeError: renamed internal');
const { chats, reportIfPageTransportError } = makeChats(refusal);
diff --git a/src/engine/adapters/wwebjs-chats.ts b/src/engine/adapters/wwebjs-chats.ts
index 540a35972..6458eebe1 100644
--- a/src/engine/adapters/wwebjs-chats.ts
+++ b/src/engine/adapters/wwebjs-chats.ts
@@ -250,9 +250,9 @@ export class WwebjsChats {
}
/**
- * The write leg of mute and pin. A dead page or an expired protocol timeout is the 503 both routes
- * document (mute, unmute, pin and unpin all converge when repeated) and a dead page is reported as
- * the early death signal. Any other rejection is rethrown untouched, for the reason above.
+ * The write leg of mute, pin and the account's own presence. A dead page or an expired protocol
+ * timeout is a 503 (mute, unmute, pin, unpin and a presence change all converge when repeated) and
+ * a dead page is reported as the early death signal. Any other rejection is rethrown untouched, for the reason above.
*/
private async chatWrite<T>(context: string, op: () => Promise<T>): Promise<T> {
try {
@@ -363,11 +363,9 @@ export class WwebjsChats {
*/
async setOnlinePresence(available: boolean): Promise<void> {
this.host.ensureReady();
- if (available) {
- await this.client().sendPresenceAvailable();
- } else {
- await this.client().sendPresenceUnavailable();
- }
+ await this.chatWrite('setOnlinePresence', () =>
+ available ? this.client().sendPresenceAvailable() : this.client().sendPresenceUnavailable(),
+ );
}
async sendChatState(chatId: string, state: ChatState): Promise<void> {