@@ -3,7 +3,7 @@ import sinon from 'sinon';
33import Producer from '../../src/producers/sqs' ;
44import Registry from '../../src/runtime/registry' ;
55import Task from '../../src/runtime/task' ;
6- import { ITask , SQSMessageRoutingOptions } from '../../src/common' ;
6+ import { ITask , SQSMessageRoutingOptions } from '../../src/common' ;
77import { createMessageMetadata } from '../../src/lib/context' ;
88import { TaskOptions } from '../../src/types/task-options' ;
99
@@ -77,12 +77,12 @@ describe('SQS Producer', () => {
7777 const subscribeStub = sandbox . stub ( ) . resolves ( { some : 'success' } ) ;
7878 const anotherRegistry = {
7979 registeredTasks : [ ] ,
80- addNewTask : ( ) => { } ,
81- removeTask : ( ) => { } ,
80+ addNewTask : ( ) => { } ,
81+ removeTask : ( ) => { } ,
8282 getTopics : ( ) => [ ] ,
8383 getTaskTopics : ( ) => [ ] ,
8484 getTask : ( ) => ( {
85- publish : ( ) => { } ,
85+ publish : ( ) => { } ,
8686 subscribe : subscribeStub ,
8787 options : {
8888 deadLetterQueue : true ,
@@ -217,7 +217,7 @@ describe('SQS Producer', () => {
217217
218218 const sentAttributes =
219219 sendMessageStub . getCall ( 0 ) . args [ 0 ] . MessageAttributes [
220- attributes [ 0 ] . name
220+ attributes [ 0 ] . name
221221 ] ;
222222 const sentDataType = sentAttributes . DataType ;
223223 const sentStringValue = sentAttributes . StringValue ;
@@ -260,7 +260,7 @@ describe('SQS Producer', () => {
260260 } ,
261261 MessageBody : JSON . stringify ( expectedMessageBody ) ,
262262 QueueUrl : undefined ,
263- MessageGroupId : undefined ,
263+ MessageGroupId : 'context' ,
264264 MessageDeduplicationId : undefined
265265 } ;
266266
@@ -408,5 +408,83 @@ describe('SQS Producer', () => {
408408 ) ;
409409 sinon . assert . calledWith ( sendMessageStub , expectedPayload ) ;
410410 } ) ;
411+
412+ it ( 'should set MessageGroupId on standard queue when key is provided' , async ( ) => {
413+ const task : ITask = new Task (
414+ //@ts -expect-error
415+ { engine : 'sqs' } ,
416+ registry ,
417+ producer ,
418+ 'test-task-standard-with-key' ,
419+ 'test-topic' ,
420+ ( ) => undefined ,
421+ { } // Standard queue (no fifo flag)
422+ ) ;
423+ registry . addNewTask ( task ) ;
424+
425+ const messagePayload : any = { a : 'payload' } ;
426+ const messageContext : SQSMessageRoutingOptions = { key : 'tenant-123' } ;
427+ const expectedMessageBody = {
428+ ...messagePayload ,
429+ _meta : { ...createMessageMetadata ( messagePayload ) , ...messageContext } ,
430+ } ;
431+
432+ const expectedPayload = {
433+ MessageAttributes : {
434+ Timestamp : { DataType : 'Number' , StringValue : clock . now . toString ( ) } ,
435+ } ,
436+ MessageBody : JSON . stringify ( expectedMessageBody ) ,
437+ QueueUrl : undefined ,
438+ MessageGroupId : messageContext . key , // Should be set on standard queue
439+ MessageDeduplicationId : undefined
440+ } ;
441+
442+ const sendMessageStub = sandbox . stub ( producer . producer , 'sendMessage' ) ;
443+ await producer . send (
444+ 'test-topic' ,
445+ messagePayload ,
446+ messageContext
447+ ) ;
448+ sinon . assert . calledWith ( sendMessageStub , expectedPayload ) ;
449+ } ) ;
450+
451+ it ( 'should not set MessageGroupId when key is not provided' , async ( ) => {
452+ const task : ITask = new Task (
453+ //@ts -expect-error
454+ { engine : 'sqs' } ,
455+ registry ,
456+ producer ,
457+ 'test-task-no-key' ,
458+ 'test-topic' ,
459+ ( ) => undefined ,
460+ { }
461+ ) ;
462+ registry . addNewTask ( task ) ;
463+
464+ const messagePayload = { a : 'payload' } ;
465+ const messageContext : SQSMessageRoutingOptions = { } ; // No key
466+ const expectedMessageBody = {
467+ ...messagePayload ,
468+ _meta : { ...createMessageMetadata ( messagePayload ) , ...messageContext } ,
469+ } ;
470+
471+ const expectedPayload = {
472+ MessageAttributes : {
473+ Timestamp : { DataType : 'Number' , StringValue : clock . now . toString ( ) } ,
474+ } ,
475+ MessageBody : JSON . stringify ( expectedMessageBody ) ,
476+ QueueUrl : undefined ,
477+ MessageGroupId : undefined , // Should be undefined without key
478+ MessageDeduplicationId : undefined
479+ } ;
480+
481+ const sendMessageStub = sandbox . stub ( producer . producer , 'sendMessage' ) ;
482+ await producer . send (
483+ 'test-topic' ,
484+ messagePayload ,
485+ messageContext
486+ ) ;
487+ sinon . assert . calledWith ( sendMessageStub , expectedPayload ) ;
488+ } ) ;
411489 } ) ;
412490} ) ;
0 commit comments