added recount unread notifications to markers
[akkoma] / test / marker_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.MarkerTest do
6 use Pleroma.DataCase
7 alias Pleroma.Marker
8
9 import Pleroma.Factory
10
11 describe "multi_set_unread_count/3" do
12 test "returns multi" do
13 user = insert(:user)
14
15 assert %Ecto.Multi{
16 operations: [marker: {:run, _}, counters: {:run, _}]
17 } =
18 Marker.multi_set_unread_count(
19 Ecto.Multi.new(),
20 user,
21 "notifications"
22 )
23 end
24
25 test "return empty multi" do
26 user = insert(:user)
27 multi = Ecto.Multi.new()
28 assert Marker.multi_set_unread_count(multi, user, "home") == multi
29 end
30 end
31
32 describe "get_markers/2" do
33 test "returns user markers" do
34 user = insert(:user)
35 marker = insert(:marker, user: user)
36 insert(:marker, timeline: "home", user: user)
37 assert Marker.get_markers(user, ["notifications"]) == [refresh_record(marker)]
38 end
39
40 test "returns user markers with recount unread notifications" do
41 user = insert(:user)
42 marker = insert(:marker, user: user)
43 insert(:notification, user: user)
44 insert(:notification, user: user)
45 insert(:marker, timeline: "home", user: user)
46
47 assert Marker.get_markers(
48 user,
49 ["notifications"],
50 %{recount_unread: true}
51 ) == [%Marker{refresh_record(marker) | unread_count: 2}]
52 end
53 end
54
55 describe "upsert/2" do
56 test "creates a marker" do
57 user = insert(:user)
58
59 {:ok, %{"notifications" => %Marker{} = marker}} =
60 Marker.upsert(
61 user,
62 %{"notifications" => %{"last_read_id" => "34"}}
63 )
64
65 assert marker.timeline == "notifications"
66 assert marker.last_read_id == "34"
67 assert marker.lock_version == 0
68 end
69
70 test "updates exist marker" do
71 user = insert(:user)
72 marker = insert(:marker, user: user, last_read_id: "8909")
73
74 {:ok, %{"notifications" => %Marker{}}} =
75 Marker.upsert(
76 user,
77 %{"notifications" => %{"last_read_id" => "9909"}}
78 )
79
80 marker = refresh_record(marker)
81 assert marker.timeline == "notifications"
82 assert marker.last_read_id == "9909"
83 assert marker.lock_version == 0
84 end
85 end
86 end