All files / tr8-script/validators/rules remaining-allocation-policy-rule.ts

100% Statements 31/31
100% Branches 22/22
100% Functions 6/6
100% Lines 30/30

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                      1x   8x 8x   8x 8x 8x 8x     8x             8x   9x       5x     8x 8x 8x                 16x 16x   16x 14x 14x     16x 16x   16x 2x               14x 2x           12x 2x           10x 1x           9x                
import { autoInjectable } from "tsyringe";
 
import {
  DestinationAllocationPolicy,
  SourceAllocationPolicy,
  Tr8AST,
  Tx,
} from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class RemainingAllocationPolicyRule implements SemanticValidationRule {
  validate(ast: Tr8AST): ValidationResult {
    let isValid = true;
    const errors: string[] = [];
 
    for (const tx of ast.txs) {
      const { isValid: txIsValid, errors: txErrors } = this.validateTx(tx);
      isValid &&= txIsValid;
      errors.push(...txErrors);
    }
 
    return {
      isValid,
      errors,
    };
  }
 
  validateTx(tx: Tx): ValidationResult {
    return [
      this.validateAllocationPolicies(
        tx.sources.map((source) => source.allocationPolicy),
      ),
 
      this.validateAllocationPolicies(
        tx.destinations.map((dest) => dest.allocationPolicy),
      ),
    ].reduce((acc, curr) => {
      acc.isValid = acc.isValid && curr.isValid;
      acc.errors.push(...curr.errors);
      return acc;
    });
  }
 
  validateAllocationPolicies(
    allocationPolicies:
      | SourceAllocationPolicy[]
      | DestinationAllocationPolicy[],
  ): ValidationResult {
    const policyCountMap: Map<string, number> = new Map();
    const policies: string[] = [];
 
    for (const p of allocationPolicies) {
      policyCountMap.set(p.policy, (policyCountMap.get(p.policy) || 0) + 1);
      policies.push(p.policy);
    }
 
    const remainingCount = policyCountMap.get("remaining") ?? 0;
    const maxCount = policyCountMap.get("max") ?? 0;
 
    if (remainingCount > 1) {
      return {
        isValid: false,
        errors: [
          "Only one remaining policy is allowed per source/destination.",
        ],
      };
    }
 
    if (remainingCount === 1 && policies.at(-1) !== "remaining") {
      return {
        isValid: false,
        errors: ["Remaining policy can only be placed at the end."],
      };
    }
 
    if (remainingCount === 1 && policies.length === 1) {
      return {
        isValid: false,
        errors: ["Remaining policy must have a max/split policy before it."],
      };
    }
 
    if (maxCount > 0 && remainingCount === 0) {
      return {
        isValid: false,
        errors: ["Max policy must be followed by remaining policy."],
      };
    }
 
    return {
      isValid: true,
      errors: [],
    };
  }
}
 
export { RemainingAllocationPolicyRule };