require 'spec_helper'

RSpec.describe 'Jekyll Build Output' do
  let(:site) { create_test_site }
  let(:context) { create_liquid_context(site) }
  let(:skyhook) { Jekyll::Skyhook.instance(site) }

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

  describe 'static file registration for Jekyll build' do
    it 'registers digested assets with correct paths for _site output' do
      # Create assets
      create_test_image(site, 'assets/images/hero.jpg', 800, 600)
      create_test_css(site, 'assets/styles.css')
      create_test_js(site, 'assets/app.js')

      # Process assets
      skyhook.process_assets

      # Find the digested static files
      digested_files = site.static_files.select do |file|
        file.name.match?(/.*-[a-f0-9]{32}\./)
      end

      expect(digested_files.length).to be >= 3

      # Check that static files have correct directory structure
      css_file = digested_files.find { |f| f.name.include?('styles-') }
      js_file = digested_files.find { |f| f.name.include?('app-') }
      img_file = digested_files.find { |f| f.name.include?('hero-') }

      expect(css_file).not_to be_nil
      expect(js_file).not_to be_nil
      expect(img_file).not_to be_nil

      # These should have the _digested directory in their path
      expect(css_file.instance_variable_get(:@dir)).to eq('_digested/assets')
      expect(js_file.instance_variable_get(:@dir)).to eq('_digested/assets')
      expect(img_file.instance_variable_get(:@dir)).to eq('_digested/assets/images')

      # Check that the base is correct (should be site.source)
      expect(css_file.instance_variable_get(:@base)).to eq(site.source)
      expect(js_file.instance_variable_get(:@base)).to eq(site.source)
      expect(img_file.instance_variable_get(:@base)).to eq(site.source)
    end

    it 'registers image transformations with correct paths' do
      # Create image
      create_test_image(site, 'assets/hero.jpg', 1200, 800)

      # Process base assets first
      skyhook.process_assets

      # Do image transformation
      tag = create_tag(Jekyll::Tags::ImageTransformTag, 'assets/hero.jpg[width="400"][format="webp"]')
      result = tag.render(context)

      # Find the transformation static file
      transform_files = site.static_files.select do |file|
        file.name.include?('formatwebp-width400')
      end

      expect(transform_files.length).to eq(1)

      transform_file = transform_files.first
      expect(transform_file.instance_variable_get(:@dir)).to eq('_digested/assets')
      expect(transform_file.instance_variable_get(:@base)).to eq(site.source)
    end

    it 'simulates Jekyll build destination paths' do
      # Create assets
      create_test_image(site, 'assets/images/nested/photo.jpg')
      create_test_css(site, 'assets/css/main.css')

      # Process assets
      skyhook.process_assets

      # Simulate what Jekyll does during build - calculate destination paths
      digested_files = site.static_files.select do |file|
        file.name.match?(/.*-[a-f0-9]{32}\./)
      end

      destination_paths = digested_files.map do |file|
        # This is what Jekyll uses internally to determine where files go in _site
        File.join(file.instance_variable_get(:@dir), file.name)
      end

      # Check that destination paths include _digested
      css_dest = destination_paths.find { |path| path.include?('main-') }
      img_dest = destination_paths.find { |path| path.include?('photo-') }

      expect(css_dest).to start_with('_digested/assets/css/')
      expect(img_dest).to start_with('_digested/assets/images/nested/')

      # These would be the final paths in _site/
      expect(css_dest).to match(%r{^_digested/assets/css/main-[a-f0-9]{32}\.css$})
      expect(img_dest).to match(%r{^_digested/assets/images/nested/photo-[a-f0-9]{32}\.jpg$})
    end

    it 'ensures tag output matches static file destinations' do
      # Create asset
      create_test_css(site, 'assets/styles.css')

      # Process assets
      skyhook.process_assets

      # Get tag output
      tag = create_tag(Jekyll::Tags::AssetTag, 'assets/styles.css')
      tag_result = tag.render(context)

      # Find corresponding static file
      static_file = site.static_files.find do |file|
        file.name.include?('styles-') && file.name.end_with?('.css')
      end

      expect(static_file).not_to be_nil

      # Calculate what Jekyll will output as the destination
      static_file_dest = File.join(static_file.instance_variable_get(:@dir), static_file.name)

      # Tag output should match static file destination (with leading slash)
      expect(tag_result).to eq("/#{static_file_dest}")
    end
  end

  describe 'nested directory structure preservation' do
    it 'maintains directory structure in _digested for deeply nested assets' do
      # Create deeply nested assets
      create_test_image(site, 'assets/images/posts/2024/01/hero.jpg')
      create_test_css(site, 'assets/css/components/navigation.css')

      # Process assets
      skyhook.process_assets

      # Check static file registration
      digested_files = site.static_files.select do |file|
        file.name.match?(/.*-[a-f0-9]{32}\./)
      end

      hero_file = digested_files.find { |f| f.name.include?('hero-') }
      nav_file = digested_files.find { |f| f.name.include?('navigation-') }

      expect(hero_file.instance_variable_get(:@dir)).to eq('_digested/assets/images/posts/2024/01')
      expect(nav_file.instance_variable_get(:@dir)).to eq('_digested/assets/css/components')
    end
  end
end
