# frozen_string_literal: true

require 'spec_helper'

describe 'EE > Projects > Licenses > Maintainer views policies', :js do
  let_it_be(:project) { create(:project) }
  let_it_be(:maintainer) do
    create(:user).tap do |user|
      project.add_maintainer(user)
    end
  end

  before do
    stub_licensed_features(license_management: true)

    sign_in(maintainer)
    visit(project_licenses_path(project))
    wait_for_requests
  end

  context 'when policies are not configured' do
    it 'displays a link to the documentation to configure license compliance' do
      expect(page).to have_content('License Compliance')
      expect(page).to have_content('Learn more about license compliance')
    end
  end

  context "when a policy is configured" do
    let_it_be(:mit) { create(:software_license, :mit) }
    let_it_be(:mit_policy) { create(:software_license_policy, :denied, software_license: mit, project: project) }
    let_it_be(:pipeline) { create(:ee_ci_pipeline, project: project, builds: [create(:ee_ci_build, :license_scan_v2, :success)]) }
    let(:report) { JSON.parse(fixture_file('security_reports/gl-license-management-report-v2.json', dir: 'ee')) }
    let(:known_licenses) { report['licenses'].find_all { |license| license['url'].present? } }

    it 'displays licenses detected in the most recent scan report' do
      known_licenses.each do |license|
        selector = "div[data-spdx-id='#{license['id']}'"
        expect(page).to have_selector(selector)

        row = page.find(selector)
        policy = policy_for(license)
        expect(row).to have_content(policy&.name || license['name'])
        expect(row).to have_content(dependencies_for(license['id']).join(' and '))
      end
    end

    context "when viewing the configured policies" do
      before do
        click_link('Policies')
        wait_for_requests
      end

      it 'displays the classification' do
        selector = "div[data-qa-selector='admin_license_compliance_row']"
        expect(page).to have_selector(selector)

        row = page.find(selector)
        expect(row).to have_content(mit.name)
        expect(row).to have_content(mit_policy.classification.titlecase)
      end
    end

    def dependencies_for(spdx_id)
      report['dependencies']
        .find_all { |dependency| dependency['licenses'].include?(spdx_id) }
        .map { |dependency| dependency['name'] }
    end

    def policy_for(license)
      SoftwareLicensePolicy.by_spdx(license['id']).first
    end
  end
end
