<commit_before>require 'test_helper'
require 'prx_access'

describe EpisodeImport do
  include PRXAccess

  let(:user) { create(:user) }
  let(:account) { create(:account, id: 8, opener: user) }
  let(:series) { create(:series) }
  let(:template) { create(:audio_version_template, series: series) }
  let(:distribution) do
    create(:podcast_distribution,
           distributable: series,
           url: 'https://feeder.prx.org/api/v1/podcasts/51')
  end

  let(:podcast_url) { 'http://feeds.prx.org/transistor_stem' }
  let(:importer) { PodcastImport.create(user: user, account: account, url: podcast_url, series: series) }
  let(:episode_import) { EpisodeImport.create!(
                                                      podcast_import: importer,
                                                      audio: { files: ['https://cdn-transistor.prx.org/wp-content/uploads/Smithsonian3_Transistor.mp3'] },
                                                      entry: entry.to_h,
                                                      guid: 'https://transistor.prx.org/?p=1286'
                                                     ) }

  let(:feed) { Feedjira::Feed.parse(test_file('/fixtures/transistor_two.xml')) }
  let(:entry) { feed.entries.first }

  let(:podcast) do
    api_resource(JSON.parse(json_file('transistor_podcast_basic')), feeder_root).tap do |r|
      r.headers = r.headers.merge('Authorization' => 'Bearer thisisnotatoken')
    end
  end

  before do
    stub_episode_requests
  end

  it 'creates a story on import' do
    f = episode_import.import
    f.description.must_match /^For the next few episodes/
    f.description.wont_match /<script/
    f.description.wont_match /<iframe/
    f.description.wont_match /feedburner/
    f.tags.must_include 'Indie Features'
    f.tags.each do |tag|
      tag.wont_match /\n/
      tag.wont_be :blank?
    end
    f.tags.wont_include '\t'
    f.clean_title.must_equal 'Sidedoor iTunes title'
    f.season_identifier.must_equal '2'
    f.episode_identifier.must_equal '4'
    f.distributions.first.get_episode.itunes_type.must_equal 'full'
    f.account_id.wont_be_nil
    f.creator_id.wont_be_nil
    f.series_id.wont_be_nil
    f.published_at.wont_be_nil
    f.images.count.must_equal 1
    f.audio_versions.count.must_equal 1
    config_audio = 'https://cdn-transistor.prx.org/wp-content/uploads/Smithsonian3_Transistor.mp3'
    f.audio_versions.first.audio_files.first.upload.must_equal config_audio
    version = f.audio_versions.first
    version.audio_version_template_id.wont_be_nil
    version.audio_version_template.segment_count.wont_be_nil
    version.label.must_equal 'Podcast Audio'
    version.explicit.must_be_nil
  end

  describe 'helper methods' do
    let(:sample_link1) do
      'https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/' +
        'radiolab_podcast/radiolab_podcast17updatecrispr.mp3'
    end
    let(:sample_link2) do
      'http://www.podtrac.com/pts/redirect.mp3/media.blubrry.com' +
        '/99percentinvisible/dovetail.prxu.org/99pi/9350e921-b910' +
        '-4b1c-bbc6-2912d79d014f/248-Atom-in-the-Garden-of-Eden.mp3'
    end
    let(:sample_link3) do
      'http://media.blubrry.com/some_name/www.podtrac.com/pts/redirect.mp3/blah'
    end

    it 'can substitute for a missing short description' do
      e = entry.to_h.with_indifferent_access
      episode_import.short_desc(e).must_equal 'An astronomer has turned the night sky into a symphony.'

      e[:itunes_subtitle] = ''
      episode_import.short_desc(e).wont_equal ''

      e[:itunes_subtitle] = nil
      episode_import.short_desc(e).must_equal 'Sidedoor from the Smithsonian: Shake it Up'

      e[:description] = 'Some text that\'s under 50 words'
      episode_import.short_desc(e).must_equal 'Some text that\'s under 50 words'
    end

    it 'can substitute for a missing description' do
      entry.description = nil<commit_msg>cleanup factory use, update short_desc method name<commit_after>require 'test_helper'
