@@ -5,7 +5,7 @@ import os from "node:os";
55import path from "node:path" ;
66import type { WAMessage } from "baileys" ;
77import { createChannelIngressQueueForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime" ;
8- import { describe , expect , it } from "vitest" ;
8+ import { describe , expect , it , vi } from "vitest" ;
99import {
1010 deserializeWhatsAppDurableInboundMessage ,
1111 serializeWhatsAppDurableInboundMessage ,
@@ -194,6 +194,43 @@ describe("createWhatsAppIngressMonitor", () => {
194194 } ) ;
195195 } ) ;
196196
197+ it ( "dispatches accepted pending records older than the legacy 30-day TTL" , async ( ) => {
198+ await withTempState ( async ( stateDir ) => {
199+ const thirtyOneDaysAgo = Date . now ( ) - 31 * 24 * 60 * 60 * 1_000 ;
200+ const queue = createChannelIngressQueueForTests < WhatsAppDurableInboundPayload > ( {
201+ channelId : "whatsapp" ,
202+ accountId : "acct" ,
203+ stateDir,
204+ now : ( ) => thirtyOneDaysAgo ,
205+ } ) ;
206+ await queue . enqueue ( eventId ( "msg-old" ) , payload ( "msg-old" ) , {
207+ laneKey : REMOTE_JID ,
208+ receivedAt : thirtyOneDaysAgo ,
209+ } ) ;
210+
211+ const dispatched : string [ ] = [ ] ;
212+ const monitor = createWhatsAppIngressMonitor ( {
213+ queue,
214+ pollIntervalMs : 10 ,
215+ dispatch : async ( admission ) => {
216+ const id = admission . message . key . id ;
217+ if ( ! id ) {
218+ throw new Error ( "expected transport id" ) ;
219+ }
220+ dispatched . push ( id ) ;
221+ return { kind : "completed" } ;
222+ } ,
223+ } ) ;
224+
225+ monitor . start ( ) ;
226+ await monitor . waitForIdle ( ) ;
227+ await monitor . stop ( ) ;
228+
229+ expect ( dispatched ) . toEqual ( [ "msg-old" ] ) ;
230+ expect ( await queue . listPending ( { limit : "all" } ) ) . toEqual ( [ ] ) ;
231+ } ) ;
232+ } ) ;
233+
197234 it ( "dispatches every accepted pending record past the legacy 450-entry cap" , async ( ) => {
198235 await withTempState ( async ( stateDir ) => {
199236 const queue = createChannelIngressQueueForTests < WhatsAppDurableInboundPayload > ( {
@@ -234,6 +271,34 @@ describe("createWhatsAppIngressMonitor", () => {
234271 expect ( await queue . listPending ( { limit : "all" } ) ) . toEqual ( [ ] ) ;
235272 } ) ;
236273 } ) ;
274+
275+ it ( "keeps completed and failed replay guards bounded" , async ( ) => {
276+ await withTempState ( async ( stateDir ) => {
277+ const queue = createChannelIngressQueueForTests < WhatsAppDurableInboundPayload > ( {
278+ channelId : "whatsapp" ,
279+ accountId : "acct" ,
280+ stateDir,
281+ } ) ;
282+ const prune = vi . spyOn ( queue , "prune" ) ;
283+ const monitor = createWhatsAppIngressMonitor ( {
284+ queue,
285+ pollIntervalMs : 10 ,
286+ dispatch : async ( ) => ( { kind : "completed" } ) ,
287+ } ) ;
288+
289+ monitor . start ( ) ;
290+ await monitor . waitForIdle ( ) ;
291+ await monitor . stop ( ) ;
292+
293+ expect ( prune ) . toHaveBeenCalledWith ( {
294+ completedTtlMs : 7 * 24 * 60 * 60 * 1_000 ,
295+ completedMaxEntries : 5_000 ,
296+ failedTtlMs : 30 * 24 * 60 * 60 * 1_000 ,
297+ failedMaxEntries : 450 ,
298+ now : expect . any ( Number ) ,
299+ } ) ;
300+ } ) ;
301+ } ) ;
237302} ) ;
238303
239304describe ( "WhatsApp durable message serialization" , ( ) => {
0 commit comments