<commit_before>require 'rails_helper'

RSpec.describe 'equivalence type management', type: :system do

  let!(:admin)  { create(:admin)}

  before do
    sign_in(admin)
    visit root_path
  end

  it 'allows the creation and editing of equivalences' do
    click_on 'Manage'
    click_on 'Equivalence Types'
    click_on 'New equivalence type'

    fill_in_trix with: 'Your school used lots of electricity in the last week, that is like driving {{ice_car_kwh_km}} in a car!'

    select 'Last month', from: 'Time period'
    select 'Electric', from: 'Meter type'

    expect(page).to have_select('Image name', selected: 'No image')

    click_on 'Create equivalence type'

    equivalence_type = EquivalenceType.first
    expect(equivalence_type.electricity?).to eq true
    expect(equivalence_type.last_month?).to eq true

    expect(equivalence_type.show_image?).to eq false
    expect(equivalence_type.image_name).to eq :no_image.to_s

    first_content = equivalence_type.current_content
    expect(first_content.equivalence.to_plain_text).to include('Your school used')

    click_on 'Edit'

    fill_in_trix with: 'You used lots of electricity in the last week, that is like driving {{ice_car_kwh_km}} in a car!'
    select 'Petrol car', from: 'Image name'

    select 'Gas', from: 'Meter type'
    click_on 'Update equivalence type'

    equivalence_type.reload
    expect(equivalence_type.gas?).to eq true

    expect(equivalence_type.image_name).to eq :petrol_car.to_s
    expect(equivalence_type.show_image?).to eq true

    expect(equivalence_type.content_versions.count).to eq(2)
    first_content = equivalence_type.current_content
    expect(first_content.equivalence.to_plain_text).to include('You used')
  end

  context 'allows the deletion equivalences with context types' do

    before(:each) do
      equivalence_type = create(:equivalence_type, meter_type: :electricity, time_period: :last_month)
      equivalence_text = "You used {{kwh}} of electricity last month, that's like {{number_trees}} trees"
      content_version = create(
        :equivalence_type_content_version,
        equivalence_type: equivalence_type,
        equivalence: equivalence_text
      )

      click_on 'Manage'
      click_on 'Equivalence Types'

      expect(page).to have_content equivalence_text
    end

    it 'only' do
      expect { click_on 'Delete' }.to change { EquivalenceType.count }.by(-1).and change { EquivalenceTypeContentVersion.count }.by(-1)
    end

    it 'and equivalences too' do
      school = create(:school)

      aggregate_school = double :aggregate_school

     # allow_any_instance_of(AggregateSchoolService).to receive(:aggregate_school).and_return(school)

      analytics = double :analytics

      expect(analytics).to receive(:new).and_return(analytics)

      expect(analytics).to receive(:front_end_convert).with(:kwh, {month: -1}, :electricity).and_return(
        {
          formatted_equivalence: '100 kwh',
          show_equivalence: true
        }
      )

      expect(analytics).to receive(:front_end_convert).with(:number_trees, {month: -1}, :electricity).and_return(<commit_msg>Remove commented out code<commit_after>require 'rails_helper'

RSpec.describe 'equivalence type management', type: :system do

  let!(:admin)  { create(:admin)}

  before do
    sign_in(admin)
    visit root_path
  end

  it 'allows the creation and editing of equivalences' do
    click_on 'Manage'
    click_on 'Equivalence Types'
    click_on 'New equivalence type'

    fill_in_trix with: 'Your school used lots of electricity in the last week, that is like driving {{ice_car_kwh_km}} in a car!'

    select 'Last month', from: 'Time period'
    select 'Electric', from: 'Meter type'

    expect(page).to have_select('Image name', selected: 'No image')

    click_on 'Create equivalence type'

    equivalence_type = EquivalenceType.first
    expect(equivalence_type.electricity?).to eq true
    expect(equivalence_type.last_month?).to eq true

    expect(equivalence_type.show_image?).to eq false
    expect(equivalence_type.image_name).to eq :no_image.to_s

    first_content = equivalence_type.current_content
    expect(first_content.equivalence.to_plain_text).to include('Your school used')

    click_on 'Edit'

    fill_in_trix with: 'You used lots of electricity in the last week, that is like driving {{ice_car_kwh_km}} in a car!'
    select 'Petrol car', from: 'Image name'

    select 'Gas', from: 'Meter type'
    click_on 'Update equivalence type'

    equivalence_type.reload
    expect(equivalence_type.gas?).to eq true

    expect(equivalence_type.image_name).to eq :petrol_car.to_s
    expect(equivalence_type.show_image?).to eq true

    expect(equivalence_type.content_versions.count).to eq(2)
    first_content = equivalence_type.current_content
    expect(first_content.equivalence.to_plain_text).to include('You used')
  end

  context 'allows the deletion equivalences with context types' do

    before(:each) do
      equivalence_type = create(:equivalence_type, meter_type: :electricity, time_period: :last_month)
      equivalence_text = "You used {{kwh}} of electricity last month, that's like {{number_trees}} trees"
      content_version = create(
        :equivalence_type_content_version,
        equivalence_type: equivalence_type,
        equivalence: equivalence_text
      )

      click_on 'Manage'
      click_on 'Equivalence Types'

      expect(page).to have_content equivalence_text
    end

    it 'only' do
      expect { click_on 'Delete' }.to change { EquivalenceType.count }.by(-1).and change { EquivalenceTypeContentVersion.count }.by(-1)
    end

    it 'and equivalences too' do
      school = create(:school)
      aggregate_school = double :aggregate_school
      analytics = double :analytics

      expect(analytics).to receive(:new).and_return(analytics)
      expect(analytics).to receive(:front_end_convert).with(:kwh, {month: -1}, :electricity).and_return(
        {
          formatted_equivalence: '100 kwh',
          show_equivalence: true
        }
      )
      expect(analytics).to receive(:front_end_convert).with(:number_trees, {month: -1}, :electricity).and_return(
{'fraction_non_alphanumeric': 0.09502189070861035, 'fraction_numerical': 0.0025944543538187126, 'mean_word_length': 3.8376470588235296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4434048', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1831, 'n_words': 482}