]> git.cworth.org Git - lmno-server/blob - test
Add new failing test case: Non-player can terminate judging
[lmno-server] / test
1 #!/bin/bash
2
3 # Arrange for some cleanup to be executed if the user interrupts the
4 # test sutie (for example, by pressing ControlC at the controlling
5 # terminal).
6 cleanup_and_report() {
7     empathy_deactivate_all >/dev/null 2>&1
8     TEST_REPORT
9     exit $?
10 }
11 trap cleanup_and_report INT
12
13 usage ()
14 {
15     echo "Usage:$0 <URL-to-test>"
16 }
17
18 if [ $# -lt 1 ]; then
19     echo "Error: No test URL given." >&2
20     echo "" >&2
21     usage >&2
22     exit 1
23 fi
24
25 URL=$1
26 CURL="curl --silent --show-error"
27
28 _TEST_SECTION()
29 {
30     echo ""
31     echo $1
32     echo $1 | sed -e "s/./$2/g"
33 }
34
35 TEST_SECTION()
36 {
37     _TEST_SECTION "$1" =
38 }
39
40 TEST_SUBSECTION()
41 {
42     _TEST_SECTION "$1" -
43 }
44
45 TEST()
46 {
47     printf "    $1"
48     printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
49     (( tests_total++ )) || true
50 }
51
52 # Result of test depends on the exit status of last command
53 TEST_END()
54 {
55     if [ $? -eq 0 ]; then
56         echo -n "OK"
57     else
58         (( tests_failed++ )) || true
59         echo -n "FAIL"
60     fi
61
62     # If we got an argument, append it after test result
63     if [ -n "$1" ]; then
64         echo " $1"
65     else
66         echo ""
67     fi
68 }
69
70 # Print report of all previous test results
71 TEST_REPORT()
72 {
73     echo ""
74     echo ""
75     echo "Test Report"
76     echo "==========="
77
78     if [ "$tests_failed" == "" ]; then
79         echo "All $tests_total tests passed."
80         echo ""
81         return 0
82     else
83         echo "$tests_failed of $tests_total tests failed."
84         echo ""
85         return 1
86     fi
87 }
88
89 # Does a string contain a regular expression pattern
90 #
91 # Example:
92 #
93 #    contains "All's well that ends well" "s.well"
94 contains()
95 {
96     grep -q "$2" <<< $1
97 }
98
99 # POST to a URL endpoint with optional JSON data
100 #
101 # Usage:
102 #
103 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
104 curl_post()
105 {
106     $CURL ${3:-} -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
107 }
108
109 # POST to a URL endpoint with optional JSON data using a cookie
110 #
111 # Usage:
112 #
113 # curl_post_cookie <name> <ENDPOINT> [data] [CURL_OPTIONS]
114 #
115 # Where <name> is a string for which there is a defined variable
116 # named ${name}_cookie which in turn holds a value that is a filename
117 # of a valid cookie
118 curl_post_cookie()
119 {
120     cookie=${1}_cookie
121     curl_post $2 "${3:-}" "-b ${!cookie} ${4:-}"
122 }
123
124 # PUT to a URL endpoint with optional JSON data
125 #
126 # Usage:
127 #
128 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
129 curl_put()
130 {
131     $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
132 }
133
134 # GET from a URL endpoint
135 #
136 # Usage:
137 #
138 # curl_get <ENDPOINT> [CURL_OPTIONS]
139 curl_get()
140 {
141     $CURL ${2:-} $URL/$1
142 }
143
144 # GET from a URL endpoint using a cookie
145 #
146 # Usage:
147 #
148 # curl_get_cookie <name> <ENDPOINT> [CURL_OPTIONS]
149 #
150 # Where <name> is a string for which there is a defined variable
151 # named ${name}_cookie which in turn holds a value that is a filename
152 # of a valid cookie
153 curl_get_cookie()
154 {
155     cookie=${1}_cookie
156     curl_get $2 "-b ${!cookie} ${3:-}"
157 }
158
159 # Create a new game of the specified engine type
160 #
161 # Usage:
162 #
163 # new_game <ENGINE>
164 new_game()
165 {
166     curl_post new/$1 | jq -r .
167 }
168
169 TEST_SECTION "LMNO (super-site for games)"
170
171 TEST_SUBSECTION "Testing home page"
172 home_page=$($CURL $URL)
173
174 TEST "Contains 'Join Game'"
175 contains "$home_page" "Join Game"
176 TEST_END
177
178 TEST "Contains 'Host a new game'"
179 contains "$home_page" "Host a new game"
180 TEST_END
181
182 TEST_SUBSECTION "Creating some new games"
183
184 TEST "Empires"
185 empires_game_id=$(new_game empires)
186 test "$empires_game_id" != ""
187 TEST_END $empires_game_id
188
189 TEST "Tic Tac Toe"
190 tictactoe_game_id=$(new_game tictactoe)
191 test "$tictactoe_game_id" != ""
192 TEST_END $tictactoe_game_id
193
194 TEST_SUBSECTION "Test redirects"
195
196 TEST "Redirect of /GAMEID at top level"
197 redirect=$(curl_get $empires_game_id)
198 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
199 TEST_END
200
201 TEST "Redirect of lowercase /gameid at top level"
202 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
203 redirect=$(curl_get $empires_game_id_lower)
204 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
205 TEST_END
206
207 TEST "Redirect of lowercase /empires/gameid"
208 redirect=$(curl_get empires/$empires_game_id_lower)
209 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
210 TEST_END
211
212 TEST_SECTION "Empires game"
213
214 empires_game_path=empires/$empires_game_id
215
216 TEST_SUBSECTION "Empires game /register"
217
218 empires_register()
219 {
220     curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
221 }
222
223 empires_players_string()
224 {
225     curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
226 }
227
228 empires_characters_string()
229 {
230     curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
231 }
232
233 TEST "Registering a player returns an ID"
234 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
235 test "$carl_id" = "1"
236 TEST_END
237
238 TEST "Registering several more players"
239 empires_register Richard "Bob Hope" > /dev/null
240 empires_register Kevin "Elvis Presley" > /dev/null
241 empires_register Stacy Phineas > /dev/null
242 empires_register David "Red Power Ranger" > /dev/null
243 empires_register Nancy "Audrey Hepburn" > /dev/null
244 bogus_id=$(empires_register Bogus "Mr. Bogus")
245 TEST_END
246
247 TEST 'Verify complete players list (with "Bogus")'
248 players=$(empires_players_string)
249 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
250 TEST_END
251
252 TEST 'Verify complete players list (with "Mr. Bogus")'
253 characters=$(empires_characters_string)
254 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
255 TEST_END
256
257 TEST_SUBSECTION "Empires game /deregister"
258
259 empires_deregister()
260 {
261     curl_post $empires_game_path/deregister/$1
262 }
263
264 TEST "Removing the bogus player"
265 empires_deregister $bogus_id
266 TEST_END
267
268 TEST 'Verify modified players list (w/o "Bogus")"'
269 players=$(empires_players_string)
270 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
271 TEST_END
272
273 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
274 characters=$(empires_characters_string)
275 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
276 TEST_END
277
278 TEST_SUBSECTION "Empires game /capture"
279
280 empires_capture()
281 {
282     curl_post $empires_game_path/capture/$1/$2
283 }
284
285 empires_empires_string()
286 {
287     # Get empires as a compact string (much more compact than JSON)
288     curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
289 }
290
291 TEST "Verify empires before any captures"
292 empires=$(empires_empires_string)
293 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
294 TEST_END
295
296 TEST "Perform some captures"
297 empires_capture 1 2
298 empires_capture 3 5
299 empires_capture 4 6
300 empires_capture 3 4
301 TEST_END
302
303 TEST "Verify empires after captures"
304 empires=$(empires_empires_string)
305 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
306 TEST_END
307
308 TEST_SUBSECTION "Empires game /liberate"
309
310 empires_liberate()
311 {
312     curl_post $empires_game_path/liberate/$1
313 }
314
315 TEST "Liberate a player"
316 empires_liberate 2
317 TEST_END
318
319 TEST "Verify empires after liberate"
320 empires=$(empires_empires_string)
321 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
322 TEST_END
323
324 TEST_SUBSECTION "Empires game /reset"
325
326 empires_reset()
327 {
328     curl_post $empires_game_path/reset
329 }
330
331 TEST "Reset the game"
332 empires_reset
333 TEST_END
334
335 TEST "Verify players is now empty"
336 players=$(empires_players_string)
337 test "$players" = ""
338 TEST_END
339
340 TEST_SECTION "Tic Tac Toe game"
341
342 tictactoe_game_path=tictactoe/$tictactoe_game_id
343
344 tictactoe_profile()
345 {
346     curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-tictactoe"
347 }
348
349 tictactoe_move()
350 {
351     curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .cookie-tictactoe"
352 }
353
354 tictactoe_player_info()
355 {
356     curl_get $tictactoe_game_path/events  "-m 0.1 -b .cookie-tictactoe" 2>&1 \
357         | grep player-info -A 1 \
358         | grep ^data
359 }
360
361 tictactoe_player_name()
362 {
363     curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-tictactoe"
364 }
365
366 tictactoe_player_team()
367 {
368     curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-tictactoe"
369 }
370
371 TEST_SUBSECTION "Tic Tac Toe player-info"
372
373 TEST "Hit LMNO /profile to set name to 'curl'"
374 tictactoe_profile curl
375 TEST_END
376
377 TEST "Verify player-info event reports 'curl' name"
378 result=$(tictactoe_player_info)
379 test "$result" = 'data: {"id":1,"name":"curl","team":""}'
380 TEST_END
381
382 TEST_SUBSECTION "Tic Tac Toe /player"
383
384 TEST "Change name to 'newname'"
385 tictactoe_player_name newname
386 result=$(tictactoe_player_info)
387 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
388 TEST_END
389
390 TEST "Change team to 'X'"
391 tictactoe_player_team X
392 result=$(tictactoe_player_info)
393 test "$result" = 'data: {"id":1,"name":"newname","team":"X"}'
394 TEST_END
395
396 TEST "Change team to 'O'"
397 tictactoe_player_team O
398 result=$(tictactoe_player_info)
399 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
400 TEST_END
401
402 TEST "Verify cannot change team to 'Z'"
403 tictactoe_player_team Z
404 result=$(tictactoe_player_info)
405 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
406 TEST_END
407
408 TEST "Leave current team"
409 tictactoe_player_team ""
410 result=$(tictactoe_player_info)
411 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
412 TEST_END
413
414 TEST_SUBSECTION "Tic Tac Toe /move"
415
416 TEST "First move doesn't require a team"
417 result=$(tictactoe_move 0)
418 test "$result" = '{"legal":true}'
419 TEST_END
420
421 TEST "Second move does require a team"
422 result=$(tictactoe_move 4)
423 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
424 TEST_END
425
426 TEST "Illegal to move when it's not your turn"
427 tictactoe_player_team X
428 result=$(tictactoe_move 4)
429 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
430 TEST_END
431
432 TEST "Legal move to center square"
433 tictactoe_player_team O
434 result=$(tictactoe_move 4)
435 test "$result" = '{"legal":true}'
436 TEST_END
437
438 TEST "Move to center square again is now illegal"
439 tictactoe_player_team X
440 result=$(tictactoe_move 4)
441 test "$result" = '{"legal":false,"message":"Square is already occupied"}'
442 TEST_END
443
444 TEST_SECTION "Empathy game"
445
446 TEST_SUBSECTION "Create a game and register 3 players"
447
448 TEST "Create the game"
449 empathy_game_id=$(new_game empathy)
450 test "$empathy_game_id" != ""
451 TEST_END $empathy_game_id
452
453 empathy_game_path=empathy/$empathy_game_id
454
455 # Usage: empathy_get <player_name> <endpoint> [curl_options]
456 empathy_get()
457 {
458     curl_get_cookie $1 $empathy_game_path/$2 "${3:-}"
459 }
460
461 # Usage: empathy_post <player_name> <endpoint> [data]
462 empathy_post()
463 {
464     curl_post_cookie $1 $empathy_game_path/$2 "${3:-}"
465 }
466
467 # Given a player name as $1 (eg. "empathy_player_activate alice") set both
468 # $1_cookie and $1_pid (that is $alice_cookie and $alice_pid) to
469 # a filename containing a cookie and the PID of a running event-streaming
470 # process.
471 empathy_player_activate()
472 {
473     player="$1"
474     player_cookie=${player}_cookie
475     player_pid=${player}_pid
476
477     eval ${player_cookie}=".cookie-empathy-$player"
478     curl_put /profile "{ \"nickname\": \"$player\" }" "-c ${!player_cookie}"
479     empathy_get $player events >/dev/null 2>&1 &
480     eval ${player_pid}=$!
481     empathy_players+=($player)
482 }
483
484 empathy_player_reactivate()
485 {
486     player="$1"
487     player_pid=${player}_pid
488
489     empathy_get $player events >/dev/null 2>&1 &
490     eval ${player_pid}=$!
491 }
492
493 # Usage: empathy_player_deactivate <player_name>
494 empathy_player_deactivate()
495 {
496     player="$1"
497     player_pid=${player}_pid
498     if [ "${!player_pid}" != "" ]; then
499         pkill -P ${!player_pid}
500     fi
501     eval ${player_pid}=""
502 }
503
504 empathy_deactivate_all()
505 {
506     for player in ${empathy_players[*]}; do
507         empathy_player_deactivate $player
508     done
509 }
510
511 # Pulls a single named event out of the empathy event stream
512 #
513 # Usage: empathy_get_event <player_name> <event_name>
514 empathy_get_event()
515 {
516     empathy_get $1 events "-m 0.1" 2>&1 \
517         | grep "^event: $2" -A 1 \
518         | grep ^data: \
519         | sed -e 's,^data: *,,'
520 }
521
522 # Usage: empathy_player_name <player_name>
523 empathy_player_name()
524 {
525     empathy_get_event $1 player-info | jq -r .name
526 }
527
528 TEST "Set 'alice' in session"
529 empathy_player_activate alice
530 test "$alice_cookie" = ".cookie-empathy-alice"
531 TEST_END
532
533 TEST "Register alice and verify name"
534 result=$(empathy_player_name alice)
535 test "$result" = "alice"
536 TEST_END
537
538 TEST "Register bob"
539 empathy_player_activate bob
540 result=$(empathy_player_name bob)
541 test "$result" = "bob"
542 TEST_END
543
544 TEST "Register charlie"
545 empathy_player_activate charlie
546 result=$(empathy_player_name charlie)
547 test "$result" = "charlie"
548 TEST_END
549
550 TEST_SUBSECTION "Category selection"
551
552 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
553 empathy_submit_prompt()
554 {
555     empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
556 }
557
558 TEST "Huge numbers are rejected"
559 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
560 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
561 TEST_END
562
563 TEST "Submit a category"
564 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
565 test "$prompt_id" = "1"
566 TEST_END
567
568 # Usage: empathy_vote <player_name> <prompt_id>
569 empathy_vote()
570 {
571     empathy_post $1 vote/$2
572 }
573
574 TEST "Vote on this category"
575 empathy_vote alice $prompt_id
576 test "$?" = "0"
577 TEST_END
578
579 # Usage: empathy_start <player_name> <prompt_id>
580 empathy_start()
581 {
582     empathy_post $1 start/$2
583 }
584
585 TEST "Start the game with this category"
586 empathy_start alice $prompt_id
587 test "$?" = "0"
588 TEST_END
589
590 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
591 empathy_answer()
592 {
593     empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
594 }
595
596 TEST_SUBSECTION "Submitting answers"
597
598 TEST "Submit from a non-player fails"
599 bogus_cookie=/dev/null
600 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
601 test "$result" = '{"valid":false,"message":"Player not found"}'
602 TEST_END
603
604 TEST "Submit from alice succeeds"
605 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
606 test "$result" = '{"valid":true}'
607 TEST_END
608
609 TEST "Submit from bob succeeds"
610 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
611 test "$result" = '{"valid":true}'
612 TEST_END
613
614 # Usage: empathy_ambiguities <player_name>
615 empathy_ambiguities()
616 {
617     empathy_get_event $1 game-state | jq .ambiguities
618 }
619
620 TEST "Judging hasn't started with player unsubmitted"
621 result=$(echo $(empathy_ambiguities alice))
622 test "$result" = "null"
623 TEST_END
624
625 TEST "Submit from charlie succeeds"
626 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
627 test "$result" = '{"valid":true}'
628 TEST_END
629
630 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
631
632 TEST "Judging already started"
633 result=$(echo $(empathy_ambiguities alice))
634 test "$result" != "null"
635 TEST_END
636
637 TEST_SUBSECTION "Judging answers"
638
639 # Usage: empathy_ambiguities_list <player_name>
640 empathy_ambiguities_list()
641 {
642     empathy_get_event $1 game-state | jq .ambiguities[]
643 }
644
645 TEST "Received all unique words"
646 # echo here is to strip newlines
647 result=$(echo $(empathy_ambiguities_list alice))
648 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
649 TEST_END
650
651 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
652 empathy_judged()
653 {
654     empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
655 }
656
657 TEST "Submit word groups from alice"
658 result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
659 test "$result" = '{"valid":true}'
660 TEST_END
661
662 TEST "Submit word groups from bob"
663 result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
664 test "$result" = '{"valid":true}'
665 TEST_END
666
667 # Usage: empathy_scores <player_name>
668 empathy_scores()
669 {
670     empathy_get_event $1 game-state | jq .scores
671 }
672
673 TEST "Scoring hasn't started with player unsubmitted"
674 result=$(echo $(empathy_scores alice))
675 test "$result" = "null"
676 TEST_END
677
678 TEST "Submit word groups from charlie"
679 result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
680 test "$result" = '{"valid":true}'
681 TEST_END
682
683 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
684
685 TEST "Scoring already started"
686 result=$(echo $(empathy_scores alice))
687 test "$result" != "null"
688 TEST_END
689
690 # Usage: empathy_scores_names_numbers <player_name>
691 empathy_scores_names_numbers()
692 {
693     empathy_get_event $1 game-state | jq '.scores.scores[]|.players[],.score'
694 }
695
696 TEST_SUBSECTION "Scoring"
697
698 TEST "Verify final scores as expected"
699 # echo here is to strip newlines
700 result=$(echo $(empathy_scores_names_numbers alice))
701 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
702 TEST_END
703
704 # Usage: empathy_words_submitted <player_name>
705 empathy_words_submitted()
706 {
707     empathy_get_event $1 game-state | jq '.scores.words[].word'
708 }
709
710 TEST "Verify final list of words submitted"
711 # echo here is to strip newlines
712 result=$(echo $(empathy_words_submitted alice))
713 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
714 TEST_END
715
716 TEST_SUBSECTION "New game (using voting to advance phases)"
717
718 empathy_reset()
719 {
720     curl_post $empathy_game_path/reset
721 }
722
723 TEST "Any post to /reset resets the game"
724 empathy_reset
725 test "$?" = "0"
726 TEST_END
727
728 TEST "Verify scoring is over"
729 result=$(echo $(empathy_scores alice))
730 test "$result" = "null"
731 TEST_END
732
733 # Usage: empathy_answering <player_name> <prompt_id>
734 empathy_answering()
735 {
736     empathy_post $1 answering/$2
737 }
738
739 TEST "Start 4-player game, 3 submissions"
740 empathy_player_activate dale
741 result=$(empathy_player_name dale)
742 test "$result" = "dale"
743 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
744 empathy_start alice $prompt_id
745 empathy_answer alice   $prompt_id '"I",    "love", "you"' >/dev/null
746 empathy_answer bob     $prompt_id '"I",    "love", "food"' >/dev/null
747 empathy_answer charlie $prompt_id '"food", "is",   "good"' >/dev/null
748 result=$(empathy_answering dale $prompt_id)
749 test "$result" = '{"valid":true}'
750 TEST_END
751
752 TEST "Judging hasn't started with player unsubmitted"
753 result=$(echo $(empathy_ambiguities alice))
754 test "$result" = "null"
755 TEST_END
756
757 # Usage: empathy_end_answers <player_name> <prompt_id>
758 empathy_end_answers()
759 {
760     empathy_post $1 end-answers/$2
761 }
762
763 TEST "Minority of players vote to end answering"
764 empathy_end_answers alice $prompt_id
765 empathy_end_answers bob $prompt_id
766 test "$?" = "0"
767 TEST_END
768
769 TEST "Judging still hasn't started"
770 result=$(echo $(empathy_ambiguities alice))
771 test "$result" = "null"
772 TEST_END
773
774 TEST "Majority of players vote to end answering"
775 empathy_end_answers charlie $prompt_id
776 test "$?" = "0"
777 TEST_END
778
779 TEST "Judging has now started"
780 result=$(echo $(empathy_ambiguities alice))
781 test "$result" != "null"
782 TEST_END
783
784 # Usage: empathy_players_judging <player_name>
785 empathy_players_judging()
786 {
787     empathy_get_event $1 game-state | jq .players_judging[]
788 }
789
790 TEST "Verify active players listed as judging"
791 # echo here is to strip newlines
792 result=$(echo $(empathy_players_judging alice))
793 test "$result" = '"alice" "bob" "charlie"'
794 TEST_END
795
796 TEST "Submit word groups from majority"
797 empathy_judged alice $prompt_id '[]' >/dev/null
798 result=$(empathy_judged bob $prompt_id '[]')
799 test "$result" = '{"valid":true}'
800 TEST_END
801
802 TEST "Scoring hasn't started with player unsubmitted"
803 result=$(echo $(empathy_scores alice))
804 test "$result" = "null"
805 TEST_END
806
807 # Usage: empathy_end_judging <player_name> <prompt_id>
808 empathy_end_judging()
809 {
810     empathy_post $1 end-judging/$2
811 }
812
813 TEST "Minority of players vote to end judging"
814 empathy_end_judging alice $prompt_id
815 test "$?" = "0"
816 TEST_END
817
818 TEST "Scoring still hasn't started"
819 result=$(echo $(empathy_scores alice))
820 test "$result" = "null"
821 TEST_END
822
823 TEST "Majority of players vote to end judging"
824 empathy_end_judging bob $prompt_id
825 test "$?" = "0"
826 TEST_END
827
828 TEST "Scoring has now started"
829 result=$(echo $(empathy_scores alice))
830 test "$result" != "null"
831 TEST_END
832
833 TEST_SUBSECTION "New game (no voting needed when all answered players judge)"
834
835 TEST "Start 4-player game, 3 submissions"
836 empathy_reset
837 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
838 empathy_start alice $prompt_id
839 empathy_answer alice   $prompt_id '"truth"' >/dev/null
840 empathy_answer bob     $prompt_id '"truth"' >/dev/null
841 empathy_answer charlie $prompt_id '"dare"' >/dev/null
842 empathy_end_answers alice $prompt_id
843 empathy_end_answers bob $prompt_id
844 empathy_end_answers charlie $prompt_id
845 test "$?" = "0"
846 TEST_END
847
848 TEST "Submit word groups from 2 players"
849 empathy_judged alice $prompt_id '[]' >/dev/null
850 result=$(empathy_judged bob $prompt_id '[]')
851 test "$result" = '{"valid":true}'
852 TEST_END
853
854 TEST "Scoring hasn't started with player unsubmitted"
855 result=$(echo $(empathy_scores alice))
856 test "$result" = "null"
857 TEST_END
858
859 TEST "Submit word groups from a non-answering player"
860 result=$(empathy_judged dale $prompt_id '[]')
861 test "$result" = '{"valid":true}'
862 TEST_END
863
864 TEST "Scoring still hasn't started"
865 result=$(echo $(empathy_scores alice))
866 test "$result" = "null"
867 TEST_END
868
869 TEST "Submit word groups from last answering player"
870 result=$(empathy_judged charlie $prompt_id '[]')
871 test "$result" = '{"valid":true}'
872 TEST_END
873
874 TEST "Scoring has now started"
875 result=$(echo $(empathy_scores alice))
876 test "$result" != "null"
877 TEST_END
878
879 TEST_SUBSECTION "Non players don't affect judging requirements"
880
881 TEST "Start 2-player game with 6 registered players"
882 empathy_reset
883 empathy_player_activate eric
884 empathy_player_activate fred
885 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
886 empathy_start alice $prompt_id
887 empathy_answer alice $prompt_id '"truth"' >/dev/null
888 empathy_answer bob   $prompt_id '"true"' >/dev/null
889 empathy_end_answers alice $prompt_id
890 empathy_end_answers bob $prompt_id
891 test "$?" = "0"
892 TEST_END
893
894 TEST "1 player votes for a match"
895 empathy_judged alice $prompt_id '[["truth","true"]]' >/dev/null
896 result=$(empathy_judged bob $prompt_id '[]')
897 test "$result" = '{"valid":true}'
898 TEST_END
899
900 TEST "Verify the match passed the vote"
901 # echo here is to strip newlines
902 result=$(echo $(empathy_scores_names_numbers alice))
903 test "$result" = '"alice" "bob" 2 "charlie" "dale" "eric" "fred" 0'
904 TEST_END
905
906 echo ""
907 echo "NOTE: Slow tests ahead!"
908 echo "If you are impatient and somehow \"know\" you don't care about the"
909 echo "tests below then you can interrupt the test suite with Control-C"
910 echo "to get a summary report on the tests that have already been run."
911
912 TEST_SUBSECTION "Inactive players don't appear in scores"
913
914 TEST "Start 2-player game with 6 registered players"
915 empathy_reset
916 prompt_id=$(empathy_submit_prompt alice 4 "1 best pet" | jq .id)
917 empathy_start alice $prompt_id
918 empathy_answer alice $prompt_id '"cats"' >/dev/null
919 empathy_answer bob   $prompt_id '"dogs"' >/dev/null
920 empathy_end_answers alice $prompt_id
921 empathy_end_answers bob $prompt_id
922 test "$?" = "0"
923 TEST_END
924
925 TEST "Deactivate 3 players"
926 empathy_player_deactivate dale
927 empathy_player_deactivate eric
928 empathy_player_deactivate fred
929 sleep 30
930 test "$?" = "0"
931 TEST_END
932
933 TEST "Finish game with 2 active players"
934 empathy_judged alice $prompt_id '[]' >/dev/null
935 result=$(empathy_judged bob $prompt_id '[]')
936 test "$result" = '{"valid":true}'
937 TEST_END
938
939 TEST "Verify scores don't include inactive players"
940 # echo here is to strip newlines
941 result=$(echo $(empathy_scores_names_numbers alice))
942 test "$result" = '"alice" "bob" 1 "charlie" 0'
943 TEST_END
944
945 TEST_SUBSECTION "Deactivated players don't block future game phase advances"
946
947 TEST "New 3-player game, 2 submit right away"
948 empathy_reset
949 prompt_id=$(empathy_submit_prompt charlie 4 "2 legit 2 quit" | jq .id)
950 empathy_start alice $prompt_id
951 empathy_answer alice $prompt_id '"what", "gives?"' >/dev/null
952 empathy_answer bob   $prompt_id '"so", "confused"' >/dev/null
953 test "$?" = "0"
954 TEST_END
955
956 TEST "Judging hasn't started with player unsubmitted"
957 result=$(echo $(empathy_ambiguities alice))
958 test "$result" = "null"
959 TEST_END
960
961 TEST "Final active player submits"
962 result=$(empathy_answer charlie $prompt_id '"best", "category"')
963 test "$result" = '{"valid":true}'
964 TEST_END
965
966 TEST "Judging has started (don't need inactive players)"
967 result=$(echo $(empathy_ambiguities alice))
968 test "$result" != "null"
969 TEST_END
970
971 TEST "Submit word groups from 2 players"
972 empathy_judged alice $prompt_id '[]' >/dev/null
973 result=$(empathy_judged bob $prompt_id '[]')
974 test "$result" = '{"valid":true}'
975 TEST_END
976
977 TEST "Scoring hasn't started with player unsubmitted"
978 result=$(echo $(empathy_scores alice))
979 test "$result" = "null"
980 TEST_END
981
982 TEST "Submit word groups from last answering player"
983 result=$(empathy_judged charlie $prompt_id '[]')
984 test "$result" = '{"valid":true}'
985 TEST_END
986
987 TEST "Scoring has now started"
988 result=$(echo $(empathy_scores alice))
989 test "$result" != "null"
990 TEST_END
991
992 TEST_SUBSECTION "Reactivated player is fully active"
993
994 TEST "The dale player is currently deactivated"
995 test "$dale_pid" = ""
996 TEST_END
997
998 TEST "Reactivate dale"
999 empathy_player_reactivate dale
1000 test "$dale_pid" != ""
1001 TEST_END
1002
1003 TEST "New 4-player game, 3 submit right away"
1004 empathy_reset
1005 prompt_id=$(empathy_submit_prompt alice 1 "favorite letter" | jq .id)
1006 empathy_start alice $prompt_id
1007 empathy_answer alice   $prompt_id '"A"' >/dev/null
1008 empathy_answer bob     $prompt_id '"B"' >/dev/null
1009 empathy_answer charlie $prompt_id '"C"' >/dev/null
1010 test "$?" = "0"
1011 TEST_END
1012
1013 TEST "Judging hasn't started with player unsubmitted"
1014 result=$(echo $(empathy_ambiguities alice))
1015 test "$result" = "null"
1016 TEST_END
1017
1018 TEST "Final active player submits"
1019 result=$(empathy_answer dale $prompt_id '"D"')
1020 test "$result" = '{"valid":true}'
1021 TEST_END
1022
1023 TEST "Judging has started now"
1024 result=$(echo $(empathy_ambiguities alice))
1025 test "$result" != "null"
1026 TEST_END
1027
1028 cleanup_and_report