test_html_generator.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require "test_helper"
  2. require "tmpdir"
  3. require "fileutils"
  4. class TestHtmlGenerator < Minitest::Test
  5. def setup
  6. @temp_source_dir = Dir.mktmpdir("fotos_source")
  7. @temp_dest_dir = Dir.mktmpdir("fotos_dest")
  8. # Create a test directory structure with date-named directories
  9. @test_date = "24-07-2025"
  10. FileUtils.mkdir_p(File.join(@temp_source_dir, @test_date))
  11. # Copy test image to the date directory
  12. source_image = File.expand_path("../fixtures/cockapoo.jpg", __FILE__)
  13. @test_image_path = File.join(@temp_source_dir, @test_date, "cockapoo.jpg")
  14. FileUtils.cp(source_image, @test_image_path)
  15. @options = {
  16. source: @temp_source_dir,
  17. dest: @temp_dest_dir,
  18. thumbnails: false
  19. }
  20. @generator = Fotos::HtmlGenerator.new(@options)
  21. end
  22. def teardown
  23. FileUtils.rm_rf(@temp_source_dir)
  24. FileUtils.rm_rf(@temp_dest_dir)
  25. end
  26. def test_initialize_with_options
  27. assert_instance_of Fotos::SourceDirectory, @generator.source_dir
  28. assert_instance_of Fotos::SourceDirectory, @generator.dest_dir
  29. assert_equal false, @generator.generate_thumbnails
  30. end
  31. def test_initialize_with_thumbnails_option
  32. options_with_thumbnails = @options.merge(thumbnails: true)
  33. generator = Fotos::HtmlGenerator.new(options_with_thumbnails)
  34. assert_equal true, generator.generate_thumbnails
  35. end
  36. def test_call_generates_html_output
  37. html_content = @generator.call
  38. # Should contain the date as heading
  39. assert_includes html_content, "2025-07-24"
  40. # Should contain image link
  41. assert_includes html_content, "cockapoo.jpg"
  42. # Should contain header
  43. assert_includes html_content, "<html>"
  44. end
  45. def test_render_period_outputs_year_headers
  46. dates = [Date.new(2025, 7, 24), Date.new(2025, 8, 15)]
  47. result = @generator.send(:render_period, dates)
  48. assert_includes result, "Lipiec"
  49. assert_includes result, "Sierpień"
  50. assert_includes result, "<h2>2025</h2>"
  51. end
  52. def test_render_template_month
  53. month_name = "Lipiec"
  54. dates = [Date.new(2025, 7, 24), Date.new(2025, 7, 28)]
  55. result = @generator.send(:render_template, 'month', month: month_name, m_dates: dates)
  56. assert_includes result, "Lipiec"
  57. assert_includes result, "24.07"
  58. assert_includes result, "28.07"
  59. assert_includes result, "href=\"#24-07-2025\""
  60. assert_includes result, "href=\"#28-07-2025\""
  61. end
  62. def test_render_template_footer
  63. result = @generator.send(:render_template, 'footer')
  64. assert_includes result, "&copy; 2023-2025"
  65. assert_includes result, "Łukasz Badura"
  66. assert_includes result, "</body>"
  67. assert_includes result, "</html>"
  68. end
  69. def test_render_template_with_missing_template
  70. assert_raises(Errno::ENOENT) do
  71. @generator.send(:render_template, 'nonexistent')
  72. end
  73. end
  74. def test_call_with_thumbnail_generation
  75. options_with_thumbnails = @options.merge(thumbnails: true)
  76. generator = Fotos::HtmlGenerator.new(options_with_thumbnails)
  77. # Mock the Thumbnail class to avoid actual image processing
  78. #thumbnail_mock = Minitest::Mock.new
  79. #thumbnail_mock.expect(:create, nil, [generator.dest_dir.path])
  80. #Fotos::Thumbnail.stub(:new, thumbnail_mock) do
  81. #html_content = generator.call
  82. ## Should still generate HTML even when thumbnails are created
  83. #assert_includes html_content, "cockapoo.jpg"
  84. #end
  85. #thumbnail_mock.verify
  86. html_content = generator.call
  87. assert_includes html_content, "cockapoo.jpg"
  88. end
  89. def test_call_handles_non_thumbnail_assets_only
  90. # Create a thumbnail file in the test directory
  91. thumbnail_path = File.join(@temp_source_dir, @test_date, "TH_cockapoo.jpg")
  92. FileUtils.cp(@test_image_path, thumbnail_path)
  93. html_content = @generator.call
  94. # Should only process the original image, not the thumbnail
  95. assert_includes html_content, "cockapoo.jpg"
  96. # The thumbnail file should be filtered out and not appear as a separate image
  97. refute_match(/TH_cockapoo\.jpg.*<\/a>/, html_content)
  98. end
  99. end