5 echo "Usage:$0 <URL-to-test>"
9 echo "Error: No test URL given." >&2
16 CURL="curl --silent --show-error"
22 echo $1 | sed -e "s/./$2/g"
38 printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
39 (( tests_total++ )) || true
42 # Result of test depends on the exit status of last command
48 (( tests_failed++ )) || true
52 # If we got an argument, append it after test result
60 # Print report of all previous test results
67 if [ "$tests_failed" == "" ]; then
68 echo "All $tests_total tests passed."
72 echo "$tests_failed of $tests_total tests failed."
78 # Does a string contain a regular expression pattern
82 # contains "All's well that ends well" "s.well"
88 # POST to a URL endpoint with optional JSON data
92 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
95 $CURL ${3:-} -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
98 # POST to a URL endpoint with optional JSON data using a cookie
102 # curl_post_cookie <name> <ENDPOINT> [data] [CURL_OPTIONS]
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
110 curl_post $2 "${3:-}" "-b ${!cookie} ${4:-}"
113 # PUT to a URL endpoint with optional JSON data
117 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
120 $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
123 # GET from a URL endpoint
127 # curl_get <ENDPOINT> [CURL_OPTIONS]
133 # GET from a URL endpoint using a cookie
137 # curl_get_cookie <name> <ENDPOINT> [CURL_OPTIONS]
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
145 curl_get $2 "-b ${!cookie} ${3:-}"
148 # Create a new game of the specified engine type
155 curl_post new/$1 | jq -r .
158 TEST_SECTION "LMNO (super-site for games)"
160 TEST_SUBSECTION "Testing home page"
161 home_page=$($CURL $URL)
163 TEST "Contains 'Join Game'"
164 contains "$home_page" "Join Game"
167 TEST "Contains 'Host a new game'"
168 contains "$home_page" "Host a new game"
171 TEST_SUBSECTION "Creating some new games"
174 empires_game_id=$(new_game empires)
175 test "$empires_game_id" != ""
176 TEST_END $empires_game_id
179 tictactoe_game_id=$(new_game tictactoe)
180 test "$tictactoe_game_id" != ""
181 TEST_END $tictactoe_game_id
183 TEST_SUBSECTION "Test redirects"
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/"
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/"
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/"
201 TEST_SECTION "Empires game"
203 empires_game_path=empires/$empires_game_id
205 TEST_SUBSECTION "Empires game /register"
209 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
212 empires_players_string()
214 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
217 empires_characters_string()
219 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
222 TEST "Registering a player returns an ID"
223 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
224 test "$carl_id" = "1"
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")
236 TEST 'Verify complete players list (with "Bogus")'
237 players=$(empires_players_string)
238 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
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,"
246 TEST_SUBSECTION "Empires game /deregister"
250 curl_post $empires_game_path/deregister/$1
253 TEST "Removing the bogus player"
254 empires_deregister $bogus_id
257 TEST 'Verify modified players list (w/o "Bogus")"'
258 players=$(empires_players_string)
259 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
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,"
267 TEST_SUBSECTION "Empires game /capture"
271 curl_post $empires_game_path/capture/$1/$2
274 empires_empires_string()
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' ','
280 TEST "Verify empires before any captures"
281 empires=$(empires_empires_string)
282 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
285 TEST "Perform some captures"
292 TEST "Verify empires after captures"
293 empires=$(empires_empires_string)
294 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
297 TEST_SUBSECTION "Empires game /liberate"
301 curl_post $empires_game_path/liberate/$1
304 TEST "Liberate a player"
308 TEST "Verify empires after liberate"
309 empires=$(empires_empires_string)
310 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
313 TEST_SUBSECTION "Empires game /reset"
317 curl_post $empires_game_path/reset
320 TEST "Reset the game"
324 TEST "Verify players is now empty"
325 players=$(empires_players_string)
329 TEST_SECTION "Tic Tac Toe game"
331 tictactoe_game_path=tictactoe/$tictactoe_game_id
335 curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-tictactoe"
340 curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .cookie-tictactoe"
343 tictactoe_player_info()
345 curl_get $tictactoe_game_path/events "-m 0.1 -b .cookie-tictactoe" 2>&1 \
346 | grep player-info -A 1 \
350 tictactoe_player_name()
352 curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-tictactoe"
355 tictactoe_player_team()
357 curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-tictactoe"
360 TEST_SUBSECTION "Tic Tac Toe player-info"
362 TEST "Hit LMNO /profile to set name to 'curl'"
363 tictactoe_profile curl
366 TEST "Verify player-info event reports 'curl' name"
367 result=$(tictactoe_player_info)
368 test "$result" = 'data: {"id":1,"name":"curl","team":""}'
371 TEST_SUBSECTION "Tic Tac Toe /player"
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":""}'
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"}'
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"}'
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"}'
397 TEST "Leave current team"
398 tictactoe_player_team ""
399 result=$(tictactoe_player_info)
400 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
403 TEST_SUBSECTION "Tic Tac Toe /move"
405 TEST "First move doesn't require a team"
406 result=$(tictactoe_move 0)
407 test "$result" = '{"legal":true}'
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"}'
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"}'
421 TEST "Legal move to center square"
422 tictactoe_player_team O
423 result=$(tictactoe_move 4)
424 test "$result" = '{"legal":true}'
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"}'
433 TEST_SECTION "Empathy game"
435 TEST_SUBSECTION "Create a game and register 3 players"
437 TEST "Create the game"
438 empathy_game_id=$(new_game empathy)
439 test "$empathy_game_id" != ""
440 TEST_END $empathy_game_id
442 empathy_game_path=empathy/$empathy_game_id
444 # Usage: empathy_get <player_name> <endpoint> [curl_options]
447 curl_get_cookie $1 $empathy_game_path/$2 "${3:-}"
450 # Usage: empathy_post <player_name> <endpoint> [data]
453 curl_post_cookie $1 $empathy_game_path/$2 "${3:-}"
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
460 empathy_player_activate()
463 player_cookie=${player}_cookie
464 player_pid=${player}_pid
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}=$!
472 # Usage: empathy_player_deactivate <player_name>
473 empathy_player_deactivate()
476 player_pid=${player}_pid
477 pkill -P ${!player_pid}
480 # Pulls a single named event out of the empathy event stream
482 # Usage: empathy_get_event <player_name> <event_name>
485 empathy_get $1 events "-m 0.1" 2>&1 \
486 | grep "^event: $2" -A 1 \
488 | sed -e 's,^data: *,,'
491 # Usage: empathy_player_name <player_name>
492 empathy_player_name()
494 empathy_get_event $1 player-info | jq -r .name
497 TEST "Set 'alice' in session"
498 empathy_player_activate alice
499 test "$alice_cookie" = ".cookie-empathy-alice"
502 TEST "Register alice and verify name"
503 result=$(empathy_player_name alice)
504 test "$result" = "alice"
508 empathy_player_activate bob
509 result=$(empathy_player_name bob)
510 test "$result" = "bob"
513 TEST "Register charlie"
514 empathy_player_activate charlie
515 result=$(empathy_player_name charlie)
516 test "$result" = "charlie"
519 TEST_SUBSECTION "Category selection"
521 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
522 empathy_submit_prompt()
524 empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
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"}'
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"
537 # Usage: empathy_vote <player_name> <prompt_id>
540 empathy_post $1 vote/$2
543 TEST "Vote on this category"
544 empathy_vote alice $prompt_id
548 # Usage: empathy_start <player_name> <prompt_id>
551 empathy_post $1 start/$2
554 TEST "Start the game with this category"
555 empathy_start alice $prompt_id
559 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
562 empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
565 TEST_SUBSECTION "Submitting answers"
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"}'
573 TEST "Submit from alice succeeds"
574 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
575 test "$result" = '{"valid":true}'
578 TEST "Submit from bob succeeds"
579 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
580 test "$result" = '{"valid":true}'
583 # Usage: empathy_ambiguities <player_name>
584 empathy_ambiguities()
586 empathy_get_event $1 game-state | jq .ambiguities
589 TEST "Judging hasn't started with player unsubmitted"
590 result=$(echo $(empathy_ambiguities alice))
591 test "$result" = "null"
594 TEST "Submit from charlie succeeds"
595 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
596 test "$result" = '{"valid":true}'
599 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
601 TEST "Judging already started"
602 result=$(echo $(empathy_ambiguities alice))
603 test "$result" != "null"
606 TEST_SUBSECTION "Judging answers"
608 # Usage: empathy_ambiguities_list <player_name>
609 empathy_ambiguities_list()
611 empathy_get_event $1 game-state | jq .ambiguities[]
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"'
620 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
623 empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
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}'
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}'
636 # Usage: empathy_scores <player_name>
639 empathy_get_event $1 game-state | jq .scores
642 TEST "Scoring hasn't started with player unsubmitted"
643 result=$(echo $(empathy_scores alice))
644 test "$result" = "null"
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}'
652 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
654 TEST "Scoring already started"
655 result=$(echo $(empathy_scores alice))
656 test "$result" != "null"
659 # Usage: empathy_scores_names_numbers <player_name>
660 empathy_scores_names_numbers()
662 empathy_get_event $1 game-state | jq '.scores.scores[]|.player,.score'
665 TEST_SUBSECTION "Scoring"
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'
673 # Usage: empathy_words_submitted <player_name>
674 empathy_words_submitted()
676 empathy_get_event $1 game-state | jq '.scores.words[].word'
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"'
685 TEST_SUBSECTION "New game (using voting to advance phases)"
689 curl_post $empathy_game_path/reset
692 TEST "Any post to /reset resets the game"
697 TEST "Verify scoring is over"
698 result=$(echo $(empathy_scores alice))
699 test "$result" = "null"
702 # Usage: empathy_answering <player_name> <prompt_id>
705 empathy_post $1 answering/$2
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}'
721 TEST "Judging hasn't started with player unsubmitted"
722 result=$(echo $(empathy_ambiguities alice))
723 test "$result" = "null"
726 # Usage: empathy_end_answers <player_name> <prompt_id>
727 empathy_end_answers()
729 empathy_post $1 end-answers/$2
732 TEST "Minority of players vote to end answering"
733 empathy_end_answers alice $prompt_id
734 empathy_end_answers bob $prompt_id
738 TEST "Judging still hasn't started"
739 result=$(echo $(empathy_ambiguities alice))
740 test "$result" = "null"
743 TEST "Majority of players vote to end answering"
744 empathy_end_answers charlie $prompt_id
748 TEST "Judging has now started"
749 result=$(echo $(empathy_ambiguities alice))
750 test "$result" != "null"
753 # Usage: empathy_players_judging <player_name>
754 empathy_players_judging()
756 empathy_get_event $1 game-state | jq .players_judging[]
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"'
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}'
771 TEST "Scoring hasn't started with player unsubmitted"
772 result=$(echo $(empathy_scores alice))
773 test "$result" = "null"
776 # Usage: empathy_end_judging <player_name> <prompt_id>
777 empathy_end_judging()
779 empathy_post $1 end-judging/$2
782 TEST "Minority of players vote to end judging"
783 empathy_end_judging alice $prompt_id
787 TEST "Scoring still hasn't started"
788 result=$(echo $(empathy_scores alice))
789 test "$result" = "null"
792 TEST "Majority of players vote to end judging"
793 empathy_end_judging bob $prompt_id
797 TEST "Scoring has now started"
798 result=$(echo $(empathy_scores alice))
799 test "$result" != "null"
802 TEST_SUBSECTION "New game (no voting needed when all answered players judge)"
804 TEST "Start 4-player game, 3 submissions"
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
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}'
823 TEST "Scoring hasn't started with player unsubmitted"
824 result=$(echo $(empathy_scores alice))
825 test "$result" = "null"
828 TEST "Submit word groups from last answering player"
829 result=$(empathy_judged charlie $prompt_id '[]')
830 test "$result" = '{"valid":true}'
833 TEST "Scoring has now started"
834 result=$(echo $(empathy_scores alice))
835 test "$result" != "null"
838 TEST_SUBSECTION "Non players don't affect judging requirements"
840 TEST "Start 2-player game with 6 registered players"
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
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}'
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'
865 TEST_SUBSECTION "Inactive players don't appear in scores"
867 TEST "Start 2-player game with 6 registered players"
869 prompt_id=$(empathy_submit_prompt alice 4 "1 best pet" | jq .id)
870 empathy_start alice $prompt_id
871 empathy_answer alice $prompt_id '"cats"' >/dev/null
872 empathy_answer bob $prompt_id '"dogs"' >/dev/null
873 empathy_end_answers alice $prompt_id
874 empathy_end_answers bob $prompt_id
878 TEST "Deactivate 3 players"
879 empathy_player_deactivate dale
880 empathy_player_deactivate eric
881 empathy_player_deactivate fred
886 TEST "Finish game with 2 active players"
887 empathy_judged alice $prompt_id '[]' >/dev/null
888 result=$(empathy_judged bob $prompt_id '[]')
889 test "$result" = '{"valid":true}'
892 TEST "Verify scores don't include inactive players"
893 # echo here is to strip newlines
894 result=$(echo $(empathy_scores_names_numbers alice))
895 test "$result" = '"alice" 1 "bob" 1 "charlie" 0'
898 empathy_player_deactivate alice
899 empathy_player_deactivate bob
900 empathy_player_deactivate charlie