1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

fix: make traffic usage test more robust by using this month's actual numbers (#9193)

The test was breaking because it assumed a month would have at least
30 days.

Because the test relies on the current month, this isn't necessarily
true.

Further, there's parts of the code that relies on "impure" state via
the "current date" (which will change based on when you run it), so
setting a specific month in the test won't work.

As such, this test makes the calculation explicit and uses the number
of days in the current month.
This commit is contained in:
Thomas Heartman 2025-02-03 14:29:07 +01:00 committed by GitHub
parent bd12cfce7c
commit fd1ad31bb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
import { getDaysInMonth } from 'date-fns';
import { import {
toSelectablePeriod, toSelectablePeriod,
calculateOverageCost, calculateOverageCost,
@ -122,18 +123,22 @@ describe('traffic overage calculation', () => {
const now = new Date(); const now = new Date();
const period = toSelectablePeriod(now); const period = toSelectablePeriod(now);
const testNow = new Date(now.getFullYear(), now.getMonth(), 5); const testNow = new Date(now.getFullYear(), now.getMonth(), 5);
const includedTraffic = 53_000_000;
const trafficUnitSize = 500_000;
const trafficUnitCost = 10;
const result = calculateEstimatedMonthlyCost( const result = calculateEstimatedMonthlyCost(
period.key, period.key,
testData, testData,
53_000_000, includedTraffic,
testNow, testNow,
10, trafficUnitCost,
500_000, trafficUnitSize,
); );
// 22_500_000 * 3 * 30 = 2_025_000_000 total usage
// 2_025_000_000 - 53_000_000 = 1_972_000_000 overage const totalExpectedUsage = 22_500_000 * 3 * getDaysInMonth(now);
// 1_972_000_000 / 500_000 = 3_944 overage units const overage = totalExpectedUsage - includedTraffic;
// 3_944 * 10 = 39_440 const overageUnits = Math.floor(overage / trafficUnitSize);
expect(result).toBeGreaterThanOrEqual(39_440); const total = overageUnits * trafficUnitCost;
expect(result).toBe(total);
}); });
}); });