<commit_before>require "spec_helper"

RSpec.describe FavoritesController do
  describe "GET index" do
    it "succeeds when logged out" do
      get :index
      expect(response).to redirect_to(root_url)
      expect(flash[:error]).to eq("You must be logged in to view that page.")
    end

    it "succeeds when logged in" do
      login
      get :index
      expect(response.status).to eq(200)
    end

    it "has no posts when no favorites" do
      login
      get :index
      expect(assigns(:posts)).to be_empty
    end

    context "it only shows favorites" do
      let (:user) { create(:user) }
      let (:user_post) { create(:post, user: user) }
      let (:post) { create(:post) }
      let (:board) { create(:board, creator: user) }
      let (:board_post) { create(:post, board: board) }
      let (:board_user_post) { create(:post, board: board, user: user) }

      it "shows user's post when user is favorited" do
        favorite = create(:favorite, favorite: user)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([user_post, board_user_post])
      end

      it "shows post when post is favorited" do
        favorite = create(:favorite, favorite: post)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([post])
      end

      it "shows board posts when board is favorited" do
        favorite = create(:favorite, favorite: board)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([board_post, board_user_post])
      end

      it "shows both post and user post when post and user are favorited" do<commit_msg>Eager evaluate posts for favorite tests<commit_after>require "spec_helper"

RSpec.describe FavoritesController do
  describe "GET index" do
    it "succeeds when logged out" do
      get :index
      expect(response).to redirect_to(root_url)
      expect(flash[:error]).to eq("You must be logged in to view that page.")
    end

    it "succeeds when logged in" do
      login
      get :index
      expect(response.status).to eq(200)
    end

    it "has no posts when no favorites" do
      login
      get :index
      expect(assigns(:posts)).to be_empty
    end

    context "it only shows favorites" do
      let (:user) { create(:user) }
      let (:user_post) { create(:post, user: user) }
      let (:post) { create(:post) }
      let (:board) { create(:board, creator: user) }
      let (:board_post) { create(:post, board: board) }
      let (:board_user_post) { create(:post, board: board, user: user) }

      before(:each) do
        user_post
        post
        board_post
        board_user_post
      end

      it "shows user's post when user is favorited" do
        favorite = create(:favorite, favorite: user)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([user_post, board_user_post])
      end

      it "shows post when post is favorited" do
        favorite = create(:favorite, favorite: post)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([post])
      end

      it "shows board posts when board is favorited" do
        favorite = create(:favorite, favorite: board)
        login_as(favorite.user)
        get :index
        expect(assigns(:posts)).to match_array([board_post, board_user_post])
      end

      it "shows both post and user post when post and user are favorited" do
{'fraction_non_alphanumeric': 0.1015446224256293, 'fraction_numerical': 0.0017162471395881006, 'mean_word_length': 2.612603305785124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2881220', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1034, 'n_words': 335}