Skip to content

Commit 2f5222e

Browse files
author
Peter Steinberger
committed
fix(whatsapp): preserve terminal retry exhaustion lifecycle
1 parent 96b4258 commit 2f5222e

2 files changed

Lines changed: 90 additions & 26 deletions

File tree

‎extensions/whatsapp/src/auto-reply/monitor-state.test.ts‎

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ describe("createWebChannelStatusController", () => {
155155
{
156156
healthState: "logged-out",
157157
statusCode: 401,
158+
error: "WhatsApp session logged out",
159+
reconnectAttempts: 0,
158160
lifecycle: "blocked",
159161
finalHealthState: "logged-out",
160162
finalLifecycle: "blocked",
@@ -163,14 +165,28 @@ describe("createWebChannelStatusController", () => {
163165
{
164166
healthState: "conflict",
165167
statusCode: 440,
168+
error: "WhatsApp session conflict",
169+
reconnectAttempts: 0,
166170
lifecycle: "blocked",
167171
finalHealthState: "conflict",
168172
finalLifecycle: "blocked",
169173
terminalDisconnect: true,
170174
},
175+
{
176+
healthState: "stopped",
177+
statusCode: 408,
178+
error: "WhatsApp reconnect attempts exhausted after a timeout",
179+
reconnectAttempts: 12,
180+
lifecycle: "blocked",
181+
finalHealthState: "stopped",
182+
finalLifecycle: "blocked",
183+
terminalDisconnect: true,
184+
},
171185
{
172186
healthState: "reconnecting",
173187
statusCode: 408,
188+
error: "WhatsApp connection timed out",
189+
reconnectAttempts: 1,
174190
lifecycle: "recovering",
175191
finalHealthState: "stopped",
176192
finalLifecycle: "stopped",
@@ -181,6 +197,8 @@ describe("createWebChannelStatusController", () => {
181197
({
182198
healthState,
183199
statusCode,
200+
error,
201+
reconnectAttempts,
184202
lifecycle,
185203
finalHealthState,
186204
finalLifecycle,
@@ -193,41 +211,80 @@ describe("createWebChannelStatusController", () => {
193211
controller.noteClose({
194212
at: 2000,
195213
statusCode,
196-
error: healthState,
197-
reconnectAttempts: healthState === "reconnecting" ? 1 : 0,
214+
error,
215+
reconnectAttempts,
198216
healthState,
199217
});
200218
expect(patches.at(-1)!.healthState).toBe(healthState);
201219
expect(patches.at(-1)!.lifecycle).toBe(lifecycle);
202220

221+
const stoppedStatus = {
222+
healthState: finalHealthState,
223+
lifecycle: finalLifecycle,
224+
terminalDisconnect,
225+
lastError: error,
226+
reconnectAttempts,
227+
lastDisconnect: {
228+
at: 2000,
229+
status: statusCode,
230+
error,
231+
},
232+
};
233+
203234
controller.markStopped(2100);
235+
expect(patches.at(-1)).toMatchObject(stoppedStatus);
204236

205-
expect(patches.at(-1)!.healthState).toBe(finalHealthState);
206-
expect(patches.at(-1)!.lifecycle).toBe(finalLifecycle);
207-
expect(patches.at(-1)!.terminalDisconnect).toBe(terminalDisconnect);
237+
controller.markStopped(2200);
238+
expect(patches.at(-1)).toMatchObject(stoppedStatus);
208239
},
209240
);
210241

211-
it("clears terminalDisconnect on noteConnected after a terminal stop", () => {
212-
const patches: Record<string, unknown>[] = [];
213-
const controller = createWebChannelStatusController((s) => patches.push({ ...s }));
242+
it.each([
243+
{ healthState: "logged-out", statusCode: 401, reconnectAttempts: 0 },
244+
{ healthState: "conflict", statusCode: 440, reconnectAttempts: 0 },
245+
{ healthState: "stopped", statusCode: 408, reconnectAttempts: 12 },
246+
] as const)(
247+
"clears terminalDisconnect on reconnect after a $healthState stop",
248+
({ healthState, statusCode, reconnectAttempts }) => {
249+
const patches: Record<string, unknown>[] = [];
250+
const controller = createWebChannelStatusController((s) => patches.push({ ...s }));
214251

215-
controller.noteConnected(1000);
216-
controller.noteClose({
217-
at: 2000,
218-
statusCode: 401,
219-
error: "logged out",
220-
reconnectAttempts: 0,
221-
healthState: "logged-out",
222-
});
223-
controller.markStopped(2100);
224-
expect(patches.at(-1)!.terminalDisconnect).toBe(true);
252+
controller.noteConnected(1000);
253+
controller.noteClose({
254+
at: 2000,
255+
statusCode,
256+
error: healthState,
257+
reconnectAttempts,
258+
healthState,
259+
});
260+
controller.markStopped(2100);
261+
expect(patches.at(-1)!.terminalDisconnect).toBe(true);
262+
expect(patches.at(-1)!.lifecycle).toBe("blocked");
263+
264+
controller.markStopped(2200);
265+
expect(patches.at(-1)!.terminalDisconnect).toBe(true);
266+
expect(patches.at(-1)!.lifecycle).toBe("blocked");
267+
268+
controller.noteConnected(3000);
269+
expect(patches.at(-1)!.terminalDisconnect).toBeUndefined();
270+
expect(patches.at(-1)!.healthState).toBe("healthy");
271+
expect(patches.at(-1)!.lifecycle).toBe("ready");
272+
273+
controller.markStopped(3100);
274+
expect(patches.at(-1)).toMatchObject({
275+
healthState: "stopped",
276+
lifecycle: "stopped",
277+
terminalDisconnect: false,
278+
});
225279

226-
controller.noteConnected(3000);
227-
expect(patches.at(-1)!.terminalDisconnect).toBeUndefined();
228-
expect(patches.at(-1)!.healthState).toBe("healthy");
229-
expect(patches.at(-1)!.lifecycle).toBe("ready");
230-
});
280+
controller.markStopped(3200);
281+
expect(patches.at(-1)).toMatchObject({
282+
healthState: "stopped",
283+
lifecycle: "stopped",
284+
terminalDisconnect: false,
285+
});
286+
},
287+
);
231288

232289
it("publishes stopped lifecycle without changing the shipped health label", () => {
233290
const patches: Record<string, unknown>[] = [];
@@ -241,6 +298,14 @@ describe("createWebChannelStatusController", () => {
241298
connected: false,
242299
healthState: "stopped",
243300
lifecycle: "stopped",
301+
terminalDisconnect: false,
302+
});
303+
304+
controller.markStopped(3000);
305+
expect(patches.at(-1)).toMatchObject({
306+
healthState: "stopped",
307+
lifecycle: "stopped",
308+
terminalDisconnect: false,
244309
});
245310
});
246311
});

‎extensions/whatsapp/src/auto-reply/monitor-state.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const LIFECYCLE_BY_HEALTH_STATE = {
1212
reconnecting: "recovering",
1313
conflict: "blocked",
1414
"logged-out": "blocked",
15-
stopped: "stopped",
15+
stopped: "blocked", // Retry exhaustion is terminal; manual stops bypass this mapping.
1616
} satisfies Record<WebChannelHealthState, NonNullable<WebChannelStatus["lifecycle"]>>;
1717

1818
function cloneStatus(status: WebChannelStatus): WebChannelStatus {
@@ -136,8 +136,7 @@ export function createWebChannelStatusController(statusSink?: (status: WebChanne
136136
status.running = false;
137137
status.connected = false;
138138
status.lastEventAt = at;
139-
status.terminalDisconnect =
140-
status.healthState === "logged-out" || status.healthState === "conflict";
139+
status.terminalDisconnect = status.lifecycle === "blocked";
141140
if (!isTerminalHealthState(status.healthState)) {
142141
status.healthState = "stopped";
143142
status.lifecycle = "stopped";

0 commit comments

Comments
 (0)