Skip to content

fix: clamp status progress bar inputs#340

Merged
subsy merged 1 commit intosubsy:mainfrom
buihongduc132:fix/status-progress-bar
Feb 25, 2026
Merged

fix: clamp status progress bar inputs#340
subsy merged 1 commit intosubsy:mainfrom
buihongduc132:fix/status-progress-bar

Conversation

@buihongduc132
Copy link
Copy Markdown

@buihongduc132 buihongduc132 commented Feb 25, 2026

Summary

  • prevent status progress bar rendering from throwing on corrupted or non-finite inputs
  • add progress bar rendering tests for negative, NaN, and non-finite values

Repro

  1. Copy and corrupt a session file:
cp /home/bhd/Documents/Projects/bhd/poli-arbt/.ralph-tui/session.json /tmp/ralph-tui-session.json
# edit /tmp/ralph-tui-session.json and set \"tasksCompleted\": -1
  1. Place in temp project:
mkdir -p /tmp/ralph-tui-repro/.ralph-tui
cp /tmp/ralph-tui-session.json /tmp/ralph-tui-repro/.ralph-tui/session.json
  1. Run:
bun run ./src/cli.tsx status --cwd /tmp/ralph-tui-repro
  1. Prior behavior: RangeError from createProgressBar on negative repeat count.

Tests

  • bun test tests/commands/status.test.ts
  • bun run typecheck && bun run build

Notes

  • No user-facing docs update required; behavior is defensive for corrupted session data.
  • Codecov >50% on changed lines via targeted tests.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced progress bar calculation to safely handle edge cases including invalid percentage values and non-finite measurements, ensuring consistent and accurate rendering across all input scenarios.
  • Tests

    • Added comprehensive test coverage for progress bar rendering, validating correct behaviour with boundary values and abnormal input conditions.

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 25, 2026

Someone is attempting to deploy a commit to the plgeek Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 25, 2026

Walkthrough

The 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

Cohort / File(s) Summary
Progress bar logic refinement
src/commands/status.ts
Enhanced createProgressBar with safe width calculation, Infinity-to-100% mapping, bounds-checked percent normalisation, and clamped filled/empty values. Added __test__ export hook for testing.
Progress bar test coverage
tests/commands/status.test.ts
New test suite for progress bar rendering covering invalid percent clamping (−10, 150, NaN) and non-finite width handling (NaN, Infinity return empty bar).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: clamp status progress bar inputs' directly and accurately summarises the main change: adding input clamping to the progress bar function to handle corrupted or non-finite values.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
tests/commands/status.test.ts (1)

534-551: Hoist __test__ import and replace require() with a top-level ESM import.

Both test bodies independently call require() to obtain createProgressBar, which is inconsistent with the file's ES-module style and duplicates the extraction. mock.module only intercepts ../../src/session/index.js, so a top-level import of __test__ from status.js is safe and idiomatic.

♻️ Proposed refactor

Add to the existing status.js imports 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; normalizedPercent branch can be simplified.

The three-way isFinite / === Infinity / else guard is correct, but it is equivalent to a single expression that leverages Math.max/Math.min behaviour on ±Infinity natively, which is simpler and removes the percent === Infinity special-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) === 0 and Math.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.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e5e51d and 6b2daeb.

📒 Files selected for processing (2)
  • src/commands/status.ts
  • tests/commands/status.test.ts

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
ralph-tui Ignored Ignored Preview Feb 25, 2026 7:57pm

Request Review

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 47.39%. Comparing base (3e5e51d) to head (6b2daeb).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
src/commands/status.ts 6.14% <100.00%> (+4.14%) ⬆️

... and 8 files with indirect coverage changes

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

@subsy subsy merged commit 17eb1bc into subsy:main Feb 25, 2026
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