<commit_before>require 'spec_helper'
require 'base64'

describe "Adding HTML and Images through the alternate chunk feature" do

  let(:report_path){"#{File.expand_path('samples', __dir__)}/report.docx"}
  let(:encoded_img){Base64.encode64(File.read("#{File.expand_path('samples', __dir__)}/caterpillar.jpg"))}

  let(:t){TemplateDocument.new(path: report_path)}

  it "should register MIME html type" do
    t.process 'my_content' => {text: 'empty', html: true}
    doc = Nokogiri::XML(t.parts['[Content_Types].xml'])
    doc.xpath('//xmlns:Default/@ContentType').map(&:value).must_include 'message/rfc822'
  end

  it "should add the chunk id to the rels file" do
    t.process 'my_content' => {text: 'empty', html: true}
    doc = Nokogiri::XML(t.parts['word/_rels/document.xml.rels'])
    doc.xpath('//xmlns:Relationship/@Id').map(&:value).must_include 'my_content'
  end

  it "should generate MIME html file" do
    t.process 'my_content' => {text: '<u>This is underlined</u>', html: true}
    t.parts['word/my_content.mht'].must_match(/#{'<u>This is underlined</u>'}/)
  end

  it "should generate MIME html file with a image" do
    content = '<h1>Look at the image</h1><img src="./image.jpg" />'

    t.process 'my_content' => {text: content, html: true, images: {"./image.jpg" => encoded_img}}
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: ./image.jpg'}/)
  end

  it "should geneate MIME html file with multiple images" do
    content = '<h1>Look at the images</h1>'
    content << '<img src="/image.jpg" /><br/><br/><img src="/image2.jpg" />'
    t.process 'my_content' => {text: content, html: true, images: {"/image.jpg" => encoded_img, '/image2.jpg' => encoded_img}}
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: /image.jpg'}/)
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: /image2.jpg'}/)
    IO.write File.expand_path('~/Downloads/test.docx'), t.to_zip_buffer.string
  end<commit_msg>removed line that was generating the word document in the test case<commit_after>require 'spec_helper'
require 'base64'

describe "Adding HTML and Images through the alternate chunk feature" do

  let(:report_path){"#{File.expand_path('samples', __dir__)}/report.docx"}
  let(:encoded_img){Base64.encode64(File.read("#{File.expand_path('samples', __dir__)}/caterpillar.jpg"))}

  let(:t){TemplateDocument.new(path: report_path)}

  it "should register MIME html type" do
    t.process 'my_content' => {text: 'empty', html: true}
    doc = Nokogiri::XML(t.parts['[Content_Types].xml'])
    doc.xpath('//xmlns:Default/@ContentType').map(&:value).must_include 'message/rfc822'
  end

  it "should add the chunk id to the rels file" do
    t.process 'my_content' => {text: 'empty', html: true}
    doc = Nokogiri::XML(t.parts['word/_rels/document.xml.rels'])
    doc.xpath('//xmlns:Relationship/@Id').map(&:value).must_include 'my_content'
  end

  it "should generate MIME html file" do
    t.process 'my_content' => {text: '<u>This is underlined</u>', html: true}
    t.parts['word/my_content.mht'].must_match(/#{'<u>This is underlined</u>'}/)
  end

  it "should generate MIME html file with a image" do
    content = '<h1>Look at the image</h1><img src="./image.jpg" />'

    t.process 'my_content' => {text: content, html: true, images: {"./image.jpg" => encoded_img}}
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: ./image.jpg'}/)
  end

  it "should geneate MIME html file with multiple images" do
    content = '<h1>Look at the images</h1>'
    content << '<img src="/image.jpg" /><br/><br/><img src="/image2.jpg" />'
    t.process 'my_content' => {text: content, html: true, images: {"/image.jpg" => encoded_img, '/image2.jpg' => encoded_img}}
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: /image.jpg'}/)
    t.parts['word/my_content.mht'].must_match(/#{'Content-Location: /image2.jpg'}/)
  end
{'fraction_non_alphanumeric': 0.18751598874392428, 'fraction_numerical': 0.008186236889229983, 'mean_word_length': 4.415512465373961, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4911366', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1371, 'n_words': 301}