@@ -434,67 +434,93 @@ describe("handleDiscordMessageAction", () => {
434434 } ) ;
435435 } ) ;
436436
437- it ( "notifies inbound event delivery after message sends" , async ( ) => {
438- const markDelivered = vi . fn ( ) ;
439- const end = beginDiscordInboundEventDeliveryCorrelation (
440- "agent:main:discord:channel:c1" ,
441- {
442- outboundTo : "channel:c1" ,
443- outboundAccountId : "default" ,
444- markInboundEventDelivered : markDelivered ,
445- } ,
446- { inboundEventKind : "room_event" } ,
447- ) ;
448-
449- try {
450- await handleDiscordMessageAction ( {
451- action : "send" ,
452- params : {
453- to : "channel:c1" ,
454- message : "hello" ,
455- } ,
456- cfg : discordConfig ( ) ,
457- accountId : "default" ,
458- sessionKey : "agent:main:discord:channel:c1" ,
459- inboundEventKind : "room_event" ,
460- } ) ;
461- } finally {
462- end ( ) ;
463- }
464-
465- expect ( markDelivered ) . toHaveBeenCalledTimes ( 1 ) ;
466- } ) ;
467-
468- it ( "notifies inbound event delivery after visible message actions" , async ( ) => {
469- const markDelivered = vi . fn ( ) ;
470- const end = beginDiscordInboundEventDeliveryCorrelation (
471- "agent:main:discord:channel:c1" ,
472- {
473- outboundTo : "channel:c1" ,
474- outboundAccountId : "default" ,
475- markInboundEventDelivered : markDelivered ,
437+ it . each ( [
438+ {
439+ action : "send" as const ,
440+ params : { to : "channel:c1" , message : "hello" } ,
441+ } ,
442+ {
443+ action : "upload-file" as const ,
444+ params : { to : "channel:c1" , filePath : "/tmp/image.png" } ,
445+ } ,
446+ {
447+ action : "poll" as const ,
448+ params : {
449+ to : "channel:c1" ,
450+ pollQuestion : "Which option?" ,
451+ pollOption : [ "first" , "second" ] ,
476452 } ,
477- { inboundEventKind : "room_event" } ,
478- ) ;
479-
480- try {
481- await handleDiscordMessageAction ( {
482- action : "upload-file" ,
483- params : {
484- to : "channel:c1" ,
485- filePath : "/tmp/image.png" ,
486- } ,
487- cfg : discordConfig ( ) ,
488- accountId : "default" ,
489- sessionKey : "agent:main:discord:channel:c1" ,
490- inboundEventKind : "room_event" ,
491- } ) ;
492- } finally {
493- end ( ) ;
494- }
495-
496- expect ( markDelivered ) . toHaveBeenCalledTimes ( 1 ) ;
497- } ) ;
453+ } ,
454+ {
455+ action : "sticker" as const ,
456+ params : { to : "channel:c1" , stickerId : [ "sticker-1" ] } ,
457+ } ,
458+ {
459+ action : "thread-reply" as const ,
460+ params : { threadId : "c1" , message : "thread update" } ,
461+ } ,
462+ {
463+ action : "thread-create" as const ,
464+ params : { channelId : "c1" , messageId : "message-1" , threadName : "investigation" } ,
465+ } ,
466+ ] ) (
467+ "records room-event delivery only after $action receives a positive receipt" ,
468+ async ( { action, params } ) => {
469+ const sessionKey = "agent:main:discord:channel:c1" ;
470+
471+ for ( const ok of [ false , true ] ) {
472+ const markDelivered = vi . fn ( ) ;
473+ const onThreadAdopted = vi . fn ( ) ;
474+ const endDelivery = beginDiscordInboundEventDeliveryCorrelation (
475+ sessionKey ,
476+ {
477+ outboundTo : "channel:c1" ,
478+ outboundAccountId : "default" ,
479+ markInboundEventDelivered : markDelivered ,
480+ } ,
481+ { inboundEventKind : "room_event" } ,
482+ ) ;
483+ const endThreadRoute =
484+ action === "thread-create"
485+ ? beginDiscordActiveTurnThreadRoute ( sessionKey , {
486+ accountId : "default" ,
487+ sourceChannelId : "c1" ,
488+ sourceMessageId : "message-1" ,
489+ onThreadAdopted,
490+ } )
491+ : ( ) => { } ;
492+
493+ handleDiscordActionMock . mockResolvedValueOnce ( {
494+ content : [ ] ,
495+ details : {
496+ ok,
497+ ...( ok ? { } : { error : "delivery failed" } ) ,
498+ ...( action === "thread-create" ? { thread : { id : "thread-1" } } : { } ) ,
499+ } ,
500+ } ) ;
501+
502+ try {
503+ const result = await handleDiscordMessageAction ( {
504+ action,
505+ params,
506+ cfg : discordConfig ( { threads : true , polls : true , stickers : true } ) ,
507+ accountId : "default" ,
508+ sessionKey,
509+ inboundEventKind : "room_event" ,
510+ } ) ;
511+
512+ expect ( result . details ) . toMatchObject ( { ok } ) ;
513+ expect ( markDelivered ) . toHaveBeenCalledTimes ( ok ? 1 : 0 ) ;
514+ if ( action === "thread-create" ) {
515+ expect ( onThreadAdopted ) . toHaveBeenCalledTimes ( ok ? 1 : 0 ) ;
516+ }
517+ } finally {
518+ endThreadRoute ( ) ;
519+ endDelivery ( ) ;
520+ }
521+ }
522+ } ,
523+ ) ;
498524
499525 it ( "maps upload-file to Discord sendMessage with media read context" , async ( ) => {
500526 const mediaReadFile = vi . fn ( async ( ) => Buffer . from ( "image" ) ) ;
@@ -644,7 +670,7 @@ describe("handleDiscordMessageAction", () => {
644670
645671 handleDiscordActionMock . mockResolvedValueOnce ( {
646672 content : [ ] ,
647- details : { thread : { id : "thread-1" } } ,
673+ details : { ok : true , thread : { id : "thread-1" } } ,
648674 } ) ;
649675 await handleDiscordMessageAction ( {
650676 action : "thread-create" ,
@@ -766,7 +792,7 @@ describe("handleDiscordMessageAction", () => {
766792 try {
767793 const expectedResult = {
768794 content : [ ] ,
769- details : { thread : { id : "thread-1" } } ,
795+ details : { ok : true , thread : { id : "thread-1" } } ,
770796 } ;
771797 handleDiscordActionMock . mockResolvedValueOnce ( expectedResult ) ;
772798
0 commit comments