1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.StatsTest do
8 alias Pleroma.Web.CommonAPI
10 describe "user count" do
11 test "it ignores internal users" do
12 _user = insert(:user, local: true)
13 _internal = insert(:user, local: true, nickname: nil)
14 _internal = Pleroma.Web.ActivityPub.Relay.get_actor()
16 assert match?(%{stats: %{user_count: 1}}, Pleroma.Stats.calculate_stat_data())
20 describe "status visibility sum count" do
21 test "on new status" do
22 instance2 = "instance2.tld"
24 other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
26 CommonAPI.post(user, %{visibility: "public", status: "hey"})
28 Enum.each(0..1, fn _ ->
29 CommonAPI.post(user, %{
30 visibility: "unlisted",
35 Enum.each(0..2, fn _ ->
36 CommonAPI.post(user, %{
38 status: "hey @#{other_user.nickname}"
42 Enum.each(0..3, fn _ ->
43 CommonAPI.post(user, %{
44 visibility: "private",
49 assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
50 Pleroma.Stats.get_status_visibility_count()
53 test "on status delete" do
55 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
56 assert %{"public" => 1} = Pleroma.Stats.get_status_visibility_count()
57 CommonAPI.delete(activity.id, user)
58 assert %{"public" => 0} = Pleroma.Stats.get_status_visibility_count()
61 test "on status visibility update" do
63 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
64 assert %{"public" => 1, "private" => 0} = Pleroma.Stats.get_status_visibility_count()
65 {:ok, _} = CommonAPI.update_activity_scope(activity.id, %{visibility: "private"})
66 assert %{"public" => 0, "private" => 1} = Pleroma.Stats.get_status_visibility_count()
69 test "doesn't count unrelated activities" do
71 other_user = insert(:user)
72 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
73 _ = CommonAPI.follow(user, other_user)
74 CommonAPI.favorite(other_user, activity.id)
75 CommonAPI.repeat(activity.id, other_user)
77 assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
78 Pleroma.Stats.get_status_visibility_count()
82 describe "status visibility by instance count" do
83 test "single instance" do
84 local_instance = Pleroma.Web.Endpoint.url() |> String.split("//") |> Enum.at(1)
85 instance2 = "instance2.tld"
87 user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
89 CommonAPI.post(user1, %{visibility: "public", status: "hey"})
91 Enum.each(1..5, fn _ ->
92 CommonAPI.post(user1, %{
93 visibility: "unlisted",
98 Enum.each(1..10, fn _ ->
99 CommonAPI.post(user1, %{
100 visibility: "direct",
101 status: "hey @#{user2.nickname}"
105 Enum.each(1..20, fn _ ->
106 CommonAPI.post(user2, %{
107 visibility: "private",
112 assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
113 Pleroma.Stats.get_status_visibility_count(local_instance)
115 assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
116 Pleroma.Stats.get_status_visibility_count(instance2)