@@ -34,10 +34,10 @@ jest.unstable_mockModule("../src/sqs", () => {
3434import { Logger } from "@aws-lambda-powertools/logger"
3535
3636import { createMockPostModifiedDataItem } from "./testUtils"
37- import { PostDatedSQSMessage } from "../src/types"
37+ import { BatchProcessingResult , PostDatedSQSMessage } from "../src/types"
3838
3939// Import the orchestration module after mocking dependencies
40- const { processMessages} = await import ( "../src/orchestration" )
40+ const { processMessages, processPostDatedQueue } = await import ( "../src/orchestration" )
4141
4242const logger = new Logger ( { serviceName : "postDatedLambdaTEST" } )
4343
@@ -71,4 +71,72 @@ describe("orchestration", () => {
7171 expect ( result . immaturePrescriptionUpdates ) . toHaveLength ( 0 )
7272 } )
7373 } )
74+
75+ describe ( "processPostDatedQueue" , ( ) => {
76+ beforeEach ( ( ) => {
77+ jest . clearAllMocks ( )
78+ } )
79+
80+ it ( "should process the SQS queue correctly" , async ( ) => {
81+ const mockMessages : Array < PostDatedSQSMessage > = [
82+ { MessageId : "1" , Body : "Message 1" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
83+ { MessageId : "2" , Body : "Message 2" , prescriptionData : createMockPostModifiedDataItem ( { } ) }
84+ ]
85+
86+ const mockEnrichedMessages = mockMessages . map ( ( message ) => ( {
87+ ...message ,
88+ existingRecords : [ ]
89+ } ) )
90+
91+ mockReceivePostDatedSQSMessages . mockReturnValueOnce ( mockMessages )
92+ mockEnrichMessagesWithExistingRecords . mockReturnValueOnce ( mockEnrichedMessages )
93+ mockProcessMessage . mockReturnValue ( true )
94+
95+ await processPostDatedQueue ( logger )
96+
97+ expect ( mockReceivePostDatedSQSMessages ) . toHaveBeenCalledWith ( logger )
98+ expect ( mockReportQueueStatus ) . not . toHaveBeenCalled ( )
99+ expect ( mockHandleProcessedMessages ) . toHaveBeenCalled ( )
100+ const [ res , lg ] =
101+ mockHandleProcessedMessages . mock . calls [ 0 ] as [ BatchProcessingResult , Logger ]
102+ expect ( lg ) . toBe ( logger )
103+ expect ( res . maturedPrescriptionUpdates ) . toHaveLength ( mockMessages . length )
104+ expect ( res . immaturePrescriptionUpdates ) . toHaveLength ( 0 )
105+ expect ( res . maturedPrescriptionUpdates . map ( ( message ) => message . MessageId ) ) . toEqual (
106+ mockMessages . map ( ( message ) => message . MessageId )
107+ )
108+ expect ( mockProcessMessage ) . toHaveBeenCalledTimes ( mockMessages . length )
109+ } )
110+
111+ it ( "Should stop processing if the max runtime is exceeded" , async ( ) => {
112+ jest . useFakeTimers ( )
113+ const mockMessages : Array < PostDatedSQSMessage > = [
114+ { MessageId : "1" , Body : "Message 1" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
115+ { MessageId : "2" , Body : "Message 2" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
116+ { MessageId : "3" , Body : "Message 3" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
117+ { MessageId : "4" , Body : "Message 4" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
118+ { MessageId : "5" , Body : "Message 5" , prescriptionData : createMockPostModifiedDataItem ( { } ) } ,
119+ { MessageId : "6" , Body : "Message 6" , prescriptionData : createMockPostModifiedDataItem ( { } ) }
120+ ]
121+
122+ mockReceivePostDatedSQSMessages . mockReturnValue ( mockMessages )
123+ mockEnrichMessagesWithExistingRecords . mockReturnValue (
124+ mockMessages . map ( ( message ) => ( {
125+ ...message ,
126+ existingRecords : [ ]
127+ } ) )
128+ )
129+ const { MAX_QUEUE_RUNTIME } = await import ( "../src/orchestration" )
130+ mockProcessMessage . mockImplementation ( async ( ) => {
131+ // Overrun by a second
132+ jest . advanceTimersByTime ( MAX_QUEUE_RUNTIME + 1000 )
133+ return true
134+ } )
135+
136+ await processPostDatedQueue ( logger )
137+
138+ expect ( mockReportQueueStatus ) . toHaveBeenCalled ( )
139+ jest . useRealTimers ( )
140+ } )
141+ } )
74142} )
0 commit comments