require "test_helper" require "tmpdir" require "fileutils" class TestHtmlGenerator < Minitest::Test def setup @temp_source_dir = Dir.mktmpdir("fotos_source") @temp_dest_dir = Dir.mktmpdir("fotos_dest") # Create a test directory structure with date-named directories @test_date = "24-07-2025" FileUtils.mkdir_p(File.join(@temp_source_dir, @test_date)) # Copy test image to the date directory source_image = File.expand_path("../fixtures/cockapoo.jpg", __FILE__) @test_image_path = File.join(@temp_source_dir, @test_date, "cockapoo.jpg") FileUtils.cp(source_image, @test_image_path) @options = { source: @temp_source_dir, dest: @temp_dest_dir, thumbnails: false } @generator = Fotos::HtmlGenerator.new(@options) end def teardown FileUtils.rm_rf(@temp_source_dir) FileUtils.rm_rf(@temp_dest_dir) end def test_initialize_with_options assert_instance_of Fotos::SourceDirectory, @generator.source_dir assert_instance_of Fotos::SourceDirectory, @generator.dest_dir assert_equal false, @generator.generate_thumbnails end def test_initialize_with_thumbnails_option options_with_thumbnails = @options.merge(thumbnails: true) generator = Fotos::HtmlGenerator.new(options_with_thumbnails) assert_equal true, generator.generate_thumbnails end def test_call_generates_html_output html_content = @generator.call # Should contain the date as heading assert_includes html_content, "2025-07-24" # Should contain image link assert_includes html_content, "cockapoo.jpg" # Should contain header assert_includes html_content, "" end def test_render_period_outputs_year_headers dates = [Date.new(2025, 7, 24), Date.new(2025, 8, 15)] result = @generator.send(:render_period, dates) assert_includes result, "Lipiec" assert_includes result, "Sierpień" assert_includes result, "

2025

" end def test_render_template_month month_name = "Lipiec" dates = [Date.new(2025, 7, 24), Date.new(2025, 7, 28)] result = @generator.send(:render_template, 'month', month: month_name, m_dates: dates) assert_includes result, "Lipiec" assert_includes result, "24.07" assert_includes result, "28.07" assert_includes result, "href=\"#24-07-2025\"" assert_includes result, "href=\"#28-07-2025\"" end def test_render_template_footer result = @generator.send(:render_template, 'footer') assert_includes result, "© 2023-2025" assert_includes result, "Łukasz Badura" assert_includes result, "" assert_includes result, "" end def test_render_template_with_missing_template assert_raises(Errno::ENOENT) do @generator.send(:render_template, 'nonexistent') end end def test_call_with_thumbnail_generation options_with_thumbnails = @options.merge(thumbnails: true) generator = Fotos::HtmlGenerator.new(options_with_thumbnails) # Mock the Thumbnail class to avoid actual image processing #thumbnail_mock = Minitest::Mock.new #thumbnail_mock.expect(:create, nil, [generator.dest_dir.path]) #Fotos::Thumbnail.stub(:new, thumbnail_mock) do #html_content = generator.call ## Should still generate HTML even when thumbnails are created #assert_includes html_content, "cockapoo.jpg" #end #thumbnail_mock.verify html_content = generator.call assert_includes html_content, "cockapoo.jpg" end def test_call_handles_non_thumbnail_assets_only # Create a thumbnail file in the test directory thumbnail_path = File.join(@temp_source_dir, @test_date, "TH_cockapoo.jpg") FileUtils.cp(@test_image_path, thumbnail_path) html_content = @generator.call # Should only process the original image, not the thumbnail assert_includes html_content, "cockapoo.jpg" # The thumbnail file should be filtered out and not appear as a separate image refute_match(/TH_cockapoo\.jpg.*<\/a>/, html_content) end end