require "rails_helper"

RSpec.describe LogEntriesHelper, type: :helper do
  describe "#render_rich_text_content" do
    def build_log_entry(content_type, fixture_path, binary: true)
      build(
        :http_log_entry,
        content_type: content_type,
        response:
          build(
            :blob_file,
            content_type: content_type,
            contents:
              (
                if binary
                  File.binread(Rails.root.join(fixture_path))
                else
                  File.read(Rails.root.join(fixture_path))
                end
              ),
          ),
      )
    end

    it "works with msword files" do
      log_entry =
        build_log_entry(
          "application/msword",
          "test/fixtures/files/5447897_Thakur_trials_13.doc",
        )

      rendered = helper.render_rich_text_content(log_entry)
      expect(rendered).not_to be_nil
      # no abiword header
      expect(rendered).not_to include("Abiword HTML Document")
      # no bbcode tags
      expect(rendered).not_to include("[i]")
      expect(rendered).not_to include("[b]")
      # remove line breaks
      expect(rendered).not_to include("<br />")
      # remove escaped html tags
      expect(rendered).not_to include("&lt;br /&gt;")
      expect(rendered).not_to include("&lt;")

      expect(rendered).to include("The next morning")
    end

    it "works with other doc files" do
      log_entry =
        build_log_entry(
          "application/msword",
          "test/fixtures/files/1665721884.wolfsnack.writer_doc.doc",
        )
      rendered = helper.render_rich_text_content(log_entry)
      expect(rendered).not_to be_nil
      expect(rendered).to include("Now, America")
      expect(rendered).to include("Years passed.")
    end

    it "works with pdf files" do
      log_entry =
        build_log_entry(
          "application/pdf",
          "test/fixtures/files/1677472939.wolfsnack.writer_maze_of_the_wolf.pdf",
        )
      rendered = helper.render_rich_text_content(log_entry)
      expect(rendered).not_to be_nil
      expect(rendered).to include("The Garden")
      expect(rendered).to include("Will promised -- as best he could")
    end

    it "parses bbcode in plain text" do
      log_entry =
        build_log_entry(
          "text/plain",
          "test/fixtures/files/1674254968.darkviolet_firedancer_text.txt",
          binary: false,
        )
      rendered = helper.render_rich_text_content(log_entry)
      expect(rendered).not_to be_nil
    end

    it "works in txt files encoded with utf-16" do
      log_entry =
        build_log_entry(
          "text/plain",
          "test/fixtures/files/1321054359.ztf_tolartist.txt",
        )

      rendered = helper.render_rich_text_content(log_entry)
      expect(rendered).not_to be_nil
      expect(rendered).to include("at the local cafe")
    end
  end

  describe "#sanitize_rich_text_document_html" do
    it "puts sequences of inline elements into a <p>" do
      html =
        "
          <div>
            Does that
            <em>really</em>
            make you...
          </div>
        "

      rendered = helper.sanitize_rich_text_document_html(html, true)
      expect(rendered).to eq_html(
        "<div><p>Does that <em>really</em> make you...</p></div>",
      )
    end
  end
end
