require 'rails_helper'

RSpec.describe PostUrlNormaliser do
  before do
    PostUrlSanitisationRule.unscoped.delete_all
  end

  describe '.normalise' do
    it 'normalises the host and trailing slash before applying sanitisation rules' do
      PostUrlSanitisationRule.create!(
        priority: 10,
        source_pattern: '\\Ahttps://example\\.com/source\\z',
        replacement: 'https://example.com/canonical'
      )

      result = described_class.normalise('  https://EXAMPLE.com/source/  ')

      expect(result).to eq('https://example.com/canonical')
    end

    it 'returns nil for unsupported or malformed URLs' do
      expect(described_class.normalise('ftp://example.com/file')).to be_nil
      expect(described_class.normalise('https://[')).to be_nil
      expect(described_class.normalise('https:/path')).to be_nil
    end
  end
end
