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 # PUT to a URL endpoint with optional JSON data
102 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
105 $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
108 # GET from a URL endpoint
112 # curl_get <ENDPOINT> [CURL_OPTIONS]
118 # Create a new game of the specified engine type
125 curl_post new/$1 | jq -r .
128 TEST_SECTION "LMNO (super-site for games)"
130 TEST_SUBSECTION "Testing home page"
131 home_page=$($CURL $URL)
133 TEST "Contains 'Join Game'"
134 contains "$home_page" "Join Game"
137 TEST "Contains 'Host a new game'"
138 contains "$home_page" "Host a new game"
141 TEST_SUBSECTION "Creating some new games"
144 empires_game_id=$(new_game empires)
145 test "$empires_game_id" != ""
146 TEST_END $empires_game_id
149 tictactoe_game_id=$(new_game tictactoe)
150 test "$tictactoe_game_id" != ""
151 TEST_END $tictactoe_game_id
153 TEST_SUBSECTION "Test redirects"
155 TEST "Redirect of /GAMEID at top level"
156 redirect=$(curl_get $empires_game_id)
157 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
160 TEST "Redirect of lowercase /gameid at top level"
161 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
162 redirect=$(curl_get $empires_game_id_lower)
163 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
166 TEST "Redirect of lowercase /empires/gameid"
167 redirect=$(curl_get empires/$empires_game_id_lower)
168 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
171 TEST_SECTION "Empires game"
173 empires_game_path=empires/$empires_game_id
175 TEST_SUBSECTION "Empires game /register"
179 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
182 empires_players_string()
184 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
187 empires_characters_string()
189 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
192 TEST "Registering a player returns an ID"
193 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
194 test "$carl_id" = "1"
197 TEST "Registering several more players"
198 empires_register Richard "Bob Hope" > /dev/null
199 empires_register Kevin "Elvis Presley" > /dev/null
200 empires_register Stacy Phineas > /dev/null
201 empires_register David "Red Power Ranger" > /dev/null
202 empires_register Nancy "Audrey Hepburn" > /dev/null
203 bogus_id=$(empires_register Bogus "Mr. Bogus")
206 TEST 'Verify complete players list (with "Bogus")'
207 players=$(empires_players_string)
208 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
211 TEST 'Verify complete players list (with "Mr. Bogus")'
212 characters=$(empires_characters_string)
213 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
216 TEST_SUBSECTION "Empires game /deregister"
220 curl_post $empires_game_path/deregister/$1
223 TEST "Removing the bogus player"
224 empires_deregister $bogus_id
227 TEST 'Verify modified players list (w/o "Bogus")"'
228 players=$(empires_players_string)
229 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
232 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
233 characters=$(empires_characters_string)
234 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
237 TEST_SUBSECTION "Empires game /capture"
241 curl_post $empires_game_path/capture/$1/$2
244 empires_empires_string()
246 # Get empires as a compact string (much more compact than JSON)
247 curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
250 TEST "Verify empires before any captures"
251 empires=$(empires_empires_string)
252 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
255 TEST "Perform some captures"
262 TEST "Verify empires after captures"
263 empires=$(empires_empires_string)
264 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
267 TEST_SUBSECTION "Empires game /liberate"
271 curl_post $empires_game_path/liberate/$1
274 TEST "Liberate a player"
278 TEST "Verify empires after liberate"
279 empires=$(empires_empires_string)
280 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
283 TEST_SUBSECTION "Empires game /reset"
287 curl_post $empires_game_path/reset
290 TEST "Reset the game"
294 TEST "Verify players is now empty"
295 players=$(empires_players_string)
299 TEST_SECTION "Tic Tac Toe game"
301 tictactoe_game_path=tictactoe/$tictactoe_game_id
305 curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-tictactoe"
310 curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .cookie-tictactoe"
313 tictactoe_player_info()
315 curl_get $tictactoe_game_path/events "-m 0.1 -b .cookie-tictactoe" 2>&1 \
316 | grep player-info -A 1 \
320 tictactoe_player_name()
322 curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-tictactoe"
325 tictactoe_player_team()
327 curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-tictactoe"
330 TEST_SUBSECTION "Tic Tac Toe player-info"
332 TEST "Hit LMNO /profile to set name to 'curl'"
333 tictactoe_profile curl
336 TEST "Verify player-info event reports 'curl' name"
337 result=$(tictactoe_player_info)
338 test "$result" = 'data: {"id":1,"name":"curl","team":""}'
341 TEST_SUBSECTION "Tic Tac Toe /player"
343 TEST "Change name to 'newname'"
344 tictactoe_player_name newname
345 result=$(tictactoe_player_info)
346 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
349 TEST "Change team to 'X'"
350 tictactoe_player_team X
351 result=$(tictactoe_player_info)
352 test "$result" = 'data: {"id":1,"name":"newname","team":"X"}'
355 TEST "Change team to 'O'"
356 tictactoe_player_team O
357 result=$(tictactoe_player_info)
358 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
361 TEST "Verify cannot change team to 'Z'"
362 tictactoe_player_team Z
363 result=$(tictactoe_player_info)
364 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
367 TEST "Leave current team"
368 tictactoe_player_team ""
369 result=$(tictactoe_player_info)
370 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
373 TEST_SUBSECTION "Tic Tac Toe /move"
375 TEST "First move doesn't require a team"
376 result=$(tictactoe_move 0)
377 test "$result" = '{"legal":true}'
380 TEST "Second move does require a team"
381 result=$(tictactoe_move 4)
382 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
385 TEST "Illegal to move when it's not your turn"
386 tictactoe_player_team X
387 result=$(tictactoe_move 4)
388 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
391 TEST "Legal move to center square"
392 tictactoe_player_team O
393 result=$(tictactoe_move 4)
394 test "$result" = '{"legal":true}'
397 TEST "Move to center square again is now illegal"
398 tictactoe_player_team X
399 result=$(tictactoe_move 4)
400 test "$result" = '{"legal":false,"message":"Square is already occupied"}'
403 TEST_SECTION "Empathy game"
405 TEST_SUBSECTION "Create a game and register 3 players"
407 TEST "Create the game"
408 empathy_game_id=$(new_game empathy)
409 test "$empathy_game_id" != ""
410 TEST_END $empathy_game_id
412 empathy_game_path=empathy/$empathy_game_id
416 cookie_file=".cookie-empathy-$1"
417 curl_put /profile "{ \"nickname\": \"$1\" }" "-c $cookie_file"
423 curl_get $1 "-m 0.1 $3" 2>&1 \
424 | grep "^event: $2" -A 1 \
426 | sed -e 's,^data: *,,'
429 empathy_player_name()
431 curl_get_event $empathy_game_path/events player-info "-b $1" | jq -r .name
434 TEST "Set 'alice' in session"
435 alice=$(empathy_profile alice)
436 test "$alice" = ".cookie-empathy-alice"
439 TEST "Register alice and verify name"
440 result=$(empathy_player_name $alice)
441 test "$result" = "alice"
445 bob=$(empathy_profile bob)
446 result=$(empathy_player_name $bob)
447 test "$result" = "bob"
450 TEST "Register charlie"
451 charlie=$(empathy_profile charlie)
452 result=$(empathy_player_name $charlie)
453 test "$result" = "charlie"
456 TEST_SUBSECTION "Category selection"
458 empathy_submit_prompt()
460 curl_post $empathy_game_path/prompts "{ \"items\": $2, \"prompt\": \"$3\"}" "-b $1"
463 TEST "Submit a category"
464 prompt_id=$(empathy_submit_prompt $alice 4 "4 things on a beach" | jq .id)
465 test "$prompt_id" = "1"
470 curl_post $empathy_game_path/vote/$2 "" "-b $1"
473 TEST "Vote on this category"
474 empathy_vote $alice $prompt_id
480 curl_post $empathy_game_path/start/$2 "" "-b $1"
483 TEST "Start the game with this category"
484 empathy_start $alice $prompt_id
490 curl_post $empathy_game_path/answer/$2 "{ \"answers\": [$3]}" "-b $1"
493 TEST_SUBSECTION "Submitting answers"
495 TEST "Submit from a non-player fails"
496 result=$(empathy_answer bogus $prompt_id '"sun", "sand", "water", "people"')
497 test "$result" = '{"valid":false,"message":"Player not found"}'
500 TEST "Submit from alice succeeds"
501 result=$(empathy_answer $alice $prompt_id '"sun", "sand", "water", "people"')
502 test "$result" = '{"valid":true}'
505 TEST "Submit from bob succeeds"
506 result=$(empathy_answer $bob $prompt_id '"surfers", "sands", "sunlight", "towels"')
507 test "$result" = '{"valid":true}'
510 TEST "Submit from charlie succeeds"
511 result=$(empathy_answer $charlie $prompt_id '"sunshine", "grains of sand", "wafer", "people"')
512 test "$result" = '{"valid":true}'
517 curl_post $empathy_game_path/judging/$2 "{ \"word_groups\": $3}" "-b $1"
520 TEST_SUBSECTION "Judging answers"
522 empathy_ambiguities()
524 curl_get_event $empathy_game_path/events game-state "-b $1" \
528 TEST "Received all unique words"
529 # echo here is to strip newlines
530 result=$(echo $(empathy_ambiguities $alice))
531 test "$result" = '"sun" "sand" "water" "people" "surfers" "sands" "sunlight" "towels" "sunshine" "grains of sand" "wafer"'
534 TEST "Submit word groups from alice"
535 result=$(empathy_judging $alice $prompt_id '[["sun","sunlight","sunshine"],["sand","sands","grains of sand"],["water","wafer"]]')
536 test "$result" = '{"valid":true}'
539 TEST "Submit word groups from bob"
540 result=$(empathy_judging $bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
541 test "$result" = '{"valid":true}'
544 TEST "Submit word groups from charlie"
545 result=$(empathy_judging $charlie $prompt_id '[["sunlight","sunshine"],["sand","grains of sand"]]')
546 test "$result" = '{"valid":true}'
551 curl_get_event $empathy_game_path/events game-state "-b $1" \
552 | jq '.scores.scores[]|.player,.score'
555 TEST_SUBSECTION "Scoring"
557 TEST "Verify final scores as expected"
558 # echo here is to strip newlines
559 result=$(echo $(empathy_scores $alice))
560 test "$result" = '"charlie" 9 "alice" 8 "bob" 7'