# frozen_string_literal: true

require File.join(File.dirname(__FILE__), '/../spec_helper')

describe Gopher::Rendering::Menu do
  before do
    @app = Gopher::Application.new
    @app.reset!
    @app.config[:host] = 'host'
    @app.config[:port] = 1234

    @ctx = described_class.new(@app)
  end

  it 'adds text as a gopher line' do
    @ctx.text('gopher forever')
    expect(@ctx.result).to eq("igopher forever\tnull\t(FALSE)\t0\r\n")
  end

  describe 'sanitize_text' do
    it 'removes extra whitespace from end of line' do
      expect(@ctx.sanitize_text('x   ')).to eq('x')
    end

    it 'converts tabs to spaces' do
      expect(@ctx.sanitize_text("x\tx")).to eq('x        x')
    end

    it 'removes newlines' do
      expect(@ctx.sanitize_text("x\nx")).to eq('xx')
    end
  end

  describe 'line' do
    it 'works' do
      expect(@ctx.line('type', 'text', 'selector', 'host', 'port')).to eq("typetext\tselector\thost\tport\r\n")
    end

    it 'uses application host/port as defaults' do
      expect(@ctx.line('type', 'text', 'selector')).to eq("typetext\tselector\thost\t1234\r\n")
    end
  end

  describe 'error' do
    it 'calls text with right selector' do
      expect(@ctx).to receive(:text).with('foo', '3')
      @ctx.error('foo')
    end
  end

  describe 'directory' do
    it 'calls link with right selector' do
      expect(@ctx).to receive(:line).with('1', 'foo', '/bar/foo', nil, nil)
      @ctx.directory('foo', '/bar/foo')
    end
  end

  describe 'link' do
    context 'with filepath' do
      it 'gets type with determine_type' do
        expect(@ctx).to receive(:determine_type).with('foo.txt').and_return('A')
        expect(@ctx.link('FILE', 'thing', 'external-server.com', 70,
                         'foo.txt')).to eq("AFILE\tthing\texternal-server.com\t70\r\n")
      end
    end

    context 'without filepath' do
      it 'gets type with determine_type' do
        expect(@ctx).to receive(:determine_type).with('foo.txt').and_return('A')
        expect(@ctx.link('FILE', 'foo.txt')).to eq("AFILE\tfoo.txt\thost\t1234\r\n")
      end
    end
  end

  describe 'text_link' do
    it 'works' do
      expect(@ctx.text_link('text link', '/files/foo.txt')).to eq("0text link\t/files/foo.txt\thost\t1234\r\n")
    end
  end

  describe 'http' do
    it 'works' do
      expect(@ctx.http('weblink', 'http://google.com')).to eq("hweblink\tURL:http://google.com\thost\t1234\r\n")
    end
  end

  describe 'search' do
    it 'outputs link type/text' do
      expect(@ctx.search('FIND', 'search')).to eq("7FIND\tsearch\thost\t1234\r\n")
    end
  end

  describe 'menu' do
    it 'outputs link type/text' do
      expect(@ctx.menu('MENU ITEM', 'item')).to eq("1MENU ITEM\titem\thost\t1234\r\n")
    end
  end

  describe 'br' do
    it 'generates an empty text line' do
      expect(@ctx).to receive(:text).with('i', '')
      @ctx.br
    end

    it 'calls #text multiple times' do
      expect(@ctx).to receive(:text).twice.with('i', '')
      @ctx.br(2)
    end

    it 'outputs an empty menu item' do
      @ctx.br
      expect(@ctx.result).to eq("i\tnull\t(FALSE)\t0\r\n")
    end
  end

  context 'determine_type' do
    {
      'foo.zip' => '5',
      'foo.gz' => '5',
      # "foo.bz2" => '5',
      'foo.gif' => 'g',
      'foo.jpg' => 'I',
      'foo.png' => 'I',
      'foo.mp3' => 's',
      'foo.wav' => 's',
      'foo.random-file' => '9'
    }.each do |file, expected|
      it "has right selector for #{file}" do
        expect(@ctx.determine_type(file)).to eq(expected)
      end
    end
  end
end
