fix: make calculate() robust to omitted optional inputs#23
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
The public calculate() API crashed with a TypeError when the
index.d.ts-optional fields compliance_needs or scanning_tools were
omitted, and emitted NaN for scan_window_utilization when scan_window
was missing. Inputs are now normalized at the entry point so the pure
function honors the optional contract declared in index.d.ts and the
README's `calculate({ /* inputs */ })` example.
- Add normalizeInputs() with safe defaults (empty arrays, 8h window)
- Make scanning_tools optional and drop the stale time_pressure field
from index.d.ts
- Add 4 regression tests for omitted optional fields
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pfju9nug2U4CWdXCKfzCce
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()API is documented to accept several optional fields —index.d.tsmarksscan_window?andcompliance_needs?as optional, and the README showscalculate({ /* inputs */ }). But the runtime didn't honor that contract:So any consumer following the published types or the README quick-start hits a crash or gets
NaNin the result. This is a contract mismatch betweenindex.d.tsand the implementation, not just a theoretical edge case.Fix
normalizeInputs()step at the entry ofcalculateResources()that fills safe defaults: empty arrays forscanning_tools/compliance_needsand a default 8-hourscan_window(matching the web component's own default). Empty arrays stay empty, so existing behavior (e.g.scanning_tools: []→ traditional path, $0 tool cost) is unchanged.index.d.ts: makescanning_toolsoptional to match runtime behavior, and drop the staletime_pressurefield that was removed from the implementation in fix(test): use scan_window instead of non-existent time_pressure field #8.Verification
npm test→ 69 passing, 0 failing (4 new tests). No behavior change for fully-specified inputs.🤖 Generated with Claude Code
Generated by Claude Code