fix: prevent calculate() crash when optional inputs are omitted#30
Open
dmchaledev wants to merge 1 commit into
Open
fix: prevent calculate() crash when optional inputs are omitted#30dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
The public calculate() function accessed data.compliance_needs.length and
data.scan_window directly, but index.d.ts documents both as optional. A
programmatic caller passing a type-valid input without these fields hit a
runtime TypeError ("Cannot read properties of undefined").
Normalize optional inputs at the entry point: scan_window defaults to 8
hours, compliance_needs and scanning_tools default to empty arrays. Add 5
regression tests covering omitted fields and a no-argument call.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L6U3fbFyrm4SQ2zmCbSe3w
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 public
calculate()function crashes for inputs that are valid according to its own type contract.index.d.tsdocumentsscan_window?andcompliance_needs?as optional, but the implementation readsdata.compliance_needs.lengthanddata.scan_windowdirectly.A programmatic caller passing a type-valid input without these fields hits a runtime error:
This is invisible to the bundled web component (its
_collectInputs()always supplies every field), so it only bites consumers using the documentedcalculate()API — exactly the audienceindex.d.tsis written for.Fix
Normalize optional inputs once at the entry of
calculateResources():scan_window→ defaults to8hours (same default the web component uses)compliance_needs→ defaults to[]scanning_tools→ defaults to[](treated as no tools →traditionalsizing)No behavior change for inputs that already supply these fields.
Tests
Added 5 regression tests under a new Optional input normalization suite. Full suite: 70 passing (was 65).
🤖 Generated with Claude Code
https://claude.ai/code/session_01L6U3fbFyrm4SQ2zmCbSe3w
Generated by Claude Code