import { SQSWidgetRequestRetriever } from "@/consumer/retrievers"; import { WidgetRequest } from "@/shared/schema"; import { DeleteMessageBatchCommand, ReceiveMessageCommand, SQSClient, } from "@aws-sdk/client-sqs"; import { expect, mock, test } from "bun:test"; import { randomUUID } from "node:crypto"; import { VoidLogger } from "@/shared/t"; test("when widget request in SQS then reads, deletes, and returns", async () => { const logger = new VoidLogger(); const widgetRequest = WidgetRequest.parse({ type: "create", requestId: randomUUID(), owner: "simponic", widgetId: randomUUID(), }); const queueUrl = "https://asdf.fdsa.asdf"; const deleteMessagesBatch = mock(() => true); const getMessages = mock(() => { return { Messages: [ { Body: JSON.stringify(widgetRequest), MessageId: "fdsa", ReceiptHandle: "asdf", }, ], }; }); const mockSQSClient = { send: async (req: ReceiveMessageCommand | DeleteMessageBatchCommand) => { if (req instanceof ReceiveMessageCommand) { if (req.input.QueueUrl === queueUrl) return getMessages(); } if (req instanceof DeleteMessageBatchCommand) { expect( req.input.Entries?.find( (x) => x.Id === "fdsa" && x.ReceiptHandle === "asdf", ), ).toBeTruthy(); if (req.input.QueueUrl === queueUrl) return deleteMessagesBatch(); } return; }, }; const retriever = new SQSWidgetRequestRetriever( queueUrl, mockSQSClient as unknown as SQSClient, logger, ); expect(await retriever.refreshRequests()).toEqual([widgetRequest]); expect(deleteMessagesBatch).toHaveBeenCalledTimes(1); expect(getMessages).toHaveBeenCalledTimes(1); }); test("when empty widget request in SQS then returns empty", async () => { const logger = new VoidLogger(); const queueUrl = "https://asdf.fdsa.asdf"; const getMessages = mock(() => { return {}; }); const deleteMessagesBatch = mock(() => true); const mockSQSClient = { send: async (req: ReceiveMessageCommand | DeleteMessageBatchCommand) => { if (req instanceof ReceiveMessageCommand) { if (req.input.QueueUrl === queueUrl) return getMessages(); } if (req instanceof DeleteMessageBatchCommand) { if (req.input.QueueUrl === queueUrl) return deleteMessagesBatch(); } return; }, }; const retriever = new SQSWidgetRequestRetriever( queueUrl, mockSQSClient as unknown as SQSClient, logger, ); expect(await retriever.refreshRequests()).toBeEmpty(); expect(getMessages).toHaveBeenCalledTimes(1); expect(deleteMessagesBatch).toHaveBeenCalledTimes(0); });