+TEST_SUBSECTION "Tic Tac Toe /player"
+
+TEST "Change name to 'newname'"
+tictactoe_player_name newname
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":""}'
+TEST_END
+
+TEST "Change team to 'X'"
+tictactoe_player_team X
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"X"}'
+TEST_END
+
+TEST "Change team to 'O'"
+tictactoe_player_team O
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
+TEST_END
+
+TEST "Verify cannot change team to 'Z'"
+tictactoe_player_team Z
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
+TEST_END
+
+TEST "Leave current team"
+tictactoe_player_team ""
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":""}'
+TEST_END
+
+TEST_SUBSECTION "Tic Tac Toe /move"
+
+TEST "First move doesn't require a team"
+result=$(tictactoe_move 0)
+test "$result" = '{"legal":true}'
+TEST_END
+
+TEST "Second move does require a team"
+result=$(tictactoe_move 4)
+test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
+TEST_END
+
+TEST "Illegal to move when it's not your turn"
+tictactoe_player_team X
+result=$(tictactoe_move 4)
+test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
+TEST_END
+
+TEST "Legal move to center square"
+tictactoe_player_team O
+result=$(tictactoe_move 4)
+test "$result" = '{"legal":true}'
+TEST_END
+
+TEST "Move to center square again is now illegal"
+tictactoe_player_team X
+result=$(tictactoe_move 4)
+test "$result" = '{"legal":false,"message":"Square is already occupied"}'
+TEST_END
+
+TEST_SECTION "Empathy game"
+
+TEST_SUBSECTION "Create a game and register 3 players"
+
+TEST "Create the game"
+empathy_game_id=$(new_game empathy)
+test "$empathy_game_id" != ""
+TEST_END $empathy_game_id
+
+empathy_game_path=empathy/$empathy_game_id
+
+# Usage: empathy_get <player_name> <endpoint> [curl_options]
+empathy_get()
+{
+ curl_get_cookie $1 $empathy_game_path/$2 "${3:-}"
+}
+
+# Usage: empathy_post <player_name> <endpoint> [data]
+empathy_post()
+{
+ curl_post_cookie $1 $empathy_game_path/$2 "${3:-}"
+}
+
+# Given a player name as $1 (eg. "empathy_player_activate alice") set both
+# $1_cookie and $1_pid (that is $alice_cookie and $alice_pid) to
+# a filename containing a cookie and the PID of a running event-streaming
+# process.
+empathy_player_activate()
+{
+ player="$1"
+ player_cookie=${player}_cookie
+ player_pid=${player}_pid
+
+ eval ${player_cookie}=".cookie-empathy-$player"
+ curl_put /profile "{ \"nickname\": \"$player\" }" "-c ${!player_cookie}"
+ empathy_get $player events >/dev/null 2>&1 &
+ eval ${player_pid}=$!
+}
+
+# Pulls a single named event out of the empathy event stream
+#
+# Usage: empathy_get_event <player_name> <event_name>
+empathy_get_event()
+{
+ empathy_get $1 events "-m 0.1" 2>&1 \
+ | grep "^event: $2" -A 1 \
+ | grep ^data: \
+ | sed -e 's,^data: *,,'
+}
+
+# Usage: empathy_player_name <player_name>
+empathy_player_name()
+{
+ empathy_get_event $1 player-info | jq -r .name
+}
+
+TEST "Set 'alice' in session"
+empathy_player_activate alice
+test "$alice_cookie" = ".cookie-empathy-alice"
+TEST_END
+
+TEST "Register alice and verify name"
+result=$(empathy_player_name alice)
+test "$result" = "alice"
+TEST_END
+
+TEST "Register bob"
+empathy_player_activate bob
+result=$(empathy_player_name bob)
+test "$result" = "bob"
+TEST_END
+
+TEST "Register charlie"
+empathy_player_activate charlie
+result=$(empathy_player_name charlie)
+test "$result" = "charlie"
+TEST_END
+
+TEST_SUBSECTION "Category selection"
+
+# Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
+empathy_submit_prompt()
+{
+ empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
+}
+
+TEST "Huge numbers are rejected"
+result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
+test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
+TEST_END
+
+TEST "Submit a category"
+prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
+test "$prompt_id" = "1"
+TEST_END
+
+# Usage: empathy_vote <player_name> <prompt_id>
+empathy_vote()
+{
+ empathy_post $1 vote/$2
+}
+
+TEST "Vote on this category"
+empathy_vote alice $prompt_id
+test "$?" = "0"
+TEST_END
+
+# Usage: empathy_start <player_name> <prompt_id>
+empathy_start()
+{
+ empathy_post $1 start/$2
+}
+
+TEST "Start the game with this category"
+empathy_start alice $prompt_id
+test "$?" = "0"
+TEST_END
+
+# Usage: empathy_answer <player_name> <prompt_id> <answers_string>
+empathy_answer()
+{
+ empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
+}
+
+TEST_SUBSECTION "Submitting answers"
+
+TEST "Submit from a non-player fails"
+bogus_cookie=/dev/null
+result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
+test "$result" = '{"valid":false,"message":"Player not found"}'
+TEST_END
+
+TEST "Submit from alice succeeds"
+result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
+test "$result" = '{"valid":true}'
+TEST_END
+
+TEST "Submit from bob succeeds"
+result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
+test "$result" = '{"valid":true}'
+TEST_END
+
+# Usage: empathy_ambiguities <player_name>
+empathy_ambiguities()
+{
+ empathy_get_event $1 game-state | jq .ambiguities
+}
+
+TEST "Judging hasn't started with player unsubmitted"
+result=$(echo $(empathy_ambiguities alice))
+test "$result" = "null"
+TEST_END
+
+TEST "Submit from charlie succeeds"
+result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
+test "$result" = '{"valid":true}'
+TEST_END
+
+TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
+
+TEST "Judging already started"
+result=$(echo $(empathy_ambiguities alice))
+test "$result" != "null"
+TEST_END
+
+TEST_SUBSECTION "Judging answers"
+
+# Usage: empathy_ambiguities_list <player_name>
+empathy_ambiguities_list()
+{
+ empathy_get_event $1 game-state | jq .ambiguities[]
+}
+
+TEST "Received all unique words"
+# echo here is to strip newlines
+result=$(echo $(empathy_ambiguities_list alice))
+test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
+TEST_END
+
+# Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
+empathy_judged()
+{
+ empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
+}
+
+TEST "Submit word groups from alice"
+result=$(empathy_judged alice $prompt_id '[["sun","SunLight","SunShine"],["sand","sands","Grains of Sand"],["water","wafer"]]')
+test "$result" = '{"valid":true}'
+TEST_END
+
+TEST "Submit word groups from bob"
+result=$(empathy_judged bob $prompt_id '[["sands","grains of sand"],["water","wafer"]]')
+test "$result" = '{"valid":true}'
+TEST_END
+
+# Usage: empathy_scores <player_name>
+empathy_scores()
+{
+ empathy_get_event $1 game-state | jq .scores
+}
+
+TEST "Scoring hasn't started with player unsubmitted"
+result=$(echo $(empathy_scores alice))
+test "$result" = "null"
+TEST_END
+
+TEST "Submit word groups from charlie"
+result=$(empathy_judged charlie $prompt_id '[["SunLight","SunShine"],["sand","Grains of Sand"]]')
+test "$result" = '{"valid":true}'
+TEST_END
+
+TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
+
+TEST "Scoring already started"
+result=$(echo $(empathy_scores alice))
+test "$result" != "null"
+TEST_END
+
+# Usage: empathy_scores_names_numbers <player_name>
+empathy_scores_names_numbers()
+{
+ empathy_get_event $1 game-state | jq '.scores.scores[]|.player,.score'