require 'rails_helper'

RSpec.describe PostImportUrlListParser do
  describe '.parse' do
    it 'trims URLs, ignores blank lines, and preserves source line numbers' do
      source = "  https://example.com/one  \r\n\r\nhttps://example.com/two\n"

      expect(described_class.parse(source)).to eq([
        { source_row: 1, url: 'https://example.com/one' },
        { source_row: 3, url: 'https://example.com/two' }
      ])
    end

    it 'rejects an empty URL list' do
      expect { described_class.parse(" \n\r\n") }
          .to raise_error(ArgumentError, 'URL を入力してください．')
    end

    it 'rejects more than 100 non-empty rows' do
      source = 101.times.map { |index| "https://example.com/#{ index }" }.join("\n")

      expect { described_class.parse(source) }
          .to raise_error(ArgumentError, '取込件数は 100 件までです．')
    end

    it 'includes the original line number in an oversized URL error' do
      source = "\n#{ 'a' * (described_class::MAX_URL_BYTES + 1) }"

      expect { described_class.parse(source) }
          .to raise_error(ArgumentError, '2 行目: URL が長すぎます．')
    end

    it 'rejects an oversized request before parsing rows' do
      source = 'a' * (described_class::MAX_BYTES + 1)

      expect { described_class.parse(source) }
          .to raise_error(ArgumentError, '入力が大きすぎます．')
    end
  end
end
