summaryrefslogtreecommitdiff
path: root/Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
commit6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch)
treeed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts
downloadmisc-undergrad-main.tar.gz
misc-undergrad-main.zip
Diffstat (limited to 'Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts')
-rw-r--r--Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts95
1 files changed, 95 insertions, 0 deletions
diff --git a/Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts b/Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts
new file mode 100644
index 0000000..cae6a65
--- /dev/null
+++ b/Homework/cs5260/consumer/t/SQSWidgetRequestRetriever.spec.ts
@@ -0,0 +1,95 @@
+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);
+});