fix: clamp status progress bar inputs#340
Conversation
|
Someone is attempting to deploy a commit to the plgeek Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe changes refine progress bar computation in the status command to handle edge cases safely—normalising invalid percentages, managing Infinity values, and clamping dimensions—whilst introducing a test hook and corresponding test coverage. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/commands/status.test.ts (1)
534-551: Hoist__test__import and replacerequire()with a top-level ESM import.Both test bodies independently call
require()to obtaincreateProgressBar, which is inconsistent with the file's ES-module style and duplicates the extraction.mock.moduleonly intercepts../../src/session/index.js, so a top-level import of__test__fromstatus.jsis safe and idiomatic.♻️ Proposed refactor
Add to the existing
status.jsimports at the top of the file (alongside line 7):-import { printStatusHelp } from '../../src/commands/status.js'; +import { printStatusHelp, __test__ } from '../../src/commands/status.js';Then update the describe block:
describe('progress bar rendering', () => { + const { createProgressBar } = __test__ as { createProgressBar: (percent: number, width: number) => string }; + test('clamps invalid percent values to avoid RangeError', () => { - const { __test__ } = require('../../src/commands/status.js'); - const { createProgressBar } = __test__ as { createProgressBar: (percent: number, width: number) => string }; - expect(createProgressBar(-10, 10)).toBe('[░░░░░░░░░░]'); expect(createProgressBar(150, 10)).toBe('[██████████]'); expect(createProgressBar(Number.NaN, 10)).toBe('[░░░░░░░░░░]'); }); test('handles non-finite width safely', () => { - const { __test__ } = require('../../src/commands/status.js'); - const { createProgressBar } = __test__ as { createProgressBar: (percent: number, width: number) => string }; - expect(createProgressBar(50, Number.NaN)).toBe('[]'); expect(createProgressBar(50, Number.POSITIVE_INFINITY)).toBe('[]'); }); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/commands/status.test.ts` around lines 534 - 551, Hoist the test-only export import by adding a top-level ESM import for __test__ from the status module and remove the inline require() calls inside the tests; then extract createProgressBar from __test__ once (e.g. const { createProgressBar } = __test__) near the other imports and update both tests to use that single binding. Ensure this change does not interfere with the existing mock.module interception of session/index.js (the import is safe because mock.module targets ../../src/session/index.js), and run the tests to verify behavior for createProgressBar with invalid percent and non-finite width.src/commands/status.ts (1)
419-428: Correctness looks good;normalizedPercentbranch can be simplified.The three-way
isFinite / === Infinity / elseguard is correct, but it is equivalent to a single expression that leveragesMath.max/Math.minbehaviour on ±Infinity natively, which is simpler and removes thepercent === Infinityspecial-case entirely:♻️ Suggested simplification
- const normalizedPercent = Number.isFinite(percent) - ? Math.min(100, Math.max(0, percent)) - : percent === Infinity - ? 100 - : 0; + const normalizedPercent = Number.isNaN(percent) + ? 0 + : Math.min(100, Math.max(0, percent));
Math.max(0, -Infinity) === 0andMath.min(100, Infinity) === 100, so the two previously explicit branches are handled implicitly.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/commands/status.ts` around lines 419 - 428, Replace the three-way normalizedPercent branch in createProgressBar with a simpler expression that removes the explicit Infinity check: compute normalizedPercent as Number.isFinite(percent) ? Math.min(100, Math.max(0, percent)) : 0 so NaN/non-finite values yield 0 while ±Infinity are clamped by Math.min/Math.max; update the normalizedPercent assignment accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/commands/status.ts`:
- Around line 419-428: Replace the three-way normalizedPercent branch in
createProgressBar with a simpler expression that removes the explicit Infinity
check: compute normalizedPercent as Number.isFinite(percent) ? Math.min(100,
Math.max(0, percent)) : 0 so NaN/non-finite values yield 0 while ±Infinity are
clamped by Math.min/Math.max; update the normalizedPercent assignment
accordingly.
In `@tests/commands/status.test.ts`:
- Around line 534-551: Hoist the test-only export import by adding a top-level
ESM import for __test__ from the status module and remove the inline require()
calls inside the tests; then extract createProgressBar from __test__ once (e.g.
const { createProgressBar } = __test__) near the other imports and update both
tests to use that single binding. Ensure this change does not interfere with the
existing mock.module interception of session/index.js (the import is safe
because mock.module targets ../../src/session/index.js), and run the tests to
verify behavior for createProgressBar with invalid percent and non-finite width.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #340 +/- ##
==========================================
+ Coverage 46.66% 47.39% +0.73%
==========================================
Files 103 103
Lines 33411 33404 -7
==========================================
+ Hits 15590 15831 +241
+ Misses 17821 17573 -248
🚀 New features to boost your workflow:
|
Summary
Repro
Tests
Notes
Summary by CodeRabbit
Bug Fixes
Tests