# Coverage for tests/e2e/test_observe.py : 100%

## 80 statements 80 run 0 missing 0 excluded 0 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# -*- coding: utf-8 -*-

2 from __future__ import annotations

3

4# -- stdlib --

5# -- third party --

6 import gevent

7

8# -- own --

9 from .mock import Environ, EventTap

10

11

12# -- code --

13 class GameEnded(Exception):

14 pass

15

16

17 def wait():

18 gevent.idle(-100)

19 gevent.sleep(0.01)

20 gevent.idle(-100)

21

22

23 class TestObserve(object):

24 def testObserve(self):

25 env = Environ()

26 t = EventTap()

27

28 s = env.server_core()

29 youmu = env.client_core()

30 yuyuko = env.client_core()

31 t.tap(youmu, yuyuko)

32

33 youmu.auth.login("Youmu")

34 yuyuko.auth.login("Yuyuko")

35 wait()

36 assert youmu.auth.uid

37 assert yuyuko.auth.uid

38

39 youmu.room.create('Boom', 'THBattleDummy1', {}); wait()

40 assert youmu.events.game_joined in t

41

42 # Grant without request

43 youmu.observe.grant(yuyuko.auth.uid, True); wait()

44 assert yuyuko.events.game_joined not in t

45

46 # Denied request

47 yuyuko.observe.observe(youmu.auth.uid); wait()

48 assert t.take(youmu.events.observe_request) == yuyuko.auth.uid

49 youmu.observe.grant(yuyuko.auth.uid, False); wait()

50 assert yuyuko.events.game_joined not in t

51

52 # Subsequent grant should not take effect

53 youmu.observe.grant(yuyuko.auth.uid, True); wait()

54 assert yuyuko.events.game_joined not in t

55

56 # Subsequent request should not take effect

57 yuyuko.observe.observe(youmu.auth.uid); wait()

58 assert t.take(youmu.events.observe_request) == yuyuko.auth.uid

59 yuyuko.observe.observe(youmu.auth.uid); wait()

60 assert youmu.events.observe_request not in t

61

62 # Happy path

63 youmu.observe.grant(yuyuko.auth.uid, True); wait()

64 assert yuyuko.events.game_joined in t

65

66 # Observers should not interference legit game players

67 yuyuko.room.get_ready(); wait()

68 assert youmu.events.game_started not in t

69 assert yuyuko.events.game_started not in t

70

71 # Kick observer

72 t.clear()

73 youmu.observe.kick(yuyuko.auth.uid); wait()

74 assert t.take(yuyuko.events.game_left)

75

76 # Big brother

77 s.observe.add_bigbrother(yuyuko.auth.uid)

78 yuyuko.observe.observe(youmu.auth.uid); wait()

79 assert yuyuko.events.game_joined in t

80

81 # Stop observing

82 t.clear()

83 yuyuko.room.leave(); wait()

84 assert yuyuko.events.game_left in t

85

86 # Big brother, again

87 s.observe.add_bigbrother(yuyuko.auth.uid)

88 yuyuko.observe.observe(youmu.auth.uid); wait()

89 assert yuyuko.events.game_joined in t

90

91 # Observee exits

92 t.clear()

93 youmu.room.leave(); wait()

94 assert t.take(youmu.events.game_left)

95 assert t.take(yuyuko.events.game_left)

96 assert s.lobby.state_of(s.lobby.get(youmu.auth.uid)) == 'lobby'

97 assert s.lobby.state_of(s.lobby.get(yuyuko.auth.uid)) == 'lobby'

98

99 # Stop being big brother

100 s.observe.remove_bigbrother(yuyuko.auth.uid)

101

102 # Happy path

103 t.clear()

104 youmu.room.create('Boom', 'THBattleDummy1', {}); wait()

105 yuyuko.observe.observe(youmu.auth.uid); wait()

106 youmu.observe.grant(yuyuko.auth.uid, True); wait()

107 youmu.room.get_ready(); wait()

108 assert not youmu.game.is_observe(t[youmu.events.game_started])

109 assert yuyuko.game.is_observe(t[yuyuko.events.game_started])

110 youmu.game.start_game(t[youmu.events.game_started])

111 yuyuko.game.start_game(t[yuyuko.events.game_started])

112 wait()

113 assert t.take(youmu.events.game_ended)

114 assert t.take(yuyuko.events.game_ended)

115

116 # Happy path, successive game

117 assert t.take(youmu.events.game_joined)

118 assert t.take(yuyuko.events.game_joined)

119 youmu.room.get_ready(); wait()

120 assert not youmu.game.is_observe(t[youmu.events.game_started])

121 assert yuyuko.game.is_observe(t[yuyuko.events.game_started])

122 youmu.game.start_game(t[youmu.events.game_started])

123 yuyuko.game.start_game(t[yuyuko.events.game_started])

124 wait()