Skip to content

feat: add Diff.compare for standalone image comparison#163

Merged
pftg merged 1 commit into
masterfrom
feat/compare-api
Apr 12, 2026
Merged

feat: add Diff.compare for standalone image comparison#163
pftg merged 1 commit into
masterfrom
feat/compare-api

Conversation

@pftg

@pftg pftg commented Apr 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Browser-independent API for comparing any two images:

result = Capybara::Screenshot::Diff.compare("baseline.png", "current.png")
result.quick_equal?  # byte-identical?
result.different?    # visually different?

Supports all options: driver, tolerance, perceptual_threshold, color_distance_limit.

Based on real usage from corsis/pet which uses the gem for PDF visual regression.

Test plan

  • 5 tests: return type, identical, different, driver option, tolerance
  • Full suite with CI=true (196 runs, 0 failures)
  • Pre-PR: brutal review (0.5, clean) + codex review (arg order fix applied)

🤖 Generated with Claude Code

Summary by Sourcery

Add a public Diff.compare API for standalone image comparison without Capybara or a browser.

New Features:

  • Introduce Diff.compare helper for comparing any two image files using existing diff logic and options.

Enhancements:

  • Document standalone image comparison usage and options in the README.

Tests:

  • Add unit tests covering Diff.compare return type, identical and different images, and option handling for driver and tolerance.

@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@pftg has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 24 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 15 minutes and 24 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7b94868b-9ad6-40cc-b25e-c183e74baf13

📥 Commits

Reviewing files that changed from the base of the PR and between 076cbac and a8716a3.

📒 Files selected for processing (3)
  • README.md
  • lib/capybara_screenshot_diff.rb
  • test/unit/compare_api_test.rb
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/compare-api

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.

@sourcery-ai

sourcery-ai Bot commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds a new public Diff.compare API for standalone, browser-independent image comparison and documents it, with tests covering behavior and options support.

Sequence diagram for the new Diff.compare standalone image comparison API

sequenceDiagram
  actor Developer
  participant Diff
  participant ImageCompare

  Developer->>Diff: compare(base_image_path, new_image_path, options)
  activate Diff
  Diff->>Diff: default_options()
  Diff-->>Diff: base_options
  Diff->>ImageCompare: new(new_image_path, base_image_path, base_options + options)
  deactivate Diff
  activate ImageCompare
  ImageCompare-->>Developer: image_compare_result
  deactivate ImageCompare

  Developer->>image_compare_result: quick_equal()
  Developer->>image_compare_result: different()
Loading

Updated class diagram for Diff and ImageCompare standalone comparison

classDiagram
  class Diff {
    +compare(base_image_path, new_image_path, options)
    +default_options()
  }

  class ImageCompare {
    +ImageCompare(new_image_path, base_image_path, options)
    +quick_equal()
    +different()
  }

  Diff ..> ImageCompare : creates
Loading

File-Level Changes

Change Details Files
Expose a standalone image comparison API via Diff.compare that returns ImageCompare and passes through configuration options.
  • Add Diff.compare class method that constructs ImageCompare with new and base image paths
  • Merge global default_options with per-call options to configure the comparison
  • Preserve argument order for compatibility with existing ImageCompare usage
lib/capybara_screenshot_diff.rb
Document the new standalone image comparison API and its options in the README.
  • Add README section describing Diff.compare usage without Capybara/browser
  • Show basic equality/difference checks via quick_equal? and different?
  • Provide example including driver, tolerance, and perceptual color comparison options
README.md
Add unit tests to validate the new Diff.compare API behavior and option handling.
  • Test that Diff.compare returns an ImageCompare instance
  • Test identical and different images using quick_equal? and different? results
  • Test handling of driver and tolerance options, skipping VIPS-specific tests when unavailable
test/unit/compare_api_test.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The Diff.compare signature uses (base_image_path, new_image_path) but then passes (new_image_path, base_image_path) into ImageCompare.new, which works but is easy to misread; consider either aligning the parameter order with ImageCompare or renaming the parameters (baseline_path, current_path) and adding a short code comment to make the reversal explicit.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `Diff.compare` signature uses `(base_image_path, new_image_path)` but then passes `(new_image_path, base_image_path)` into `ImageCompare.new`, which works but is easy to misread; consider either aligning the parameter order with `ImageCompare` or renaming the parameters (`baseline_path`, `current_path`) and adding a short code comment to make the reversal explicit.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Browser-independent API for comparing any two images:

  result = Capybara::Screenshot::Diff.compare("a.png", "b.png")
  result.quick_equal?  # byte-identical?
  result.different?    # visually different?

Supports all existing options (driver, tolerance, perceptual_threshold).
Based on real usage from corsis/pet which uses the gem for PDF regression.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@pftg pftg force-pushed the feat/compare-api branch from 92aaccc to a8716a3 Compare April 12, 2026 00:48
@pftg pftg merged commit 1364048 into master Apr 12, 2026
5 checks passed
@pftg pftg deleted the feat/compare-api branch April 12, 2026 00:48
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.

1 participant