Skip to content

Commit b65a46c

Browse files
fix flaky tests
1 parent eed9947 commit b65a46c

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

internal/datastore/src/supplier-config-repository.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export class SupplierConfigRepository {
125125
},
126126
}),
127127
);
128-
129128
return $SupplierPack.array().parse(result.Items);
130129
}
131130

tests/component-tests/allocation-tests/allocation-target-percentage.spec.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { randomUUID } from "node:crypto";
22
import test, { expect } from "playwright/test";
33
import {
44
getAllocationLogForDomainId,
5+
getLetterVariantConfigFromDb,
6+
getSupplierFromSupplierPack,
57
getVariantsForAllocation,
68
updateSupplierAllocation,
79
} from "tests/helpers/allocation-helper";
@@ -14,10 +16,13 @@ test.describe("Allocation Target Percentage Tests", () => {
1416
test("Verify that supplier with zero target percentage is handled correctly", async () => {
1517
const letterVariant = getVariantsForAllocation(8);
1618
const domainId = `Zero-Percentage-${randomUUID()}`;
17-
const volumeGroupId = "volumeGroup-test2";
19+
20+
const letterVariantConfig =
21+
await getLetterVariantConfigFromDb(letterVariant);
22+
const volGroupId = letterVariantConfig.volumeGroupId;
1823

1924
// update target percentage
20-
await updateSupplierAllocation("supplier1", volumeGroupId, 0);
25+
await updateSupplierAllocation("supplier1", volGroupId, 0);
2126
const preparedEvent = createPreparedV1Event({
2227
domainId,
2328
letterVariantId: letterVariant,
@@ -44,12 +49,18 @@ test.describe("Allocation Target Percentage Tests", () => {
4449
});
4550

4651
test("Verify that supplier with less than 100 target percentage is handled correctly", async () => {
47-
const letterVariant = getVariantsForAllocation(8);
52+
const letterVariant = getVariantsForAllocation(5);
4853
const domainId = `Less-Than-100-Percentage-${randomUUID()}`;
49-
const volumeGroupId = "volumeGroup-test2";
54+
55+
const letterVariantConfig =
56+
await getLetterVariantConfigFromDb(letterVariant);
57+
const volGroupId = letterVariantConfig.volumeGroupId;
58+
const [packSpecificationId] = letterVariantConfig.packSpecificationIds;
5059

5160
// update target percentage
52-
await updateSupplierAllocation("supplier1", volumeGroupId, 50);
61+
const [supplier] = await getSupplierFromSupplierPack(packSpecificationId);
62+
63+
await updateSupplierAllocation(supplier, volGroupId, 50);
5364
const preparedEvent = createPreparedV1Event({
5465
domainId,
5566
letterVariantId: letterVariant,
@@ -68,6 +79,6 @@ test.describe("Allocation Target Percentage Tests", () => {
6879
expect(lettersInDb.supplierId).toBe(
6980
allocationLog.msg?.allocationDetails?.supplierSpec?.supplierId,
7081
);
71-
updateSupplierAllocation("supplier1", volumeGroupId, 100); // reset target percentage to avoid impact on other tests
82+
await updateSupplierAllocation(supplier, volGroupId, 100); // reset target percentage to avoid impact on other tests
7283
});
7384
});

tests/component-tests/allocation-tests/letter-allocation-rejected.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,18 @@ test.describe("Allocator Rejected Allocation Tests", () => {
212212
expect(lettersInDb.reasonText).toContain(
213213
`Volume group with id ${volumeGroupId} is not active`,
214214
);
215+
216+
const resolvedOriginalEndDate =
217+
originalEndDate ??
218+
new Date(Date.now() + 365 * 24 * 60 * 60 * 1000)
219+
.toISOString()
220+
.split("T")[0];
221+
215222
await updateVolumeGroupData(
216223
volumeGroupId,
217-
fieldToUpdate === "startDate" ? originalStartDate : originalEndDate,
224+
fieldToUpdate === "startDate"
225+
? originalStartDate
226+
: resolvedOriginalEndDate,
218227
fieldToUpdate,
219228
);
220229
});

tests/helpers/allocation-helper.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
DynamoDBDocumentClient,
44
GetCommand,
55
PutCommand,
6+
QueryCommand,
67
UpdateCommand,
78
} from "@aws-sdk/lib-dynamodb";
89
import { envName } from "tests/constants/api-constants";
@@ -91,6 +92,7 @@ type LetterVariantConfig = {
9192
sides: Record<string, number>;
9293
sheets: Record<string, number>;
9394
};
95+
volumeGroupId: string;
9496
};
9597

9698
type DailyAllocationConfig = {
@@ -484,3 +486,37 @@ export async function updateLetterVariantConfig(
484486
}),
485487
);
486488
}
489+
490+
export async function getSupplierFromSupplierPack(
491+
packSpecificationId: string,
492+
): Promise<string[]> {
493+
const { Items } = await docClient.send(
494+
new QueryCommand({
495+
TableName: getSupplierConfigTableName(),
496+
IndexName: "packSpecificationId-index",
497+
KeyConditionExpression:
498+
"#pk = :pk AND #packSpecificationId = :packSpecificationId",
499+
FilterExpression: "#status = :status AND #approval = :approval",
500+
ExpressionAttributeNames: {
501+
"#pk": "pk",
502+
"#packSpecificationId": "packSpecificationId",
503+
"#status": "status",
504+
"#approval": "approval",
505+
},
506+
ExpressionAttributeValues: {
507+
":pk": "ENTITY#supplier-pack",
508+
":packSpecificationId": packSpecificationId,
509+
":status": "PROD",
510+
":approval": "APPROVED",
511+
},
512+
}),
513+
);
514+
515+
if (!Items || Items.length === 0) {
516+
throw new Error(
517+
`No suppliers found for pack specification ${packSpecificationId}`,
518+
);
519+
}
520+
521+
return Items.map((item) => String(item.supplierId));
522+
}

0 commit comments

Comments
 (0)