All files / linter checkPercentageExceedingSlot.ts

68.18% Statements 15/22
58.33% Branches 7/12
100% Functions 1/1
68.18% Lines 15/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x         3x 6x   6x 6x 6x   6x       6x     6x       6x   6x   6x 12x   12x 15x                                                
import type { Group, Rule } from "@featurevisor/types";
 
import { Datasource } from "../datasource";
 
// @NOTE: ideally in future, this check should be done from Feature level,
// as well as Group level as done here
export async function checkForFeatureExceedingGroupSlotPercentage(
  datasource: Datasource,
  group: Group,
  availableFeatureKeys: string[],
) {
  for (const slot of group.slots) {
    const maxPercentageForRule = slot.percentage;
 
    if (slot.feature) {
      const featureKey = slot.feature;
      const featureExists = availableFeatureKeys.indexOf(featureKey) > -1;
 
      Iif (!featureExists) {
        throw new Error(`Unknown feature "${featureKey}"`);
      }
 
      const parsedFeature = await datasource.readFeature(featureKey);
 
      const hasEnvironments =
        parsedFeature.rules &&
        !Array.isArray(parsedFeature.rules) &&
        Object.keys(parsedFeature.rules).length > 0;
 
      if (hasEnvironments && parsedFeature.rules) {
        // with environments
        const environmentKeys = Object.keys(parsedFeature.rules);
 
        for (const environmentKey of environmentKeys) {
          const rules = parsedFeature.rules[environmentKey];
 
          for (const rule of rules) {
            Iif (rule.percentage > maxPercentageForRule) {
              // @NOTE: this does not help with same feature belonging to multiple slots. fix that.
              throw new Error(
                `Feature ${featureKey}'s rule ${rule.key} in ${environmentKey} has a percentage of ${rule.percentage} which is greater than the maximum percentage of ${maxPercentageForRule} for the slot`,
              );
            }
          }
        }
      } else IEif (parsedFeature.rules) {
        // no environments
        const rules = parsedFeature.rules as Rule[];
 
        for (const rule of rules) {
          Iif (rule.percentage > maxPercentageForRule) {
            // @NOTE: this does not help with same feature belonging to multiple slots. fix that.
            throw new Error(
              `Feature ${featureKey}'s rule ${rule.key} has a percentage of ${rule.percentage} which is greater than the maximum percentage of ${maxPercentageForRule} for the slot`,
            );
          }
        }
      }
    }
  }
}