Skip to content

fix(MatchScore): clamp score to avoid invalid array length runtime error - #1171

Merged
kheinrich-eightfold merged 5 commits into
mainfrom
kheinrich/clamp-negative-match-score
Sep 24, 2026
Merged

kheinrich-eightfold merged 5 commits into
mainfrom
kheinrich/clamp-negative-match-score

Conversation

@kheinrich-eightfold

@kheinrich-eightfold kheinrich-eightfold commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

SUMMARY:

Previously, the MatchScore component computed its filled/half/empty circle counts directly from an unbounded score prop, so a score outside the [0, total] range (observed in production as score = -1) produced a negative circle count and threw Uncaught RangeError: Invalid array length on Array(n).

This PR clamps score to [0, total] before it's used in any of the circle/label calculations, so out-of-range scores degrade gracefully (rendering 0 or total circles) instead of crashing:

  • Clamp negative score values to 0.
  • Clamp score values above total down to total.
  • Rename the misleadingly-named absScore (it rounded, but never took an absolute value) to displayScore for clarity.
  • Add regression tests covering negative and above-total scores.

Additional hardening from review feedback: score/total are plain number props, so a caller could also pass NaN or a fractional total, which would still throw the same RangeError even with the clamp above. Addressed by:

  • Sanitizing a non-finite score to 0 and a non-finite total to the component's own default (extracted as DEFAULT_SCORE/DEFAULT_TOTAL constants so the fallback can't drift from the prop default).
  • Truncating total before use, so a fractional total (e.g. 5.5) can't produce a fractional circle count.
  • Strengthened tests to assert actual rendered circle counts (not just label text), plus new cases for NaN score, NaN total, and fractional total.

GITHUB ISSUE (Open Source Contributors)

JIRA TASK (Eightfold Employees Only):

https://eightfoldai.atlassian.net/browse/ENG-215683

CHANGE TYPE:

  • Bugfix Pull Request
  • Feature Pull Request

TEST COVERAGE:

  • Tests for this change already exist
  • I have added unittests for this change

TEST PLAN:

  • yarn jest — full suite passing
  • New tests pass
  • No behavior change when score is a valid value between 0 and total (5 by default)

@codesandbox-ci

codesandbox-ci Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.45%. Comparing base (9081f8c) to head (17a4079).

Files with missing lines Patch % Lines
src/components/MatchScore/MatchScore.tsx 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1171      +/-   ##
==========================================
+ Coverage   85.44%   85.45%   +0.01%     
==========================================
  Files        1231     1231              
  Lines       21775    21780       +5     
  Branches     8293     8295       +2     
==========================================
+ Hits        18606    18613       +7     
+ Misses       3081     3079       -2     
  Partials       88       88              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/components/MatchScore/MatchScore.tsx Outdated
Comment thread src/components/MatchScore/MatchScore.tsx
Comment thread src/components/MatchScore/MatchScore.test.tsx
@kheinrich-eightfold
kheinrich-eightfold merged commit f39c1bf into main Sep 24, 2026
7 of 9 checks passed
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