@@ -7,13 +7,17 @@ import {
77import { logger } from "tests/helpers/pino-logger" ;
88import { sendSnsBatchEvent , sendSnsEvent } from "tests/helpers/send-sns-event" ;
99import {
10- pollUpsertLetterLogForWarning ,
10+ pollUpsertLetterLogForError ,
1111 supplierIdFromSupplierAllocatorLog ,
1212} from "tests/helpers/aws-cloudwatch-helper" ;
1313import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper" ;
1414import { SUPPLIER_LETTERS } from "tests/constants/api-constants" ;
1515import { supplierDataSetup } from "tests/helpers/suppliers-setup-helper" ;
16- import { checkLetterQueueTable } from "tests/helpers/generate-fetch-test-data" ;
16+ import {
17+ checkLetterQueueTable ,
18+ getLetterFromQueueById ,
19+ } from "tests/helpers/generate-fetch-test-data" ;
20+ import { createValidRequestHeaders } from "../../constants/request-headers" ;
1721import {
1822 patchRequestHeaders ,
1923 patchValidRequestBody ,
@@ -102,6 +106,70 @@ test.describe("Letter Queue Tests", () => {
102106 expect ( letterExists ) . toBe ( true ) ;
103107 expect ( itemCount ) . toBe ( 1 ) ;
104108
105- await pollUpsertLetterLogForWarning ( "Letter already exists" , letterId ) ;
109+ await pollUpsertLetterLogForError (
110+ `Letter with id ${ letterId } already exists for supplier ${ supplierId } "` ,
111+ letterId ,
112+ ) ;
113+ } ) ;
114+
115+ test ( "Verify if VisibilityTimeStamp is updated when Get /letters endpoint is called and subsequent calls returns no data until VisibilityTimeStamp is reached" , async ( {
116+ request,
117+ } ) => {
118+ const letterId = randomUUID ( ) ;
119+ logger . info ( `Sending event with domainId: ${ letterId } ` ) ;
120+ const preparedEvent = createPreparedV1Event ( { domainId : letterId } ) ;
121+ const response = await sendSnsEvent ( preparedEvent ) ;
122+
123+ expect ( response . MessageId ) . toBeTruthy ( ) ;
124+
125+ const supplierId = await supplierIdFromSupplierAllocatorLog ( letterId ) ;
126+
127+ await supplierDataSetup ( supplierId ) ;
128+
129+ const letters = await getLetterFromQueueById ( supplierId , letterId ) ;
130+ expect ( letters ) . toHaveLength ( 1 ) ;
131+ const letter = letters [ 0 ] ;
132+ expect ( letter . visibilityTimestamp ) . toBe ( letter . queueTimestamp ) ;
133+ logger . info (
134+ "Visibility timestamp is same as queue timestamp before calling Get /letters endpoint" ,
135+ ) ;
136+
137+ // call get letters endpoint which should update the visibility timestamp
138+ const header = createValidRequestHeaders ( supplierId ) ;
139+ const getLettersResponse = await request . get (
140+ `${ baseUrl } /${ SUPPLIER_LETTERS } ` ,
141+ {
142+ headers : header ,
143+ } ,
144+ ) ;
145+
146+ expect ( getLettersResponse . status ( ) ) . toBe ( 200 ) ;
147+ const currentTimeWithTimeOut = Math . floor (
148+ ( Date . now ( ) + 5 * 60 * 1000 ) / 1000 ,
149+ ) ;
150+
151+ logger . info (
152+ "Called Get /letters endpoint verify visibility timestamp is updated and subsequent calls returns no data until visibility timestamp is reached" ,
153+ ) ;
154+ const lettersAfterGet = await getLetterFromQueueById ( supplierId , letterId ) ;
155+ const visibilityTimestampAfterGet = Math . floor (
156+ new Date ( lettersAfterGet [ 0 ] . visibilityTimestamp ) . getTime ( ) / 1000 ,
157+ ) ;
158+
159+ // allow a 1 second tolerance
160+ expect (
161+ Math . abs ( visibilityTimestampAfterGet - currentTimeWithTimeOut ) ,
162+ ) . toBeLessThanOrEqual ( 1 ) ;
163+
164+ const getLettersWithInVisibility = await request . get (
165+ `${ baseUrl } /${ SUPPLIER_LETTERS } ` ,
166+ {
167+ headers : header ,
168+ } ,
169+ ) ;
170+
171+ expect ( getLettersWithInVisibility . status ( ) ) . toBe ( 200 ) ;
172+ const responseBody = await getLettersWithInVisibility . json ( ) ;
173+ expect ( responseBody . data ) . toHaveLength ( 0 ) ;
106174 } ) ;
107175} ) ;
0 commit comments