transmogrifier: Drop incoming create early if it already exists
[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
8 import Pleroma.Factory
9
10 alias Pleroma.Stats
11 alias Pleroma.Web.CommonAPI
12
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()
18
19 assert match?(%{stats: %{user_count: 1}}, Stats.calculate_stat_data())
20 end
21 end
22
23 describe "status visibility sum count" do
24 test "on new status" do
25 instance2 = "instance2.tld"
26 user = insert(:user)
27 other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
28
29 CommonAPI.post(user, %{visibility: "public", status: "hey"})
30
31 Enum.each(0..1, fn _ ->
32 CommonAPI.post(user, %{
33 visibility: "unlisted",
34 status: "hey"
35 })
36 end)
37
38 Enum.each(0..2, fn _ ->
39 CommonAPI.post(user, %{
40 visibility: "direct",
41 status: "hey @#{other_user.nickname}"
42 })
43 end)
44
45 Enum.each(0..3, fn _ ->
46 CommonAPI.post(user, %{
47 visibility: "private",
48 status: "hey"
49 })
50 end)
51
52 assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
53 Stats.get_status_visibility_count()
54 end
55
56 test "on status delete" do
57 user = insert(:user)
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()
62 end
63
64 test "on status visibility update" do
65 user = insert(:user)
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()
70 end
71
72 test "doesn't count unrelated activities" do
73 user = insert(:user)
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)
79
80 assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
81 Stats.get_status_visibility_count()
82 end
83 end
84
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"
89 user1 = insert(:user)
90 user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
91
92 CommonAPI.post(user1, %{visibility: "public", status: "hey"})
93
94 Enum.each(1..5, fn _ ->
95 CommonAPI.post(user1, %{
96 visibility: "unlisted",
97 status: "hey"
98 })
99 end)
100
101 Enum.each(1..10, fn _ ->
102 CommonAPI.post(user1, %{
103 visibility: "direct",
104 status: "hey @#{user2.nickname}"
105 })
106 end)
107
108 Enum.each(1..20, fn _ ->
109 CommonAPI.post(user2, %{
110 visibility: "private",
111 status: "hey"
112 })
113 end)
114
115 assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
116 Stats.get_status_visibility_count(local_instance)
117
118 assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
119 Stats.get_status_visibility_count(instance2)
120 end
121 end
122 end