require 'spec_helper'

RSpec.describe Jekyll::Tags::ImageTransformTag do
  let(:site) { create_test_site }
  let(:context) { create_liquid_context(site) }
  let(:skyhook) { Jekyll::Skyhook.instance(site) }

  before do
    create_test_image(site, 'assets/test.jpg', 200, 200)
    skyhook.process_assets
  end

  after do
    FileUtils.rm_rf(site.source)
    FileUtils.rm_rf(site.dest)
    FileUtils.rm_rf(site.cache_dir)
  end

  describe '#parse_markup' do
    it 'parses simple path without transformations' do
      tag = Jekyll::Tags::ImageTransformTag.allocate
      tag.instance_variable_set(:@markup, 'assets/test.jpg')
      path, transformations = tag.send(:parse_markup, 'assets/test.jpg')

      expect(path).to eq('assets/test.jpg')
      expect(transformations).to eq({})
    end

    it 'parses path with single transformation' do
      tag = Jekyll::Tags::ImageTransformTag.allocate
      path, transformations = tag.send(:parse_markup, 'assets/test.jpg[width="400"]')

      expect(path).to eq('assets/test.jpg')
      expect(transformations).to eq({'width' => '400'})
    end

    it 'parses path with multiple transformations' do
      tag = Jekyll::Tags::ImageTransformTag.allocate
      path, transformations = tag.send(:parse_markup, 'assets/test.jpg[width="400"][format="webp"]')

      expect(path).to eq('assets/test.jpg')
      expect(transformations).to eq({'width' => '400', 'format' => 'webp'})
    end

    it 'handles paths with spaces' do
      tag = Jekyll::Tags::ImageTransformTag.allocate
      path, transformations = tag.send(:parse_markup, 'assets/my image.jpg[width="400"]')

      expect(path).to eq('assets/my image.jpg')
      expect(transformations).to eq({'width' => '400'})
    end
  end

  describe '#render' do
    it 'returns original digested path when no transformations' do
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg')
      result = tag.render(context)

      expect(result).to start_with('/_digested/assets/test-')
      expect(result).to end_with('.jpg')
    end

    it 'returns transformed path with width transformation' do
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[width="100"]')
      result = tag.render(context)

      expect(result).to start_with('/_digested/assets/test-width100-')
      expect(result).to end_with('.jpg')
    end

    it 'returns transformed path with format transformation' do
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[format="webp"]')
      result = tag.render(context)

      expect(result).to start_with('/_digested/assets/test-formatwebp-')
      expect(result).to end_with('.webp')
    end

    it 'returns transformed path with multiple transformations' do
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[width="100"][format="png"]')
      result = tag.render(context)

      expect(result).to start_with('/_digested/assets/test-formatpng-width100-')
      expect(result).to end_with('.png')
    end

    it 'raises error for missing file' do
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/missing.jpg[width="100"]')

      expect { tag.render(context) }.to raise_error(/Image file not found/)
    end

    it 'processes liquid variables in path' do
      var_context = create_liquid_context(site, { 'image_path' => 'assets/test.jpg' })
      tag = create_tag(Jekyll::Tags::ImageTransformTag, '{{ image_path }}[width="100"]')
      result = tag.render(var_context)

      expect(result).to start_with('/_digested/assets/test-width100-')
    end

    it 'processes liquid variables in transformation values' do
      var_context = create_liquid_context(site, { 'target_width' => '150' })
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[width="{{ target_width }}"]')
      result = tag.render(var_context)

      expect(result).to start_with('/_digested/assets/test-width150-')
    end
  end

  describe 'caching behavior' do
    it 'returns same result for identical transformations' do
      tag1 = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[width="100"]')
      tag2 = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/test.jpg[width="100"]')

      result1 = tag1.render(context)
      result2 = tag2.render(context)

      expect(result1).to eq(result2)
    end
  end
end
