# frozen_string_literal: true

require "helper"
require "support/sandbox_project"

# Make sure that a fairly standard rspec-rails setup within a rails
# application works. See issue #873 for an example of how this might
# break. The group tab presentation is covered by the bun suite; the
# group contents and percentages are asserted from the report data.
RSpec.describe "rspec-rails integration", :sandbox do
  before do
    setup_project("rails/rspec_rails")
    install_dependencies
  end

  def reported_groups(data)
    data.fetch("groups").to_h do |name, group|
      coverage = displayed_percent(group.fetch("lines").fetch("percent"))
      [name, {"coverage" => coverage, "files" => group.fetch("files").length}]
    end
  end

  it "produces a coverage report with the rails profile's groups" do
    result = run_command_and_expect_success("bundle exec rspec", timeout: 120)
    expect_coverage_report_generated(result)

    data = html_report_data
    expect(reported_total_percent(data)).to eq(50.00)
    expect(data.fetch("coverage").keys.length).to eq(5)

    expect(reported_groups(data)).to eq(
      "Controllers" => {"coverage" => 0.00, "files" => 1},
      "Channels" => {"coverage" => 100.00, "files" => 0},
      "Models" => {"coverage" => 60.00, "files" => 2},
      "Mailers" => {"coverage" => 100.00, "files" => 0},
      "Helpers" => {"coverage" => 100.00, "files" => 1},
      "Jobs" => {"coverage" => 0.00, "files" => 1},
      "Libraries" => {"coverage" => 100.00, "files" => 0}
    )
  end
end
