fix: addon free_threshold calculation fixed

This commit is contained in:
2026-08-12 10:53:16 +07:00
parent 42f168e1ac
commit 3d8b78ec52
5 changed files with 36 additions and 17 deletions

View File

@@ -14,8 +14,8 @@ import type { AddonData, PricingPlans } from '../types';
const pricing: PricingPlans = {
device_price: 100,
addons: [
{ id: 'a', name: 'A', price: 50, free_threshold: 0 },
{ id: 'b', name: 'B', price: 30, free_threshold: 0 },
{ id: 'a', name: 'A', price: 50, free_threshold: -1 },
{ id: 'b', name: 'B', price: 30, free_threshold: -1 },
],
};
@@ -30,10 +30,20 @@ describe('round2', () => {
describe('sumAddonPrices', () => {
const addons: AddonData[] = pricing.addons;
it('sums selected addon prices', () => {
expect(sumAddonPrices(addons, ['a', 'b'])).toBe(80);
expect(sumAddonPrices(addons, ['a'])).toBe(50);
expect(sumAddonPrices(addons, ['unknown'])).toBe(0);
expect(sumAddonPrices(addons, [])).toBe(0);
expect(sumAddonPrices(addons, ['a', 'b'], 6)).toBe(80);
expect(sumAddonPrices(addons, ['a'], 6)).toBe(50);
expect(sumAddonPrices(addons, ['unknown'], 6)).toBe(0);
expect(sumAddonPrices(addons, [], 6)).toBe(0);
});
it('makes addons free at their device threshold but keeps -1 paid', () => {
const thresholdAddons: AddonData[] = [
{ id: 'threshold', name: 'Threshold', price: 50, free_threshold: 6 },
{ id: 'paid', name: 'Paid', price: 30, free_threshold: -1 },
];
expect(sumAddonPrices(thresholdAddons, ['threshold', 'paid'], 5)).toBe(80);
expect(sumAddonPrices(thresholdAddons, ['threshold', 'paid'], 6)).toBe(30);
});
});