# frozen_string_literal: true require 'email_cook' require 'pretty_text' RSpec.describe EmailCook do it "uses to PrettyText when there is no [plaintext] in raw" do raw = "**Hello world!**" expect(cook(raw)).to eq(PrettyText.cook(raw)) end it "adds linebreaks to short lines" do raw = plaintext("hello\nworld\n") expect(cook(raw)).to eq("hello\n
world\n
") end it "doesn't add linebreaks to long lines" do long = plaintext(<<~EMAIL) Hello, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc convallis volutpat risus. Nulla ac faucibus quam, quis cursus lorem. Sed rutrum eget nunc sed accumsan. Vestibulum feugiat mi vitae turpis tempor dignissim. EMAIL long_cooked = (+<<~HTML).strip! Hello,

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc convallis volutpat risus. Nulla ac faucibus quam, quis cursus lorem. Sed rutrum eget nunc sed accumsan. Vestibulum feugiat mi vitae turpis tempor dignissim.
HTML expect(cook(long)).to eq(long_cooked) end it "replaces a blank line with 2 linebreaks" do long = plaintext(<<~EMAIL) Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc convallis volutpat risus. Nulla ac faucibus quam, quis cursus lorem. Sed rutrum eget nunc sed accumsan. Vestibulum feugiat mi vitae turpis tempor dignissim. Stet clita kasd gubergren. EMAIL long_cooked = (+<<~HTML).strip! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc convallis volutpat risus.
Nulla ac faucibus quam, quis cursus lorem. Sed rutrum eget nunc sed accumsan.

Vestibulum feugiat mi vitae turpis tempor dignissim.

Stet clita kasd gubergren.
HTML expect(cook(long)).to eq(long_cooked) end it "escapes HTML" do long = plaintext(<<~EMAIL) Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc convallis volutpat risus. EMAIL long_cooked = (+<<~HTML).strip! Lorem ipsum dolor sit amet, consectetur adipiscing elit.

<form name="f1" method="post" action="test.html" onsubmit="javascript:showAlert()"> <input type="submit" name="submit" value="Click this button" /> </form>

Nunc convallis volutpat risus.
HTML expect(cook(long)).to eq(long_cooked) end it "replaces indentation of more than 2 spaces with corresponding amount of non-breaking spaces" do nbsp = "\u00A0" long = plaintext(<<~EMAIL) Lorem ipsum dolor sit amet, consectetur adipiscing elit. this is intended by 4 spaces this is intended by 1 space no indentation, but lots of spaces EMAIL long_cooked = (+<<~HTML).strip! Lorem ipsum dolor sit amet, consectetur adipiscing elit.

#{nbsp}#{nbsp}#{nbsp}#{nbsp}this is intended by 4 spaces
this is intended by 1 space
no indentation, but lots of spaces
HTML expect(cook(long)).to eq(long_cooked) end it "creates oneboxed link when the line contains only a link" do raw = plaintext("https://www.eviltrout.com") expect(cook(raw)).to eq('https://www.eviltrout.com
') end it "autolinks without the beginning of a line" do raw = plaintext("my site: https://www.eviltrout.com") expect(cook(raw)).to eq('my site: https://www.eviltrout.com
') end it "autolinks without the end of a line" do raw = plaintext("https://www.eviltrout.com is my site") expect(cook(raw)).to eq('https://www.eviltrout.com is my site
') end it "links even within a quote" do raw = plaintext("> https://www.eviltrout.com is my site") expect(cook(raw)).to eq('
https://www.eviltrout.com is my site
') end it "it works and does not interpret Markdown in plaintext and elided" do long = <<~EMAIL [plaintext] *Lorem ipsum* dolor sit amet, consectetur adipiscing elit. [/plaintext] [attachments] [/attachments] [elided] At vero eos *et accusam* et justo duo dolores et ea rebum. [/elided] EMAIL long_cooked = <<~HTML *Lorem ipsum* dolor sit amet, consectetur adipiscing elit.



··· At vero eos *et accusam* et justo duo dolores et ea rebum.
HTML expect(cook(long)).to eq(long_cooked) end it "works without attachments" do long = <<~EMAIL [plaintext] *Lorem ipsum* dolor sit amet, consectetur adipiscing elit. [/plaintext] [elided] At vero eos *et accusam* et justo duo dolores et ea rebum. [/elided] EMAIL long_cooked = <<~HTML *Lorem ipsum* dolor sit amet, consectetur adipiscing elit.


··· At vero eos *et accusam* et justo duo dolores et ea rebum.
HTML expect(cook(long)).to eq(long_cooked) end def cook(raw) EmailCook.new(raw).cook end def plaintext(text) "[plaintext]\n#{text}\n[/plaintext]" end end