a6bb6d66b6497350a511c9bc48b1db2718bdd054
[mudpy.git] / mudpy / tests / selftest.py
1 # Copyright (c) 2004-2017 Jeremy Stanley <fungi@yuggoth.org>. Permission
2 # to use, copy, modify, and distribute this software is granted under
3 # terms provided in the LICENSE file distributed with this software.
4
5 import re
6 import sys
7 import telnetlib
8 import time
9
10 test_account0_setup = (
11     (0, "Identify yourself:", "luser0"),
12     (0, "Enter your choice:", "n"),
13     (0, "Enter a new password for \"luser0\":", "Test123"),
14     (0, "Enter the same new password again:", "Test123"),
15     (0, "What would you like to do\?", "c"),
16     (0, "Pick a birth gender for your new avatar:", "f"),
17     (0, "Choose a name for her:", "1"),
18     (0, "What would you like to do?", "a"),
19     (0, "Whom would you like to awaken?", ""),
20 )
21
22 test_account1_setup = (
23     (1, "Identify yourself:", "luser1"),
24     (1, "Enter your choice:", "n"),
25     (1, "Enter a new password for \"luser1\":", "Test456"),
26     (1, "Enter the same new password again:", "Test456"),
27     (1, "What would you like to do\?", "c"),
28     (1, "Pick a birth gender for your new avatar:", "m"),
29     (1, "Choose a name for him:", "1"),
30     (1, "What would you like to do?", "a"),
31     (1, "Whom would you like to awaken?", ""),
32 )
33
34 test_actor_appears = (
35     (0, "You suddenly realize that .* is here\.", ""),
36 )
37
38 test_explicit_punctuation = (
39     (0, "> ", "say Hello there!"),
40     (0, 'You exclaim, "Hello there\!"', ""),
41     (1, 'exclaims, "Hello there\!"', "say And you are?"),
42     (1, 'You ask, "And you are\?"', ""),
43     (0, 'asks, "And you are\?"', "say I'm me, of course."),
44     (0, 'You say, "I\'m me, of course\."', ""),
45     (1, 'says, "I\'m me, of course\."', "say I wouldn't be so sure..."),
46     (1, 'You muse, "I wouldn\'t be so sure\.\.\."', ""),
47     (0, 'muses, "I wouldn\'t be so sure\.\.\."', "say You mean,"),
48     (0, 'You begin, "You mean,"', ""),
49     (1, 'begins, "You mean,"', "say I know-"),
50     (1, 'You begin, "I know-"', ""),
51     (0, 'begins, "I know-"', "say Don't interrupt:"),
52     (0, 'You begin, "Don\'t interrupt:"', ""),
53     (1, 'begins, "Don\'t interrupt:"', "say I wasn't interrupting;"),
54     (1, 'You begin, "I wasn\'t interrupting;"', ""),
55     (0, 'begins, "I wasn\'t interrupting;"', ""),
56 )
57
58 test_implicit_punctuation = (
59     (0, '> ', "say Whatever"),
60     (0, 'You say, "Whatever\."', ""),
61     (1, 'says, "Whatever\."', ""),
62 )
63
64 test_typo_replacement = (
65     (1, '> ', "say That's what i think."),
66     (1, 'You say, "That\'s what I think\."', ""),
67     (0, 'says, "That\'s what I think\."', "say You know what i'd like."),
68     (0, 'You say, "You know what I\'d like\."', ""),
69     (1, 'says, "You know what I\'d like\."', "say Then i'll tell you."),
70     (1, 'You say, "Then I\'ll tell you\."', ""),
71     (0, 'says, "Then I\'ll tell you\."', "say Now i'm ready."),
72     (0, 'You say, "Now I\'m ready\."', ""),
73     (1, 'says, "Now I\'m ready\."', "say That's teh idea."),
74     (1, 'You say, "That\'s the idea\."', ""),
75     (0, 'says, "That\'s the idea\."', "say It's what theyre saying."),
76     (0, 'You say, "It\'s what they\'re saying\."', ""),
77     (1, 'says, "It\'s what they\'re saying\."', "say Well, youre right."),
78     (1, 'You say, "Well, you\'re right\."', ""),
79     (0, 'says, "Well, you\'re right\."', ""),
80 )
81
82 test_sentence_capitalization = (
83     (0, "> ", "say this sentence should start with a capital T."),
84     (0, 'You say, "This sentence', ""),
85     (1, 'says, "This sentence', ""),
86 )
87
88 test_chat_mode = (
89     (1, '> ', "chat"),
90     (1, '(?s)Entering chat mode .*> \(chat\) ', "Feeling chatty."),
91     (1, 'You say, "Feeling chatty\."', "!chat"),
92     (0, 'says, "Feeling chatty\."', ""),
93     (1, '> ', "say Now less chatty."),
94     (1, 'You say, "Now less chatty\."', ""),
95     (0, 'says, "Now less chatty\."', ""),
96 )
97
98 test_movement = (
99     (0, "> ", "move north"),
100     (0, "You exit to the north\.", ""),
101     (1, "exits to the north\.", "move north"),
102     (0, "arrives from the south\.", "move south"),
103     (0, "You exit to the south\.", ""),
104     (1, "exits to the south\.", "move south"),
105     (0, "arrives from the north\.", "move east"),
106     (0, "You exit to the east\.", ""),
107     (1, "exits to the east\.", "move east"),
108     (0, "arrives from the west\.", "move west"),
109     (0, "You exit to the west\.", ""),
110     (1, "exits to the west\.", "move west"),
111     (0, "arrives from the east\.", "move up"),
112     (0, "You exit upward\.", ""),
113     (1, "exits upward\.", "move up"),
114     (0, "arrives from below\.", "move down"),
115     (0, "You exit downward\.", ""),
116     (1, "exits downward\.", "move down"),
117     (0, "arrives from above\.", ""),
118 )
119
120 test_actor_disappears = (
121     (1, "> ", "quit"),
122     (0, "You suddenly wonder where .* went\.", ""),
123 )
124
125 test_account1_teardown = (
126     (1, "What would you like to do?", "d"),
127     (1, "Whom would you like to delete?", ""),
128     (1, "What would you like to do?", "p"),
129     (1, "permanently delete your account?", "y"),
130     (1, "Disconnecting...", ""),
131 )
132
133 test_admin_setup = (
134     (2, "Identify yourself:", "admin"),
135     (2, "Enter your choice:", "n"),
136     (2, "Enter a new password for \"admin\":", "Test789"),
137     (2, "Enter the same new password again:", "Test789"),
138     (2, "What would you like to do\?", "c"),
139     (2, "Pick a birth gender for your new avatar:", "m"),
140     (2, "Choose a name for him:", "1"),
141     (2, "What would you like to do?", "a"),
142     (2, "Whom would you like to awaken?", ""),
143 )
144
145 test_admin_restriction = (
146     (0, "> ", "help halt"),
147     (0, "That is not an available command\.", "halt"),
148     (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
149 )
150
151 test_admin_help = (
152     (2, "> ", "help"),
153     (2, "halt.*Shut down the world\.", "help halt"),
154     (2, "This will save all active accounts", ""),
155 )
156
157 test_show_element = (
158     (2, "> ", "show element internal:counters"),
159     (2, "These are the properties of the \"internal:counters\" element "
160         "\(in.*data/internal\.yaml\"\):.*   \x1b\[32melapsed: "
161         "\x1b\[31m[0-9]+\x1b\[0m", ""),
162 )
163
164 test_show_log = (
165     (2, "> ", "show log"),
166     (2, "There are [0-9]+ log lines in memory and [0-9]+ at or above level "
167         "[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
168 )
169
170 test_custom_loglevel = (
171     (2, "> ", "set account:admin loglevel 2"),
172     (2, "You have successfully .*> ", "show log"),
173     (2, "There are [0-9]+ log lines in memory and [0-9]+ at or above level "
174         "[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
175 )
176
177 test_invalid_loglevel = (
178     (2, "> ", "set account:admin loglevel two"),
179     (2, "Value \"two\" of type \"<class 'str'>\" cannot be coerced .*> ", ""),
180 )
181
182 test_log_no_errors = (
183     (2, "> ", "show log 7"),
184     (2, "None of the [0-9]+ lines in memory matches your request\.", ""),
185 )
186
187 dialogue = (
188     (test_account0_setup, "first account setup"),
189     (test_account1_setup, "second account setup"),
190     (test_actor_appears, "actor spontaneous appearance"),
191     (test_explicit_punctuation, " explicit punctuation"),
192     (test_implicit_punctuation, "implicit punctuation"),
193     (test_typo_replacement, "typo replacement"),
194     (test_sentence_capitalization, "sentence capitalization"),
195     (test_chat_mode, "chat mode"),
196     (test_movement, "movement"),
197     (test_actor_disappears, "actor spontaneous disappearance"),
198     (test_account1_teardown, "second account teardown"),
199     (test_admin_setup, "admin account setup"),
200     (test_admin_restriction, "restricted admin commands"),
201     (test_admin_help, "admin help"),
202     (test_show_element, "show element"),
203     (test_show_log, "show log"),
204     (test_custom_loglevel, "show log"),
205     (test_invalid_loglevel, "invalid loglevel"),
206     (test_log_no_errors, "no errors logged"),
207 )
208
209
210 def main():
211     captures = ["", "", ""]
212     lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
213     success = True
214     start = time.time()
215     for luser in lusers:
216         luser.open("::1", 6669)
217     for test, description in dialogue:
218         print("\nTesting %s..." % description)
219         test_start = time.time()
220         for conversant, question, answer in test:
221             print("luser%s waiting for: %s" % (conversant, question))
222             index, match, received = lusers[conversant].expect(
223                 [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
224             captures[conversant] += received.decode("utf-8")
225             try:
226                 captures[conversant] += lusers[
227                     conversant].read_very_eager().decode("utf-8")
228             except:
229                 pass
230             if index is not 0:
231                 print("ERROR: luser%s did not receive expected string:\n\n"
232                       "%s\n\n"
233                       "Check the end of capture_%s.log for received data."
234                       % (conversant, question, conversant))
235                 success = False
236                 break
237             print("luser%s sending: %s" % (conversant, answer))
238             lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
239             captures[conversant] += "%s\r\n" % answer
240         if not success:
241             break
242         print("Completed in %.3f seconds." % (time.time() - test_start))
243     duration = time.time() - start
244     print("")
245     for conversant in range(len(captures)):
246         try:
247             captures[conversant] += lusers[
248                 conversant].read_very_eager().decode("utf-8")
249         except:
250             pass
251         lusers[conversant].close()
252         logfile = "capture_%s.log" % conversant
253         print("Recording session %s as %s." % (conversant, logfile))
254         log = open(logfile, "w")
255         log.write(captures[conversant])
256         log.close()
257     print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
258     if success:
259         print("SUCCESS")
260     else:
261         print("FAILURE")
262         sys.exit(1)
263
264
265 if __name__ == '__main__':
266     sys.exit(main())