fix: make calculate() tolerate omitted optional inputs#29
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
The exported calculate() API crashed or produced NaN when called with the
fields that index.d.ts marks as optional:
- omitting `compliance_needs` threw "Cannot read properties of undefined
(reading 'length')"
- omitting `scan_window` produced NaN window utilization and a bogus
"poor" efficiency rating
Add a normalizeInputs() step that defaults and clamps inputs to the same
limits the form UI already enforces (target_hosts 1–50000, scan_window
1–24), coerces non-array tool/compliance lists, and supplies sensible
defaults. Empty scanning_tools is preserved as "no tools" (zero cost);
the web component keeps its own openvas fallback.
Add 6 regression tests covering omitted/invalid/out-of-range inputs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VyuV5gj943Td821veoK9AL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The exported pure
calculate()API crashes or returns garbage when called with the very fields that the publishedindex.d.tsmarks as optional:Verified against the current code:
calculate({ target_hosts, scan_intensity, scan_frequency, scan_window, scanning_tools })— omitcompliance_needsTypeError: Cannot read properties of undefined (reading 'length')(calcAsmResources→data.compliance_needs.length)scan_windowscan_window_utilization: NaN,efficiency_rating: "poor"(bogus)So any consumer who follows the published type contract and omits an optional field gets a crash or nonsense output. The DOM path is unaffected because
_collectInputs()already fills/clamps these — but the pure function (a documented public export) had no such guard.Fix
Add a
normalizeInputs()step at the entry of the calculation engine that:scan_intensity(medium),scan_frequency(weekly),scan_window(8)target_hoststo 1–50000 andscan_windowto 1–24 — the same limits the form UI already enforces in_collectInputs(), now centralized so the API and UI agreescanning_tools/compliance_needsto[]scanning_toolslist as "no tools" → zero tool cost (existing documented behavior; the web component keeps its ownopenvasfallback in_run())No behavior change for valid in-range inputs — all 65 pre-existing tests still pass.
Tests
Added 6 regression tests (
Input normalization & optional fields) covering omittedcompliance_needs/scan_window, non-array values, out-of-range clamping, and a fully empty{}input.🤖 Generated with Claude Code
https://claude.ai/code/session_01VyuV5gj943Td821veoK9AL
Generated by Claude Code