<commit_before>defmodule AtomTweaksWeb.PageControllerTest do
  use AtomTweaksWeb.ConnCase

  describe "GET home page" do
    setup(context) do
      conn = get(context.conn, :index, [])

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response}
    end

    test "renders the index.html template", context do
      assert view_template(context.conn) == "index.html"
    end

    test "assigns no current user", context do
      refute fetch_assign(context.conn, :current_user)
    end

    test "assigns an empty tweaks list", context do
      assert fetch_assign(context.conn, :tweaks) == []
    end

    test "assigns an empty params list", context do
      assert fetch_assign(context.conn, :params) == %{}
    end

    test "shows the home page link", context do
      link = find(context.response, "a.masthead-logo")

      assert text(link) == "Atom Tweaks"
      assert attribute(link, "href") == [page_path(context.conn, :index, [])]
    end

    test "does not show the new tweak button", context do
      refute has_selector?(context.response, "a#new-tweak-button")
    end

    test "shows the about page link", context do
      link = find(context.response, "footer a#about-link")

      assert text(link) == "About"
      assert attribute(link, "href") == [page_path(context.conn, :about)]
    end

    test "shows the GitHub link", context do
      link = find(context.response, "footer a#github-link")

      assert attribute(link, "href") == ["https://github.com/lee-dohm/atom-style-tweaks"]
    end
  end

  describe "GET home page with a logged in user" do
    setup(context) do
      user = build(:user)

      conn =
        context.conn
        |> log_in_as(user)
        |> get(page_path(context.conn, :index, []))

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response, user: user}
    end

    test "assigns a current user", context do
      assert fetch_assign(context.conn, :current_user) == context.user
    end

    test "shows the new tweak button", context do
      link = find(context.response, "a#new-tweak-button")

      assert text(link) == "New tweak"
    end
  end

  describe "GET home page when tweaks exist in the database" do
    setup(context) do
      tweaks = insert_list(3, :tweak)

      conn = get(context.conn, page_path(context.conn, :index, []))

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response, tweaks: tweaks}
    end

    test "assigns them to the tweaks list", context do
      assigned_tweaks = fetch_assign(context.conn, :tweaks)

      assert Enum.count(assigned_tweaks) == 3
      assert Enum.all?(context.tweaks, &Enum.find(assigned_tweaks, fn(item) -> item.id == &1.id end))
    end

    test "displays the links to each tweak", context do
      links = find(context.response, "a.title")

      assert Enum.all?(context.tweaks, fn tweak -> text(links) =~ tweak.title end)
    end
  end

  describe "GET home page when the styles tab is selected" do
    setup(context) do
      insert(:tweak, title: "Init tweak", type: "init")
      tweak = insert(:tweak, title: "Style tweak", type: "style")

      conn = get(context.conn, page_path(context.conn, :index, type: :style))

      response = html_response(conn, :ok)<commit_msg>:shirt:<commit_after>defmodule AtomTweaksWeb.PageControllerTest do
  use AtomTweaksWeb.ConnCase

  describe "GET home page" do
    setup(context) do
      conn = get(context.conn, :index, [])

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response}
    end

    test "renders the index.html template", context do
      assert view_template(context.conn) == "index.html"
    end

    test "assigns no current user", context do
      refute fetch_assign(context.conn, :current_user)
    end

    test "assigns an empty tweaks list", context do
      assert fetch_assign(context.conn, :tweaks) == []
    end

    test "assigns an empty params list", context do
      assert fetch_assign(context.conn, :params) == %{}
    end

    test "shows the home page link", context do
      link = find(context.response, "a.masthead-logo")

      assert text(link) == "Atom Tweaks"
      assert attribute(link, "href") == [page_path(context.conn, :index, [])]
    end

    test "does not show the new tweak button", context do
      refute has_selector?(context.response, "a#new-tweak-button")
    end

    test "shows the about page link", context do
      link = find(context.response, "footer a#about-link")

      assert text(link) == "About"
      assert attribute(link, "href") == [page_path(context.conn, :about)]
    end

    test "shows the GitHub link", context do
      link = find(context.response, "footer a#github-link")

      assert attribute(link, "href") == ["https://github.com/lee-dohm/atom-style-tweaks"]
    end
  end

  describe "GET home page with a logged in user" do
    setup(context) do
      user = build(:user)

      conn =
        context.conn
        |> log_in_as(user)
        |> get(page_path(context.conn, :index, []))

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response, user: user}
    end

    test "assigns a current user", context do
      assert fetch_assign(context.conn, :current_user) == context.user
    end

    test "shows the new tweak button", context do
      link = find(context.response, "a#new-tweak-button")

      assert text(link) == "New tweak"
    end
  end

  describe "GET home page when tweaks exist in the database" do
    setup(context) do
      tweaks = insert_list(3, :tweak)

      conn = get(context.conn, page_path(context.conn, :index, []))

      response = html_response(conn, :ok)

      {:ok, conn: conn, response: response, tweaks: tweaks}
    end

    test "assigns them to the tweaks list", context do
      assigned_tweaks = fetch_assign(context.conn, :tweaks)

      assert Enum.count(assigned_tweaks) == 3

      assert Enum.all?(
               context.tweaks,
               &Enum.find(assigned_tweaks, fn item -> item.id == &1.id end)
             )
    end

    test "displays the links to each tweak", context do
      links = find(context.response, "a.title")

      assert Enum.all?(context.tweaks, fn tweak -> text(links) =~ tweak.title end)
    end
  end

  describe "GET home page when the styles tab is selected" do
    setup(context) do
      insert(:tweak, title: "Init tweak", type: "init")
      tweak = insert(:tweak, title: "Style tweak", type: "style")

      conn = get(context.conn, page_path(context.conn, :index, type: :style))

      response = html_response(conn, :ok)
{'fraction_non_alphanumeric': 0.11983658647299138, 'fraction_numerical': 0.0009078529278256923, 'mean_word_length': 2.9298454221165278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '6740919', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2055, 'n_words': 625}