All files / tr8-script/validators/rules unbounded-overdraft-rule.ts

96.87% Statements 31/32
80% Branches 16/20
83.33% Functions 5/6
96.77% Lines 30/31

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            1x   8x 8x   8x 8x       8x 8x     8x             8x 8x   8x   11x 5x   2x 2x         8x 3x 2x 3x 3x 3x 1x     3x 2x 2x           1x 1x         1x 1x             8x                
import { autoInjectable } from "tsyringe";
 
import { Tr8AST, Tr8ASTType, Tx } from "../../domain/ast.js";
import { SemanticValidationRule, ValidationResult } from "../validator.js";
 
@autoInjectable()
class UnboundedOverdraftRule 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,
        ast.type,
      );
      isValid &&= txIsValid;
      errors.push(...txErrors);
    }
 
    return {
      isValid,
      errors,
    };
  }
 
  validateTx(tx: Tx, astType: Tr8ASTType) {
    let isValid = true;
    const errors: string[] = [];
 
    if (
      tx.sources
        .flatMap((source) => source.constraints)
        .some((c) => c.amount === "unbounded" && c.constraint !== "overdraft")
    ) {
      isValid &&= false;
      errors.push(
        "Unbounded amount can only be used with overdraft constraint.",
      );
    }
 
    if (astType === "NOTIF") {
      if (tx.refValue.type === "absolute") {
        for (const [index, source] of tx.sources.entries()) {
          const { constraints, allocationPolicy } = source;
          const hasMax = allocationPolicy.policy === "max";
          const hasUnboundedOverdraft = constraints.some(
            (c) => c.amount === "unbounded" && c.constraint === "overdraft",
          );
 
          if (!hasMax && !hasUnboundedOverdraft) {
            isValid &&= false;
            errors.push(
              `[sources-${index}] NOTIF must have max allocation policy or unbounded overdraft constraint.`,
            );
          }
        }
      } else {
        const constraints = tx.sources.at(-1)?.constraints ?? [];
        Eif (
          !constraints.some(
            (c) => c.amount === "unbounded" && c.constraint === "overdraft",
          )
        ) {
          isValid &&= false;
          errors.push(
            "[sources] NOTIF must have last source with unbounded overdraft constraint.",
          );
        }
      }
    }
 
    return {
      isValid,
      errors,
    };
  }
}
 
export { UnboundedOverdraftRule };