Repository navigation
Conversation
Co-authored-by: Claude <[email protected]>
| throw new RangeError(`Negative quantity for item ${i}`); | ||
| } | ||
| total += items[i].price * items[i].qty; | ||
| total += Math.round(items[i].price * items[i].qty); |
There was a problem hiding this comment.
Bug: The code rounds each line item's subtotal individually. Subtotals less than 0.5 will be rounded to 0, leading to an incorrect final cart total and potential under-charging.
Severity: MEDIUM
Suggested Fix
To prevent precision loss, sum the exact subtotals of all line items first, and then round the final total only once at the very end. For example: return Math.round(items.reduce((acc, item) => acc + (item.price * item.qty), 0));. This ensures fractional values are not discarded prematurely.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: apps/pr-reviewer/fixtures/demo-pr/cart-total.ts#L7
Potential issue: The function calculates the total cart value by rounding the subtotal
for each line item before summing them. The use of `Math.round(items[i].price *
items[i].qty)` can lead to accumulated rounding errors. If an item's subtotal is a
fractional value less than 0.5, it will be rounded down to 0. If a cart contains
multiple such items, their cost will be completely lost from the final total, resulting
in the customer being under-charged. This edge case is not covered by existing tests but
is a plausible scenario, especially if discounts create fractional prices.
Did we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 4b9abd1. Configure here.
| throw new RangeError(`Negative quantity for item ${i}`); | ||
| } | ||
| total += items[i].price * items[i].qty; | ||
| total += Math.round(items[i].price * items[i].qty); |
There was a problem hiding this comment.
Unsafe floating-point line rounding
Medium Severity
Math.round on the raw price * qty product can pick the wrong whole cent when binary floating point lands just below a half-cent. Checkout then undercharges those lines by one cent. The new coverage stays far enough from a .5 boundary that this miss is not caught.
Reviewed by Cursor Bugbot for commit 4b9abd1. Configure here.
|
Verdict: Needs changes — per-line rounding introduces unvalidated/ambiguous behavior and the new test doesn't actually pin down the intended semantics. Correctness
Style
SummaryTests pass, but the core logic change (per-line vs. total rounding) is under-specified and under-tested, and input validation for |
|
The store branch work is on main now (973ed2e). The cache-lifetime demo pull request replaces this one. |


Each cart line is now rounded to whole cents before it is added to the total, so fractional prices no longer leave sub-cent remainders in the checkout total. A test covers a cart with fractional line totals.
Created with Claude Code