fix: make pure calculate() tolerate omitted optional inputs#14
Open
dmchaledev wants to merge 1 commit into
Open
fix: make pure calculate() tolerate omitted optional inputs#14dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
The public calculate() API crashed or produced NaN when called with the fields that index.d.ts documents as optional: - omitting `compliance_needs` threw "Cannot read properties of undefined (reading 'length')" in calcAsmResources - omitting `scan_window` produced NaN scan_window_utilization and a misleading "poor" efficiency rating (NaN <= 60 is false, so the rating chain fell through to the worst bucket) - omitting `scanning_tools` threw on `.includes` The DOM component guards these in _collectInputs()/_run(), but the pure calculate() — the headline programmatic API — did not, so TypeScript consumers got green typechecking followed by a runtime crash. Add normalizeInputs() at the calculation entry point to default the optional fields (scan_window -> 8, compliance_needs -> [], a missing/invalid scanning_tools -> [], a non-positive scan_window -> 8, non-finite target_hosts -> 1). An empty scanning_tools array stays a valid zero-cost state, matching the existing edge-case test. Add regression coverage: 5 tests in calculate.test.mjs plus one smoke-level test so the case is guarded by the CI `npm test` script today (which currently runs only the smoke suite). https://claude.ai/code/session_01Aed4GvoDBzgz6fuPXSHHif
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
index.d.tsdeclaresscan_windowandcompliance_needsas optional:But the pure
calculate()API — the headline DOM-free function — crashed or silently producedNaNwhen those documented-optional fields were omitted. So a TypeScript consumer gets green typechecking, then a runtime failure.Reproduced against
main:maincompliance_needsCannot read properties of undefined (reading 'length')(incalcAsmResources)scan_windowscan_window_utilization=NaN, efficiency rating wrongly reported as"poor"(the rating chain falls through becauseNaN <= 60isfalse)scanning_toolsCannot read properties of undefined (reading 'includes')The DOM web component never hit this because
_collectInputs()/_run()clamp and default everything first — but the exportedcalculate()had no such guard. (This is the same class of issue that bit the test suite in #8, which had to manually addscan_window.)Fix
Add a small
normalizeInputs()step at the calculation entry point (calculateResources) that defaults the optional fields before they're used:scan_window→8when missing or non-positive (also avoids a divide-by-zero →Infinity)compliance_needs→[]when missingscanning_tools→[]when missing/invalid — note an empty array stays a valid zero-cost state, matching the existingempty scanning_tools array does not crashtest (the DOM component injects its own UI-only fallback, the pure function should not)scan_intensity/scan_frequency→ existing defaults made explicittarget_hosts→1(defensive; preventsNaNpropagating through every metric)No calculation logic changed for already-valid inputs — this only fills in omitted optional values.
Tests
test/calculate.test.mjs(Optional input handlingblock) covering each omitted field, the zero/invalidscan_windowcase, and a minimal{ target_hosts }-only call.test/smoke.test.mjsso the regression is actually guarded by CI today —npm testcurrently runs only the smoke suite, so the fullcalculate.test.mjsdoesn't run in CI until test: run full test suite in CI, not just smoke tests #11/test: run full test suite in npm test (was only smoke tests) #13 land.Note (out of scope)
This PR makes the regression visible in CI via the smoke test, but the broader gap —
npm testnot runningcalculate.test.mjsat all — is already tracked by open PRs #11 and #13. Worth merging one of those so the full 71-test suite gates every push.https://claude.ai/code/session_01Aed4GvoDBzgz6fuPXSHHif
Generated by Claude Code