3 # Arrange for some cleanup to be executed if the user interrupts the
4 # test sutie (for example, by pressing ControlC at the controlling
7 empathy_deactivate_all >/dev/null 2>&1
11 trap cleanup_and_report INT
15 echo "Usage:$0 <URL-to-test>"
19 echo "Error: No test URL given." >&2
26 CURL="curl --silent --show-error"
32 echo $1 | sed -e "s/./$2/g"
48 printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
49 (( tests_total++ )) || true
52 # Result of test depends on the exit status of last command
58 (( tests_failed++ )) || true
62 # If we got an argument, append it after test result
70 # Print report of all previous test results
78 if [ "$tests_failed" == "" ]; then
79 echo "All $tests_total tests passed."
83 echo "$tests_failed of $tests_total tests failed."
89 # Does a string contain a regular expression pattern
93 # contains "All's well that ends well" "s.well"
99 # POST to a URL endpoint with optional JSON data
103 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
106 $CURL ${3:-} -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
109 # POST to a URL endpoint with optional JSON data using a cookie
113 # curl_post_cookie <name> <ENDPOINT> [data] [CURL_OPTIONS]
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
121 curl_post $2 "${3:-}" "-b ${!cookie} ${4:-}"
124 # PUT to a URL endpoint with optional JSON data
128 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
131 $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
134 # GET from a URL endpoint
138 # curl_get <ENDPOINT> [CURL_OPTIONS]
144 # GET from a URL endpoint using a cookie
148 # curl_get_cookie <name> <ENDPOINT> [CURL_OPTIONS]
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
156 curl_get $2 "-b ${!cookie} ${3:-}"
159 # Create a new game of the specified engine type
166 curl_post new/$1 | jq -r .
169 TEST_SECTION "LMNO (super-site for games)"
171 TEST_SUBSECTION "Testing home page"
172 home_page=$($CURL $URL)
174 TEST "Contains 'Join Game'"
175 contains "$home_page" "Join Game"
178 TEST "Contains 'Host a new game'"
179 contains "$home_page" "Host a new game"
182 TEST_SUBSECTION "Creating some new games"
185 empires_game_id=$(new_game empires)
186 test "$empires_game_id" != ""
187 TEST_END $empires_game_id
190 tictactoe_game_id=$(new_game tictactoe)
191 test "$tictactoe_game_id" != ""
192 TEST_END $tictactoe_game_id
194 TEST_SUBSECTION "Test redirects"
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/"
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/"
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/"
212 TEST_SECTION "Empires game"
214 empires_game_path=empires/$empires_game_id
216 TEST_SUBSECTION "Empires game /register"
220 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
223 empires_players_string()
225 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
228 empires_characters_string()
230 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
233 TEST "Registering a player returns an ID"
234 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
235 test "$carl_id" = "1"
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")
247 TEST 'Verify complete players list (with "Bogus")'
248 players=$(empires_players_string)
249 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
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,"
257 TEST_SUBSECTION "Empires game /deregister"
261 curl_post $empires_game_path/deregister/$1
264 TEST "Removing the bogus player"
265 empires_deregister $bogus_id
268 TEST 'Verify modified players list (w/o "Bogus")"'
269 players=$(empires_players_string)
270 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
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,"
278 TEST_SUBSECTION "Empires game /capture"
282 curl_post $empires_game_path/capture/$1/$2
285 empires_empires_string()
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' ','
291 TEST "Verify empires before any captures"
292 empires=$(empires_empires_string)
293 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
296 TEST "Perform some captures"
303 TEST "Verify empires after captures"
304 empires=$(empires_empires_string)
305 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
308 TEST_SUBSECTION "Empires game /liberate"
312 curl_post $empires_game_path/liberate/$1
315 TEST "Liberate a player"
319 TEST "Verify empires after liberate"
320 empires=$(empires_empires_string)
321 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
324 TEST_SUBSECTION "Empires game /reset"
328 curl_post $empires_game_path/reset
331 TEST "Reset the game"
335 TEST "Verify players is now empty"
336 players=$(empires_players_string)
340 TEST_SECTION "Tic Tac Toe game"
342 tictactoe_game_path=tictactoe/$tictactoe_game_id
346 curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-tictactoe"
351 curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .cookie-tictactoe"
354 tictactoe_player_info()
356 curl_get $tictactoe_game_path/events "-m 0.1 -b .cookie-tictactoe" 2>&1 \
357 | grep player-info -A 1 \
361 tictactoe_player_name()
363 curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-tictactoe"
366 tictactoe_player_team()
368 curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-tictactoe"
371 TEST_SUBSECTION "Tic Tac Toe player-info"
373 TEST "Hit LMNO /profile to set name to 'curl'"
374 tictactoe_profile curl
377 TEST "Verify player-info event reports 'curl' name"
378 result=$(tictactoe_player_info)
379 test "$result" = 'data: {"id":1,"name":"curl","team":""}'
382 TEST_SUBSECTION "Tic Tac Toe /player"
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":""}'
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"}'
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"}'
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"}'
408 TEST "Leave current team"
409 tictactoe_player_team ""
410 result=$(tictactoe_player_info)
411 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
414 TEST_SUBSECTION "Tic Tac Toe /move"
416 TEST "First move doesn't require a team"
417 result=$(tictactoe_move 0)
418 test "$result" = '{"legal":true}'
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"}'
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"}'
432 TEST "Legal move to center square"
433 tictactoe_player_team O
434 result=$(tictactoe_move 4)
435 test "$result" = '{"legal":true}'
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"}'
444 TEST_SECTION "Empathy game"
446 TEST_SUBSECTION "Create a game and register 3 players"
448 TEST "Create the game"
449 empathy_game_id=$(new_game empathy)
450 test "$empathy_game_id" != ""
451 TEST_END $empathy_game_id
453 empathy_game_path=empathy/$empathy_game_id
455 # Usage: empathy_get <player_name> <endpoint> [curl_options]
458 curl_get_cookie $1 $empathy_game_path/$2 "${3:-}"
461 # Usage: empathy_post <player_name> <endpoint> [data]
464 curl_post_cookie $1 $empathy_game_path/$2 "${3:-}"
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
471 empathy_player_activate()
474 player_cookie=${player}_cookie
475 player_pid=${player}_pid
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)
484 # Usage: empathy_player_deactivate <player_name>
485 empathy_player_deactivate()
488 player_pid=${player}_pid
489 if [ "${!player_pid}" != "" ]; then
490 pkill -P ${!player_pid}
492 eval ${player_pid}=""
495 empathy_deactivate_all()
497 for player in ${empathy_players[*]}; do
498 empathy_player_deactivate $player
502 # Pulls a single named event out of the empathy event stream
504 # Usage: empathy_get_event <player_name> <event_name>
507 empathy_get $1 events "-m 0.1" 2>&1 \
508 | grep "^event: $2" -A 1 \
510 | sed -e 's,^data: *,,'
513 # Usage: empathy_player_name <player_name>
514 empathy_player_name()
516 empathy_get_event $1 player-info | jq -r .name
519 TEST "Set 'alice' in session"
520 empathy_player_activate alice
521 test "$alice_cookie" = ".cookie-empathy-alice"
524 TEST "Register alice and verify name"
525 result=$(empathy_player_name alice)
526 test "$result" = "alice"
530 empathy_player_activate bob
531 result=$(empathy_player_name bob)
532 test "$result" = "bob"
535 TEST "Register charlie"
536 empathy_player_activate charlie
537 result=$(empathy_player_name charlie)
538 test "$result" = "charlie"
541 TEST_SUBSECTION "Category selection"
543 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
544 empathy_submit_prompt()
546 empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
549 TEST "Huge numbers are rejected"
550 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
551 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
554 TEST "Submit a category"
555 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
556 test "$prompt_id" = "1"
559 # Usage: empathy_vote <player_name> <prompt_id>
562 empathy_post $1 vote/$2
565 TEST "Vote on this category"
566 empathy_vote alice $prompt_id
570 # Usage: empathy_start <player_name> <prompt_id>
573 empathy_post $1 start/$2
576 TEST "Start the game with this category"
577 empathy_start alice $prompt_id
581 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
584 empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
587 TEST_SUBSECTION "Submitting answers"
589 TEST "Submit from a non-player fails"
590 bogus_cookie=/dev/null
591 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
592 test "$result" = '{"valid":false,"message":"Player not found"}'
595 TEST "Submit from alice succeeds"
596 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
597 test "$result" = '{"valid":true}'
600 TEST "Submit from bob succeeds"
601 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
602 test "$result" = '{"valid":true}'
605 # Usage: empathy_ambiguities <player_name>
606 empathy_ambiguities()
608 empathy_get_event $1 game-state | jq .ambiguities
611 TEST "Judging hasn't started with player unsubmitted"
612 result=$(echo $(empathy_ambiguities alice))
613 test "$result" = "null"
616 TEST "Submit from charlie succeeds"
617 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
618 test "$result" = '{"valid":true}'
621 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
623 TEST "Judging already started"
624 result=$(echo $(empathy_ambiguities alice))
625 test "$result" != "null"
628 TEST_SUBSECTION "Judging answers"
630 # Usage: empathy_ambiguities_list <player_name>
631 empathy_ambiguities_list()
633 empathy_get_event $1 game-state | jq .ambiguities[]
636 TEST "Received all unique words"
637 # echo here is to strip newlines
638 result=$(echo $(empathy_ambiguities_list alice))
639 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
642 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
645 empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
648 TEST "Submit word groups from alice"
649 result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
650 test "$result" = '{"valid":true}'
653 TEST "Submit word groups from bob"
654 result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
655 test "$result" = '{"valid":true}'
658 # Usage: empathy_scores <player_name>
661 empathy_get_event $1 game-state | jq .scores
664 TEST "Scoring hasn't started with player unsubmitted"
665 result=$(echo $(empathy_scores alice))
666 test "$result" = "null"
669 TEST "Submit word groups from charlie"
670 result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
671 test "$result" = '{"valid":true}'
674 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
676 TEST "Scoring already started"
677 result=$(echo $(empathy_scores alice))
678 test "$result" != "null"
681 # Usage: empathy_scores_names_numbers <player_name>
682 empathy_scores_names_numbers()
684 empathy_get_event $1 game-state | jq '.scores.scores[]|.player,.score'
687 TEST_SUBSECTION "Scoring"
689 TEST "Verify final scores as expected"
690 # echo here is to strip newlines
691 result=$(echo $(empathy_scores_names_numbers alice))
692 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
695 # Usage: empathy_words_submitted <player_name>
696 empathy_words_submitted()
698 empathy_get_event $1 game-state | jq '.scores.words[].word'
701 TEST "Verify final list of words submitted"
702 # echo here is to strip newlines
703 result=$(echo $(empathy_words_submitted alice))
704 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
707 TEST_SUBSECTION "New game (using voting to advance phases)"
711 curl_post $empathy_game_path/reset
714 TEST "Any post to /reset resets the game"
719 TEST "Verify scoring is over"
720 result=$(echo $(empathy_scores alice))
721 test "$result" = "null"
724 # Usage: empathy_answering <player_name> <prompt_id>
727 empathy_post $1 answering/$2
730 TEST "Start 4-player game, 3 submissions"
731 empathy_player_activate dale
732 result=$(empathy_player_name dale)
733 test "$result" = "dale"
734 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
735 empathy_start alice $prompt_id
736 empathy_answer alice $prompt_id '"I", "love", "you"' >/dev/null
737 empathy_answer bob $prompt_id '"I", "love", "food"' >/dev/null
738 empathy_answer charlie $prompt_id '"food", "is", "good"' >/dev/null
739 result=$(empathy_answering dale $prompt_id)
740 test "$result" = '{"valid":true}'
743 TEST "Judging hasn't started with player unsubmitted"
744 result=$(echo $(empathy_ambiguities alice))
745 test "$result" = "null"
748 # Usage: empathy_end_answers <player_name> <prompt_id>
749 empathy_end_answers()
751 empathy_post $1 end-answers/$2
754 TEST "Minority of players vote to end answering"
755 empathy_end_answers alice $prompt_id
756 empathy_end_answers bob $prompt_id
760 TEST "Judging still hasn't started"
761 result=$(echo $(empathy_ambiguities alice))
762 test "$result" = "null"
765 TEST "Majority of players vote to end answering"
766 empathy_end_answers charlie $prompt_id
770 TEST "Judging has now started"
771 result=$(echo $(empathy_ambiguities alice))
772 test "$result" != "null"
775 # Usage: empathy_players_judging <player_name>
776 empathy_players_judging()
778 empathy_get_event $1 game-state | jq .players_judging[]
781 TEST "Verify active players listed as judging"
782 # echo here is to strip newlines
783 result=$(echo $(empathy_players_judging alice))
784 test "$result" = '"alice" "bob" "charlie"'
787 TEST "Submit word groups from majority"
788 empathy_judged alice $prompt_id '[]' >/dev/null
789 result=$(empathy_judged bob $prompt_id '[]')
790 test "$result" = '{"valid":true}'
793 TEST "Scoring hasn't started with player unsubmitted"
794 result=$(echo $(empathy_scores alice))
795 test "$result" = "null"
798 # Usage: empathy_end_judging <player_name> <prompt_id>
799 empathy_end_judging()
801 empathy_post $1 end-judging/$2
804 TEST "Minority of players vote to end judging"
805 empathy_end_judging alice $prompt_id
809 TEST "Scoring still hasn't started"
810 result=$(echo $(empathy_scores alice))
811 test "$result" = "null"
814 TEST "Majority of players vote to end judging"
815 empathy_end_judging bob $prompt_id
819 TEST "Scoring has now started"
820 result=$(echo $(empathy_scores alice))
821 test "$result" != "null"
824 TEST_SUBSECTION "New game (no voting needed when all answered players judge)"
826 TEST "Start 4-player game, 3 submissions"
828 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
829 empathy_start alice $prompt_id
830 empathy_answer alice $prompt_id '"truth"' >/dev/null
831 empathy_answer bob $prompt_id '"truth"' >/dev/null
832 empathy_answer charlie $prompt_id '"dare"' >/dev/null
833 empathy_end_answers alice $prompt_id
834 empathy_end_answers bob $prompt_id
835 empathy_end_answers charlie $prompt_id
839 TEST "Submit word groups from 2 players"
840 empathy_judged alice $prompt_id '[]' >/dev/null
841 result=$(empathy_judged bob $prompt_id '[]')
842 test "$result" = '{"valid":true}'
845 TEST "Scoring hasn't started with player unsubmitted"
846 result=$(echo $(empathy_scores alice))
847 test "$result" = "null"
850 TEST "Submit word groups from last answering player"
851 result=$(empathy_judged charlie $prompt_id '[]')
852 test "$result" = '{"valid":true}'
855 TEST "Scoring has now started"
856 result=$(echo $(empathy_scores alice))
857 test "$result" != "null"
860 TEST_SUBSECTION "Non players don't affect judging requirements"
862 TEST "Start 2-player game with 6 registered players"
864 empathy_player_activate eric
865 empathy_player_activate fred
866 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
867 empathy_start alice $prompt_id
868 empathy_answer alice $prompt_id '"truth"' >/dev/null
869 empathy_answer bob $prompt_id '"true"' >/dev/null
870 empathy_end_answers alice $prompt_id
871 empathy_end_answers bob $prompt_id
875 TEST "1 player votes for a match"
876 empathy_judged alice $prompt_id '[["truth","true"]]' >/dev/null
877 result=$(empathy_judged bob $prompt_id '[]')
878 test "$result" = '{"valid":true}'
881 TEST "Verify the match passed the vote"
882 # echo here is to strip newlines
883 result=$(echo $(empathy_scores_names_numbers alice))
884 test "$result" = '"alice" 2 "bob" 2 "charlie" 0 "dale" 0 "eric" 0 "fred" 0'
888 echo "NOTE: Slow tests ahead!"
889 echo "If you are impatient and somehow \"know\" you don't care about the"
890 echo "tests below then you can interrupt the test suite with Control-C"
891 echo "to get a summary report on the tests that have already been run."
893 TEST_SUBSECTION "Inactive players don't appear in scores"
895 TEST "Start 2-player game with 6 registered players"
897 prompt_id=$(empathy_submit_prompt alice 4 "1 best pet" | jq .id)
898 empathy_start alice $prompt_id
899 empathy_answer alice $prompt_id '"cats"' >/dev/null
900 empathy_answer bob $prompt_id '"dogs"' >/dev/null
901 empathy_end_answers alice $prompt_id
902 empathy_end_answers bob $prompt_id
906 TEST "Deactivate 3 players"
907 empathy_player_deactivate dale
908 empathy_player_deactivate eric
909 empathy_player_deactivate fred
914 TEST "Finish game with 2 active players"
915 empathy_judged alice $prompt_id '[]' >/dev/null
916 result=$(empathy_judged bob $prompt_id '[]')
917 test "$result" = '{"valid":true}'
920 TEST "Verify scores don't include inactive players"
921 # echo here is to strip newlines
922 result=$(echo $(empathy_scores_names_numbers alice))
923 test "$result" = '"alice" 1 "bob" 1 "charlie" 0'