|
| 1 | +import { |
| 2 | + describe, |
| 3 | + it, |
| 4 | + expect, |
| 5 | + jest |
| 6 | +} from "@jest/globals" |
| 7 | +import {SpiedFunction} from "jest-mock" |
| 8 | + |
| 9 | +import {Logger} from "@aws-lambda-powertools/logger" |
| 10 | +import {LogItemMessage, LogItemExtraInput} from "@aws-lambda-powertools/logger/lib/cjs/types/Logger" |
| 11 | +import * as sqs from "@aws-sdk/client-sqs" |
| 12 | + |
| 13 | +export function mockSQSClient() { |
| 14 | + const mockSend = jest.fn() |
| 15 | + jest.unstable_mockModule("@aws-sdk/client-sqs", () => { |
| 16 | + return { |
| 17 | + ...sqs, |
| 18 | + SQSClient: jest.fn().mockImplementation(() => ({ |
| 19 | + send: mockSend |
| 20 | + })) |
| 21 | + } |
| 22 | + }) |
| 23 | + return {mockSend} |
| 24 | +} |
| 25 | + |
| 26 | +const {mockSend} = mockSQSClient() |
| 27 | + |
| 28 | +const {getQueueUrl, reportQueueStatus} = await import("../src/sqs") |
| 29 | + |
| 30 | +const ORIGINAL_ENV = {...process.env} |
| 31 | + |
| 32 | +describe("sqs", () => { |
| 33 | + let logger: Logger |
| 34 | + let infoSpy: SpiedFunction<(input: LogItemMessage, ...extraInput: LogItemExtraInput) => void> |
| 35 | + let errorSpy: SpiedFunction<(input: LogItemMessage, ...extraInput: LogItemExtraInput) => void> |
| 36 | + // let warnSpy: SpiedFunction<(input: LogItemMessage, ...extraInput: LogItemExtraInput) => void> |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + jest.resetModules() |
| 40 | + jest.clearAllMocks() |
| 41 | + |
| 42 | + // Reset environment |
| 43 | + process.env = {...ORIGINAL_ENV} |
| 44 | + |
| 45 | + // Fresh logger and spies |
| 46 | + logger = new Logger({serviceName: "test-service"}) |
| 47 | + infoSpy = jest.spyOn(logger, "info") |
| 48 | + errorSpy = jest.spyOn(logger, "error") |
| 49 | + // warnSpy = jest.spyOn(logger, "warn") |
| 50 | + }) |
| 51 | + |
| 52 | + describe("getQueueUrl", () => { |
| 53 | + it("Should return the SQS queue URL from environment variables", () => { |
| 54 | + const testUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/test-queue" |
| 55 | + process.env.POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL = testUrl |
| 56 | + |
| 57 | + const result = getQueueUrl(logger) |
| 58 | + expect(result).toBe(testUrl) |
| 59 | + }) |
| 60 | + |
| 61 | + it("Should throw an error if the SQS queue URL is not configured", () => { |
| 62 | + delete process.env.POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL |
| 63 | + |
| 64 | + expect(() => getQueueUrl(logger)).toThrow("POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL not set") |
| 65 | + expect(errorSpy).toHaveBeenCalledWith("Post-dated prescriptions SQS URL not configured") |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe("reportQueueStatus", () => { |
| 70 | + it("Should report the current status of the SQS queue", async () => { |
| 71 | + const testUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/test-queue" |
| 72 | + process.env.POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL = testUrl |
| 73 | + |
| 74 | + // Mock SQS response |
| 75 | + mockSend.mockReturnValueOnce({ |
| 76 | + Attributes: { |
| 77 | + ApproximateNumberOfMessages: "5", |
| 78 | + ApproximateNumberOfMessagesNotVisible: "2", |
| 79 | + ApproximateNumberOfMessagesDelayed: "1" |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + await reportQueueStatus(logger) |
| 84 | + |
| 85 | + expect(mockSend).toHaveBeenCalledTimes(1) |
| 86 | + expect(infoSpy).toHaveBeenCalledWith( |
| 87 | + "Current post-dated queue attributes (if a value failed to fetch, it will be reported as -1):", |
| 88 | + { |
| 89 | + ApproximateNumberOfMessages: 5, |
| 90 | + ApproximateNumberOfMessagesNotVisible: 2, |
| 91 | + ApproximateNumberOfMessagesDelayed: 1 |
| 92 | + } |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it("Should handle missing attributes gracefully", async () => { |
| 97 | + const testUrl = "https://sqs.us-east-1.amazonaws.com/123456789012/test-queue" |
| 98 | + process.env.POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL = testUrl |
| 99 | + |
| 100 | + // Mock SQS response with missing attributes |
| 101 | + mockSend.mockReturnValueOnce({ |
| 102 | + Attributes: {} |
| 103 | + }) |
| 104 | + |
| 105 | + await reportQueueStatus(logger) |
| 106 | + |
| 107 | + expect(mockSend).toHaveBeenCalledTimes(1) |
| 108 | + expect(infoSpy).toHaveBeenCalledWith( |
| 109 | + "Current post-dated queue attributes (if a value failed to fetch, it will be reported as -1):", |
| 110 | + { |
| 111 | + ApproximateNumberOfMessages: -1, |
| 112 | + ApproximateNumberOfMessagesNotVisible: -1, |
| 113 | + ApproximateNumberOfMessagesDelayed: -1 |
| 114 | + } |
| 115 | + ) |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments