qb_akkoma/test/stats_test.exs

123 lines
3.7 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.StatsTest do
use Pleroma.DataCase
import Pleroma.Factory
alias Pleroma.Stats
alias Pleroma.Web.CommonAPI
describe "user count" do
test "it ignores internal users" do
_user = insert(:user, local: true)
_internal = insert(:user, local: true, nickname: nil)
_internal = Pleroma.Web.ActivityPub.Relay.get_actor()
assert match?(%{stats: %{user_count: 1}}, Stats.calculate_stat_data())
end
end
2020-05-09 08:30:37 +00:00
describe "status visibility sum count" do
test "on new status" do
2020-05-09 08:30:37 +00:00
instance2 = "instance2.tld"
user = insert(:user)
2020-05-09 08:30:37 +00:00
other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
2020-05-12 19:59:26 +00:00
CommonAPI.post(user, %{visibility: "public", status: "hey"})
Enum.each(0..1, fn _ ->
CommonAPI.post(user, %{
2020-05-12 19:59:26 +00:00
visibility: "unlisted",
status: "hey"
})
end)
Enum.each(0..2, fn _ ->
CommonAPI.post(user, %{
2020-05-12 19:59:26 +00:00
visibility: "direct",
status: "hey @#{other_user.nickname}"
})
end)
Enum.each(0..3, fn _ ->
CommonAPI.post(user, %{
2020-05-12 19:59:26 +00:00
visibility: "private",
status: "hey"
})
end)
2020-05-09 08:30:37 +00:00
assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
Stats.get_status_visibility_count()
end
test "on status delete" do
user = insert(:user)
2020-05-12 19:59:26 +00:00
{:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
assert %{"public" => 1} = Stats.get_status_visibility_count()
CommonAPI.delete(activity.id, user)
assert %{"public" => 0} = Stats.get_status_visibility_count()
end
test "on status visibility update" do
user = insert(:user)
2020-05-12 19:59:26 +00:00
{:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
assert %{"public" => 1, "private" => 0} = Stats.get_status_visibility_count()
2020-05-12 19:59:26 +00:00
{:ok, _} = CommonAPI.update_activity_scope(activity.id, %{visibility: "private"})
assert %{"public" => 0, "private" => 1} = Stats.get_status_visibility_count()
end
test "doesn't count unrelated activities" do
user = insert(:user)
other_user = insert(:user)
2020-05-12 19:59:26 +00:00
{:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
_ = CommonAPI.follow(user, other_user)
CommonAPI.favorite(other_user, activity.id)
CommonAPI.repeat(activity.id, other_user)
2020-05-09 08:30:37 +00:00
assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
Stats.get_status_visibility_count()
end
end
2020-05-09 08:30:37 +00:00
describe "status visibility by instance count" do
test "single instance" do
local_instance = Pleroma.Web.Endpoint.url() |> String.split("//") |> Enum.at(1)
instance2 = "instance2.tld"
user1 = insert(:user)
user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
2020-05-13 16:56:40 +00:00
CommonAPI.post(user1, %{visibility: "public", status: "hey"})
2020-05-09 08:30:37 +00:00
Enum.each(1..5, fn _ ->
CommonAPI.post(user1, %{
2020-05-13 16:56:40 +00:00
visibility: "unlisted",
status: "hey"
2020-05-09 08:30:37 +00:00
})
end)
Enum.each(1..10, fn _ ->
CommonAPI.post(user1, %{
2020-05-13 16:56:40 +00:00
visibility: "direct",
status: "hey @#{user2.nickname}"
2020-05-09 08:30:37 +00:00
})
end)
Enum.each(1..20, fn _ ->
CommonAPI.post(user2, %{
2020-05-13 16:56:40 +00:00
visibility: "private",
status: "hey"
2020-05-09 08:30:37 +00:00
})
end)
assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
Stats.get_status_visibility_count(local_instance)
2020-05-09 08:30:37 +00:00
assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
Stats.get_status_visibility_count(instance2)
2020-05-09 08:30:37 +00:00
end
end
end