|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { randomUUID } from "node:crypto"; |
| 3 | +import { |
| 4 | + createPreparedEventBatchWithSameDomainId, |
| 5 | + createPreparedV1Event, |
| 6 | +} from "tests/helpers/event-fixtures"; |
| 7 | +import { logger } from "tests/helpers/pino-logger"; |
| 8 | +import { sendSnsBatchEvent, sendSnsEvent } from "tests/helpers/send-sns-event"; |
| 9 | +import { |
| 10 | + pollUpsertLetterLogForError, |
| 11 | + supplierIdFromSupplierAllocatorLog, |
| 12 | +} from "tests/helpers/aws-cloudwatch-helper"; |
| 13 | +import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper"; |
| 14 | +import { SUPPLIER_LETTERS } from "tests/constants/api-constants"; |
| 15 | +import { supplierDataSetup } from "tests/helpers/suppliers-setup-helper"; |
| 16 | +import { checkLetterQueueTable } from "tests/helpers/generate-fetch-test-data"; |
| 17 | +import { |
| 18 | + patchRequestHeaders, |
| 19 | + patchValidRequestBody, |
| 20 | +} from "../apiGateway-tests/testCases/update-letter-status"; |
| 21 | + |
| 22 | +let baseUrl: string; |
| 23 | + |
| 24 | +test.beforeAll(async () => { |
| 25 | + baseUrl = await getRestApiGatewayBaseUrl(); |
| 26 | +}); |
| 27 | + |
| 28 | +test.describe("Letter Queue Tests", () => { |
| 29 | + test.setTimeout(180_000); |
| 30 | + |
| 31 | + test("Verify that the letter queue operation inserts data into the letter queue table for pending letters", async ({ |
| 32 | + request, |
| 33 | + }) => { |
| 34 | + const letterId = randomUUID(); |
| 35 | + const status = "ACCEPTED"; |
| 36 | + |
| 37 | + logger.info(`Sending event with domainId: ${letterId}`); |
| 38 | + const preparedEvent = createPreparedV1Event({ domainId: letterId }); |
| 39 | + const response = await sendSnsEvent(preparedEvent); |
| 40 | + |
| 41 | + expect(response.MessageId).toBeTruthy(); |
| 42 | + |
| 43 | + const supplierId = await supplierIdFromSupplierAllocatorLog(letterId); |
| 44 | + |
| 45 | + await supplierDataSetup(supplierId); |
| 46 | + |
| 47 | + const [letterQueue, count] = await checkLetterQueueTable( |
| 48 | + supplierId, |
| 49 | + letterId, |
| 50 | + ); |
| 51 | + expect(letterQueue).toBe(true); |
| 52 | + expect(count).toBe(1); |
| 53 | + |
| 54 | + // update the letter status to ACCEPTED and verify if letter queue table is cleaned up |
| 55 | + |
| 56 | + const headers = patchRequestHeaders(supplierId); |
| 57 | + const body = patchValidRequestBody(letterId, status); |
| 58 | + |
| 59 | + const patchResponse = await request.patch( |
| 60 | + `${baseUrl}/${SUPPLIER_LETTERS}/${letterId}`, |
| 61 | + { |
| 62 | + headers, |
| 63 | + data: body, |
| 64 | + }, |
| 65 | + ); |
| 66 | + expect(patchResponse.status()).toBe(202); |
| 67 | + logger.info( |
| 68 | + `Updated letter status to ${status} for letterId ${letterId}, now polling letter queue table for cleanup confirmation`, |
| 69 | + ); |
| 70 | + |
| 71 | + const [letterQueueAfterUpdate, countAfterUpdate] = |
| 72 | + await checkLetterQueueTable(supplierId, letterId, true); |
| 73 | + expect(letterQueueAfterUpdate).toBe(true); |
| 74 | + expect(countAfterUpdate).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + test("Verify if the only one entry is inserted in the letter queue table for a batch of events with the same letterId", async () => { |
| 78 | + const letterId = randomUUID(); |
| 79 | + const eventBatch = createPreparedEventBatchWithSameDomainId({ |
| 80 | + domainId: letterId, |
| 81 | + }); |
| 82 | + logger.info( |
| 83 | + `Sending batch event with ${eventBatch.length} events ${letterId}`, |
| 84 | + ); |
| 85 | + const response = await sendSnsBatchEvent( |
| 86 | + eventBatch.map((event) => ({ |
| 87 | + id: event.id, |
| 88 | + message: event, |
| 89 | + })), |
| 90 | + ); |
| 91 | + expect(response.Successful).toHaveLength(eventBatch.length); |
| 92 | + |
| 93 | + const supplierId = await supplierIdFromSupplierAllocatorLog(letterId); |
| 94 | + |
| 95 | + logger.info( |
| 96 | + `Verifying duplicate queue inserts are ignored for the batch of events with same letterId ${letterId}`, |
| 97 | + ); |
| 98 | + const [letterExists, itemCount] = await checkLetterQueueTable( |
| 99 | + supplierId, |
| 100 | + letterId, |
| 101 | + ); |
| 102 | + expect(letterExists).toBe(true); |
| 103 | + expect(itemCount).toBe(1); |
| 104 | + |
| 105 | + await pollUpsertLetterLogForError( |
| 106 | + `Letter with id ${letterId} already exists for supplier ${supplierId}"`, |
| 107 | + letterId, |
| 108 | + ); |
| 109 | + }); |
| 110 | +}); |
0 commit comments