Skip to content

Commit 3f2dcfb

Browse files
committed
Fix tests
1 parent a8f0f98 commit 3f2dcfb

4 files changed

Lines changed: 38 additions & 60 deletions

File tree

tests/component-tests/events-tests/event-subscription.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SUPPLIER_LETTERS, envName } from "tests/constants/api-constants";
99
import {
1010
pollSupplierAllocatorLogForResolvedSpec,
1111
pollUpsertLetterLogForError,
12+
pollUpsertLetterLogForWarning,
1213
} from "tests/helpers/aws-cloudwatch-helper";
1314
import { supplierDataSetup } from "tests/helpers/suppliers-setup-helper";
1415
import { pollForLetterStatus } from "tests/helpers/poll-for-letters-helper";
@@ -133,8 +134,6 @@ test.describe("Event Subscription SNS Tests", () => {
133134
expect(duplicateResponse.MessageId).toBeTruthy();
134135

135136
// poll supplier upsert to check if duplicate event was processed
136-
await pollUpsertLetterLogForError(
137-
`Letter with id ${domainId} already exists for supplier ${supplierId}"`,
138-
);
137+
await pollUpsertLetterLogForWarning(domainId, "Letter already exists");
139138
});
140139
});

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { logger } from "tests/helpers/pino-logger";
88
import { sendSnsBatchEvent, sendSnsEvent } from "tests/helpers/send-sns-event";
99
import {
10-
pollUpsertLetterLogForError,
10+
pollUpsertLetterLogForWarning,
1111
supplierIdFromSupplierAllocatorLog,
1212
} from "tests/helpers/aws-cloudwatch-helper";
1313
import getRestApiGatewayBaseUrl from "tests/helpers/aws-gateway-helper";
@@ -102,9 +102,6 @@ test.describe("Letter Queue Tests", () => {
102102
expect(letterExists).toBe(true);
103103
expect(itemCount).toBe(1);
104104

105-
await pollUpsertLetterLogForError(
106-
`Letter with id ${letterId} already exists for supplier ${supplierId}"`,
107-
letterId,
108-
);
105+
await pollUpsertLetterLogForWarning(letterId, "Letter already exists");
109106
});
110107
});

tests/constants/api-constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const SUPPLIER_LETTERS = "letters";
22
export const SUPPLIER_API_URL_SANDBOX =
33
"https://internal-dev-sandbox.api.service.nhs.uk/nhs-notify-supplier";
44
export const AWS_REGION = "eu-west-2";
5-
export const envName = process.env.PR_NUMBER ?? "main";
5+
export const envName = "pr495";
66
export const API_NAME = `nhs-${envName}-supapi`;
77
export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
88
export const SUPPLIERID = "TestSupplier1";

tests/helpers/aws-cloudwatch-helper.ts

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ const sleep = (ms: number) =>
1010
setTimeout(resolve, ms);
1111
});
1212

13-
export async function pollSupplierAllocatorLogForResolvedSpec(
14-
domainId: string,
13+
async function pollLambdaLog(
14+
lambdaName: string,
15+
filterPatterns: string[],
16+
extraPatterns?: string[],
1517
): Promise<string> {
1618
const intervalMs = 5000;
1719
const startTimeMs = Date.now() - 5 * 60_000;
1820
const timeoutMs = 120_000;
1921

2022
const client = new CloudWatchLogsClient({ region: AWS_REGION });
21-
const logGroupName = `/aws/lambda/nhs-${envName}-supapi-supplier-allocator`;
23+
const logGroupName = `/aws/lambda/nhs-${envName}-supapi-${lambdaName}`;
2224
const deadline = Date.now() + timeoutMs;
2325

2426
while (Date.now() < deadline) {
@@ -28,18 +30,15 @@ export async function pollSupplierAllocatorLogForResolvedSpec(
2830
startTime: startTimeMs,
2931
interleaved: true,
3032
limit: 100,
31-
filterPattern: `"Sending message to upsert letter queue" "${domainId}"`,
33+
filterPattern: filterPatterns.join(" "),
3234
}),
3335
);
3436

3537
const foundEvent = (response.events ?? []).find((event) => {
3638
const message = event.message ?? "";
37-
return (
38-
message.includes(
39-
'"description":"Sending message to upsert letter queue"',
40-
) &&
41-
(!domainId || message.includes(domainId))
42-
);
39+
return extraPatterns
40+
? extraPatterns.some((pattern) => message.includes(pattern))
41+
: true;
4342
});
4443
if (foundEvent?.message) {
4544
return foundEvent.message;
@@ -48,55 +47,38 @@ export async function pollSupplierAllocatorLogForResolvedSpec(
4847
await sleep(intervalMs);
4948
}
5049

51-
throw new Error(
52-
`Timed out waiting for resolved supplier spec log in ${logGroupName}`,
53-
);
50+
throw new Error(`Timed out waiting for resolved log in ${logGroupName}`);
51+
}
52+
53+
export async function pollSupplierAllocatorLogForResolvedSpec(
54+
domainId: string,
55+
): Promise<string> {
56+
return pollLambdaLog("supplier-allocator", [
57+
'"Sending message to upsert letter queue"',
58+
`"${domainId}"`,
59+
]);
5460
}
5561

5662
export async function pollUpsertLetterLogForError(
5763
msgToCheck: string,
5864
domainId?: string,
5965
): Promise<string> {
60-
const intervalMs = 5000;
61-
const startTimeMs = Date.now() - 5 * 60_000;
62-
const timeoutMs = 120_000;
63-
64-
const client = new CloudWatchLogsClient({ region: AWS_REGION });
65-
const logGroupName = `/aws/lambda/nhs-${envName}-supapi-upsertletter`;
66-
const deadline = Date.now() + timeoutMs;
67-
68-
while (Date.now() < deadline) {
69-
const response = await client.send(
70-
new FilterLogEventsCommand({
71-
logGroupName,
72-
startTime: startTimeMs,
73-
interleaved: true,
74-
limit: 100,
75-
filterPattern: domainId
76-
? `"Error processing upsert of record" "${domainId}"`
77-
: `"Error processing upsert of record"`,
78-
}),
79-
);
80-
81-
const foundEvent = (response.events ?? []).find((event) => {
82-
const message = event.message ?? "";
83-
return (
84-
message.includes('"description":"Error processing upsert of record"') &&
85-
(message.includes(`"message":"${msgToCheck}`) ||
86-
message.includes(`"message": "${msgToCheck}`))
87-
);
88-
});
89-
90-
if (foundEvent?.message) {
91-
return foundEvent.message;
92-
}
93-
94-
await sleep(intervalMs);
66+
const filterPatterns = ['"Error processing upsert of record"'];
67+
if (domainId) {
68+
filterPatterns.push(`"${domainId}"`);
9569
}
70+
return pollLambdaLog("upsertletter", filterPatterns, [
71+
`"message": "${msgToCheck}`,
72+
`"message":"${msgToCheck}`,
73+
]);
74+
}
9675

97-
throw new Error(
98-
`Timed out waiting for upsert letter error log in ${logGroupName}`,
99-
);
76+
export async function pollUpsertLetterLogForWarning(
77+
description: string,
78+
domainId: string,
79+
): Promise<string> {
80+
const filterPatterns = ['"WARN"', `"${domainId}"`, `"${description}"`];
81+
return pollLambdaLog("upsertletter", filterPatterns);
10082
}
10183

10284
export async function supplierIdFromSupplierAllocatorLog(

0 commit comments

Comments
 (0)