<commit_before># -*- coding: utf-8 -*-
require_relative 'helper'
require 'epub/searcher'
require 'epub/parser/cfi'

class TestSearcher < Test::Unit::TestCase
  class TestPublication < self
    def setup
      super
      opf_path = File.expand_path('../fixtures/book/OPS/ルートファイル.opf', __FILE__)
      nav_path = File.expand_path('../fixtures/book/OPS/nav.xhtml', __FILE__)
      @package = EPUB::Parser::Publication.new(open(opf_path)).parse
      @package.spine.each_itemref do |itemref|
        stub(itemref.item).read {
          itemref.idref == 'nav' ? File.read(nav_path) : '<html></html>'
        }
      end
    end

    def test_no_result
      assert_empty EPUB::Searcher::Publication.search_text(@package, 'no result')
    end

    def test_simple
      assert_equal(
        results([
          [[[:element, 2, {:name => 'spine', :id => nil}], [:itemref, 0, {:id => nil}], [:element, 0, {:name => 'head', :id => nil}], [:element, 0, {:name => 'title', :id => nil}], [:text, 0]], [[:character, 9]], [[:character, 16]]],
          [[[:element, 2, {:name => 'spine', :id => nil}], [:itemref, 0, {:id => nil}], [:element, 1, {:name => 'body', :id => nil}], [:element, 0, {:name => 'div', :id => nil}], [:element, 0, {:name => 'nav', :id => 'idid'}], [:element, 0, {:name => 'hgroup', :id => nil}], [:element, 1, {:name => 'h1', :id => nil}], [:text, 0]], [[:character, 9]], [[:character, 16]]]
        ]),
        EPUB::Searcher::Publication.search_text(@package, 'Content')
      )
    end

    def test_search_element_xpath_without_namespaces
      assert_equal(
        [
          "epubcfi(/4/2!/4/2/2[idid]/4/2/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/2/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/4/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/6/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/8/2)"
        ],
        EPUB::Searcher::Publication.search_element(@package, xpath: './/xhtml:a').collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    def test_search_element_xpath_with_namespaces
      assert_equal(
        [
          "epubcfi(/4/2!/4/2/2[idid]/4/2/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/2/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/4/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/6/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/8/2)"
        ],
        EPUB::Searcher::Publication.search_element(@package, xpath: './/customnamespace:a', namespaces: {'customnamespace' => 'http://www.w3.org/1999/xhtml'}).collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    def test_search_element_css_selector
      assert_equal(
        [
          "epubcfi(/4/2!/4/2/2[idid]/4/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/2)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/4)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/6)",
          "epubcfi(/4/2!/4/2/2[idid]/4/4/4/8)"
        ],
        EPUB::Searcher::Publication.search_element(@package, css: 'ol > li').collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    class TesetResult < self
      def test_to_cfi
        assert_equal 'epubcfi(/6/2!/4/2/2[idid]/2/4/1,:9,:16)', EPUB::Searcher::Publication.search_text(@package, 'Content').last.to_cfi.to_fragment
      end
    end
  end

  class TestXHTML < self
    def setup<commit_msg>Fix CFIs in searcher tests<commit_after># -*- coding: utf-8 -*-
require_relative 'helper'
require 'epub/searcher'
require 'epub/parser/cfi'

class TestSearcher < Test::Unit::TestCase
  class TestPublication < self
    def setup
      super
      opf_path = File.expand_path('../fixtures/book/OPS/ルートファイル.opf', __FILE__)
      nav_path = File.expand_path('../fixtures/book/OPS/nav.xhtml', __FILE__)
      @package = EPUB::Parser::Publication.new(open(opf_path)).parse
      @package.spine.each_itemref do |itemref|
        stub(itemref.item).read {
          itemref.idref == 'nav' ? File.read(nav_path) : '<html></html>'
        }
      end
    end

    def test_no_result
      assert_empty EPUB::Searcher::Publication.search_text(@package, 'no result')
    end

    def test_simple
      assert_equal(
        results([
          [[[:element, 2, {:name => 'spine', :id => nil}], [:itemref, 0, {:id => nil}], [:element, 0, {:name => 'head', :id => nil}], [:element, 0, {:name => 'title', :id => nil}], [:text, 0]], [[:character, 9]], [[:character, 16]]],
          [[[:element, 2, {:name => 'spine', :id => nil}], [:itemref, 0, {:id => nil}], [:element, 1, {:name => 'body', :id => nil}], [:element, 0, {:name => 'div', :id => nil}], [:element, 0, {:name => 'nav', :id => 'idid'}], [:element, 0, {:name => 'hgroup', :id => nil}], [:element, 1, {:name => 'h1', :id => nil}], [:text, 0]], [[:character, 9]], [[:character, 16]]]
        ]),
        EPUB::Searcher::Publication.search_text(@package, 'Content')
      )
    end

    def test_search_element_xpath_without_namespaces
      assert_equal(
        [
          "epubcfi(/6/2!/4/2/2[idid]/4/2/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/2/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/4/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/6/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/8/2)"
        ],
        EPUB::Searcher::Publication.search_element(@package, xpath: './/xhtml:a').collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    def test_search_element_xpath_with_namespaces
      assert_equal(
        [
          "epubcfi(/6/2!/4/2/2[idid]/4/2/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/2/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/4/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/6/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/8/2)"
        ],
        EPUB::Searcher::Publication.search_element(@package, xpath: './/customnamespace:a', namespaces: {'customnamespace' => 'http://www.w3.org/1999/xhtml'}).collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    def test_search_element_css_selector
      assert_equal(
        [
          "epubcfi(/6/2!/4/2/2[idid]/4/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/2)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/4)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/6)",
          "epubcfi(/6/2!/4/2/2[idid]/4/4/4/8)"
        ],
        EPUB::Searcher::Publication.search_element(@package, css: 'ol > li').collect {|result| result[:location]}.map(&:to_fragment)
      )
    end

    class TesetResult < self
      def test_to_cfi
        assert_equal 'epubcfi(/6/2!/4/2/2[idid]/2/4/1,:9,:16)', EPUB::Searcher::Publication.search_text(@package, 'Content').last.to_cfi.to_fragment
      end
    end
  end

  class TestXHTML < self
    def setup
{'fraction_non_alphanumeric': 0.22924429870323446, 'fraction_numerical': 0.054553584736920555, 'mean_word_length': 2.582487987186332, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 51, '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': '1420731', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2737, 'n_words': 379}