Skip to content

Commit a9b5143

Browse files
More unit test fixes
1 parent b92401b commit a9b5143

1 file changed

Lines changed: 71 additions & 35 deletions

File tree

lambdas/upsert-letter/src/handler/__tests__/upsert-handler.test.ts

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe("createUpsertLetterHandler", () => {
354354
};
355355

356356
const evt: SQSEvent = createSQSEvent([
357-
createSqsRecord("bad-schema", JSON.stringify(message)),
357+
createSqsRecord("bad-notification-schema", JSON.stringify(message)),
358358
]);
359359

360360
const result = await createUpsertLetterHandler(mockedDeps)(
@@ -420,8 +420,14 @@ describe("createUpsertLetterHandler", () => {
420420
});
421421

422422
test("invalid event type produces batch failure and logs error", async () => {
423+
const message = {
424+
letterEvent: { someField: "invalid" },
425+
operationType:
426+
"uk.nhs.notify.letter-rendering.letter-request.prepared.v2",
427+
supplierSpec: { supplierId: "supplier1", specId: "spec1" },
428+
};
423429
const evt: SQSEvent = createSQSEvent([
424-
createSqsRecord("unknown-op", JSON.stringify(message)),
430+
createSqsRecord("bad-event-type", JSON.stringify(message)),
425431
]);
426432

427433
const result = await createUpsertLetterHandler(mockedDeps)(
@@ -453,18 +459,50 @@ describe("createUpsertLetterHandler", () => {
453459
);
454460
});
455461

462+
test("unknown operation type produces batch failure and logs error", async () => {
463+
const message = {
464+
letterEvent: createPreparedV1Event(),
465+
operationType: "unknown.operation.type",
466+
supplierSpec: { supplierId: "supplier1", specId: "spec1" },
467+
};
468+
const evt: SQSEvent = createSQSEvent([
469+
createSqsRecord("bad-operation-type", JSON.stringify(message)),
470+
]);
471+
472+
const result = await createUpsertLetterHandler(mockedDeps)(
473+
evt,
474+
{} as any,
475+
{} as any,
476+
);
477+
478+
expect(result).toBeDefined();
479+
if (!result) throw new Error("expected BatchResponse, got void");
480+
expect(result.batchItemFailures).toHaveLength(1);
481+
expect(result.batchItemFailures[0].itemIdentifier).toBe(
482+
"bad-operation-type",
483+
);
484+
expect(mockedDeps.letterRepo.putLetter).not.toHaveBeenCalled();
485+
expect(mockedDeps.letterRepo.updateLetterStatus).not.toHaveBeenCalled();
486+
expect((mockedDeps.logger.error as jest.Mock).mock.calls[0][0]).toEqual(
487+
expect.objectContaining({
488+
description: "Error processing upsert of record",
489+
messageId: "bad-operation-type",
490+
}),
491+
);
492+
expect(mockMetrics.setNamespace).toHaveBeenCalledWith("upsertLetter");
493+
expect(mockMetrics.putDimensions).toHaveBeenCalledWith({
494+
Supplier: "supplier1",
495+
});
496+
expect(mockMetrics.putMetric).toHaveBeenCalledWith(
497+
"MessageFailed",
498+
1,
499+
"Count",
500+
);
501+
});
502+
456503
test("valid event type and invalid schema produces batch failure and logs error", async () => {
457504
const evt: SQSEvent = createSQSEvent([
458-
createSqsRecord(
459-
"ok-msg",
460-
JSON.stringify({
461-
letterEvent: createPreparedV2Event(),
462-
operationType:
463-
"uk.nhs.notify.letter-rendering.letter-request.prepared.v2",
464-
supplierSpec: { supplierId: "supplier1", specId: "spec1" },
465-
}),
466-
),
467-
createSqsRecord("fail-msg", "invalid-json"),
505+
createSqsRecord("bad-event-schema", "invalid-json"),
468506
]);
469507

470508
const result = await createUpsertLetterHandler(mockedDeps)(
@@ -492,29 +530,27 @@ describe("createUpsertLetterHandler", () => {
492530
.mockResolvedValueOnce({})
493531
.mockRejectedValueOnce(new Error("ddb error"));
494532

533+
const message1 = {
534+
letterEvent: createPreparedV2Event({
535+
id: "7b9a03ca-342a-4150-b56b-989109c45615",
536+
domainId: "ok",
537+
}),
538+
operationType:
539+
"uk.nhs.notify.letter-rendering.letter-request.prepared.v2",
540+
supplierSpec: { supplierId: "supplier1", specId: "spec1" },
541+
};
542+
const message2 = {
543+
letterEvent: createPreparedV2Event({
544+
id: "7b9a03ca-342a-4150-b56b-989109c45616",
545+
domainId: "fail",
546+
}),
547+
operationType:
548+
"uk.nhs.notify.letter-rendering.letter-request.prepared.v2",
549+
supplierSpec: { supplierId: "supplier1", specId: "spec1" },
550+
};
495551
const evt: SQSEvent = createSQSEvent([
496-
createSqsRecord(
497-
"ok-msg",
498-
JSON.stringify(
499-
createNotification(
500-
createPreparedV2Event({
501-
id: "7b9a03ca-342a-4150-b56b-989109c45615",
502-
domainId: "ok",
503-
}),
504-
),
505-
),
506-
),
507-
createSqsRecord(
508-
"fail-msg",
509-
JSON.stringify(
510-
createNotification(
511-
createPreparedV2Event({
512-
id: "7b9a03ca-342a-4150-b56b-989109c45616",
513-
domainId: "fail",
514-
}),
515-
),
516-
),
517-
),
552+
createSqsRecord("ok-msg", JSON.stringify(message1)),
553+
createSqsRecord("fail-msg", JSON.stringify(message2)),
518554
]);
519555

520556
const result = await createUpsertLetterHandler(mockedDeps)(
@@ -529,6 +565,6 @@ describe("createUpsertLetterHandler", () => {
529565
expect(result.batchItemFailures).toHaveLength(1);
530566
expect(result.batchItemFailures[0].itemIdentifier).toBe("fail-msg");
531567

532-
expect(mockedDeps.letterRepo.putLetter).toHaveBeenCalledTimes(1);
568+
expect(mockedDeps.logger.error).toHaveBeenCalled();
533569
});
534570
});

0 commit comments

Comments
 (0)