# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
  use Pleroma.Web.ConnCase

  alias Pleroma.Object
  alias Pleroma.Web.CommonAPI
  import Pleroma.Factory
  import ExUnit.CaptureLog
  import Tesla.Mock
  import Mock

  setup do
    Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig)
    :ok
  end

  setup_all do
    mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end

  describe ".search2" do
    test "it returns empty result if user or status search return undefined error", %{conn: conn} do
      with_mocks [
        {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
        {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
      ] do
        capture_log(fn ->
          results =
            conn
            |> get("/api/v2/search?q=2hu")
            |> json_response_and_validate_schema(200)

          assert results["accounts"] == []
          assert results["statuses"] == []
        end) =~
          "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
      end
    end

    @tag :skip_darwin
    test "search", %{conn: conn} do
      user = insert(:user)
      user_two = insert(:user, %{nickname: "shp@shitposter.club"})
      user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})

      {:ok, _activity} =
        CommonAPI.post(user, %{
          status: "This is about 2hu, but private",
          visibility: "private"
        })

      {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
        |> json_response_and_validate_schema(200)

      [account | _] = results["accounts"]
      assert account["id"] == to_string(user_three.id)

      assert results["hashtags"] == []

      [status] = results["statuses"]
      assert status["id"] == to_string(activity.id)

      results =
        get(conn, "/api/v2/search?q=天子")
        |> json_response_and_validate_schema(200)

      assert results["hashtags"] == []

      [status] = results["statuses"]
      assert status["id"] == to_string(activity.id)
    end

    test "search local-only status as an authenticated user" do
      user = insert(:user)
      %{conn: conn} = oauth_access(["read:search"])

      {:ok, activity} =
        CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
        |> json_response_and_validate_schema(200)

      [status] = results["statuses"]
      assert status["id"] == to_string(activity.id)
    end

    test "search local-only status as an unauthenticated user" do
      user = insert(:user)
      %{conn: conn} = oauth_access([])

      {:ok, _activity} =
        CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
        |> json_response_and_validate_schema(200)

      assert [] = results["statuses"]
    end

    test "search local-only status as an anonymous user" do
      user = insert(:user)

      {:ok, _activity} =
        CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})

      results =
        build_conn()
        |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
        |> json_response_and_validate_schema(200)

      assert [] = results["statuses"]
    end

    test "returns empty results when no hashtags match", %{conn: conn} do
      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "nonexistent"})}")
        |> json_response_and_validate_schema(200)

      assert results["hashtags"] == []
    end

    test "searches hashtags by multiple words in query", %{conn: conn} do
      user = insert(:user)

      {:ok, _activity1} = CommonAPI.post(user, %{status: "This is my new #computer"})
      {:ok, _activity2} = CommonAPI.post(user, %{status: "Check out this #laptop"})
      {:ok, _activity3} = CommonAPI.post(user, %{status: "My #desktop setup"})
      {:ok, _activity4} = CommonAPI.post(user, %{status: "New #phone arrived"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "new computer"})}")
        |> json_response_and_validate_schema(200)

      hashtag_names = Enum.map(results["hashtags"], & &1["name"])
      assert "computer" in hashtag_names
      refute "laptop" in hashtag_names
      refute "desktop" in hashtag_names
      refute "phone" in hashtag_names

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "computer laptop"})}")
        |> json_response_and_validate_schema(200)

      hashtag_names = Enum.map(results["hashtags"], & &1["name"])
      assert "computer" in hashtag_names
      assert "laptop" in hashtag_names
      refute "desktop" in hashtag_names
      refute "phone" in hashtag_names
    end

    test "supports pagination of hashtags search results", %{conn: conn} do
      user = insert(:user)

      {:ok, _activity1} = CommonAPI.post(user, %{status: "First #alpha hashtag"})
      {:ok, _activity2} = CommonAPI.post(user, %{status: "Second #beta hashtag"})
      {:ok, _activity3} = CommonAPI.post(user, %{status: "Third #gamma hashtag"})
      {:ok, _activity4} = CommonAPI.post(user, %{status: "Fourth #delta hashtag"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "a", limit: 2, offset: 1})}")
        |> json_response_and_validate_schema(200)

      hashtag_names = Enum.map(results["hashtags"], & &1["name"])

      # Should return 2 hashtags (alpha, beta, gamma, delta all contain 'a')
      # With offset 1, we skip the first one, so we get 2 of the remaining 3
      assert length(hashtag_names) == 2
      assert Enum.all?(hashtag_names, &String.contains?(&1, "a"))
    end

    test "searches real hashtags from database", %{conn: conn} do
      user = insert(:user)

      {:ok, _activity1} = CommonAPI.post(user, %{status: "Check out this #car"})
      {:ok, _activity2} = CommonAPI.post(user, %{status: "Fast #racecar on the track"})
      {:ok, _activity3} = CommonAPI.post(user, %{status: "NASCAR #nascar racing"})

      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "car"})}")
        |> json_response_and_validate_schema(200)

      hashtag_names = Enum.map(results["hashtags"], & &1["name"])

      # Should return car, racecar, and nascar since they all contain "car"
      assert "car" in hashtag_names
      assert "racecar" in hashtag_names
      assert "nascar" in hashtag_names

      # Search for "race" - should return racecar
      results =
        conn
        |> get("/api/v2/search?#{URI.encode_query(%{q: "race"})}")
        |> json_response_and_validate_schema(200)

      hashtag_names = Enum.map(results["hashtags"], & &1["name"])

      assert "racecar" in hashtag_names
      refute "car" in hashtag_names
      refute "nascar" in hashtag_names
    end

    test "excludes a blocked users from search results", %{conn: conn} do
      user = insert(:user)
      user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
      user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})

      {:ok, act1} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
      {:ok, act2} = CommonAPI.post(user_smith, %{status: "Agent Smith"})
      {:ok, act3} = CommonAPI.post(user_neo, %{status: "Agent Smith"})
      Pleroma.User.block(user, user_smith)

      results =
        conn
        |> assign(:user, user)
        |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
        |> get("/api/v2/search?q=Agent")
        |> json_response_and_validate_schema(200)

      status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)

      assert act3.id in status_ids
      refute act2.id in status_ids
      refute act1.id in status_ids
    end
  end

  describe ".account_search" do
    test "account search", %{conn: conn} do
      user_two = insert(:user, %{nickname: "shp@shitposter.club"})
      user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      results =
        conn
        |> get("/api/v1/accounts/search?q=shp")
        |> json_response_and_validate_schema(200)

      result_ids = for result <- results, do: result["acct"]

      assert user_two.nickname in result_ids
      assert user_three.nickname in result_ids

      results =
        conn
        |> get("/api/v1/accounts/search?q=2hu")
        |> json_response_and_validate_schema(200)

      result_ids = for result <- results, do: result["acct"]

      assert user_three.nickname in result_ids
    end

    test "returns account if query contains a space", %{conn: conn} do
      insert(:user, %{nickname: "shp@shitposter.club"})

      results =
        conn
        |> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
        |> json_response_and_validate_schema(200)

      assert length(results) == 1
    end
  end

  describe ".search" do
    test "it returns empty result if user or status search return undefined error", %{conn: conn} do
      with_mocks [
        {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
        {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
      ] do
        capture_log(fn ->
          results =
            conn
            |> get("/api/v1/search?q=2hu")
            |> json_response_and_validate_schema(200)

          assert results["accounts"] == []
          assert results["statuses"] == []
        end) =~
          "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
      end
    end

    test "search", %{conn: conn} do
      user = insert(:user)
      user_two = insert(:user, %{nickname: "shp@shitposter.club"})
      user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})

      {:ok, _activity} =
        CommonAPI.post(user, %{
          status: "This is about 2hu, but private",
          visibility: "private"
        })

      {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})

      results =
        conn
        |> get("/api/v1/search?q=2hu")
        |> json_response_and_validate_schema(200)

      [account | _] = results["accounts"]
      assert account["id"] == to_string(user_three.id)

      assert results["hashtags"] == []

      [status] = results["statuses"]
      assert status["id"] == to_string(activity.id)
    end

    test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
      {:ok, %{id: activity_id}} =
        CommonAPI.post(insert(:user), %{
          status: "check out http://mastodon.example.org/@admin/99541947525187367"
        })

      %{"url" => result_url, "id" => result_id} =
        conn
        |> get("/api/v1/search?q=http://mastodon.example.org/@admin/99541947525187367")
        |> json_response_and_validate_schema(200)
        |> Map.get("statuses")
        |> List.first()

      refute match?(^result_id, activity_id)
      assert match?(^result_url, "http://mastodon.example.org/@admin/99541947525187367")
    end

    test "search doesn't show statuses that it shouldn't", %{conn: conn} do
      {:ok, activity} =
        CommonAPI.post(insert(:user), %{
          status: "This is about 2hu, but private",
          visibility: "private"
        })

      capture_log(fn ->
        q = Object.normalize(activity, fetch: false).data["id"]

        results =
          conn
          |> get("/api/v1/search?q=#{q}")
          |> json_response_and_validate_schema(200)

        [] = results["statuses"]
      end)
    end

    test "search fetches remote accounts", %{conn: conn} do
      user = insert(:user)

      query = URI.encode_query(%{q: "       mike@osada.macgirvin.com          ", resolve: true})

      results =
        conn
        |> assign(:user, user)
        |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
        |> get("/api/v1/search?#{query}")
        |> json_response_and_validate_schema(200)

      [account] = results["accounts"]
      assert account["acct"] == "mike@osada.macgirvin.com"
    end

    test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
      results =
        conn
        |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
        |> json_response_and_validate_schema(200)

      assert [] == results["accounts"]
    end

    test "search with limit and offset", %{conn: conn} do
      user = insert(:user)
      _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
      _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      {:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
      {:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})

      result =
        conn
        |> get("/api/v1/search?q=2hu&limit=1")

      assert results = json_response_and_validate_schema(result, 200)
      assert [%{"id" => activity_id1}] = results["statuses"]
      assert [_] = results["accounts"]

      results =
        conn
        |> get("/api/v1/search?q=2hu&limit=1&offset=1")
        |> json_response_and_validate_schema(200)

      assert [%{"id" => activity_id2}] = results["statuses"]
      assert [] = results["accounts"]

      assert activity_id1 != activity_id2
    end

    test "search returns results only for the given type", %{conn: conn} do
      user = insert(:user)
      _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      {:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})

      assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
               conn
               |> get("/api/v1/search?q=2hu&type=statuses")
               |> json_response_and_validate_schema(200)

      assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
               conn
               |> get("/api/v1/search?q=2hu&type=accounts")
               |> json_response_and_validate_schema(200)
    end

    test "search uses account_id to filter statuses by the author", %{conn: conn} do
      user = insert(:user, %{nickname: "shp@shitposter.club"})
      user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})

      {:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
      {:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})

      results =
        conn
        |> get("/api/v1/search?q=2hu&account_id=#{user.id}")
        |> json_response_and_validate_schema(200)

      assert [%{"id" => activity_id1}] = results["statuses"]
      assert activity_id1 == activity1.id
      assert [_] = results["accounts"]

      results =
        conn
        |> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
        |> json_response_and_validate_schema(200)

      assert [%{"id" => activity_id2}] = results["statuses"]
      assert activity_id2 == activity2.id
    end
  end
end
