<commit_before>require 'rails_helper'

RSpec.describe Text::Generator do
  let(:report)         { create(:report) }
  let(:intention)      { :real }
  subject { described_class.new( report, intention) }

  describe '#choose_heading' do
    subject { super().choose_heading }
    let(:report) { create(:report, text_components: text_components) }

    context 'for text components with several headings and priorities' do
      let(:text_components) do
        [:low, :medium, :high].collect { |p| create(:text_component, heading: "Heading with priority #{p}", priority: p) }
      end
      it { is_expected.to eq 'Heading with priority high'}
    end

    context 'for text components with several headings but without priorities' do
      let(:text_components) do
        [:low, :medium, :high].collect { |p| create(:text_component, heading: "Heading with priority #{p}", priority: nil) }
      end
      it { is_expected.to include 'Heading'} # any of those headings
    end
  end

  describe '#generate' do
    subject { super().generate }

    it { is_expected.to eq({heading: '', introduction: '', main_part: '', closing: ''}) }

    context 'given text components' do
      let(:report)         { create(:report, text_components: [text_component]) }
      let(:text_component) { create(:text_component, main_part: main_part) }
      let(:main_part)      { "some content" }

      it { is_expected.to have_value("some content")}

      describe 'report markup' do
        describe 'report name' do
          let(:report)    { create(:report, text_components: [text_component], name: 'Foobar') }
          let(:main_part) { "Say something about { report}." }
          it { is_expected.to have_value("Say something about Foobar.")}
        end

        describe 'report variables' do
          let(:report)    { create(:report, text_components: [text_component], variables: variables) }
          let(:variables) { [ create(:variable, key: 'cool_thing', value: 'sth. cool') ] }
          let(:main_part) { "Say something about { cool_thing }." }
          it { is_expected.to have_value("Say something about sth. cool.")}

          context 'many variables' do
            let(:main_part) { "Write about { v1 } and { v2 }." }
            let(:variables) { [ create(:variable, key: 'v1', value: 'this'),
                                create(:variable, key: 'v2', value: 'that'),] }
            it { is_expected.to have_value("Write about this and that.")}
          end
        end
      end

      context 'given a condition' do<commit_msg>spec to check for rendered date of event<commit_after>require 'rails_helper'

RSpec.describe Text::Generator do
  let(:report)         { create(:report) }
  let(:intention)      { :real }
  subject { described_class.new( report, intention) }

  describe '#choose_heading' do
    subject { super().choose_heading }
    let(:report) { create(:report, text_components: text_components) }

    context 'for text components with several headings and priorities' do
      let(:text_components) do
        [:low, :medium, :high].collect { |p| create(:text_component, heading: "Heading with priority #{p}", priority: p) }
      end
      it { is_expected.to eq 'Heading with priority high'}
    end

    context 'for text components with several headings but without priorities' do
      let(:text_components) do
        [:low, :medium, :high].collect { |p| create(:text_component, heading: "Heading with priority #{p}", priority: nil) }
      end
      it { is_expected.to include 'Heading'} # any of those headings
    end
  end

  describe '#generate' do
    subject { super().generate }

    it { is_expected.to eq({heading: '', introduction: '', main_part: '', closing: ''}) }

    context 'given text components' do
      let(:report)         { create(:report, text_components: [text_component]) }
      let(:text_component) { create(:text_component, main_part: main_part) }
      let(:main_part)      { "some content" }

      it { is_expected.to have_value("some content")}

      describe 'report markup' do
        describe 'report name' do
          let(:report)    { create(:report, text_components: [text_component], name: 'Foobar') }
          let(:main_part) { "Say something about { report}." }
          it { is_expected.to have_value("Say something about Foobar.")}
        end

        describe 'report variables' do
          let(:report)    { create(:report, text_components: [text_component], variables: variables) }
          let(:variables) { [ create(:variable, key: 'cool_thing', value: 'sth. cool') ] }
          let(:main_part) { "Say something about { cool_thing }." }
          it { is_expected.to have_value("Say something about sth. cool.")}

          context 'many variables' do
            let(:main_part) { "Write about { v1 } and { v2 }." }
            let(:variables) { [ create(:variable, key: 'v1', value: 'this'),
                                create(:variable, key: 'v2', value: 'that'),] }
            it { is_expected.to have_value("Write about this and that.")}
          end
        end
      end

      context 'given an event' do
        let(:event)      { create(:event, id: 42, name: "DEATH", text_components: [text_component], happened_at: nil) }
        before { event }
        it('event must happen first') { is_expected.to eq({heading: '', introduction: '', main_part: '', closing: ''}) }
        context 'has happened' do
          before { event.happened_at = DateTime.parse('2018-02-02'); event.save! }
          it { is_expected.to have_value("some content")}

          describe 'markup for the day of the event' do
            let(:main_part)      { 'Day of your death: { date(42) }' }
            it 'renders the day of the event' do
              is_expected.to have_value('Day of your death: February 2nd 2018')
            end
          end
        end
      end

      context 'given a condition' do
{'fraction_non_alphanumeric': 0.1278360988824924, 'fraction_numerical': 0.004232983406705046, 'mean_word_length': 2.698810269254853, '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': '2200029', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1646, 'n_words': 505}