Skip to content

#747 evaluateBalanceAlerts does not handle negative balances or NaN edge cases - #824

Merged
k-deejah merged 1 commit into
Sorokit:mainfrom
jotel-dev:fix/747-evaluateBalanceAlerts-invalid-values
Oct 18, 2026
Merged

k-deejah merged 1 commit into
Sorokit:mainfrom
jotel-dev:fix/747-evaluateBalanceAlerts-invalid-values

Conversation

@jotel-dev

Copy link
Copy Markdown
Contributor

Summary

Fixes #747

Adds validation guards and logging for null, NaN, non-finite, and negative balance values and rule thresholds in evaluateBalanceAlerts.

How Invalid Values Are Handled (Skipped vs Logged vs Error)

  1. Logged:
    • Every detected invalid value (e.g. NaN, negative balance numbers < 0, non-finite numbers like Infinity, and malformed rule thresholds) emits a descriptive warning using console.warn that identifies the asset code, rule ID, and the invalid value.
  2. Skipped:
    • Invalid comparisons are skipped (continue) rather than proceeding with direct arithmetic. This prevents false alerts (such as a negative balance -10 incorrectly firing a below: 50 alert, or a negative baseline distorting change_percent).
    • If rule.threshold is NaN, negative, or non-finite, the rule is logged and skipped.
    • If baseline (oldFloat) exists but is NaN or negative, it is logged and skipped.
  3. No Errors Thrown:
    • The function remains pure, resilient, and non-throwing. It gracefully accepts malformed or null array inputs (null or non-array rules, oldBalances, newBalances, or array elements with null items) without throwing exceptions, ensuring long-running streamAccount and createBalanceAlert polling loops remain stable.
  4. Consistent Null Baseline Handling:
    • When no baseline exists (oldFloat === null), below and above evaluate against the initial balance alone (firing once on initial crossing if past threshold), while change_percent consistently does not fire (changePercent = null) because percentage change requires a non-zero baseline.
    • Explicit null baseline entries in oldBalances are handled consistently with empty baselines.

Acceptance Criteria Covered

  • NaN and negative balance values are detected and logged or skipped.
  • null baseline values are handled consistently across all conditions.
  • Tests cover invalid balance data (NaN new balance, negative new balance, NaN baseline, negative baseline, non-finite values, null arrays, and malformed thresholds).
  • Alert types and crossing semantics are completely unchanged.

…eBalanceAlerts (Sorokit#747)

Add validation guards in evaluateBalanceAlerts to detect and handle NaN, negative, non-finite, and null balance values and rule thresholds. Malformed and negative values are logged via console.warn and skipped without throwing to prevent false alerts or stream crashes. Null baselines are handled consistently across all conditions.
@drips-wave

drips-wave Bot commented Oct 17, 2026

Copy link
Copy Markdown

@jotel-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@k-deejah
k-deejah merged commit 2857e89 into Sorokit:main Oct 18, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

evaluateBalanceAlerts does not handle negative balances or NaN edge cases

2 participants