require "test_helper"

class TagControllerTest < ActionController::TestCase
  fixtures :users

  def create_post(tags, post_number = 1, params = {})
    Thread.current["danbooru-user_id"] = 1
    Post.create({ user_id: 1, score: 0, source: "", rating: "s", width: 100, height: 100, ip_addr: "127.0.0.1", updater_ip_addr: "127.0.0.1", updater_user_id: 1, tags: tags, status: "active", file: upload_file("#{Rails.root}/test/mocks/test/test#{post_number}.jpg") }.merge(params))
  end

  def test_cloud
    create_post("hoge", 1)
    create_post("hoge moge", 2)
    create_post("lodge", 3)

    get :cloud
    assert_response :success
  end

  def test_index
    create_post("hoge", 1)
    create_post("hoge moge", 2)
    create_post("lodge", 3)

    get :index
    assert_response :success

    # TODO: test other params
  end

  def test_mass_edit
    get :mass_edit, session: { user_id: 2 }
    assert_response :success

    # Can't easily test the mass_edit action. The daemon process does the actual work.
    # Anything we create inside this test is created within a transaction, so any database
    # connection outside of this one won't see any changes. We can disable transactional
    # fixtures but this interferes with other tests. Just assume the action works correctly
    # and test the logic of mass_edit in the unit tests.
  end

  def test_edit_preview
    create_post("hoge", 1)
    create_post("hoge moge", 2)
    create_post("lodge", 3)

    get :edit_preview, params: { tags: "hoge" }, session: { user_id: 2 }
    assert_response :success
  end

  def test_update
    create_post("hoge", 1)

    get :edit, params: { name: "hoge" }, session: { user_id: 3 }
    assert_response :success

    post :update, params: { tag: { name: "hoge", tag_type: CONFIG["tag_types"]["Artist"] } }, session: { user_id: 3 }
    assert_equal(CONFIG["tag_types"]["Artist"], Tag.find_by_name("hoge").tag_type)
  end

  def test_related
    create_post("hoge", 1)
    create_post("hoge moge", 2)
    create_post("lodge", 3)

    get :related, params: { tags: "hoge", format: "json" }
    assert_response :success
  end

  def test_popular
    create_post("hoge", 1)
    create_post("hoge moge", 2)
    create_post("lodge", 3)

    get :popular_by_day
    assert_response :success

    get :popular_by_week
    assert_response :success

    get :popular_by_month
    assert_response :success
  end
end
