-
Notifications
You must be signed in to change notification settings - Fork 199
fix(FitPlugin): handle edge cases in fitCenter calculation #831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Prevent division by zero and non-finite scaling values when graph bounds have zero dimensions (width/height = 0) or when container is improperly sized. Maintains original scale in these invalid scenarios to ensure consistent behavior. This issue particularly used to affect headless environments (tests, Node.js) where containers may have zero height/width, which is less common in browser contexts.
WalkthroughA safeguard was added to the Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Jest Test
participant Graph as BaseGraph
participant Plugin as FitPlugin
Test->>Graph: Create instance with FitPlugin
Test->>Plugin: Call fitCenter()
Plugin->>Plugin: Compute newScale
Plugin->>Plugin: Check if newScale is finite
alt newScale not finite
Plugin->>Plugin: Reset newScale to original scale
end
Plugin-->>Test: Return scale
Possibly related PRs
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm warn config production Use Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure ✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/core/src/view/plugins/FitPlugin.ts (1)
87-89: Robust defensive check for non-finite scale values.This addition addresses the core issue by ensuring that when calculations result in non-finite values (Infinity, NaN), the system falls back to the original scale. This is particularly important in headless environments or scenarios with zero-width/height bounds, where division by zero would otherwise occur.
However, for better maintainability, consider adding a debug log message when this fallback occurs. This would make troubleshooting easier in production environments.
if (!Number.isFinite(newScale)) { + // Log fallback for easier troubleshooting + if (this.graph.logger?.isDebugEnabled()) { + this.graph.logger.debug('FitPlugin: non-finite scale detected, falling back to original scale'); + } newScale = originalScale; }packages/core/__tests__/view/plugins/FitPlugin.test.ts (1)
20-24: Test case validates the edge case fix.The test appropriately verifies that the plugin returns a scale of 1 when graph dimensions are zero, validating the non-finite scale handling.
Consider expanding test coverage with additional edge cases.
test('fitCenter when graph has dimensions set to zero', () => { const graph = new BaseGraph({ plugins: [FitPlugin] }); const scale = graph.getPlugin<FitPlugin>('fit').fitCenter(); expect(scale).toBe(1); }); +test('fitCenter when container has zero dimensions', () => { + // Mock container with zero dimensions + const mockContainer = { clientWidth: 0, clientHeight: 0 }; + const graph = new BaseGraph({ + plugins: [FitPlugin], + container: mockContainer as any + }); + const scale = graph.getPlugin<FitPlugin>('fit').fitCenter(); + // Should fall back to original scale + expect(scale).toBe(1); +}); + +test('fitCenter respects maxFitScale setting', () => { + const graph = new BaseGraph({ plugins: [FitPlugin] }); + const fitPlugin = graph.getPlugin<FitPlugin>('fit'); + // Set a custom max scale + fitPlugin.maxFitScale = 2; + // Mock a situation where calculated scale would be higher + // but gets limited by maxFitScale + // ... setup test conditions + const scale = fitPlugin.fitCenter(); + expect(scale).toBeLessThanOrEqual(2); +});
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/core/__tests__/view/plugins/FitPlugin.test.ts(1 hunks)packages/core/src/view/plugins/FitPlugin.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/core/__tests__/view/plugins/FitPlugin.test.ts (1)
packages/core/src/view/plugins/FitPlugin.ts (1)
FitPlugin(43-112)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build (windows-2022)
- GitHub Check: build (ubuntu-22.04)
🔇 Additional comments (1)
packages/core/__tests__/view/plugins/FitPlugin.test.ts (1)
17-19: LGTM - Imports look good.Imports are correctly defined for the test file.



Prevent division by zero and non-finite scaling values when graph
bounds have zero dimensions (width/height = 0) or when container
is improperly sized. Maintains original scale in these invalid
scenarios to ensure consistent behavior.
This issue particularly used to affect headless environments (tests, Node.js)
where containers may have zero height/width, which is less common in
browser contexts.
Summary by CodeRabbit
Bug Fixes
Tests