<commit_before>require 'rails_helper'

RSpec.describe 'Broadcasts', type: :request do
  let(:headers) { {} }
  let(:params)  { {} }
  let(:broadcasts) {  create_list(:broadcast, 23) }
  let(:user) { create :user }

  let(:tv)    { Medium.create(id: 0, name: 'tv') }
  let(:radio) { Medium.create(id: 1, name: 'radio') }
  let(:other) { Medium.create(id: 2, name: 'other') }

  let(:tv_broadcast)    { create(:broadcast, id: 0, medium: tv) }
  let(:radio_broadcast) do
    create(:broadcast,
           id: 1,
           medium: radio,
           broadcast_url: 'https://www.zdf.de/assets/teamfoto-102~768x43')
  end
  let(:other_broadcast) { create(:broadcast, id: 2, medium: other) }

  let(:dasErste) { create(:station, id: 47, name: 'Das Erste') }

  describe 'GET /broadcasts' do
    let(:url) { '/broadcasts' }
    let(:action) { get url, params: params, headers: headers }
    let(:js) { JSON.parse(response.body) }
    before { broadcasts }

    subject do
      action
      js
    end

    it 'returns a limited set of broadcast' do
      expect(subject['data'].length).to eq(10)
    end

    it 'returns the total number of broadcasts' do
      expect(subject['meta']['total-count']).to eq 23
    end

    describe 'relationships' do
      describe '#medium' do
        let(:broadcasts) { [tv_broadcast] }
        it 'exposes the medium' do
          expect(subject['data'][0]['relationships']['medium']['data']['id']).to eq(tv.id.to_s)
        end
      end

      describe '#impressions' do
        before  { create(:impression) }
        subject do
          action
          js['data'][0]['relationships']['impressions']['data']
        end

        it 'does not expose foreign impressions' do
          is_expected.to be_empty
        end
      end
    end

    describe 'pagination' do
      before do
        create_list(:broadcast, 10)
      end

      it 'returns a reduced set' do
        expect(subject['data'].length).to eq 10
      end

      context 'page param' do
        let(:params) { { page: 4 } }
        it 'returns the next page' do
          expect(Broadcast.count).to eq 33
          expect(subject['data'].length).to eq 3
        end
      end
    end
  end

  describe 'GET /broadcast/:id' do
    let(:action) { get "/broadcasts/#{radio_broadcast.id}", headers: headers }

    context 'logged in' do
      let(:headers) { super().merge(authenticated_header(user)) }

      it "returns a valid object" do
        action
        expect(response.status).to eq 200
        expect(response).to match_response_schema('broadcast')
      end
    end

  end

  describe 'POST /broadcasts' do
    before { radio }

    let(:params) do
      {
        data: {
          type: 'broadcasts',
          attributes: {
            title: 'Nice broadcast',
            description: 'A nice broadcast, everybody will love it',
            image_url: 'https://www.zdf.de/assets/teamfoto-102~768x432'
          }, relationships: {
            medium: {<commit_msg>Clean: testing for the broadcast url<commit_after># frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Broadcasts', type: :request do
  let(:headers) { {} }
  let(:params)  { {} }
  let(:broadcasts) {  create_list(:broadcast, 23) }
  let(:user) { create :user }

  let(:tv)    { Medium.create(id: 0, name: 'tv') }
  let(:radio) { Medium.create(id: 1, name: 'radio') }
  let(:other) { Medium.create(id: 2, name: 'other') }

  let(:tv_broadcast)    { create(:broadcast, id: 0, medium: tv) }
  let(:radio_broadcast) do
    create(:broadcast,
           id: 1,
           medium: radio,
           broadcast_url: 'https://www.zdf.de/assets/teamfoto-102~768x43')
  end
  let(:other_broadcast) { create(:broadcast, id: 2, medium: other) }

  let(:dasErste) { create(:station, id: 47, name: '<NAME>') }

  describe 'GET /broadcasts' do
    let(:url) { '/broadcasts' }
    let(:action) { get url, params: params, headers: headers }
    let(:js) { JSON.parse(response.body) }
    before { broadcasts }

    subject do
      action
      js
    end

    it 'returns a limited set of broadcast' do
      expect(subject['data'].length).to eq(10)
    end

    it 'returns the total number of broadcasts' do
      expect(subject['meta']['total-count']).to eq 23
    end

    describe 'relationships' do
      describe '#medium' do
        let(:broadcasts) { [tv_broadcast] }
        it 'exposes the medium' do
          expect(subject['data'][0]['relationships']['medium']['data']['id'])
            .to eq(tv.id.to_s)
        end
      end

      describe '#impressions' do
        before  { create(:impression) }
        subject do
          action
          js['data'][0]['relationships']['impressions']['data']
        end

        it 'does not expose foreign impressions' do
          is_expected.to be_empty
        end
      end
    end

    describe 'pagination' do
      before do
        create_list(:broadcast, 10)
      end

      it 'returns a reduced set' do
        expect(subject['data'].length).to eq 10
      end

      context 'page param' do
        let(:params) { { page: 4 } }
        it 'returns the next page' do
          expect(Broadcast.count).to eq 33
          expect(subject['data'].length).to eq 3
        end
      end
    end
  end

  describe 'GET /broadcast/:id' do
    let(:action) { get "/broadcasts/#{radio_broadcast.id}", headers: headers }

    context 'logged in' do
      let(:headers) { super().merge(authenticated_header(user)) }
      before do
        action
      end

      describe 'http status' do
        subject { response }
        it { is_expected.to have_http_status(:ok) }
      end

      describe 'response schema' do
        subject { response }
        it { is_expected.to match_response_schema('broadcast') }
      end

      describe '#broadcast_url' do
        subject { parse_json(response.body, 'data/attributes/broadcast-url') }
        it { is_expected.to eq 'https://www.zdf.de/assets/teamfoto-102~768x43'  }
      end
    end
  end

  describe 'POST /broadcasts' do
    before { radio }

    let(:params) do
      {
        data: {
          type: 'broadcasts',
          attributes: {
            title: 'Nice broadcast',
            description: 'A nice broadcast, everybody will love it',
            image_url: 'https://www.zdf.de/assets/teamfoto-102~768x432'
          }, relationships: {
            medium: {
{'fraction_non_alphanumeric': 0.13206658291457288, 'fraction_numerical': 0.01460427135678392, 'mean_word_length': 2.3085714285714287, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 5, 'lorem ipsum': 0, 'www.': 5, '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': '6257827', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1941, 'n_words': 512}