Skip to content

fix: make calculate() robust to omitted optional inputs#23

Open
dmchaledev wants to merge 1 commit into
mainfrom
claude/sleepy-rubin-rrgjm9
Open

fix: make calculate() robust to omitted optional inputs#23
dmchaledev wants to merge 1 commit into
mainfrom
claude/sleepy-rubin-rrgjm9

Conversation

@dmchaledev

Copy link
Copy Markdown
Contributor

Problem

The public calculate() API is documented to accept several optional fields — index.d.ts marks scan_window? and compliance_needs? as optional, and the README shows calculate({ /* inputs */ }). But the runtime didn't honor that contract:

calculate({ target_hosts: 1000, scan_intensity: 'medium', scan_frequency: 'weekly', scanning_tools: ['hailbytes_asm'] });
// ❌ TypeError: Cannot read properties of undefined (reading 'length')   ← compliance_needs

calculate({ target_hosts: 1000, scan_intensity: 'medium', scan_frequency: 'weekly', compliance_needs: [] });
// ❌ TypeError: Cannot read properties of undefined (reading 'includes') ← scanning_tools

calculate({ /* …no scan_window… */ });
// ⚠️ scan_window_utilization: NaN, efficiency_rating: 'poor'

So any consumer following the published types or the README quick-start hits a crash or gets NaN in the result. This is a contract mismatch between index.d.ts and the implementation, not just a theoretical edge case.

Fix

  • Add a normalizeInputs() step at the entry of calculateResources() that fills safe defaults: empty arrays for scanning_tools/compliance_needs and a default 8-hour scan_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: make scanning_tools optional to match runtime behavior, and drop the stale time_pressure field that was removed from the implementation in fix(test): use scan_window instead of non-existent time_pressure field #8.
  • Add 4 regression tests covering each omitted-optional-field case.

Verification

npm test69 passing, 0 failing (4 new tests). No behavior change for fully-specified inputs.

🤖 Generated with Claude Code


Generated by Claude Code

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
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.

2 participants