]> git.cworth.org Git - empires-server/blob - test
Add a new empathy_player_deactivate function
[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 }
471
472 # Usage: empathy_player_deactivate <player_name>
473 empathy_player_deactivate()
474 {
475     player="$1"
476     player_pid=${player}_pid
477     pkill -P ${!player_pid}
478 }
479
480 # Pulls a single named event out of the empathy event stream
481 #
482 # Usage: empathy_get_event <player_name> <event_name>
483 empathy_get_event()
484 {
485     empathy_get $1 events "-m 0.1" 2>&1 \
486         | grep "^event: $2" -A 1 \
487         | grep ^data: \
488         | sed -e 's,^data: *,,'
489 }
490
491 # Usage: empathy_player_name <player_name>
492 empathy_player_name()
493 {
494     empathy_get_event $1 player-info | jq -r .name
495 }
496
497 TEST "Set 'alice' in session"
498 empathy_player_activate alice
499 test "$alice_cookie" = ".cookie-empathy-alice"
500 TEST_END
501
502 TEST "Register alice and verify name"
503 result=$(empathy_player_name alice)
504 test "$result" = "alice"
505 TEST_END
506
507 TEST "Register bob"
508 empathy_player_activate bob
509 result=$(empathy_player_name bob)
510 test "$result" = "bob"
511 TEST_END
512
513 TEST "Register charlie"
514 empathy_player_activate charlie
515 result=$(empathy_player_name charlie)
516 test "$result" = "charlie"
517 TEST_END
518
519 TEST_SUBSECTION "Category selection"
520
521 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
522 empathy_submit_prompt()
523 {
524     empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
525 }
526
527 TEST "Huge numbers are rejected"
528 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
529 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
530 TEST_END
531
532 TEST "Submit a category"
533 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
534 test "$prompt_id" = "1"
535 TEST_END
536
537 # Usage: empathy_vote <player_name> <prompt_id>
538 empathy_vote()
539 {
540     empathy_post $1 vote/$2
541 }
542
543 TEST "Vote on this category"
544 empathy_vote alice $prompt_id
545 test "$?" = "0"
546 TEST_END
547
548 # Usage: empathy_start <player_name> <prompt_id>
549 empathy_start()
550 {
551     empathy_post $1 start/$2
552 }
553
554 TEST "Start the game with this category"
555 empathy_start alice $prompt_id
556 test "$?" = "0"
557 TEST_END
558
559 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
560 empathy_answer()
561 {
562     empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
563 }
564
565 TEST_SUBSECTION "Submitting answers"
566
567 TEST "Submit from a non-player fails"
568 bogus_cookie=/dev/null
569 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
570 test "$result" = '{"valid":false,"message":"Player not found"}'
571 TEST_END
572
573 TEST "Submit from alice succeeds"
574 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
575 test "$result" = '{"valid":true}'
576 TEST_END
577
578 TEST "Submit from bob succeeds"
579 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
580 test "$result" = '{"valid":true}'
581 TEST_END
582
583 # Usage: empathy_ambiguities <player_name>
584 empathy_ambiguities()
585 {
586     empathy_get_event $1 game-state | jq .ambiguities
587 }
588
589 TEST "Judging hasn't started with player unsubmitted"
590 result=$(echo $(empathy_ambiguities alice))
591 test "$result" = "null"
592 TEST_END
593
594 TEST "Submit from charlie succeeds"
595 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
596 test "$result" = '{"valid":true}'
597 TEST_END
598
599 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
600
601 TEST "Judging already started"
602 result=$(echo $(empathy_ambiguities alice))
603 test "$result" != "null"
604 TEST_END
605
606 TEST_SUBSECTION "Judging answers"
607
608 # Usage: empathy_ambiguities_list <player_name>
609 empathy_ambiguities_list()
610 {
611     empathy_get_event $1 game-state | jq .ambiguities[]
612 }
613
614 TEST "Received all unique words"
615 # echo here is to strip newlines
616 result=$(echo $(empathy_ambiguities_list alice))
617 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
618 TEST_END
619
620 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
621 empathy_judged()
622 {
623     empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
624 }
625
626 TEST "Submit word groups from alice"
627 result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
628 test "$result" = '{"valid":true}'
629 TEST_END
630
631 TEST "Submit word groups from bob"
632 result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
633 test "$result" = '{"valid":true}'
634 TEST_END
635
636 # Usage: empathy_scores <player_name>
637 empathy_scores()
638 {
639     empathy_get_event $1 game-state | jq .scores
640 }
641
642 TEST "Scoring hasn't started with player unsubmitted"
643 result=$(echo $(empathy_scores alice))
644 test "$result" = "null"
645 TEST_END
646
647 TEST "Submit word groups from charlie"
648 result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
649 test "$result" = '{"valid":true}'
650 TEST_END
651
652 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
653
654 TEST "Scoring already started"
655 result=$(echo $(empathy_scores alice))
656 test "$result" != "null"
657 TEST_END
658
659 # Usage: empathy_scores_names_numbers <player_name>
660 empathy_scores_names_numbers()
661 {
662     empathy_get_event $1 game-state | jq '.scores.scores[]|.player,.score'
663 }
664
665 TEST_SUBSECTION "Scoring"
666
667 TEST "Verify final scores as expected"
668 # echo here is to strip newlines
669 result=$(echo $(empathy_scores_names_numbers alice))
670 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
671 TEST_END
672
673 # Usage: empathy_words_submitted <player_name>
674 empathy_words_submitted()
675 {
676     empathy_get_event $1 game-state | jq '.scores.words[].word'
677 }
678
679 TEST "Verify final list of words submitted"
680 # echo here is to strip newlines
681 result=$(echo $(empathy_words_submitted alice))
682 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
683 TEST_END
684
685 TEST_SUBSECTION "New game (using voting to advance phases)"
686
687 empathy_reset()
688 {
689     curl_post $empathy_game_path/reset
690 }
691
692 TEST "Any post to /reset resets the game"
693 empathy_reset
694 test "$?" = "0"
695 TEST_END
696
697 TEST "Verify scoring is over"
698 result=$(echo $(empathy_scores alice))
699 test "$result" = "null"
700 TEST_END
701
702 # Usage: empathy_answering <player_name> <prompt_id>
703 empathy_answering()
704 {
705     empathy_post $1 answering/$2
706 }
707
708 TEST "Start 4-player game, 3 submissions"
709 empathy_player_activate dale
710 result=$(empathy_player_name dale)
711 test "$result" = "dale"
712 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
713 empathy_start alice $prompt_id
714 empathy_answer alice   $prompt_id '"I",    "love", "you"' >/dev/null
715 empathy_answer bob     $prompt_id '"I",    "love", "food"' >/dev/null
716 empathy_answer charlie $prompt_id '"food", "is",   "good"' >/dev/null
717 result=$(empathy_answering dale $prompt_id)
718 test "$result" = '{"valid":true}'
719 TEST_END
720
721 TEST "Judging hasn't started with player unsubmitted"
722 result=$(echo $(empathy_ambiguities alice))
723 test "$result" = "null"
724 TEST_END
725
726 # Usage: empathy_end_answers <player_name> <prompt_id>
727 empathy_end_answers()
728 {
729     empathy_post $1 end-answers/$2
730 }
731
732 TEST "Minority of players vote to end answering"
733 empathy_end_answers alice $prompt_id
734 empathy_end_answers bob $prompt_id
735 test "$?" = "0"
736 TEST_END
737
738 TEST "Judging still hasn't started"
739 result=$(echo $(empathy_ambiguities alice))
740 test "$result" = "null"
741 TEST_END
742
743 TEST "Majority of players vote to end answering"
744 empathy_end_answers charlie $prompt_id
745 test "$?" = "0"
746 TEST_END
747
748 TEST "Judging has now started"
749 result=$(echo $(empathy_ambiguities alice))
750 test "$result" != "null"
751 TEST_END
752
753 # Usage: empathy_players_judging <player_name>
754 empathy_players_judging()
755 {
756     empathy_get_event $1 game-state | jq .players_judging[]
757 }
758
759 TEST "Verify active players listed as judging"
760 # echo here is to strip newlines
761 result=$(echo $(empathy_players_judging alice))
762 test "$result" = '"alice" "bob" "charlie"'
763 TEST_END
764
765 TEST "Submit word groups from majority"
766 empathy_judged alice $prompt_id '[]' >/dev/null
767 result=$(empathy_judged bob $prompt_id '[]')
768 test "$result" = '{"valid":true}'
769 TEST_END
770
771 TEST "Scoring hasn't started with player unsubmitted"
772 result=$(echo $(empathy_scores alice))
773 test "$result" = "null"
774 TEST_END
775
776 # Usage: empathy_end_judging <player_name> <prompt_id>
777 empathy_end_judging()
778 {
779     empathy_post $1 end-judging/$2
780 }
781
782 TEST "Minority of players vote to end judging"
783 empathy_end_judging alice $prompt_id
784 test "$?" = "0"
785 TEST_END
786
787 TEST "Scoring still hasn't started"
788 result=$(echo $(empathy_scores alice))
789 test "$result" = "null"
790 TEST_END
791
792 TEST "Majority of players vote to end judging"
793 empathy_end_judging bob $prompt_id
794 test "$?" = "0"
795 TEST_END
796
797 TEST "Scoring has now started"
798 result=$(echo $(empathy_scores alice))
799 test "$result" != "null"
800 TEST_END
801
802 TEST_SUBSECTION "New game (no voting needed when all answered players judge)"
803
804 TEST "Start 4-player game, 3 submissions"
805 empathy_reset
806 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
807 empathy_start alice $prompt_id
808 empathy_answer alice   $prompt_id '"truth"' >/dev/null
809 empathy_answer bob     $prompt_id '"truth"' >/dev/null
810 empathy_answer charlie $prompt_id '"dare"' >/dev/null
811 empathy_end_answers alice $prompt_id
812 empathy_end_answers bob $prompt_id
813 empathy_end_answers charlie $prompt_id
814 test "$?" = "0"
815 TEST_END
816
817 TEST "Submit word groups from 2 players"
818 empathy_judged alice $prompt_id '[]' >/dev/null
819 result=$(empathy_judged bob $prompt_id '[]')
820 test "$result" = '{"valid":true}'
821 TEST_END
822
823 TEST "Scoring hasn't started with player unsubmitted"
824 result=$(echo $(empathy_scores alice))
825 test "$result" = "null"
826 TEST_END
827
828 TEST "Submit word groups from last answering player"
829 result=$(empathy_judged charlie $prompt_id '[]')
830 test "$result" = '{"valid":true}'
831 TEST_END
832
833 TEST "Scoring has now started"
834 result=$(echo $(empathy_scores alice))
835 test "$result" != "null"
836 TEST_END
837
838 TEST_SUBSECTION "Non players don't affect judging requirements"
839
840 TEST "Start 2-player game with 6 registered players"
841 empathy_reset
842 empathy_player_activate eric
843 empathy_player_activate fred
844 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
845 empathy_start alice $prompt_id
846 empathy_answer alice $prompt_id '"truth"' >/dev/null
847 empathy_answer bob   $prompt_id '"true"' >/dev/null
848 empathy_end_answers alice $prompt_id
849 empathy_end_answers bob $prompt_id
850 test "$?" = "0"
851 TEST_END
852
853 TEST "1 player votes for a match"
854 empathy_judged alice $prompt_id '[["truth","true"]]' >/dev/null
855 result=$(empathy_judged bob $prompt_id '[]')
856 test "$result" = '{"valid":true}'
857 TEST_END
858
859 TEST "Verify the match passed the vote"
860 # echo here is to strip newlines
861 result=$(echo $(empathy_scores_names_numbers alice))
862 test "$result" = '"alice" 2 "bob" 2 "charlie" 0 "dale" 0 "eric" 0 "fred" 0'
863 TEST_END
864
865 empathy_player_deactivate alice
866 empathy_player_deactivate bob
867 empathy_player_deactivate charlie
868 empathy_player_deactivate dale
869 empathy_player_deactivate eric
870 empathy_player_deactivate fred
871
872 TEST_REPORT