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 # Pulls a single named event out of the empathy event stream
474 # Usage: empathy_get_event <player_name> <event_name>
477 empathy_get $1 events "-m 0.1" 2>&1 \
478 | grep "^event: $2" -A 1 \
480 | sed -e 's,^data: *,,'
483 # Usage: empathy_player_name <player_name>
484 empathy_player_name()
486 empathy_get_event $1 player-info | jq -r .name
489 TEST "Set 'alice' in session"
490 empathy_player_activate alice
491 test "$alice_cookie" = ".cookie-empathy-alice"
494 TEST "Register alice and verify name"
495 result=$(empathy_player_name alice)
496 test "$result" = "alice"
500 empathy_player_activate bob
501 result=$(empathy_player_name bob)
502 test "$result" = "bob"
505 TEST "Register charlie"
506 empathy_player_activate charlie
507 result=$(empathy_player_name charlie)
508 test "$result" = "charlie"
511 TEST_SUBSECTION "Category selection"
513 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
514 empathy_submit_prompt()
516 empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
519 TEST "Huge numbers are rejected"
520 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
521 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
524 TEST "Submit a category"
525 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
526 test "$prompt_id" = "1"
529 # Usage: empathy_vote <player_name> <prompt_id>
532 empathy_post $1 vote/$2
535 TEST "Vote on this category"
536 empathy_vote alice $prompt_id
540 # Usage: empathy_start <player_name> <prompt_id>
543 empathy_post $1 start/$2
546 TEST "Start the game with this category"
547 empathy_start alice $prompt_id
551 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
554 empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
557 TEST_SUBSECTION "Submitting answers"
559 TEST "Submit from a non-player fails"
560 bogus_cookie=/dev/null
561 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
562 test "$result" = '{"valid":false,"message":"Player not found"}'
565 TEST "Submit from alice succeeds"
566 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
567 test "$result" = '{"valid":true}'
570 TEST "Submit from bob succeeds"
571 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
572 test "$result" = '{"valid":true}'
575 # Usage: empathy_ambiguities <player_name>
576 empathy_ambiguities()
578 empathy_get_event $1 game-state | jq .ambiguities
581 TEST "Judging hasn't started with player unsubmitted"
582 result=$(echo $(empathy_ambiguities alice))
583 test "$result" = "null"
586 TEST "Submit from charlie succeeds"
587 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
588 test "$result" = '{"valid":true}'
591 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
593 TEST "Judging already started"
594 result=$(echo $(empathy_ambiguities alice))
595 test "$result" != "null"
598 TEST_SUBSECTION "Judging answers"
600 # Usage: empathy_ambiguities_list <player_name>
601 empathy_ambiguities_list()
603 empathy_get_event $1 game-state | jq .ambiguities[]
606 TEST "Received all unique words"
607 # echo here is to strip newlines
608 result=$(echo $(empathy_ambiguities_list alice))
609 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
612 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
615 empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
618 TEST "Submit word groups from alice"
619 result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
620 test "$result" = '{"valid":true}'
623 TEST "Submit word groups from bob"
624 result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
625 test "$result" = '{"valid":true}'
628 # Usage: empathy_scores <player_name>
631 empathy_get_event $1 game-state | jq .scores
634 TEST "Scoring hasn't started with player unsubmitted"
635 result=$(echo $(empathy_scores alice))
636 test "$result" = "null"
639 TEST "Submit word groups from charlie"
640 result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
641 test "$result" = '{"valid":true}'
644 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
646 TEST "Scoring already started"
647 result=$(echo $(empathy_scores alice))
648 test "$result" != "null"
651 # Usage: empathy_scores_names_numbers <player_name>
652 empathy_scores_names_numbers()
654 empathy_get_event $1 game-state | jq '.scores.scores[]|.player,.score'
657 TEST_SUBSECTION "Scoring"
659 TEST "Verify final scores as expected"
660 # echo here is to strip newlines
661 result=$(echo $(empathy_scores_names_numbers alice))
662 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
665 # Usage: empathy_words_submitted <player_name>
666 empathy_words_submitted()
668 empathy_get_event $1 game-state | jq '.scores.words[].word'
671 TEST "Verify final list of words submitted"
672 # echo here is to strip newlines
673 result=$(echo $(empathy_words_submitted alice))
674 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
677 TEST_SUBSECTION "New game (using voting to advance phases)"
681 curl_post $empathy_game_path/reset
684 TEST "Any post to /reset resets the game"
689 TEST "Verify scoring is over"
690 result=$(echo $(empathy_scores alice))
691 test "$result" = "null"
694 # Usage: empathy_answering <player_name> <prompt_id>
697 empathy_post $1 answering/$2
700 TEST "Start 4-player game, 3 submissions"
701 empathy_player_activate dale
702 result=$(empathy_player_name dale)
703 test "$result" = "dale"
704 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
705 empathy_start alice $prompt_id
706 empathy_answer alice $prompt_id '"I", "love", "you"' >/dev/null
707 empathy_answer bob $prompt_id '"I", "love", "food"' >/dev/null
708 empathy_answer charlie $prompt_id '"food", "is", "good"' >/dev/null
709 result=$(empathy_answering dale $prompt_id)
710 test "$result" = '{"valid":true}'
713 TEST "Judging hasn't started with player unsubmitted"
714 result=$(echo $(empathy_ambiguities alice))
715 test "$result" = "null"
718 # Usage: empathy_end_answers <player_name> <prompt_id>
719 empathy_end_answers()
721 empathy_post $1 end-answers/$2
724 TEST "Minority of players vote to end answering"
725 empathy_end_answers alice $prompt_id
726 empathy_end_answers bob $prompt_id
730 TEST "Judging still hasn't started"
731 result=$(echo $(empathy_ambiguities alice))
732 test "$result" = "null"
735 TEST "Majority of players vote to end answering"
736 empathy_end_answers charlie $prompt_id
740 TEST "Judging has now started"
741 result=$(echo $(empathy_ambiguities alice))
742 test "$result" != "null"
745 TEST "Submit word groups from majority"
746 empathy_judged alice $prompt_id '[]' >/dev/null
747 result=$(empathy_judged bob $prompt_id '[]')
748 test "$result" = '{"valid":true}'
751 TEST "Scoring hasn't started with player unsubmitted"
752 result=$(echo $(empathy_scores alice))
753 test "$result" = "null"
756 # Usage: empathy_end_judging <player_name> <prompt_id>
757 empathy_end_judging()
759 empathy_post $1 end-judging/$2
762 TEST "Minority of players vote to end judging"
763 empathy_end_judging alice $prompt_id
767 TEST "Scoring still hasn't started"
768 result=$(echo $(empathy_scores alice))
769 test "$result" = "null"
772 TEST "Majority of players vote to end judging"
773 empathy_end_judging bob $prompt_id
777 TEST "Scoring has now started"
778 result=$(echo $(empathy_scores alice))
779 test "$result" != "null"
784 pkill -P $charlie_pid