1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.StatsTest do
6 use Pleroma.DataCase, async: true
11 alias Pleroma.Web.CommonAPI
13 describe "user count" do
14 test "it ignores internal users" do
15 _user = insert(:user, local: true)
16 _internal = insert(:user, local: true, nickname: nil)
17 _internal = Pleroma.Web.ActivityPub.Relay.get_actor()
19 assert match?(%{stats: %{user_count: 1}}, Stats.calculate_stat_data())
23 describe "status visibility sum count" do
24 test "on new status" do
25 instance2 = "instance2.tld"
27 other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
29 CommonAPI.post(user, %{visibility: "public", status: "hey"})
31 Enum.each(0..1, fn _ ->
32 CommonAPI.post(user, %{
33 visibility: "unlisted",
38 Enum.each(0..2, fn _ ->
39 CommonAPI.post(user, %{
41 status: "hey @#{other_user.nickname}"
45 Enum.each(0..3, fn _ ->
46 CommonAPI.post(user, %{
47 visibility: "private",
52 assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
53 Stats.get_status_visibility_count()
56 test "on status delete" do
58 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
59 assert %{"public" => 1} = Stats.get_status_visibility_count()
60 CommonAPI.delete(activity.id, user)
61 assert %{"public" => 0} = Stats.get_status_visibility_count()
64 test "on status visibility update" do
66 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
67 assert %{"public" => 1, "private" => 0} = Stats.get_status_visibility_count()
68 {:ok, _} = CommonAPI.update_activity_scope(activity.id, %{visibility: "private"})
69 assert %{"public" => 0, "private" => 1} = Stats.get_status_visibility_count()
72 test "doesn't count unrelated activities" do
74 other_user = insert(:user)
75 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
76 _ = CommonAPI.follow(user, other_user)
77 CommonAPI.favorite(other_user, activity.id)
78 CommonAPI.repeat(activity.id, other_user)
80 assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
81 Stats.get_status_visibility_count()
85 describe "status visibility by instance count" do
86 test "single instance" do
87 local_instance = Pleroma.Web.Endpoint.url() |> String.split("//") |> Enum.at(1)
88 instance2 = "instance2.tld"
90 user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
92 CommonAPI.post(user1, %{visibility: "public", status: "hey"})
94 Enum.each(1..5, fn _ ->
95 CommonAPI.post(user1, %{
96 visibility: "unlisted",
101 Enum.each(1..10, fn _ ->
102 CommonAPI.post(user1, %{
103 visibility: "direct",
104 status: "hey @#{user2.nickname}"
108 Enum.each(1..20, fn _ ->
109 CommonAPI.post(user2, %{
110 visibility: "private",
115 assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
116 Stats.get_status_visibility_count(local_instance)
118 assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
119 Stats.get_status_visibility_count(instance2)