42e01fe2728933405e52be8f16970ba1ded9e8c8
[akkoma] / test / chat_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.ChatTest do
6 use Pleroma.DataCase, async: true
7
8 alias Pleroma.Chat
9 alias Pleroma.Web.CommonAPI
10
11 import Pleroma.Factory
12
13 describe "creation and getting" do
14 test "it only works if the recipient is a valid user (for now)" do
15 user = insert(:user)
16
17 assert {:error, _chat} = Chat.bump_or_create(user.id, "http://some/nonexisting/account")
18 assert {:error, _chat} = Chat.get_or_create(user.id, "http://some/nonexisting/account")
19 end
20
21 test "it creates a chat for a user and recipient" do
22 user = insert(:user)
23 other_user = insert(:user)
24
25 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
26
27 assert chat.id
28 end
29
30 test "it returns and bumps a chat for a user and recipient if it already exists" do
31 user = insert(:user)
32 other_user = insert(:user)
33
34 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
35 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
36
37 assert chat.id == chat_two.id
38 assert chat_two.unread == 2
39 end
40
41 test "it returns a chat for a user and recipient if it already exists" do
42 user = insert(:user)
43 other_user = insert(:user)
44
45 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
46 {:ok, chat_two} = Chat.get_or_create(user.id, other_user.ap_id)
47
48 assert chat.id == chat_two.id
49 end
50
51 test "a returning chat will have an updated `update_at` field and an incremented unread count" do
52 user = insert(:user)
53 other_user = insert(:user)
54
55 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
56 assert chat.unread == 1
57 :timer.sleep(1500)
58 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
59 assert chat_two.unread == 2
60
61 assert chat.id == chat_two.id
62 assert chat.updated_at != chat_two.updated_at
63 end
64 end
65 end