require "test_helper"

class CommentsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:contributor_user)
    @entry = entries(:one)
    @supported_language = supported_languages(:one)
  end

  test "should not create comment if not logged in" do
    assert_no_difference("Comment.count") do
      post entry_comments_url(@entry), params: { comment: { body: "Test comment", language_code: @supported_language.code } }
    end
    assert_redirected_to new_user_session_url
  end

  test "should create comment if logged in" do
    login_as @user
    assert_difference("Comment.count", 1) do
      post entry_comments_url(@entry), params: { comment: { body: "Test comment", language_code: @supported_language.code } }
    end
    assert_redirected_to @entry
    assert_equal "Test comment", Comment.last.body
    assert_equal @supported_language.code, Comment.last.language_code
    assert_equal @user, Comment.last.user
  end

  # Assuming you want to test turbo stream responses as well
  test "should create comment and respond with turbo stream" do
    login_as @user
    post entry_comments_url(@entry), params: { comment: { body: "Test turbo comment", language_code: @supported_language.code } }, as: :turbo_stream
    assert_response :success
    assert_match(/turbo-stream action=\"append\" target=\"comments-#{@supported_language.code}\"/, response.body)
    assert_match(/Test turbo comment/, response.body)
  end

  private

  def new_user_session_url
    login_path
  end
end
