# frozen_string_literal: true
require 'application_system_test_case'

class UploadsTest < ApplicationSystemTestCase
  test 'upload broken screenshot anonymously and get error message' do
    visit package_path(name: 'package-1')
    click_on 'Upload a screenshot'
    # The actual file field is hidden in favor of a more beautiful button -> visible=false
    attach_file 'file[]', Rails.root.join('test/fixtures/files/large-broken.png'), visible: false
    # Upload starts automatically through Javascript trigger
    assert_text 'large-broken.png must be a valid'
  end

  test 'upload png screenshot anonymously and await moderation' do
    visit package_path(name: "package-1")

    click_on 'Upload a screenshot'
    attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png'), visible: false

    # Upload starts automatically when file is selected.
    # Wait for upload to complete.
    # https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends
    assert page.has_content?('Uploaded by you')
    assert page.has_content?('needs to be approved')

    # Screenshot must be moderated
    newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first
    assert_not newest_screenshot.approved

    # Delete the screenshot
    click_on 'Delete'
    page.accept_alert

    # Wait for redirect to details page
    assert page.has_content?('Homepage')
  end

  test 'upload png screenshot as moderator and expect auto-approval' do
    # Delete all screenshots for the Firefox package
    Package.find_by_name(:firefox).screenshots.delete_all

    # Sign in
    visit new_user_session_path
    fill_in 'user[email]', with: users(:moderator).email
    fill_in 'user[password]', with: 'moderatorsecret'
    click_on 'Sign in'

    # Get details page of Firefox package
    visit package_path(name: 'firefox')

    # Select a valid image for upload
    click_on 'Upload a screenshot'
    attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png'), visible: false

    # Upload starts automatically when file is selected.
    # Wait for upload to complete.
    # https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends
    assert page.has_content?('Uploaded by you')

    # Find a label reading "Public"
    assert_selector '.adminlabel', text: /Public/

    # Check if the screenshot is auto-approved
    package = Package.find_by_name(:firefox)
    assert_not_nil package
    newest_screenshot = package.screenshots.first
    assert_not_nil newest_screenshot
    assert_not newest_screenshot&.approved

    # Delete the screenshot
    click_on 'Delete'
    page.accept_alert

    # Wait for redirect to details page
    assert page.has_content?('Homepage')
  end

  # test "upload screenshot as Debian SSO user and get auto-approval" do
  #   sign_in users(:debian)
  #   visit upload_path(name: 'firefox')
  #   img_count_before = page.find_all('img').count
  #   attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png')
  #   click_on 'Start upload'
  #   img_count_after = page.find_all('img').count
  #   assert_equal img_count_after, img_count_before + 1

  #   # Screenshot is instantly public
  #   newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first
  #   assert_equal newest_screenshot.approved, true

  #   # Clean up
  #   newest_screenshot.destroy
  #   sign_out users(:debian)
  # end

  # test "upload screenshot anonymously, login in, get screenshots assigned" do
  #   visit upload_path(name: 'firefox')
  #   attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png')
  #   click_on 'Start upload'

  #   # Screenshot has not yet a user assigned
  #   newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first
  #   assert_nil newest_screenshot.user

  #   # Login (sign_in is just a stub and would not call the callbacks!)
  #   visit root_path
  #   click_link 'Login'
  #   within 'form' do
  #     fill_in 'email address', with: users(:normal).email
  #     fill_in 'password', with: 'normalsecret'
  #     click_button 'Log in'
  #   end

  #   # Check if screenshot was assigned to the user
  #   newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first
  #   assert_equal newest_screenshot.user, users(:normal)

  #   # Clean up
  #   newest_screenshot.destroy
  #   sign_out users(:debian)
  # end
end