require 'prx_access'

describe EpisodeImport do
  include PRXAccess

  let(:user) { create(:user) }
  let(:account) { create(:account, id: 8, opener: user) }
  let(:series) { create(:series) }
  let(:template) { create(:audio_version_template, series: series) }
  let(:distribution) do
    create(:podcast_distribution,
           distributable: series,
           url: 'https://feeder.prx.org/api/v1/podcasts/51')
  end

  let(:importer) { create(:podcast_import, user: user, account: account, series: series) }

  let(:feed) { Feedjira::Feed.parse(test_file('/fixtures/transistor_two.xml')) }
  let(:entry) { feed.entries.first }

  let(:episode_import) do
    EpisodeImport.create!(
      podcast_import: importer,
      audio: {
        files: ['https://cdn-transistor.prx.org/wp-content/uploads/Smithsonian3_Transistor.mp3']
      },
      entry: entry.to_h,
      guid: 'https://transistor.prx.org/?p=1286'
    )
  end

  let(:podcast) do
    api_resource(JSON.parse(json_file('transistor_podcast_basic')), feeder_root).tap do |r|
      r.headers = r.headers.merge('Authorization' => 'Bearer thisisnotatoken')
    end
  end

  before do
    stub_episode_requests
  end

  it 'creates a story on import' do
    f = episode_import.import
    f.description.must_match /^For the next few episodes/
    f.description.wont_match /<script/
    f.description.wont_match /<iframe/
    f.description.wont_match /feedburner/
    f.tags.must_include 'Indie Features'
    f.tags.each do |tag|
      tag.wont_match /\n/
      tag.wont_be :blank?
    end
    f.tags.wont_include '\t'
    f.clean_title.must_equal 'Sidedoor iTunes title'
    f.season_identifier.must_equal '2'
    f.episode_identifier.must_equal '4'
    f.distributions.first.get_episode.itunes_type.must_equal 'full'
    f.account_id.wont_be_nil
    f.creator_id.wont_be_nil
    f.series_id.wont_be_nil
    f.published_at.wont_be_nil
    f.images.count.must_equal 1
    f.audio_versions.count.must_equal 1
    config_audio = 'https://cdn-transistor.prx.org/wp-content/uploads/Smithsonian3_Transistor.mp3'
    f.audio_versions.first.audio_files.first.upload.must_equal config_audio
    version = f.audio_versions.first
    version.audio_version_template_id.wont_be_nil
    version.audio_version_template.segment_count.wont_be_nil
    version.label.must_equal 'Podcast Audio'
    version.explicit.must_be_nil
  end

  describe 'helper methods' do
    let(:sample_link1) do
      'https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/' +
        'radiolab_podcast/radiolab_podcast17updatecrispr.mp3'
    end
    let(:sample_link2) do
      'http://www.podtrac.com/pts/redirect.mp3/media.blubrry.com' +
        '/99percentinvisible/dovetail.prxu.org/99pi/9350e921-b910' +
        '-4b1c-bbc6-2912d79d014f/248-Atom-in-the-Garden-of-Eden.mp3'
    end
    let(:sample_link3) do
      'http://media.blubrry.com/some_name/www.podtrac.com/pts/redirect.mp3/blah'
    end

    it 'can substitute for a missing short description' do
      e = entry.to_h.with_indifferent_access
      episode_import.episode_short_desc(e).must_equal 'An astronomer has turned the night sky into a symphony.'

      e[:itunes_subtitle] = ''
      episode_import.episode_short_desc(e).wont_equal ''

      e[:itunes_subtitle] = nil
      episode_import.episode_short_desc(e).must_equal 'Sidedoor from the Smithsonian: Shake it Up'

      e[:description] = 'Some text that\'s under 50 words'
      episode_import.episode_short_desc(e).must_equal 'Some text that\'s under 50 words'
    end

    it 'can substitute for a missing description' do
      entry.description = nil
{'fraction_non_alphanumeric': 0.11334913112164297, 'fraction_numerical': 0.015534491837809373, 'mean_word_length': 3.0409574468085108, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6868935', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2606, 'n_words': 436}