Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/prices/__tests__/is-variable-price.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,28 @@ describe('isVariablePrice', () => {
expect(isVariablePrice(makePrice({ pricing_model: 'external_getag' }))).toBe(false);
});

it('returns false for CompositePrice', () => {
it('returns true for undefined pricing_model with variable_price true', () => {
expect(isVariablePrice({ variable_price: true } as Price)).toBe(true);
});

it('returns false for undefined pricing_model with variable_price false', () => {
expect(isVariablePrice({ variable_price: false } as Price)).toBe(false);
});

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests cover undefined pricing_model, but there’s no coverage for pricing_model: null, which appears in fixtures and is treated as missing elsewhere (e.g., pricing_model || PricingModel.perUnit). Add a test case for null to lock in the intended behavior (especially when variable_price is true/false).

Suggested change
it('returns true for null pricing_model with variable_price true', () => {
expect(isVariablePrice({ pricing_model: null, variable_price: true } as Price)).toBe(true);
});
it('returns false for null pricing_model with variable_price false', () => {
expect(isVariablePrice({ pricing_model: null, variable_price: false } as Price)).toBe(false);
});

Copilot uses AI. Check for mistakes.
it('returns true for null pricing_model with variable_price true', () => {
expect(isVariablePrice({ pricing_model: null, variable_price: true } as unknown as Price)).toBe(true);
});
it('returns false for null pricing_model with variable_price false', () => {
expect(isVariablePrice({ pricing_model: null, variable_price: false } as unknown as Price)).toBe(false);
});

it('returns false for CompositePrice with is_composite_price', () => {
const composite = { is_composite_price: true, price_components: [] } as unknown as CompositePrice;

expect(isVariablePrice(composite)).toBe(false);
});

it('returns false for CompositePrice with price_components array', () => {
const composite = { price_components: [makePrice()] } as unknown as CompositePrice;

expect(isVariablePrice(composite)).toBe(false);
Expand Down
25 changes: 14 additions & 11 deletions src/prices/is-variable-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ const isTieredPrice = (price: Price): boolean => {
};

export const isVariablePrice = (price: Price | CompositePrice): boolean => {
if (price.is_composite_price) {
return false;
}
if (price.is_composite_price || Array.isArray(price.price_components)) return false;

const p = price as Price;
const { pricing_model, get_ag, variable_price } = p;

if (isTieredPrice(p) || pricing_model === PricingModel.dynamicTariff) return true;

if (pricing_model === PricingModel.externalGetAG) {
return (
get_ag?.type === TypeGetAg.workPrice ||
(get_ag?.type === TypeGetAg.basePrice && get_ag?.markup_pricing_model === MarkupPricingModel.tieredFlatFee)
);
}

if (isTieredPrice(p)) return true;
if (p.pricing_model === PricingModel.dynamicTariff) return true;
if (p.pricing_model === PricingModel.externalGetAG) {
if (p.get_ag?.type === TypeGetAg.workPrice) return true;
if (p.get_ag?.type === TypeGetAg.basePrice && p.get_ag?.markup_pricing_model === MarkupPricingModel.tieredFlatFee) {
return true;
}
// We treat undefined or null pricing_model as perUnit
if (pricing_model === PricingModel.perUnit || !pricing_model) {
return Boolean(variable_price);
}
if (p.pricing_model === PricingModel.perUnit && p.variable_price) return true;

return false;
};
Expand Down
Loading