Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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
10 import Pleroma.Factory
11
12 describe "creation and getting" do
13 test "it only works if the recipient is a valid user (for now)" do
14 user = insert(:user)
15
16 assert {:error, _chat} = Chat.bump_or_create(user.id, "http://some/nonexisting/account")
17 assert {:error, _chat} = Chat.get_or_create(user.id, "http://some/nonexisting/account")
18 end
19
20 test "it creates a chat for a user and recipient" do
21 user = insert(:user)
22 other_user = insert(:user)
23
24 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
25
26 assert chat.id
27 end
28
29 test "it returns a chat for a user and recipient if it already exists" do
30 user = insert(:user)
31 other_user = insert(:user)
32
33 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
34 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
35
36 assert chat.id == chat_two.id
37 end
38
39 test "a returning chat will have an updated `update_at` field and an incremented unread count" do
40 user = insert(:user)
41 other_user = insert(:user)
42
43 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
44 assert chat.unread == 1
45 :timer.sleep(1500)
46 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
47 assert chat_two.unread == 2
48
49 assert chat.id == chat_two.id
50 assert chat.updated_at != chat_two.updated_at
51 end
52 end
53 end