Skip to content

Commit 72c1a69

Browse files
Add Test for LetterQueue to verify visibilityTimeStamp (#530)
* test * test * fix * all actual endpoint
1 parent 5d9a746 commit 72c1a69

3 files changed

Lines changed: 131 additions & 2 deletions

File tree

tests/component-tests/integration-tests/urgent-letter-priority.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from "@playwright/test";
1+
import { expect, test } from "@playwright/test";
22
import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper";
33
import { pollForLetterStatus } from "tests/helpers/poll-for-letters-helper";
44
import { getLettersFromQueueViaIndex } from "tests/helpers/generate-fetch-test-data";
@@ -9,6 +9,12 @@ import {
99
verifyAllocationLogsContainPriority,
1010
verifyIndexPositionOfLetterVariants,
1111
} from "tests/helpers/urgent-letter-priority-helper";
12+
import { createValidRequestHeaders } from "tests/constants/request-headers";
13+
import { SUPPLIER_LETTERS } from "tests/constants/api-constants";
14+
import {
15+
GetLettersResponse,
16+
GetLettersResponseSchema,
17+
} from "../../../lambdas/api-handler/src/contracts/letters";
1218

1319
let baseUrl: string;
1420

@@ -41,6 +47,21 @@ test.describe("Urgent Letter Priority Tests", () => {
4147
(letter) => letter.letterId,
4248
);
4349

50+
const header = createValidRequestHeaders(supplier);
51+
const response = await request.get(`${baseUrl}/${SUPPLIER_LETTERS}`, {
52+
headers: header,
53+
});
54+
55+
expect(response.status()).toBe(200);
56+
const responseBody = await response.json();
57+
expect(responseBody.data.length).toBeGreaterThanOrEqual(1);
58+
59+
const getLettersResponse: GetLettersResponse =
60+
GetLettersResponseSchema.parse(responseBody);
61+
62+
const letterIds = getLettersResponse.data.map((letter) => letter.id);
63+
expect(letterIds).toEqual(letterIdsFromQueue);
64+
4465
verifyIndexPositionOfLetterVariants(
4566
letterIdsFromQueue,
4667
urgencyTenLetterIds,

tests/component-tests/letterQueue-tests/queue-operations.spec.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper";
1414
import { SUPPLIER_LETTERS } from "tests/constants/api-constants";
1515
import { 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";
1721
import {
1822
patchRequestHeaders,
1923
patchValidRequestBody,
@@ -104,4 +108,65 @@ test.describe("Letter Queue Tests", () => {
104108

105109
await pollUpsertLetterLogForWarning("Letter already exists", letterId);
106110
});
111+
112+
test("Verify if VisibilityTimeStamp is updated when Get /letters endpoint is called and subsequent calls returns no data until VisibilityTimeStamp is reached", async ({
113+
request,
114+
}) => {
115+
const letterId = randomUUID();
116+
logger.info(`Sending event with domainId: ${letterId}`);
117+
const preparedEvent = createPreparedV1Event({ domainId: letterId });
118+
const response = await sendSnsEvent(preparedEvent);
119+
120+
expect(response.MessageId).toBeTruthy();
121+
122+
const supplierId = await supplierIdFromSupplierAllocatorLog(letterId);
123+
124+
await supplierDataSetup(supplierId);
125+
126+
const letters = await getLetterFromQueueById(supplierId, letterId);
127+
expect(letters).toHaveLength(1);
128+
const letter = letters[0];
129+
expect(letter.visibilityTimestamp).toBe(letter.queueTimestamp);
130+
logger.info(
131+
"Visibility timestamp is same as queue timestamp before calling Get /letters endpoint",
132+
);
133+
134+
// call get letters endpoint which should update the visibility timestamp
135+
const header = createValidRequestHeaders(supplierId);
136+
const getLettersResponse = await request.get(
137+
`${baseUrl}/${SUPPLIER_LETTERS}`,
138+
{
139+
headers: header,
140+
},
141+
);
142+
143+
expect(getLettersResponse.status()).toBe(200);
144+
const currentTimeWithTimeOut = Math.floor(
145+
(Date.now() + 5 * 60 * 1000) / 1000,
146+
);
147+
148+
logger.info(
149+
"Called Get /letters endpoint verify visibility timestamp is updated and subsequent calls returns no data until visibility timestamp is reached",
150+
);
151+
const lettersAfterGet = await getLetterFromQueueById(supplierId, letterId);
152+
const visibilityTimestampAfterGet = Math.floor(
153+
new Date(lettersAfterGet[0].visibilityTimestamp).getTime() / 1000,
154+
);
155+
156+
// allow a 1 second tolerance
157+
expect(
158+
Math.abs(visibilityTimestampAfterGet - currentTimeWithTimeOut),
159+
).toBeLessThanOrEqual(1);
160+
161+
const getLettersWithInVisibility = await request.get(
162+
`${baseUrl}/${SUPPLIER_LETTERS}`,
163+
{
164+
headers: header,
165+
},
166+
);
167+
168+
expect(getLettersWithInVisibility.status()).toBe(200);
169+
const responseBody = await getLettersWithInVisibility.json();
170+
expect(responseBody.data).toHaveLength(0);
171+
});
107172
});

tests/helpers/generate-fetch-test-data.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,46 @@ export async function getLettersFromQueueViaIndex(
263263
return [];
264264
}
265265
}
266+
267+
export async function getLetterFromQueueById(
268+
supplierId: string,
269+
letterId: string,
270+
): Promise<PendingLetter[]> {
271+
const MAX_ATTEMPTS = 5;
272+
const RETRY_DELAY_MS = 10_000;
273+
274+
try {
275+
const params = {
276+
TableName: LETTERQUEUE_TABLENAME,
277+
KeyConditionExpression:
278+
"supplierId = :supplierId AND letterId = :letterId",
279+
ExpressionAttributeValues: {
280+
":supplierId": supplierId,
281+
":letterId": letterId,
282+
},
283+
};
284+
285+
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
286+
const { Items } = await docClient.send(new QueryCommand(params));
287+
288+
if (Items !== undefined && Items.length > 0) {
289+
logger.info(
290+
`Queried letter queue table to verify existence for supplier ${supplierId} and found items.`,
291+
);
292+
293+
// assumes no pagination needed as we expect a small number of letters in the queue for the test supplier
294+
return z.array(PendingLetterSchema).parse(Items);
295+
}
296+
if (attempt < MAX_ATTEMPTS) {
297+
logger.info(
298+
`Retrying get letters from queue for supplierId ${supplierId} in ${RETRY_DELAY_MS}ms`,
299+
);
300+
await delay(RETRY_DELAY_MS);
301+
}
302+
}
303+
return [];
304+
} catch (error) {
305+
logger.error({ supplierId, error }, "Letter queue query failed");
306+
return [];
307+
}
308+
}

0 commit comments

Comments
 (0)