comparison libervia/backend/test/test_plugin_misc_room_game.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/test/test_plugin_misc_room_game.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 """ Tests for the plugin room game (base class for MUC games) """
22
23 from libervia.backend.core.i18n import _
24 from .constants import Const
25 from libervia.backend.test import helpers, helpers_plugins
26 from libervia.backend.plugins import plugin_misc_room_game as plugin
27 from twisted.words.protocols.jabber.jid import JID
28 from wokkel.muc import User
29
30 from logging import WARNING
31
32 # Data used for test initialization
33 NAMESERVICE = "http://www.goffi.org/protocol/dummy"
34 TAG = "dummy"
35 PLUGIN_INFO = {
36 "name": "Dummy plugin",
37 "import_name": "DUMMY",
38 "type": "MISC",
39 "protocols": [],
40 "dependencies": [],
41 "main": "Dummy",
42 "handler": "no", # handler MUST be "no" (dynamic inheritance)
43 "description": _("""Dummy plugin to test room game"""),
44 }
45
46 ROOM_JID = JID(Const.MUC_STR[0])
47 PROFILE = Const.PROFILE[0]
48 OTHER_PROFILE = Const.PROFILE[1]
49
50
51 class RoomGameTest(helpers.SatTestCase):
52 def setUp(self):
53 self.host = helpers.FakeSAT()
54
55 def reinit(self, game_init={}, player_init={}):
56 self.host.reinit()
57 self.plugin = plugin.RoomGame(self.host)
58 self.plugin._init_(
59 self.host, PLUGIN_INFO, (NAMESERVICE, TAG), game_init, player_init
60 )
61 self.plugin_0045 = self.host.plugins["XEP-0045"] = helpers_plugins.FakeXEP_0045(
62 self.host
63 )
64 self.plugin_0249 = self.host.plugins["XEP-0249"] = helpers_plugins.FakeXEP_0249(
65 self.host
66 )
67 for profile in Const.PROFILE:
68 self.host.get_client(profile) # init self.host.profiles[profile]
69
70 def init_game(self, muc_index, user_index):
71 self.plugin_0045.join_room(user_index, muc_index)
72 self.plugin._init_game(JID(Const.MUC_STR[muc_index]), Const.JID[user_index].user)
73
74 def _expected_message(self, to, type_, tag, players=[]):
75 content = "<%s" % tag
76 if not players:
77 content += "/>"
78 else:
79 content += ">"
80 for i in range(0, len(players)):
81 content += "<player index='%s'>%s</player>" % (i, players[i])
82 content += "</%s>" % tag
83 return "<message to='%s' type='%s'><%s xmlns='%s'>%s</dummy></message>" % (
84 to.full(),
85 type_,
86 TAG,
87 NAMESERVICE,
88 content,
89 )
90
91 def test_create_or_invite_solo(self):
92 self.reinit()
93 self.plugin_0045.join_room(0, 0)
94 self.plugin._create_or_invite(self.plugin_0045.get_room(0, 0), [], Const.PROFILE[0])
95 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
96
97 def test_create_or_invite_multi_not_waiting(self):
98 self.reinit()
99 self.plugin_0045.join_room(0, 0)
100 other_players = [Const.JID[1], Const.JID[2]]
101 self.plugin._create_or_invite(
102 self.plugin_0045.get_room(0, 0), other_players, Const.PROFILE[0]
103 )
104 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
105
106 def test_create_or_invite_multi_waiting(self):
107 self.reinit(player_init={"score": 0})
108 self.plugin_0045.join_room(0, 0)
109 other_players = [Const.JID[1], Const.JID[2]]
110 self.plugin._create_or_invite(
111 self.plugin_0045.get_room(0, 0), other_players, Const.PROFILE[0]
112 )
113 self.assertTrue(self.plugin._game_exists(ROOM_JID, False))
114 self.assertFalse(self.plugin._game_exists(ROOM_JID, True))
115
116 def test_init_game(self):
117 self.reinit()
118 self.init_game(0, 0)
119 self.assertTrue(self.plugin.is_referee(ROOM_JID, Const.JID[0].user))
120 self.assertEqual([], self.plugin.games[ROOM_JID]["players"])
121
122 def test_check_join_auth(self):
123 self.reinit()
124 check = lambda value: getattr(self, "assert%s" % value)(
125 self.plugin._check_join_auth(ROOM_JID, Const.JID[0], Const.JID[0].user)
126 )
127 check(False)
128 # to test the "invited" mode, the referee must be different than the user to test
129 self.init_game(0, 1)
130 self.plugin.join_mode = self.plugin.ALL
131 check(True)
132 self.plugin.join_mode = self.plugin.INVITED
133 check(False)
134 self.plugin.invitations[ROOM_JID] = [(None, [Const.JID[0].userhostJID()])]
135 check(True)
136 self.plugin.join_mode = self.plugin.NONE
137 check(False)
138 self.plugin.games[ROOM_JID]["players"].append(Const.JID[0].user)
139 check(True)
140
141 def test_update_players(self):
142 self.reinit()
143 self.init_game(0, 0)
144 self.assertEqual(self.plugin.games[ROOM_JID]["players"], [])
145 self.plugin._update_players(ROOM_JID, [], True, Const.PROFILE[0])
146 self.assertEqual(self.plugin.games[ROOM_JID]["players"], [])
147 self.plugin._update_players(ROOM_JID, ["user1"], True, Const.PROFILE[0])
148 self.assertEqual(self.plugin.games[ROOM_JID]["players"], ["user1"])
149 self.plugin._update_players(ROOM_JID, ["user2", "user3"], True, Const.PROFILE[0])
150 self.assertEqual(
151 self.plugin.games[ROOM_JID]["players"], ["user1", "user2", "user3"]
152 )
153 self.plugin._update_players(
154 ROOM_JID, ["user2", "user3"], True, Const.PROFILE[0]
155 ) # should not be stored twice
156 self.assertEqual(
157 self.plugin.games[ROOM_JID]["players"], ["user1", "user2", "user3"]
158 )
159
160 def test_synchronize_room(self):
161 self.reinit()
162 self.init_game(0, 0)
163 self.plugin._synchronize_room(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
164 self.assertEqual(
165 self.host.get_sent_message_xml(0),
166 self._expected_message(ROOM_JID, "groupchat", "players", []),
167 )
168 self.plugin.games[ROOM_JID]["players"].append("test1")
169 self.plugin._synchronize_room(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
170 self.assertEqual(
171 self.host.get_sent_message_xml(0),
172 self._expected_message(ROOM_JID, "groupchat", "players", ["test1"]),
173 )
174 self.plugin.games[ROOM_JID]["started"] = True
175 self.plugin.games[ROOM_JID]["players"].append("test2")
176 self.plugin._synchronize_room(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
177 self.assertEqual(
178 self.host.get_sent_message_xml(0),
179 self._expected_message(ROOM_JID, "groupchat", "started", ["test1", "test2"]),
180 )
181 self.plugin.games[ROOM_JID]["players"].append("test3")
182 self.plugin.games[ROOM_JID]["players"].append("test4")
183 user1 = JID(ROOM_JID.userhost() + "/" + Const.JID[0].user)
184 user2 = JID(ROOM_JID.userhost() + "/" + Const.JID[1].user)
185 self.plugin._synchronize_room(ROOM_JID, [user1, user2], Const.PROFILE[0])
186 self.assert_equal_xml(
187 self.host.get_sent_message_xml(0),
188 self._expected_message(
189 user1, "normal", "started", ["test1", "test2", "test3", "test4"]
190 ),
191 )
192 self.assert_equal_xml(
193 self.host.get_sent_message_xml(0),
194 self._expected_message(
195 user2, "normal", "started", ["test1", "test2", "test3", "test4"]
196 ),
197 )
198
199 def test_invite_players(self):
200 self.reinit()
201 self.init_game(0, 0)
202 self.plugin_0045.join_room(0, 1)
203 self.assertEqual(self.plugin.invitations[ROOM_JID], [])
204 room = self.plugin_0045.get_room(0, 0)
205 nicks = self.plugin._invite_players(
206 room, [Const.JID[1], Const.JID[2]], Const.JID[0].user, Const.PROFILE[0]
207 )
208 self.assertEqual(
209 self.plugin.invitations[ROOM_JID][0][1],
210 [Const.JID[1].userhostJID(), Const.JID[2].userhostJID()],
211 )
212 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
213 self.assertEqual(nicks, [Const.JID[1].user, Const.JID[2].user])
214
215 nicks = self.plugin._invite_players(
216 room, [Const.JID[1], Const.JID[3]], Const.JID[0].user, Const.PROFILE[0]
217 )
218 self.assertEqual(
219 self.plugin.invitations[ROOM_JID][1][1],
220 [Const.JID[1].userhostJID(), Const.JID[3].userhostJID()],
221 )
222 # this time Const.JID[1] and Const.JID[3] have the same user but the host differs
223 self.assertEqual(nicks, [Const.JID[1].user])
224
225 def test_check_invite_auth(self):
226 def check(value, index):
227 nick = self.plugin_0045.get_nick(0, index)
228 getattr(self, "assert%s" % value)(
229 self.plugin._check_invite_auth(ROOM_JID, nick)
230 )
231
232 self.reinit()
233
234 for mode in [
235 self.plugin.FROM_ALL,
236 self.plugin.FROM_NONE,
237 self.plugin.FROM_REFEREE,
238 self.plugin.FROM_PLAYERS,
239 ]:
240 self.plugin.invite_mode = mode
241 check(True, 0)
242
243 self.init_game(0, 0)
244 self.plugin.invite_mode = self.plugin.FROM_ALL
245 check(True, 0)
246 check(True, 1)
247 self.plugin.invite_mode = self.plugin.FROM_NONE
248 check(True, 0) # game initialized but not started yet, referee can invite
249 check(False, 1)
250 self.plugin.invite_mode = self.plugin.FROM_REFEREE
251 check(True, 0)
252 check(False, 1)
253 user_nick = self.plugin_0045.join_room(0, 1)
254 self.plugin.games[ROOM_JID]["players"].append(user_nick)
255 self.plugin.invite_mode = self.plugin.FROM_PLAYERS
256 check(True, 0)
257 check(True, 1)
258 check(False, 2)
259
260 def test_is_referee(self):
261 self.reinit()
262 self.init_game(0, 0)
263 self.assertTrue(self.plugin.is_referee(ROOM_JID, self.plugin_0045.get_nick(0, 0)))
264 self.assertFalse(self.plugin.is_referee(ROOM_JID, self.plugin_0045.get_nick(0, 1)))
265
266 def test_is_player(self):
267 self.reinit()
268 self.init_game(0, 0)
269 self.assertTrue(self.plugin.is_player(ROOM_JID, self.plugin_0045.get_nick(0, 0)))
270 user_nick = self.plugin_0045.join_room(0, 1)
271 self.plugin.games[ROOM_JID]["players"].append(user_nick)
272 self.assertTrue(self.plugin.is_player(ROOM_JID, user_nick))
273 self.assertFalse(self.plugin.is_player(ROOM_JID, self.plugin_0045.get_nick(0, 2)))
274
275 def test_check_wait_auth(self):
276 def check(value, other_players, confirmed, rest):
277 room = self.plugin_0045.get_room(0, 0)
278 self.assertEqual(
279 (value, confirmed, rest), self.plugin._check_wait_auth(room, other_players)
280 )
281
282 self.reinit()
283 self.init_game(0, 0)
284 other_players = [Const.JID[1], Const.JID[3]]
285 self.plugin.wait_mode = self.plugin.FOR_NONE
286 check(True, [], [], [])
287 check(
288 True, [Const.JID[0]], [], [Const.JID[0]]
289 ) # getRoomNickOfUser checks for the other users only
290 check(True, other_players, [], other_players)
291 self.plugin.wait_mode = self.plugin.FOR_ALL
292 check(True, [], [], [])
293 check(False, [Const.JID[0]], [], [Const.JID[0]])
294 check(False, other_players, [], other_players)
295 self.plugin_0045.join_room(0, 1)
296 check(False, other_players, [], other_players)
297 self.plugin_0045.join_room(0, 4)
298 check(
299 False,
300 other_players,
301 [self.plugin_0045.get_nick_of_user(0, 1, 0)],
302 [Const.JID[3]],
303 )
304 self.plugin_0045.join_room(0, 3)
305 check(
306 True,
307 other_players,
308 [
309 self.plugin_0045.get_nick_of_user(0, 1, 0),
310 self.plugin_0045.get_nick_of_user(0, 3, 0),
311 ],
312 [],
313 )
314
315 other_players = [Const.JID[1], Const.JID[3], Const.JID[2]]
316 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
317 check(
318 True,
319 other_players,
320 [
321 self.plugin_0045.get_nick_of_user(0, 1, 0),
322 self.plugin_0045.get_nick_of_user(0, 3, 0),
323 self.plugin_0045.get_nick_of_user(0, 2, 0),
324 ],
325 [],
326 )
327
328 def test_prepare_room_trivial(self):
329 self.reinit()
330 other_players = []
331 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
332 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
333 self.assertTrue(
334 self.plugin._check_join_auth(ROOM_JID, Const.JID[0], Const.JID[0].user)
335 )
336 self.assertTrue(self.plugin._check_invite_auth(ROOM_JID, Const.JID[0].user))
337 self.assertEqual((True, [], []), self.plugin._check_wait_auth(ROOM_JID, []))
338 self.assertTrue(self.plugin.is_referee(ROOM_JID, Const.JID[0].user))
339 self.assertTrue(self.plugin.is_player(ROOM_JID, Const.JID[0].user))
340 self.assertEqual(
341 (False, True), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
342 )
343
344 def test_prepare_room_invite(self):
345 self.reinit()
346 other_players = [Const.JID[1], Const.JID[2]]
347 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
348 room = self.plugin_0045.get_room(0, 0)
349
350 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
351 self.assertTrue(
352 self.plugin._check_join_auth(ROOM_JID, Const.JID[1], Const.JID[1].user)
353 )
354 self.assertFalse(
355 self.plugin._check_join_auth(ROOM_JID, Const.JID[3], Const.JID[3].user)
356 )
357 self.assertFalse(self.plugin._check_invite_auth(ROOM_JID, Const.JID[1].user))
358 self.assertEqual(
359 (True, [], other_players), self.plugin._check_wait_auth(room, other_players)
360 )
361
362 player2_nick = self.plugin_0045.join_room(0, 1)
363 self.plugin.user_joined_trigger(room, room.roster[player2_nick], PROFILE)
364 self.assertTrue(self.plugin.is_player(ROOM_JID, player2_nick))
365 self.assertTrue(self.plugin._check_invite_auth(ROOM_JID, player2_nick))
366 self.assertFalse(self.plugin.is_referee(ROOM_JID, player2_nick))
367 self.assertTrue(self.plugin.is_player(ROOM_JID, player2_nick))
368 self.assertTrue(
369 self.plugin.is_player(ROOM_JID, self.plugin_0045.get_nick_of_user(0, 2, 0))
370 )
371 self.assertFalse(self.plugin.is_player(ROOM_JID, "xxx"))
372 self.assertEqual(
373 (False, False),
374 self.plugin._check_create_game_and_init(ROOM_JID, Const.PROFILE[1]),
375 )
376
377 def test_prepare_room_score_1(self):
378 self.reinit(player_init={"score": 0})
379 other_players = [Const.JID[1], Const.JID[2]]
380 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
381 room = self.plugin_0045.get_room(0, 0)
382
383 self.assertFalse(self.plugin._game_exists(ROOM_JID, True))
384 self.assertTrue(
385 self.plugin._check_join_auth(ROOM_JID, Const.JID[1], Const.JID[1].user)
386 )
387 self.assertFalse(
388 self.plugin._check_join_auth(ROOM_JID, Const.JID[3], Const.JID[3].user)
389 )
390 self.assertFalse(self.plugin._check_invite_auth(ROOM_JID, Const.JID[1].user))
391 self.assertEqual(
392 (False, [], other_players), self.plugin._check_wait_auth(room, other_players)
393 )
394
395 user_nick = self.plugin_0045.join_room(0, 1)
396 self.plugin.user_joined_trigger(room, room.roster[user_nick], PROFILE)
397 self.assertTrue(self.plugin.is_player(ROOM_JID, user_nick))
398 self.assertFalse(self.plugin._check_invite_auth(ROOM_JID, user_nick))
399 self.assertFalse(self.plugin.is_referee(ROOM_JID, user_nick))
400 self.assertTrue(self.plugin.is_player(ROOM_JID, user_nick))
401 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
402 self.assertTrue(
403 self.plugin.is_player(ROOM_JID, self.plugin_0045.get_nick_of_user(0, 2, 0))
404 )
405 # the following assertion is True because Const.JID[1] nick in the room is equal to Const.JID[3].user
406 self.assertTrue(self.plugin.is_player(ROOM_JID, Const.JID[3].user))
407 # but Const.JID[3] is actually not in the room
408 self.assertEqual(self.plugin_0045.get_nick_of_user(0, 3, 0), None)
409 self.assertEqual(
410 (True, False), self.plugin._check_create_game_and_init(ROOM_JID, Const.PROFILE[0])
411 )
412
413 def test_prepare_room_score_2(self):
414 self.reinit(player_init={"score": 0})
415 other_players = [Const.JID[1], Const.JID[4]]
416 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
417 room = self.plugin_0045.get_room(0, 0)
418
419 user_nick = self.plugin_0045.join_room(0, 1)
420 self.plugin.user_joined_trigger(room, room.roster[user_nick], PROFILE)
421 self.assertEqual(
422 (True, False), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
423 )
424 user_nick = self.plugin_0045.join_room(0, 4)
425 self.plugin.user_joined_trigger(room, room.roster[user_nick], PROFILE)
426 self.assertEqual(
427 (False, True), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
428 )
429
430 def test_user_joined_trigger(self):
431 self.reinit(player_init={"xxx": "xyz"})
432 other_players = [Const.JID[1], Const.JID[3]]
433 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
434 nicks = [self.plugin_0045.get_nick(0, 0)]
435
436 self.assertEqual(
437 self.host.get_sent_message_xml(0),
438 self._expected_message(ROOM_JID, "groupchat", "players", nicks),
439 )
440 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 1)
441
442 # wrong profile
443 user_nick = self.plugin_0045.join_room(0, 1)
444 room = self.plugin_0045.get_room(0, 1)
445 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE)
446 self.assertEqual(
447 self.host.get_sent_message(0), None
448 ) # no new message has been sent
449 self.assertFalse(self.plugin._game_exists(ROOM_JID, True)) # game not started
450
451 # referee profile, user is allowed, wait for one more
452 room = self.plugin_0045.get_room(0, 0)
453 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[1]), PROFILE)
454 nicks.append(user_nick)
455 self.assertEqual(
456 self.host.get_sent_message_xml(0),
457 self._expected_message(ROOM_JID, "groupchat", "players", nicks),
458 )
459 self.assertFalse(self.plugin._game_exists(ROOM_JID, True)) # game not started
460
461 # referee profile, user is not allowed
462 user_nick = self.plugin_0045.join_room(0, 4)
463 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[4]), PROFILE)
464 self.assertEqual(
465 self.host.get_sent_message_xml(0),
466 self._expected_message(
467 JID(ROOM_JID.userhost() + "/" + user_nick), "normal", "players", nicks
468 ),
469 )
470 self.assertFalse(self.plugin._game_exists(ROOM_JID, True)) # game not started
471
472 # referee profile, user is allowed, everybody here
473 user_nick = self.plugin_0045.join_room(0, 3)
474 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[3]), PROFILE)
475 nicks.append(user_nick)
476 self.assertEqual(
477 self.host.get_sent_message_xml(0),
478 self._expected_message(ROOM_JID, "groupchat", "started", nicks),
479 )
480 self.assertTrue(self.plugin._game_exists(ROOM_JID, True)) # game started
481 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
482
483 # wait for none
484 self.reinit()
485 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
486 self.assertNotEqual(self.host.get_sent_message(0), None) # init messages
487 room = self.plugin_0045.get_room(0, 0)
488 nicks = [self.plugin_0045.get_nick(0, 0)]
489 user_nick = self.plugin_0045.join_room(0, 3)
490 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[3]), PROFILE)
491 nicks.append(user_nick)
492 self.assertEqual(
493 self.host.get_sent_message_xml(0),
494 self._expected_message(ROOM_JID, "groupchat", "started", nicks),
495 )
496 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
497
498 def test_user_left_trigger(self):
499 self.reinit(player_init={"xxx": "xyz"})
500 other_players = [Const.JID[1], Const.JID[3], Const.JID[4]]
501 self.plugin.prepare_room(other_players, ROOM_JID, PROFILE)
502 room = self.plugin_0045.get_room(0, 0)
503 nicks = [self.plugin_0045.get_nick(0, 0)]
504 self.assertEqual(
505 self.plugin.invitations[ROOM_JID][0][1],
506 [
507 Const.JID[1].userhostJID(),
508 Const.JID[3].userhostJID(),
509 Const.JID[4].userhostJID(),
510 ],
511 )
512
513 # one user joins
514 user_nick = self.plugin_0045.join_room(0, 1)
515 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[1]), PROFILE)
516 nicks.append(user_nick)
517
518 # the user leaves
519 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
520 room = self.plugin_0045.get_room(0, 1)
521 # to not call self.plugin_0045.leave_room(0, 1) here, we are testing the trigger with a wrong profile
522 self.plugin.user_left_trigger(
523 room, User(user_nick, Const.JID[1]), Const.PROFILE[1]
524 ) # not the referee
525 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
526 room = self.plugin_0045.get_room(0, 0)
527 user_nick = self.plugin_0045.leave_room(0, 1)
528 self.plugin.user_left_trigger(
529 room, User(user_nick, Const.JID[1]), PROFILE
530 ) # referee
531 nicks.pop()
532 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
533
534 # all the users join
535 user_nick = self.plugin_0045.join_room(0, 1)
536 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[1]), PROFILE)
537 nicks.append(user_nick)
538 user_nick = self.plugin_0045.join_room(0, 3)
539 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[3]), PROFILE)
540 nicks.append(user_nick)
541 user_nick = self.plugin_0045.join_room(0, 4)
542 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[4]), PROFILE)
543 nicks.append(user_nick)
544 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
545 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
546
547 # one user leaves
548 user_nick = self.plugin_0045.leave_room(0, 4)
549 self.plugin.user_left_trigger(room, User(user_nick, Const.JID[4]), PROFILE)
550 nicks.pop()
551 self.assertEqual(
552 self.plugin.invitations[ROOM_JID][0][1], [Const.JID[4].userhostJID()]
553 )
554
555 # another leaves
556 user_nick = self.plugin_0045.leave_room(0, 3)
557 self.plugin.user_left_trigger(room, User(user_nick, Const.JID[3]), PROFILE)
558 nicks.pop()
559 self.assertEqual(
560 self.plugin.invitations[ROOM_JID][0][1],
561 [Const.JID[4].userhostJID(), Const.JID[3].userhostJID()],
562 )
563
564 # they can join again
565 user_nick = self.plugin_0045.join_room(0, 3)
566 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[3]), PROFILE)
567 nicks.append(user_nick)
568 user_nick = self.plugin_0045.join_room(0, 4)
569 self.plugin.user_joined_trigger(room, User(user_nick, Const.JID[4]), PROFILE)
570 nicks.append(user_nick)
571 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
572 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
573
574 def test_check_create_game_and_init(self):
575 self.reinit()
576 helpers.mute_logging()
577 self.assertEqual(
578 (False, False), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
579 )
580 helpers.unmute_logging()
581
582 nick = self.plugin_0045.join_room(0, 0)
583 self.assertEqual(
584 (True, False), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
585 )
586 self.assertTrue(self.plugin._game_exists(ROOM_JID, False))
587 self.assertFalse(self.plugin._game_exists(ROOM_JID, True))
588 self.assertTrue(self.plugin.is_referee(ROOM_JID, nick))
589
590 helpers.mute_logging()
591 self.assertEqual(
592 (False, False), self.plugin._check_create_game_and_init(ROOM_JID, OTHER_PROFILE)
593 )
594 helpers.unmute_logging()
595
596 self.plugin_0045.join_room(0, 1)
597 self.assertEqual(
598 (False, False), self.plugin._check_create_game_and_init(ROOM_JID, OTHER_PROFILE)
599 )
600
601 self.plugin.create_game(ROOM_JID, [Const.JID[1]], PROFILE)
602 self.assertEqual(
603 (False, True), self.plugin._check_create_game_and_init(ROOM_JID, PROFILE)
604 )
605 self.assertEqual(
606 (False, False), self.plugin._check_create_game_and_init(ROOM_JID, OTHER_PROFILE)
607 )
608
609 def test_create_game(self):
610
611 self.reinit(player_init={"xxx": "xyz"})
612 nicks = []
613 for i in [0, 1, 3, 4]:
614 nicks.append(self.plugin_0045.join_room(0, i))
615
616 # game not exists
617 self.plugin.create_game(ROOM_JID, nicks, PROFILE)
618 self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
619 self.assertEqual(self.plugin.games[ROOM_JID]["players"], nicks)
620 self.assertEqual(
621 self.host.get_sent_message_xml(0),
622 self._expected_message(ROOM_JID, "groupchat", "started", nicks),
623 )
624 for nick in nicks:
625 self.assertEqual("init", self.plugin.games[ROOM_JID]["status"][nick])
626 self.assertEqual(
627 self.plugin.player_init, self.plugin.games[ROOM_JID]["players_data"][nick]
628 )
629 self.plugin.games[ROOM_JID]["players_data"][nick]["xxx"] = nick
630 for nick in nicks:
631 # checks that a copy of self.player_init has been done and not a reference
632 self.assertEqual(
633 nick, self.plugin.games[ROOM_JID]["players_data"][nick]["xxx"]
634 )
635
636 # game exists, current profile is referee
637 self.reinit(player_init={"xxx": "xyz"})
638 self.init_game(0, 0)
639 self.plugin.games[ROOM_JID]["started"] = True
640 self.plugin.create_game(ROOM_JID, nicks, PROFILE)
641 self.assertEqual(
642 self.host.get_sent_message_xml(0),
643 self._expected_message(ROOM_JID, "groupchat", "started", nicks),
644 )
645
646 # game exists, current profile is not referee
647 self.reinit(player_init={"xxx": "xyz"})
648 self.init_game(0, 0)
649 self.plugin.games[ROOM_JID]["started"] = True
650 self.plugin_0045.join_room(0, 1)
651 self.plugin.create_game(ROOM_JID, nicks, OTHER_PROFILE)
652 self.assertEqual(
653 self.host.get_sent_message(0), None
654 ) # no sync message has been sent by other_profile