-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathletter-allocation-capacity.spec.ts
More file actions
209 lines (182 loc) · 6.98 KB
/
Copy pathletter-allocation-capacity.spec.ts
File metadata and controls
209 lines (182 loc) · 6.98 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { expect, test } from "@playwright/test";
import { sendSnsEvent } from "tests/helpers/send-sns-event";
import { createPreparedV1Event } from "tests/helpers/event-fixtures";
import { randomUUID } from "node:crypto";
import { logger } from "tests/helpers/pino-logger";
import {
getAllocationLog,
getAllocationLogForDomainId,
getExceededDailyCapacityLog,
getLetterVariantConfigFromDb,
getOrSeedLetterDailyAllocationFromDb,
getVariantsForAllocation,
updateSupplierDailyAllocation,
} from "tests/helpers/allocation-helper";
import { getLettersFromSupplierTable } from "tests/helpers/generate-fetch-test-data";
test.describe("Allocator Lambda Tests", () => {
test.setTimeout(180_000); // 3 minutes for long running polling
test(`Verify that allocator successfully allocates a letter and emits PENDING event`, async () => {
const domainId = randomUUID();
logger.info(`Testing event subscription with domainId: ${domainId}`);
const letterVariant = getVariantsForAllocation(1);
const preparedEvent = createPreparedV1Event({
domainId,
letterVariantId: letterVariant,
});
const response = await sendSnsEvent(preparedEvent);
expect(response.MessageId).toBeTruthy();
const supplierAllocatorLog = await getAllocationLogForDomainId(domainId);
const supplierId =
supplierAllocatorLog.msg?.allocationDetails?.supplierSpec?.supplierId;
const lettersInDb = await getLettersFromSupplierTable(
supplierId!,
domainId,
"PENDING",
);
const allocationStatus =
supplierAllocatorLog.msg?.allocationDetails?.allocationStatus?.status;
expect(lettersInDb).toBeTruthy();
expect(lettersInDb.status).toBe(allocationStatus);
});
test("Verify that first eligible supplier is selected", async () => {
const letterVariant = getVariantsForAllocation(1);
const domainId = randomUUID();
const letterVariantConfig =
await getLetterVariantConfigFromDb(letterVariant);
expect(letterVariantConfig.packSpecificationIds).toEqual(
expect.arrayContaining(["notify-c4", "notify-c5"]),
);
const preparedEvent = createPreparedV1Event({
domainId,
letterVariantId: letterVariant,
pageCount: 11, // pagecount that makes notify-c5 ineligible and notify-c4 eligible based on their pack configs (note packs are duplex)
});
const response = await sendSnsEvent(preparedEvent);
expect(response.MessageId).toBeTruthy();
const supplierAllocatorLog = await getAllocationLog(
"Pack specification filtered out based on pageCount constraints",
{ extraPatterns: [domainId] },
);
const filteredPackSpecId = supplierAllocatorLog.packSpecId;
logger.info(`Pack spec filtered out ${filteredPackSpecId}`);
expect(filteredPackSpecId).toBe("notify-c5");
expect(letterVariantConfig.packSpecificationIds).toContain(
filteredPackSpecId as string,
);
const allocationLog = await getAllocationLogForDomainId(domainId);
const lettersInDb = await getLettersFromSupplierTable(
allocationLog.msg?.allocationDetails?.supplierSpec?.supplierId!,
domainId,
"PENDING",
);
expect(lettersInDb.status).toBe("PENDING");
const allocatedPackSpecId =
allocationLog.msg?.allocationDetails?.supplierSpec?.specId;
expect(allocatedPackSpecId).toBe("notify-c4");
expect(lettersInDb.specificationId).toBe(allocatedPackSpecId);
});
test("Verify if suppliers without capacity are filtered out", async () => {
const letterVariant = getVariantsForAllocation(4);
const domainId = `supcapacity-4-${randomUUID()}`;
const dailyAllocatedCapacity = 500_000;
const allocationDate = new Intl.DateTimeFormat("en-CA", {
timeZone: "Europe/London",
}).format(new Date());
const dailyAllocation = await getOrSeedLetterDailyAllocationFromDb(
{
supplier1: 0,
supplier2: 0,
},
allocationDate,
);
logger.info(
`Daily allocation before test execution ${JSON.stringify(dailyAllocation.allocations)}`,
);
const originalSupplier1Allocation =
dailyAllocation.allocations.supplier3 ?? 0;
// set supplier1 to exactly daily capacity so allocator filters it out
if (dailyAllocation.allocations.supplier1 !== dailyAllocatedCapacity) {
await updateSupplierDailyAllocation(
"supplier3",
dailyAllocatedCapacity,
allocationDate,
);
}
const preparedEvent = createPreparedV1Event({
domainId,
letterVariantId: letterVariant,
});
const response = await sendSnsEvent(preparedEvent);
expect(response.MessageId).toBeTruthy();
const exceededCapacityLog = await getExceededDailyCapacityLog("supplier3");
expect(exceededCapacityLog.description).toBe(
"Supplier has exceeded daily capacity",
);
const lettersInDb = await getLettersFromSupplierTable(
"supplier4",
domainId,
"PENDING",
);
expect(lettersInDb.status).toBe("PENDING");
expect(lettersInDb.supplierId).toBe("supplier4");
await updateSupplierDailyAllocation(
"supplier3",
originalSupplier1Allocation,
allocationDate,
);
});
test("Verify that fallback is triggered when a suppliers are at daily capacity, ignoring capacity", async () => {
const letterVariant = getVariantsForAllocation(5);
const domainId = `supcapacity-5-${randomUUID()}`;
const dailyAllocatedCapacity = 500_000;
const allocationDate = new Intl.DateTimeFormat("en-CA", {
timeZone: "Europe/London",
}).format(new Date());
const dailyAllocation = await getOrSeedLetterDailyAllocationFromDb(
{
supplier5: 0,
},
allocationDate,
);
logger.info(
`Daily allocation before test execution ${JSON.stringify(dailyAllocation.allocations)}`,
);
const originalSupplierAllocation =
dailyAllocation.allocations.supplier5 ?? 0;
// set supplier1 to exactly daily capacity so allocator filters it out
if (dailyAllocation.allocations.supplier1 !== dailyAllocatedCapacity) {
await updateSupplierDailyAllocation(
"supplier5",
dailyAllocatedCapacity,
allocationDate,
);
}
const preparedEvent = createPreparedV1Event({
domainId,
letterVariantId: letterVariant,
});
const response = await sendSnsEvent(preparedEvent);
expect(response.MessageId).toBeTruthy();
const exceededCapacityLog = await getExceededDailyCapacityLog("supplier5");
expect(exceededCapacityLog.description).toBe(
"Supplier has exceeded daily capacity",
);
const lettersInDb = await getLettersFromSupplierTable(
"supplier5",
domainId,
"PENDING",
);
expect(lettersInDb.status).toBe("PENDING");
const fallbackDailyAllocation = await getOrSeedLetterDailyAllocationFromDb({
supplier5: 0,
});
expect(fallbackDailyAllocation.allocations.supplier5).toBeGreaterThan(
dailyAllocatedCapacity,
);
await updateSupplierDailyAllocation(
"supplier5",
originalSupplierAllocation,
allocationDate,
);
});
});