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