1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
});
|