<commit_before>require "test_helper"

class DaimonSkycrawlersCrawlerTest < Test::Unit::TestCase
  sub_test_case "fetch html" do
    setup do
      stubs = Faraday::Adapter::Test::Stubs.new do |stub|
        stub.get("/") {|env|
          [200, {}, "body"]
        }
      end
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com")
      @crawler.setup_connection do |faraday|
        faraday.adapter :test, stubs
      end
      @crawler.storage = DaimonSkycrawlers::Storage::Null.new
      mock(@crawler).schedule_to_process("http://example.com/", { depth: 3 })
    end

    def test_on_fetch
      @crawler.fetch("http://example.com/", depth: 3) do |url, headers, body|
        assert_equal("http://example.com/", url)
        assert_equal({}, headers)
        assert_equal("body", body)
      end
    end
  end

  sub_test_case "process" do
    setup do
      @body = fixture_path("www.clear-code.com/blog.html").read
      stubs = Faraday::Adapter::Test::Stubs.new do |stub|
        stub.get("/blog") {|env|
          [200, {}, @body]
        }
      end
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com")
      @crawler.setup_connection do |faraday|
        faraday.adapter :test, stubs
      end
      @crawler.storage = DaimonSkycrawlers::Storage::Null.new
      mock(@crawler).schedule_to_process("http://example.com/blog", { depth: 1 })
    end

    def test_fetch_blog
      message = {
        url: "http://example.com/blog",
        depth: 1
      }
      @crawler.process(message) do |url, headers, body|
        assert_equal(url, "http://example.com/blog")
        assert_equal(headers, {})
        assert_equal(body, @body)
      end
    end
  end

  sub_test_case "filter" do
    setup do
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com", options: { obey_robots_txt: true })
      @crawler.storage = DaimonSkycrawlers::Storage::Null.new
      @url = "http://example.com/blog"<commit_msg>Fix test<commit_after>require "test_helper"

class DaimonSkycrawlersCrawlerTest < Test::Unit::TestCase
  sub_test_case "fetch html" do
    setup do
      stubs = Faraday::Adapter::Test::Stubs.new do |stub|
        stub.get("/") {|env|
          [200, {}, "body"]
        }
      end
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com")
      @crawler.setup_connection do |faraday|
        faraday.adapter :test, stubs
      end
    end

    def test_fetch
      @crawler.fetch("http://example.com/", depth: 3) do |data|
        url = data[:url]
        message = data[:message]
        response = data[:response]
        assert_equal("http://example.com/", url)
        assert_equal({ depth: 3 }, message)
        assert_equal({}, response.headers)
        assert_equal("body", response.body)
      end
      assert_equal(1, @crawler.instance_variable_get(:@after_process_callbacks).size)
    end
  end

  sub_test_case "process" do
    setup do
      @body = fixture_path("www.clear-code.com/blog.html").read
      stubs = Faraday::Adapter::Test::Stubs.new do |stub|
        stub.get("/blog") {|env|
          [200, {}, @body]
        }
      end
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com")
      @crawler.setup_connection do |faraday|
        faraday.adapter :test, stubs
      end
      @crawler.storage = DaimonSkycrawlers::Storage::Null.new
      mock(@crawler).schedule_to_process("http://example.com/blog", { depth: 1 })
    end

    def test_fetch_blog
      message = {
        url: "http://example.com/blog",
        depth: 1
      }
      @crawler.process(message) do |data|
        url = data[:url]
        m = data[:message]
        response = data[:response]
        assert_equal(url, "http://example.com/blog")
        assert_equal(m, message)
        assert_equal(response.headers, {})
        assert_equal(response.body, @body)
      end
    end
  end

  sub_test_case "filter" do
    setup do
      @crawler = ::DaimonSkycrawlers::Crawler::Default.new("http://example.com", options: { obey_robots_txt: true })
      @crawler.storage = DaimonSkycrawlers::Storage::Null.new
      @url = "http://example.com/blog"
{'fraction_non_alphanumeric': 0.14630647860387283, 'fraction_numerical': 0.005020320344250538, 'mean_word_length': 2.6573426573426575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6599753', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1404, 'n_words': 247}