require 'spec_helper'

RSpec.describe Jekyll::Tags::SrcsetTag 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_srcset_markup' do
    let(:tag) { Jekyll::Tags::SrcsetTag.allocate }

    it 'parses simple path with widths' do
      path, format, widths = tag.send(:parse_srcset_markup, 'assets/test.jpg 400 800 1200')

      expect(path).to eq('assets/test.jpg')
      expect(format).to be_nil
      expect(widths).to eq([400, 800, 1200])
    end

    it 'parses path with format and widths' do
      path, format, widths = tag.send(:parse_srcset_markup, 'assets/test.jpg[format="webp"] 400 800 1200')

      expect(path).to eq('assets/test.jpg')
      expect(format).to eq('webp')
      expect(widths).to eq([400, 800, 1200])
    end

    it 'handles single width' do
      path, format, widths = tag.send(:parse_srcset_markup, 'assets/test.jpg 400')

      expect(path).to eq('assets/test.jpg')
      expect(format).to be_nil
      expect(widths).to eq([400])
    end

    it 'handles format without widths gracefully' do
      path, format, widths = tag.send(:parse_srcset_markup, 'assets/test.jpg[format="webp"]')

      expect(path).to eq('assets/test.jpg')
      expect(format).to be_nil  # Falls back to simple parsing
      expect(widths).to eq([])
    end
  end

  describe '#render' do
    it 'generates srcset with original format' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg 400 800')
      result = tag.render(context)

      expect(result).to start_with('srcset="')
      expect(result).to end_with('"')
      expect(result).to include('400w')
      expect(result).to include('800w')
      expect(result).to include('_digested/assets/test-width400-')
      expect(result).to include('_digested/assets/test-width800-')
    end

    it 'generates srcset with specified format' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg[format="webp"] 400 800')
      result = tag.render(context)

      expect(result).to start_with('srcset="')
      expect(result).to include('400w')
      expect(result).to include('800w')
      expect(result).to include('formatwebp-width400-')
      expect(result).to include('formatwebp-width800-')
      expect(result).to include('.webp')
    end

    it 'handles single width' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg 400')
      result = tag.render(context)

      expect(result).to eq('srcset="/_digested/assets/test-width400-' + result.match(/width400-([^\.]+)\.jpg/)[1] + '.jpg 400w"')
    end

    it 'maintains width order' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg 1200 400 800')
      result = tag.render(context)

      srcset_content = result.match(/srcset="([^"]+)"/)[1]
      entries = srcset_content.split(', ')

      expect(entries[0]).to include('1200w')
      expect(entries[1]).to include('400w')
      expect(entries[2]).to include('800w')
    end

    it 'raises error for missing file' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/missing.jpg 400')

      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::SrcsetTag, '{{ image_path }} 400')
      result = tag.render(var_context)

      expect(result).to include('_digested/assets/test-width400-')
    end

    it 'handles complex file paths' do
      create_test_image(site, 'assets/images/posts/2024/my-post/hero.jpg')
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/images/posts/2024/my-post/hero.jpg 400')
      result = tag.render(context)

      expect(result).to include('_digested/assets/images/posts/2024/my-post/hero-width400-')
    end
  end

  describe 'format handling' do
    it 'uses original format when no format specified' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg 400')
      result = tag.render(context)

      expect(result).to include('.jpg')
    end

    it 'converts to webp when specified' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg[format="webp"] 400')
      result = tag.render(context)

      expect(result).to include('.webp')
      expect(result).not_to include('.jpg')
    end

    it 'converts to avif when specified' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg[format="avif"] 400')
      result = tag.render(context)

      expect(result).to include('.avif')
    end

    it 'converts to png when specified' do
      tag = create_tag(Jekyll::Tags::SrcsetTag, 'assets/test.jpg[format="png"] 400')
      result = tag.render(context)

      expect(result).to include('.png')
    end
  end
end
