@@ -37,7 +37,8 @@ vi.mock("../logging.js", () => ({
3737 } ) ) ,
3838} ) ) ;
3939
40- const { sendFailureNotificationAnnounce } = await import ( "./delivery.js" ) ;
40+ const { sendCronAnnouncePayloadStrict, sendFailureNotificationAnnounce } =
41+ await import ( "./delivery.js" ) ;
4142
4243type DeliveryRequest = {
4344 abortSignal ?: unknown ;
@@ -184,6 +185,39 @@ describe("sendFailureNotificationAnnounce", () => {
184185 ) ;
185186 } ) ;
186187
188+ it ( "does not begin strict delivery when target resolution settles after cancellation" , async ( ) => {
189+ let resolvePendingTarget : ( value : unknown ) => void = ( ) => { } ;
190+ mocks . resolveDeliveryTarget . mockImplementationOnce (
191+ ( ) =>
192+ new Promise ( ( resolve ) => {
193+ resolvePendingTarget = resolve ;
194+ } ) ,
195+ ) ;
196+ const abortController = new AbortController ( ) ;
197+
198+ const delivery = sendCronAnnouncePayloadStrict ( {
199+ deps : { } as never ,
200+ cfg : { } as never ,
201+ agentId : "main" ,
202+ jobId : "job-1" ,
203+ target : { channel : "telegram" , to : "123" } ,
204+ message : "Cron failed" ,
205+ abortSignal : abortController . signal ,
206+ } ) ;
207+
208+ abortController . abort ( new Error ( "delivery deadline exceeded" ) ) ;
209+ resolvePendingTarget ( {
210+ ok : true ,
211+ channel : "telegram" ,
212+ to : "123" ,
213+ accountId : "bot-a" ,
214+ mode : "explicit" ,
215+ } ) ;
216+
217+ await expect ( delivery ) . rejects . toThrow ( "delivery deadline exceeded" ) ;
218+ expect ( mocks . deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
219+ } ) ;
220+
187221 it ( "does not send when target resolution fails" , async ( ) => {
188222 mocks . resolveDeliveryTarget . mockResolvedValue ( {
189223 ok : false ,
@@ -206,6 +240,131 @@ describe("sendFailureNotificationAnnounce", () => {
206240 ) ;
207241 } ) ;
208242
243+ it ( "logs thrown target-resolution failures without masking the failed cron run" , async ( ) => {
244+ mocks . resolveDeliveryTarget . mockRejectedValueOnce ( new Error ( "target lookup failed" ) ) ;
245+
246+ await expect (
247+ sendFailureNotificationAnnounce (
248+ { } as never ,
249+ { } as never ,
250+ "main" ,
251+ "job-1" ,
252+ { channel : "telegram" , to : "123" } ,
253+ "Cron failed" ,
254+ ) ,
255+ ) . resolves . toBeUndefined ( ) ;
256+
257+ expect ( mocks . deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
258+ expect ( mocks . warn ) . toHaveBeenCalledWith (
259+ { err : "target lookup failed" , channel : "telegram" , to : "123" } ,
260+ "cron: failure destination announce failed" ,
261+ ) ;
262+ } ) ;
263+
264+ it ( "bounds stalled target resolution without starting a late channel send" , async ( ) => {
265+ vi . useFakeTimers ( ) ;
266+ let resolvePendingTarget : ( value : unknown ) => void = ( ) => { } ;
267+ mocks . resolveDeliveryTarget . mockImplementationOnce (
268+ ( ) =>
269+ new Promise ( ( resolve ) => {
270+ resolvePendingTarget = resolve ;
271+ } ) ,
272+ ) ;
273+
274+ const notification = sendFailureNotificationAnnounce (
275+ { } as never ,
276+ { } as never ,
277+ "main" ,
278+ "job-1" ,
279+ { channel : "telegram" , to : "123" } ,
280+ "Cron failed" ,
281+ ) ;
282+
283+ await vi . advanceTimersByTimeAsync ( 29_999 ) ;
284+ expect ( mocks . warn ) . not . toHaveBeenCalled ( ) ;
285+ expect ( mocks . deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
286+
287+ await vi . advanceTimersByTimeAsync ( 1 ) ;
288+ await expect ( notification ) . resolves . toBeUndefined ( ) ;
289+ expect ( mocks . warn ) . toHaveBeenCalledWith (
290+ {
291+ err : "cron: failure destination announcement timed out" ,
292+ channel : "telegram" ,
293+ to : "123" ,
294+ } ,
295+ "cron: failure destination announce failed" ,
296+ ) ;
297+
298+ resolvePendingTarget ( {
299+ ok : true ,
300+ channel : "telegram" ,
301+ to : "123" ,
302+ accountId : "bot-a" ,
303+ mode : "explicit" ,
304+ } ) ;
305+ await vi . advanceTimersByTimeAsync ( 0 ) ;
306+ expect ( mocks . deliverOutboundPayloads ) . not . toHaveBeenCalled ( ) ;
307+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
308+ } ) ;
309+
310+ it . each ( [
311+ { description : "honors cancellation" , honorsCancellation : true } ,
312+ { description : "ignores cancellation" , honorsCancellation : false } ,
313+ ] ) (
314+ "bounds a stalled failure notification when its channel $description" ,
315+ async ( { honorsCancellation } ) => {
316+ vi . useFakeTimers ( ) ;
317+ let deliverySignal : AbortSignal | undefined ;
318+ mocks . deliverOutboundPayloads . mockImplementationOnce (
319+ ( { abortSignal } : { abortSignal : AbortSignal } ) =>
320+ new Promise < void > ( ( _resolve , reject ) => {
321+ deliverySignal = abortSignal ;
322+ if ( honorsCancellation ) {
323+ abortSignal . addEventListener (
324+ "abort" ,
325+ ( ) =>
326+ reject (
327+ abortSignal . reason instanceof Error
328+ ? abortSignal . reason
329+ : new Error ( "failure notification was aborted" ) ,
330+ ) ,
331+ { once : true } ,
332+ ) ;
333+ }
334+ } ) ,
335+ ) ;
336+
337+ const notification = sendFailureNotificationAnnounce (
338+ { } as never ,
339+ { } as never ,
340+ "main" ,
341+ "job-1" ,
342+ { channel : "telegram" , to : "123" } ,
343+ "Cron failed" ,
344+ ) ;
345+
346+ await vi . advanceTimersByTimeAsync ( 0 ) ;
347+ expect ( mocks . deliverOutboundPayloads ) . toHaveBeenCalledOnce ( ) ;
348+ expect ( deliverySignal ?. aborted ) . toBe ( false ) ;
349+
350+ await vi . advanceTimersByTimeAsync ( 29_999 ) ;
351+ expect ( deliverySignal ?. aborted ) . toBe ( false ) ;
352+
353+ await vi . advanceTimersByTimeAsync ( 1 ) ;
354+ await expect ( notification ) . resolves . toBeUndefined ( ) ;
355+ expect ( deliverySignal ?. aborted ) . toBe ( true ) ;
356+ expect ( mocks . warn ) . toHaveBeenCalledWith (
357+ {
358+ err : "cron: failure destination announcement timed out" ,
359+ channel : "telegram" ,
360+ to : "123" ,
361+ } ,
362+ "cron: failure destination announce failed" ,
363+ ) ;
364+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
365+ } ,
366+ ) ;
367+
209368 it ( "swallows outbound delivery errors after logging" , async ( ) => {
210369 mocks . deliverOutboundPayloads . mockRejectedValue ( new Error ( "send failed" ) ) ;
211370
0 commit comments