# frozen_string_literal: true require 'spec_helper' describe Slack::Messages::Formatting do subject(:formatting) do described_class end context '#unescape' do it 'plain text' do expect(formatting.unescape('plain text')).to eq 'plain text' end it 'decodes an HTML-encoded message' do expect(formatting.unescape('Hello & <world>')).to eq 'Hello & ' end it 'unescapes a user reference' do expect(formatting.unescape('Hey <@U024BE7LH|bob>, did you see my file?')).to( eq('Hey @bob, did you see my file?') ) end it 'unescapes a user reference without a name' do expect(formatting.unescape('<@U02BEFY4U> ^^^')).to eq '@U02BEFY4U ^^^' end it 'unescapes a URL without text' do expect(formatting.unescape('This message contains a URL ')).to( eq('This message contains a URL http://foo.com/') ) end it 'unescapes a URL with text' do expect(formatting.unescape('So does this one: ')).to( eq('So does this one: www.foo.com') ) end it 'removes mailto' do expect(formatting.unescape('')).to eq 'Bob' end it 'unlinkifies references' do expect( formatting.unescape('Hello <@U123|bob>, say hi to in <#C1234|general>') ).to( eq('Hello @bob, say hi to @everyone in #general') ) end it 'can handle a lone >' do expect(formatting.unescape('Hello <@U123|bob> > file.txt')).to eq 'Hello @bob > file.txt' end it 'unescapes a double smart quote' do expect(formatting.unescape('“hello”')).to eq '"hello"' end it 'unescapes a single smart quote' do expect(formatting.unescape('‘hello’')).to eq "'hello'" end end context '#escape' do it 'plain text' do expect(formatting.escape('plain text')).to eq 'plain text' end it 'escapes a message' do expect(formatting.escape('Hello & ')).to eq 'Hello & <world>' end end context '#date' do let(:timestamp) { -2955646679 } let(:time) { Time.at(timestamp) } let(:custom_format) { '{date_short}' } let(:optional_link) { 'https://www.timeanddate.com/worldclock/fixedtime.html?year=1876&month=5&day=4&hour=03&min=02&sec=01' } let(:fallback_text) { 'a long, long time ago' } it 'formats a timestamp' do expect(formatting.date(time)).to eq "" end it 'can use custom format' do expect(formatting.date(time, format: custom_format)).to eq "" end it 'can add a custom link' do expect(formatting.date(time, link: optional_link)).to eq "" end it 'can add custom fallback text' do expect(formatting.date(time, text: fallback_text)).to eq "" end end context '#channel_link' do let(:channel_id) { 'C0000000001' } it 'links to a channel by its ID' do expect(formatting.channel_link(channel_id)).to eq "<##{channel_id}>" end end context '#user_link' do let(:user_id) { 'U0000000001' } it 'links to a user by its ID' do expect(formatting.user_link(user_id)).to eq "<@#{user_id}>" end end context '#url_link' do let(:text) { 'super cool website' } let(:url) { 'https://theuselessweb.site/' } it 'formats a URL with custom link text' do expect(formatting.url_link(text, url)).to eq "<#{url}|#{text}>" end end context '#markdown' do it 'formats markdown bold' do expect(formatting.markdown('**Le bold**')).to eq '*Le bold*' end it 'formats markdown italic' do expect(formatting.markdown("*L'italic*")).to eq "_L'italic_" end it 'formats markdown bold and italic' do expect(formatting.markdown('***Le bold italic***')).to eq '*_Le bold italic_*' end it 'formats markdown strikethrough' do expect(formatting.markdown('~~Le strikethrough~~')).to eq '~Le strikethrough~' end it 'formats markdown links' do expect(formatting.markdown('[Le link](https://theuselessweb.site)')).to eq '' end it 'formats nested markdown' do expect(formatting.markdown('**[Le **bold and ~~struckout with *italic*~~** link](https://theuselessweb.site)**')).to( eq '**' ) end it "doesn't format other markdown" do expect(formatting.markdown('## A heading\n_Italics_\n`code`')).to eq '## A heading\n_Italics_\n`code`' end end end