<commit_before>require 'spec_helper'

module Calagator

describe Source::Parser, "when reading content", :type => :model do
  it "should read from a normal URL" do
    stub_request(:get, "http://a.real/~url").to_return(body: "42")
    expect(Source::Parser.read_url("http://a.real/~url")).to eq "42"
  end
end

describe Source::Parser, "when subclassing", :type => :model do
  it "should demand that #to_events is implemented" do
    expect{ Source::Parser.new.to_events }.to raise_error NotImplementedError
  end
end

describe Source::Parser, "when parsing events", :type => :model do
  it "should have site-specific parsers first, then generics" do
    expect(Source::Parser.parsers.to_a).to eq [
      Source::Parser::Facebook,
      Source::Parser::Meetup,
      Source::Parser::Plancast,
      Source::Parser::Hcal,
      Source::Parser::Ical,
    ]
  end

  it "should use first successful parser's results" do<commit_msg>test unauthorized case in Source::Parser.read_url.<commit_after>require 'spec_helper'

module Calagator

describe Source::Parser, "when reading content", :type => :model do
  it "should read from a normal URL" do
    stub_request(:get, "http://a.real/~url").to_return(body: "42")
    expect(Source::Parser.read_url("http://a.real/~url")).to eq "42"
  end

  it "should raise an error when unauthorized" do
    stub_request(:get, "http://a.private/~url").to_return(status: [401, "Forbidden"])
    expect {
      Source::Parser.read_url("http://a.private/~url")
    }.to raise_error Source::Parser::HttpAuthenticationRequiredError
  end
end

describe Source::Parser, "when subclassing", :type => :model do
  it "should demand that #to_events is implemented" do
    expect{ Source::Parser.new.to_events }.to raise_error NotImplementedError
  end
end

describe Source::Parser, "when parsing events", :type => :model do
  it "should have site-specific parsers first, then generics" do
    expect(Source::Parser.parsers.to_a).to eq [
      Source::Parser::Facebook,
      Source::Parser::Meetup,
      Source::Parser::Plancast,
      Source::Parser::Hcal,
      Source::Parser::Ical,
    ]
  end

  it "should use first successful parser's results" do
{'fraction_non_alphanumeric': 0.13575701794753797, 'fraction_numerical': 0.0050621260929590425, 'mean_word_length': 4.328431372549019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7578918', 'n_tokens_mistral': 701, 'n_tokens_neox': 668, 'n_words': 187}