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 # Given a player name as $1 (eg. "empathy_player_activate alice") set both
445 # $1_cookie and $1_pid (that is $alice_cookie and $alice_pid) to
446 # a filename containing a cookie and the PID of a running event-streaming
448 empathy_player_activate()
451 player_cookie=${player}_cookie
452 player_pid=${player}_pid
454 eval ${player_cookie}=".cookie-empathy-$player"
455 curl_put /profile "{ \"nickname\": \"$player\" }" "-c ${!player_cookie}"
456 curl_get_cookie $player $empathy_game_path/events >/dev/null 2>&1 &
457 eval ${player_pid}=$!
462 curl_get_cookie $1 $2 "-m 0.1" 2>&1 \
463 | grep "^event: $3" -A 1 \
465 | sed -e 's,^data: *,,'
468 empathy_player_name()
470 empathy_get_event $1 $empathy_game_path/events player-info | jq -r .name
473 TEST "Set 'alice' in session"
474 empathy_player_activate alice
475 test "$alice_cookie" = ".cookie-empathy-alice"
478 TEST "Register alice and verify name"
479 result=$(empathy_player_name alice)
480 test "$result" = "alice"
484 empathy_player_activate bob
485 result=$(empathy_player_name bob)
486 test "$result" = "bob"
489 TEST "Register charlie"
490 empathy_player_activate charlie
491 result=$(empathy_player_name charlie)
492 test "$result" = "charlie"
495 TEST_SUBSECTION "Category selection"
497 empathy_submit_prompt()
499 curl_post_cookie $1 $empathy_game_path/prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
502 TEST "Huge numbers are rejected"
503 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
504 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
507 TEST "Submit a category"
508 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
509 test "$prompt_id" = "1"
514 curl_post_cookie $1 $empathy_game_path/vote/$2
517 TEST "Vote on this category"
518 empathy_vote alice $prompt_id
524 curl_post_cookie $1 $empathy_game_path/start/$2
527 TEST "Start the game with this category"
528 empathy_start alice $prompt_id
534 curl_post_cookie $1 $empathy_game_path/answer/$2 "{ \"answers\": [$3]}"
537 TEST_SUBSECTION "Submitting answers"
539 TEST "Submit from a non-player fails"
540 bogus_cookie=/dev/null
541 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
542 test "$result" = '{"valid":false,"message":"Player not found"}'
545 TEST "Submit from alice succeeds"
546 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
547 test "$result" = '{"valid":true}'
550 TEST "Submit from bob succeeds"
551 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
552 test "$result" = '{"valid":true}'
555 empathy_ambiguities()
557 empathy_get_event $1 $empathy_game_path/events game-state \
561 TEST "Judging hasn't started with player unsubmitted"
562 result=$(echo $(empathy_ambiguities alice))
563 test "$result" = "null"
566 TEST "Submit from charlie succeeds"
567 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
568 test "$result" = '{"valid":true}'
571 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
573 TEST "Judging already started"
574 result=$(echo $(empathy_ambiguities alice))
575 test "$result" != "null"
578 TEST_SUBSECTION "Judging answers"
580 empathy_ambiguities_list()
582 empathy_get_event $1 $empathy_game_path/events game-state \
586 TEST "Received all unique words"
587 # echo here is to strip newlines
588 result=$(echo $(empathy_ambiguities_list alice))
589 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
594 curl_post_cookie $1 $empathy_game_path/judged/$2 "{ \"word_groups\": $3}"
597 TEST "Submit word groups from alice"
598 result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
599 test "$result" = '{"valid":true}'
602 TEST "Submit word groups from bob"
603 result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
604 test "$result" = '{"valid":true}'
609 empathy_get_event $1 $empathy_game_path/events game-state \
613 TEST "Scoring hasn't started with player unsubmitted"
614 result=$(echo $(empathy_scores alice))
615 test "$result" = "null"
618 TEST "Submit word groups from charlie"
619 result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
620 test "$result" = '{"valid":true}'
623 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
625 TEST "Scoring already started"
626 result=$(echo $(empathy_scores alice))
627 test "$result" != "null"
630 empathy_scores_names_numbers()
632 empathy_get_event $1 $empathy_game_path/events game-state \
633 | jq '.scores.scores[]|.player,.score'
636 TEST_SUBSECTION "Scoring"
638 TEST "Verify final scores as expected"
639 # echo here is to strip newlines
640 result=$(echo $(empathy_scores_names_numbers alice))
641 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
644 empathy_words_submitted()
646 empathy_get_event $1 $empathy_game_path/events game-state \
647 | jq '.scores.words[].word'
650 TEST "Verify final list of words submitted"
651 # echo here is to strip newlines
652 result=$(echo $(empathy_words_submitted alice))
653 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
656 TEST_SUBSECTION "New game (using voting to advance phases)"
660 curl_post $empathy_game_path/reset
663 TEST "Any post to /reset resets the game"
668 TEST "Verify scoring is over"
669 result=$(echo $(empathy_scores alice))
670 test "$result" = "null"
675 curl_post_cookie $1 $empathy_game_path/answering/$2
678 TEST "Start 4-player game, 3 submissions"
679 empathy_player_activate dale
680 result=$(empathy_player_name dale)
681 test "$result" = "dale"
682 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
683 empathy_start alice $prompt_id
684 empathy_answer alice $prompt_id '"I", "love", "you"' >/dev/null
685 empathy_answer bob $prompt_id '"I", "love", "food"' >/dev/null
686 empathy_answer charlie $prompt_id '"food", "is", "good"' >/dev/null
687 result=$(empathy_answering dale $prompt_id)
688 test "$result" = '{"valid":true}'
691 TEST "Judging hasn't started with player unsubmitted"
692 result=$(echo $(empathy_ambiguities alice))
693 test "$result" = "null"
696 empathy_end_answers()
698 curl_post_cookie $1 $empathy_game_path/end-answers/$2
701 TEST "Minority of players vote to end answering"
702 empathy_end_answers alice $prompt_id
703 empathy_end_answers bob $prompt_id
707 TEST "Judging still hasn't started"
708 result=$(echo $(empathy_ambiguities alice))
709 test "$result" = "null"
712 TEST "Majority of players vote to end answering"
713 empathy_end_answers charlie $prompt_id
717 TEST "Judging has now started"
718 result=$(echo $(empathy_ambiguities alice))
719 test "$result" != "null"
722 TEST "Submit word groups from majority"
723 empathy_judged alice $prompt_id '[]' >/dev/null
724 result=$(empathy_judged bob $prompt_id '[]')
725 test "$result" = '{"valid":true}'
728 TEST "Scoring hasn't started with player unsubmitted"
729 result=$(echo $(empathy_scores alice))
730 test "$result" = "null"
733 empathy_end_judging()
735 curl_post_cookie $1 $empathy_game_path/end-judging/$2
738 TEST "Minority of players vote to end judging"
739 empathy_end_judging alice $prompt_id
743 TEST "Scoring still hasn't started"
744 result=$(echo $(empathy_scores alice))
745 test "$result" = "null"
748 TEST "Majority of players vote to end judging"
749 empathy_end_judging bob $prompt_id
753 TEST "Scoring has now started"
754 result=$(echo $(empathy_scores alice))
755 test "$result" != "null"
760 pkill -P $charlie_pid