@@ -19,6 +19,7 @@ import {
1919 withTempHeartbeatSandbox ,
2020 withTempTelegramHeartbeatSandbox ,
2121} from "./heartbeat-runner.test-utils.js" ;
22+ import { enqueueSystemEvent , peekSystemEvents } from "./system-events.js" ;
2223
2324installHeartbeatRunnerTestRuntime ( ) ;
2425
@@ -219,6 +220,40 @@ describe("runHeartbeatOnce ack handling", () => {
219220 return cfg ;
220221 }
221222
223+ async function runRelayableExecHeartbeat ( params : {
224+ tmpDir : string ;
225+ storePath : string ;
226+ replySpy : HeartbeatReplySpy ;
227+ reply : Record < string , unknown > ;
228+ } ) {
229+ const cfg = createWhatsAppHeartbeatConfig ( {
230+ tmpDir : params . tmpDir ,
231+ storePath : params . storePath ,
232+ } ) ;
233+ const sessionKey = await seedMainSessionStore ( params . storePath , cfg , {
234+ lastChannel : "whatsapp" ,
235+ lastProvider : "whatsapp" ,
236+ lastTo : WHATSAPP_GROUP ,
237+ } ) ;
238+ enqueueSystemEvent ( "Exec completed (heartbeat-test, code 0) :: uploaded report.txt" , {
239+ sessionKey,
240+ contextKey : "exec:heartbeat-test" ,
241+ } ) ;
242+ params . replySpy . mockResolvedValue ( params . reply as never ) ;
243+ const sendWhatsApp = createMessageSendSpy ( ) ;
244+
245+ const result = await runHeartbeatOnce ( {
246+ cfg,
247+ reason : "exec-event" ,
248+ deps : {
249+ ...makeWhatsAppDeps ( { sendWhatsApp } ) ,
250+ getReplyFromConfig : params . replySpy ,
251+ } ,
252+ } ) ;
253+
254+ return { cfg, result, sendWhatsApp, sessionKey } ;
255+ }
256+
222257 it ( "uses the fixed ack budget to suppress short heartbeat acknowledgements" , async ( ) => {
223258 await withTempHeartbeatSandbox ( async ( { tmpDir, storePath, replySpy } ) => {
224259 const cfg = createWhatsAppHeartbeatConfig ( {
@@ -513,6 +548,67 @@ describe("runHeartbeatOnce ack handling", () => {
513548 } ) ;
514549 } ) ;
515550
551+ it . each ( [ "HEARTBEAT_OK" , "NO_REPLY" ] ) (
552+ "keeps relayable exec reply %s silent and consumes the event" ,
553+ async ( replyText ) => {
554+ await withTempHeartbeatSandbox ( async ( { tmpDir, storePath, replySpy } ) => {
555+ const { result, sendWhatsApp, sessionKey } = await runRelayableExecHeartbeat ( {
556+ tmpDir,
557+ storePath,
558+ replySpy,
559+ reply : { text : replyText } ,
560+ } ) ;
561+
562+ expect ( result . status ) . toBe ( "ran" ) ;
563+ expect ( sendWhatsApp ) . not . toHaveBeenCalled ( ) ;
564+ expect ( peekSystemEvents ( sessionKey ) ) . toEqual ( [ ] ) ;
565+ } ) ;
566+ } ,
567+ ) ;
568+
569+ it . each ( [
570+ [ "Command completed: uploaded report.txt" , "Command completed: uploaded report.txt" ] ,
571+ [
572+ "Command completed: uploaded report.txt\nHEARTBEAT_OK" ,
573+ "Command completed: uploaded report.txt" ,
574+ ] ,
575+ ] ) ( "delivers one relayable exec summary from %j" , async ( replyText , expectedText ) => {
576+ await withTempHeartbeatSandbox ( async ( { tmpDir, storePath, replySpy } ) => {
577+ const { cfg, sendWhatsApp } = await runRelayableExecHeartbeat ( {
578+ tmpDir,
579+ storePath,
580+ replySpy,
581+ reply : { text : replyText } ,
582+ } ) ;
583+
584+ expectWhatsAppMessageSend ( sendWhatsApp , {
585+ to : WHATSAPP_GROUP ,
586+ text : expectedText ,
587+ cfg,
588+ } ) ;
589+ } ) ;
590+ } ) ;
591+
592+ it ( "keeps relayable exec media and structured reply content deliverable" , async ( ) => {
593+ await withTempHeartbeatSandbox ( async ( { tmpDir, storePath, replySpy } ) => {
594+ const { result, sendWhatsApp } = await runRelayableExecHeartbeat ( {
595+ tmpDir,
596+ storePath,
597+ replySpy,
598+ reply : {
599+ text : "HEARTBEAT_OK" ,
600+ mediaUrl : "https://example.test/report.png" ,
601+ presentation : {
602+ blocks : [ { type : "text" , text : "Report uploaded." } ] ,
603+ } ,
604+ } ,
605+ } ) ;
606+
607+ expect ( result . status ) . toBe ( "ran" ) ;
608+ expect ( sendWhatsApp ) . toHaveBeenCalledOnce ( ) ;
609+ } ) ;
610+ } ) ;
611+
516612 it ( "does not regress updatedAt when restoring heartbeat sessions" , async ( ) => {
517613 await withTempHeartbeatSandbox ( async ( { tmpDir, storePath, replySpy } ) => {
518614 const originalUpdatedAt = 1000 ;
0 commit comments