Translated using Weblate (Italian)
[akkoma] / test / stats_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.StatsTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 alias Pleroma.Web.CommonAPI
9
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()
15
16 assert match?(%{stats: %{user_count: 1}}, Pleroma.Stats.calculate_stat_data())
17 end
18 end
19
20 describe "status visibility sum count" do
21 test "on new status" do
22 instance2 = "instance2.tld"
23 user = insert(:user)
24 other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
25
26 CommonAPI.post(user, %{visibility: "public", status: "hey"})
27
28 Enum.each(0..1, fn _ ->
29 CommonAPI.post(user, %{
30 visibility: "unlisted",
31 status: "hey"
32 })
33 end)
34
35 Enum.each(0..2, fn _ ->
36 CommonAPI.post(user, %{
37 visibility: "direct",
38 status: "hey @#{other_user.nickname}"
39 })
40 end)
41
42 Enum.each(0..3, fn _ ->
43 CommonAPI.post(user, %{
44 visibility: "private",
45 status: "hey"
46 })
47 end)
48
49 assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
50 Pleroma.Stats.get_status_visibility_count()
51 end
52
53 test "on status delete" do
54 user = insert(:user)
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()
59 end
60
61 test "on status visibility update" do
62 user = insert(:user)
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()
67 end
68
69 test "doesn't count unrelated activities" do
70 user = insert(:user)
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)
76
77 assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
78 Pleroma.Stats.get_status_visibility_count()
79 end
80 end
81
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"
86 user1 = insert(:user)
87 user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
88
89 CommonAPI.post(user1, %{visibility: "public", status: "hey"})
90
91 Enum.each(1..5, fn _ ->
92 CommonAPI.post(user1, %{
93 visibility: "unlisted",
94 status: "hey"
95 })
96 end)
97
98 Enum.each(1..10, fn _ ->
99 CommonAPI.post(user1, %{
100 visibility: "direct",
101 status: "hey @#{user2.nickname}"
102 })
103 end)
104
105 Enum.each(1..20, fn _ ->
106 CommonAPI.post(user2, %{
107 visibility: "private",
108 status: "hey"
109 })
110 end)
111
112 assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
113 Pleroma.Stats.get_status_visibility_count(local_instance)
114
115 assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
116 Pleroma.Stats.get_status_visibility_count(instance2)
117 end
118 end
119 end