1- import { expect , test } from "@playwright/test" ;
2- import { sendSnsEvent } from "tests/helpers/send-sns-event" ;
3- import { createPreparedV1Event } from "tests/helpers/event-fixtures" ;
4- import { randomUUID } from "node:crypto" ;
5- import { logger } from "tests/helpers/pino-logger" ;
1+ import { test } from "@playwright/test" ;
62import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper" ;
73import { pollForLetterStatus } from "tests/helpers/poll-for-letters-helper" ;
8- import { pollSupplierAllocatorLogForResolvedSpec } from "tests/helpers/aws-cloudwatch-helper" ;
94import { getLettersFromQueueViaIndex } from "tests/helpers/generate-fetch-test-data" ;
105import {
11- AllocatedLetter ,
12- AllocatedLetterSchema ,
13- } from "../../../lambdas/upsert-letter/src/handler/schemas" ;
14-
15- // Values for CI/CD are kept in group_nhs-notify-supplier-api-dev.tfvars in the nhs-notify-internal repo
16- // If running locally see default of variant_map in infrastructure/terraform/components/api/variables.tf
17- const variantUrgencyMap : Record < string , number > = {
18- "digitrials-aspiring" : 0 ,
19- "digitrials-dmapp" : 1 ,
20- "digitrials-globalminds" : 2 ,
21- "digitrials-mymelanoma" : 3 ,
22- "digitrials-ofh" : 4 ,
23- "digitrials-prostateprogress" : 5 ,
24- "digitrials-protectc" : 6 ,
25- "digitrials-restore" : 7 ,
26- "gpreg-admail" : 8 ,
27- "nces-abnormal-results" : 9 ,
28- "nces-abnormal-results-braille" : 10 ,
29- "nces-invites" : 10 ,
30- "nces-invites-braille" : 10 ,
31- "nces-standard" : 11 ,
32- "nces-standard-braille" : 12 ,
33- "notify-braille" : 13 ,
34- "notify-digital-letters-standard" : 97 ,
35- "notify-standard" : 98 ,
36- "notify-standard-colour" : 99 ,
37- } ;
38- const supplier = "supplier1" ;
6+ getVariantsWithUrgency ,
7+ sendEventsForVariants ,
8+ supplier ,
9+ verifyAllocationLogsContainPriority ,
10+ verifyIndexPositionOfLetterVariants ,
11+ } from "tests/helpers/urgent-letter-priority-helper" ;
3912
4013let baseUrl : string ;
4114
@@ -47,11 +20,12 @@ test.describe("Urgent Letter Priority Tests", () => {
4720 test . setTimeout ( 180_000 ) ; // 3 minutes for long running polling
4821
4922 test ( "Letter with higher urgency gets picked first" , async ( { request } ) => {
23+ const variantsUrgencyTen = getVariantsWithUrgency ( 10 ) ;
24+ const urgencyTenLetterIds = await sendEventsForVariants ( variantsUrgencyTen ) ;
25+
5026 const variantsUrgencyNine = getVariantsWithUrgency ( 9 ) ;
5127 const urgencyNineLetterIds =
5228 await sendEventsForVariants ( variantsUrgencyNine ) ;
53- const variantsUrgencyTen = getVariantsWithUrgency ( 10 ) ;
54- const urgencyTenLetterIds = await sendEventsForVariants ( variantsUrgencyTen ) ;
5529
5630 await Promise . all (
5731 [ ...urgencyNineLetterIds , ...urgencyTenLetterIds ] . map ( async ( domainId ) =>
@@ -69,71 +43,8 @@ test.describe("Urgent Letter Priority Tests", () => {
6943
7044 verifyIndexPositionOfLetterVariants (
7145 letterIdsFromQueue ,
72- urgencyNineLetterIds ,
7346 urgencyTenLetterIds ,
47+ urgencyNineLetterIds ,
7448 ) ;
7549 } ) ;
7650} ) ;
77-
78- function getVariantsWithUrgency ( urgency : number ) {
79- const variants = Object . keys ( variantUrgencyMap ) . filter (
80- // safe has comes from map's keys which are controlled by us
81- // eslint-disable-next-line security/detect-object-injection
82- ( variant ) => variantUrgencyMap [ variant ] === urgency ,
83- ) ;
84- if ( variants . length === 0 ) {
85- throw new Error ( `No variants found with urgency ${ urgency } ` ) ;
86- }
87- return variants ;
88- }
89-
90- async function sendEventsForVariants ( variants : string [ ] ) {
91- const domainIds : string [ ] = [ ] ;
92- for ( const variant of variants ) {
93- const domainId = randomUUID ( ) ;
94- logger . info (
95- `Testing event subscription with domainId: ${ domainId } and variant: ${ variant } ` ,
96- ) ;
97- const preparedEvent = createPreparedV1Event ( {
98- domainId,
99- letterVariantId : variant ,
100- } ) ;
101- const response = await sendSnsEvent ( preparedEvent ) ;
102- expect ( response . MessageId ) . toBeTruthy ( ) ;
103- domainIds . push ( domainId ) ;
104- }
105- return domainIds ;
106- }
107-
108- function verifyIndexPositionOfLetterVariants (
109- letterIds : string [ ] ,
110- letterIdsLeastUrgency : string [ ] ,
111- letterIdsHigherUrgency : string [ ] ,
112- ) {
113- for ( const leastUrgencyLetterId of letterIdsLeastUrgency ) {
114- expect ( letterIds ) . toContain ( leastUrgencyLetterId ) ; // in case limit param is hit
115- const indexToTest = letterIds . indexOf ( leastUrgencyLetterId ) ;
116- for ( const higherUrgencyLetterId of letterIdsHigherUrgency ) {
117- expect ( letterIds ) . toContain ( higherUrgencyLetterId ) ; // in case limit param is hit
118- const higherUrgencyIndex = letterIds . indexOf ( higherUrgencyLetterId ) ;
119- expect ( indexToTest ) . toBeLessThan ( higherUrgencyIndex ) ; // higher urgency letters should come before lower urgency letters
120- }
121- }
122- }
123-
124- async function verifyAllocationLogsContainPriority (
125- letterIds : string [ ] ,
126- priority : number ,
127- ) {
128- for ( const domainId of letterIds ) {
129- const message = await pollSupplierAllocatorLogForResolvedSpec ( domainId ) ;
130- const supplierAllocatorLog = JSON . parse ( message ) ;
131- const allocatedLetter : AllocatedLetter = AllocatedLetterSchema . parse (
132- supplierAllocatorLog . msg ,
133- ) ;
134- const { supplierSpec } = allocatedLetter ;
135- expect ( supplierSpec ) . toBeDefined ( ) ;
136- expect ( supplierSpec . priority ) . toBeDefined ( ) ;
137- expect ( supplierSpec . priority ) . toBe ( priority ) ;
138- }
139- }
0 commit comments