]> git.cworth.org Git - lmno-server/blob - test
Add some autofocus attributes to several forms
[lmno-server] / test
1 #!/bin/bash
2
3 # Arrange for some cleanup to be executed if the user interrupts the
4 # test sutie (for example, by pressing ControlC at the controlling
5 # terminal).
6 cleanup_and_report() {
7     empathy_deactivate_all >/dev/null 2>&1
8     TEST_REPORT
9     exit $?
10 }
11 trap cleanup_and_report INT
12
13 usage ()
14 {
15     echo "Usage:$0 <URL-to-test>"
16 }
17
18 if [ $# -lt 1 ]; then
19     echo "Error: No test URL given." >&2
20     echo "" >&2
21     usage >&2
22     exit 1
23 fi
24
25 URL=$1
26 CURL="curl --silent --show-error"
27
28 _TEST_SECTION()
29 {
30     echo ""
31     echo $1
32     echo $1 | sed -e "s/./$2/g"
33 }
34
35 TEST_SECTION()
36 {
37     _TEST_SECTION "$1" =
38 }
39
40 TEST_SUBSECTION()
41 {
42     _TEST_SECTION "$1" -
43 }
44
45 TEST()
46 {
47     printf "    $1"
48     printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
49     (( tests_total++ )) || true
50 }
51
52 # Result of test depends on the exit status of last command
53 TEST_END()
54 {
55     if [ $? -eq 0 ]; then
56         echo -n "OK"
57     else
58         (( tests_failed++ )) || true
59         echo -n "FAIL"
60     fi
61
62     # If we got an argument, append it after test result
63     if [ -n "$1" ]; then
64         echo " $1"
65     else
66         echo ""
67     fi
68 }
69
70 # Print report of all previous test results
71 TEST_REPORT()
72 {
73     echo ""
74     echo ""
75     echo "Test Report"
76     echo "==========="
77
78     if [ "$tests_failed" == "" ]; then
79         echo "All $tests_total tests passed."
80         echo ""
81         return 0
82     else
83         echo "$tests_failed of $tests_total tests failed."
84         echo ""
85         return 1
86     fi
87 }
88
89 # Does a string contain a regular expression pattern
90 #
91 # Example:
92 #
93 #    contains "All's well that ends well" "s.well"
94 contains()
95 {
96     grep -q "$2" <<< $1
97 }
98
99 # POST to a URL endpoint with optional JSON data
100 #
101 # Usage:
102 #
103 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
104 curl_post()
105 {
106     $CURL ${3:-} -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
107 }
108
109 # POST to a URL endpoint with optional JSON data using a cookie
110 #
111 # Usage:
112 #
113 # curl_post_cookie <name> <ENDPOINT> [data] [CURL_OPTIONS]
114 #
115 # Where <name> is a string for which there is a defined variable
116 # named ${name}_cookie which in turn holds a value that is a filename
117 # of a valid cookie
118 curl_post_cookie()
119 {
120     cookie=${1}_cookie
121     curl_post $2 "${3:-}" "-b ${!cookie} ${4:-}"
122 }
123
124 # PUT to a URL endpoint with optional JSON data
125 #
126 # Usage:
127 #
128 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
129 curl_put()
130 {
131     $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
132 }
133
134 # GET from a URL endpoint
135 #
136 # Usage:
137 #
138 # curl_get <ENDPOINT> [CURL_OPTIONS]
139 curl_get()
140 {
141     $CURL ${2:-} $URL/$1
142 }
143
144 # GET from a URL endpoint using a cookie
145 #
146 # Usage:
147 #
148 # curl_get_cookie <name> <ENDPOINT> [CURL_OPTIONS]
149 #
150 # Where <name> is a string for which there is a defined variable
151 # named ${name}_cookie which in turn holds a value that is a filename
152 # of a valid cookie
153 curl_get_cookie()
154 {
155     cookie=${1}_cookie
156     curl_get $2 "-b ${!cookie} ${3:-}"
157 }
158
159 # Create a new game of the specified engine type
160 #
161 # Usage:
162 #
163 # new_game <ENGINE>
164 new_game()
165 {
166     curl_post new/$1 | jq -r .
167 }
168
169 TEST_SECTION "LMNO (super-site for games)"
170
171 TEST_SUBSECTION "Testing home page"
172 home_page=$($CURL $URL)
173
174 TEST "Contains 'Join Game'"
175 contains "$home_page" "Join Game"
176 TEST_END
177
178 TEST "Contains 'Host a new game'"
179 contains "$home_page" "Host a new game"
180 TEST_END
181
182 TEST_SUBSECTION "Creating some new games"
183
184 TEST "Empires"
185 empires_game_id=$(new_game empires)
186 test "$empires_game_id" != ""
187 TEST_END $empires_game_id
188
189 TEST "Tic Tac Toe"
190 tictactoe_game_id=$(new_game tictactoe)
191 test "$tictactoe_game_id" != ""
192 TEST_END $tictactoe_game_id
193
194 TEST_SUBSECTION "Test redirects"
195
196 TEST "Redirect of /GAMEID at top level"
197 redirect=$(curl_get $empires_game_id)
198 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
199 TEST_END
200
201 TEST "Redirect of lowercase /gameid at top level"
202 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
203 redirect=$(curl_get $empires_game_id_lower)
204 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
205 TEST_END
206
207 TEST "Redirect of lowercase /empires/gameid"
208 redirect=$(curl_get empires/$empires_game_id_lower)
209 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
210 TEST_END
211
212 TEST_SECTION "Empires game"
213
214 empires_game_path=empires/$empires_game_id
215
216 TEST_SUBSECTION "Empires game /register"
217
218 empires_register()
219 {
220     curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
221 }
222
223 empires_players_string()
224 {
225     curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
226 }
227
228 empires_characters_string()
229 {
230     curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
231 }
232
233 TEST "Registering a player returns an ID"
234 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
235 test "$carl_id" = "1"
236 TEST_END
237
238 TEST "Registering several more players"
239 empires_register Richard "Bob Hope" > /dev/null
240 empires_register Kevin "Elvis Presley" > /dev/null
241 empires_register Stacy Phineas > /dev/null
242 empires_register David "Red Power Ranger" > /dev/null
243 empires_register Nancy "Audrey Hepburn" > /dev/null
244 bogus_id=$(empires_register Bogus "Mr. Bogus")
245 TEST_END
246
247 TEST 'Verify complete players list (with "Bogus")'
248 players=$(empires_players_string)
249 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
250 TEST_END
251
252 TEST 'Verify complete players list (with "Mr. Bogus")'
253 characters=$(empires_characters_string)
254 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
255 TEST_END
256
257 TEST_SUBSECTION "Empires game /deregister"
258
259 empires_deregister()
260 {
261     curl_post $empires_game_path/deregister/$1
262 }
263
264 TEST "Removing the bogus player"
265 empires_deregister $bogus_id
266 TEST_END
267
268 TEST 'Verify modified players list (w/o "Bogus")"'
269 players=$(empires_players_string)
270 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
271 TEST_END
272
273 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
274 characters=$(empires_characters_string)
275 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
276 TEST_END
277
278 TEST_SUBSECTION "Empires game /capture"
279
280 empires_capture()
281 {
282     curl_post $empires_game_path/capture/$1/$2
283 }
284
285 empires_empires_string()
286 {
287     # Get empires as a compact string (much more compact than JSON)
288     curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
289 }
290
291 TEST "Verify empires before any captures"
292 empires=$(empires_empires_string)
293 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
294 TEST_END
295
296 TEST "Perform some captures"
297 empires_capture 1 2
298 empires_capture 3 5
299 empires_capture 4 6
300 empires_capture 3 4
301 TEST_END
302
303 TEST "Verify empires after captures"
304 empires=$(empires_empires_string)
305 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
306 TEST_END
307
308 TEST_SUBSECTION "Empires game /liberate"
309
310 empires_liberate()
311 {
312     curl_post $empires_game_path/liberate/$1
313 }
314
315 TEST "Liberate a player"
316 empires_liberate 2
317 TEST_END
318
319 TEST "Verify empires after liberate"
320 empires=$(empires_empires_string)
321 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
322 TEST_END
323
324 TEST_SUBSECTION "Empires game /reset"
325
326 empires_reset()
327 {
328     curl_post $empires_game_path/reset
329 }
330
331 TEST "Reset the game"
332 empires_reset
333 TEST_END
334
335 TEST "Verify players is now empty"
336 players=$(empires_players_string)
337 test "$players" = ""
338 TEST_END
339
340 TEST_SECTION "Tic Tac Toe game"
341
342 tictactoe_game_path=tictactoe/$tictactoe_game_id
343
344 tictactoe_profile()
345 {
346     curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-tictactoe"
347 }
348
349 tictactoe_move()
350 {
351     curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .cookie-tictactoe"
352 }
353
354 tictactoe_player_info()
355 {
356     curl_get $tictactoe_game_path/events  "-m 0.1 -b .cookie-tictactoe" 2>&1 \
357         | grep player-info -A 1 \
358         | grep ^data
359 }
360
361 tictactoe_player_name()
362 {
363     curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-tictactoe"
364 }
365
366 tictactoe_player_team()
367 {
368     curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-tictactoe"
369 }
370
371 TEST_SUBSECTION "Tic Tac Toe player-info"
372
373 TEST "Hit LMNO /profile to set name to 'curl'"
374 tictactoe_profile curl
375 TEST_END
376
377 TEST "Verify player-info event reports 'curl' name"
378 result=$(tictactoe_player_info)
379 test "$result" = 'data: {"id":1,"active":true,"name":"curl","team":""}'
380 TEST_END
381
382 TEST_SUBSECTION "Tic Tac Toe /player"
383
384 TEST "Change name to 'newname'"
385 tictactoe_player_name newname
386 result=$(tictactoe_player_info)
387 test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":""}'
388 TEST_END
389
390 TEST "Change team to 'X'"
391 tictactoe_player_team X
392 result=$(tictactoe_player_info)
393 test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"X"}'
394 TEST_END
395
396 TEST "Change team to 'O'"
397 tictactoe_player_team O
398 result=$(tictactoe_player_info)
399 test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"O"}'
400 TEST_END
401
402 TEST "Verify cannot change team to 'Z'"
403 tictactoe_player_team Z
404 result=$(tictactoe_player_info)
405 test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":"O"}'
406 TEST_END
407
408 TEST "Leave current team"
409 tictactoe_player_team ""
410 result=$(tictactoe_player_info)
411 test "$result" = 'data: {"id":1,"active":true,"name":"newname","team":""}'
412 TEST_END
413
414 TEST_SUBSECTION "Tic Tac Toe /move"
415
416 TEST "First move doesn't require a team"
417 result=$(tictactoe_move 0)
418 test "$result" = '{"legal":true}'
419 TEST_END
420
421 TEST "Second move does require a team"
422 result=$(tictactoe_move 4)
423 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
424 TEST_END
425
426 TEST "Illegal to move when it's not your turn"
427 tictactoe_player_team X
428 result=$(tictactoe_move 4)
429 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
430 TEST_END
431
432 TEST "Legal move to center square"
433 tictactoe_player_team O
434 result=$(tictactoe_move 4)
435 test "$result" = '{"legal":true}'
436 TEST_END
437
438 TEST "Move to center square again is now illegal"
439 tictactoe_player_team X
440 result=$(tictactoe_move 4)
441 test "$result" = '{"legal":false,"message":"Square is already occupied"}'
442 TEST_END
443
444 TEST_SECTION "Scribe game"
445
446 TEST "Create Scribe game"
447 scribe_game_id=$(new_game scribe)
448 test "$scribe_game_id" != ""
449 TEST_END $scribe_game_id
450
451 scribe_game_path=scribe/$scribe_game_id
452
453 # Usage: scribe_profile <name>
454 scribe_profile()
455 {
456     curl_put /profile "{ \"nickname\": \"$1\" }" "-c .cookie-scribe"
457 }
458
459 # Pulls a single named event out of the scribe event stream
460 #
461 # Usage: scribe_get_event <event_name>
462 scribe_get_event()
463 {
464     curl_get $scribe_game_path/events "-m 0.1 -b .cookie-scribe" 2>&1 \
465         | grep "^event: $1" -A 1 \
466         | grep ^data: \
467         | sed -e 's,^data: *,,'
468 }
469
470 # Usage: scribe_player_name
471 scribe_get_player_name()
472 {
473     scribe_get_event player-info | jq -r .name
474 }
475
476 TEST_SUBSECTION "Scribe player-info"
477
478 TEST "Hit LMNO /profile to set name to 'test-suite'"
479 scribe_profile test-suite
480 TEST_END
481
482 TEST "Verify player-info event reports 'test-suite' name"
483 result=$(scribe_get_player_name)
484 test "$result" = "test-suite"
485 TEST_END
486
487 scribe_player_info()
488 {
489     scribe_get_event player-info
490 }
491
492 scribe_set_player_name()
493 {
494     curl_put $scribe_game_path/player "{ \"name\": \"$1\" }" "-b .cookie-scribe"
495 }
496
497 scribe_set_player_team()
498 {
499     curl_put $scribe_game_path/player "{ \"team\": \"$1\" }" "-b .cookie-scribe"
500 }
501
502 TEST_SUBSECTION "Scribe /player"
503
504 TEST "Change name to 'testy'"
505 scribe_set_player_name testy
506 result=$(scribe_player_info)
507 test "$result" = '{"id":1,"active":true,"name":"testy","team":""}'
508 TEST_END
509
510 TEST "Change team to '+'"
511 scribe_set_player_team +
512 result=$(scribe_player_info)
513 test "$result" = '{"id":1,"active":true,"name":"testy","team":"+"}'
514 TEST_END
515
516 TEST "Change team to 'o'"
517 scribe_set_player_team o
518 result=$(scribe_player_info)
519 test "$result" = '{"id":1,"active":true,"name":"testy","team":"o"}'
520 TEST_END
521
522 TEST "Verify cannot change team to 'X'"
523 scribe_set_player_team X
524 result=$(scribe_player_info)
525 test "$result" = '{"id":1,"active":true,"name":"testy","team":"o"}'
526 TEST_END
527
528 TEST "Leave current team"
529 scribe_set_player_team ""
530 result=$(scribe_player_info)
531 test "$result" = '{"id":1,"active":true,"name":"testy","team":""}'
532 TEST_END
533
534 scribe_move()
535 {
536     curl_post $scribe_game_path/move "{ \"move\": $1 }" "-b .cookie-scribe"
537 }
538
539 TEST_SUBSECTION "Scribe /move"
540
541 TEST "First move doesn't require a team"
542 result=$(scribe_move '[4,0]')
543 test "$result" = '{"legal":true}'
544 TEST_END
545
546 TEST "Second move does require a team"
547 result=$(scribe_move '[0,3]')
548 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
549 TEST_END
550
551 TEST "Illegal to move when it's not your turn"
552 scribe_set_player_team +
553 result=$(scribe_move '[0,3]')
554 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
555 TEST_END
556
557 TEST "Legal move to an empty square"
558 scribe_set_player_team o
559 result=$(scribe_move '[0,3]')
560 test "$result" = '{"legal":true}'
561 TEST_END
562
563 TEST "Move to same again is now illegal"
564 scribe_set_player_team +
565 result=$(scribe_move '[0,3]')
566 test "$result" = '{"legal":false,"message":"Square is already occupied"}'
567 TEST_END
568
569 TEST "Move must be in correct mini-grid by last move"
570 scribe_set_player_team +
571 result=$(scribe_move '[1,8]')
572 test "$result" = '{"legal":false,"message":"Move is inconsistent with your previous move"}'
573 TEST_END
574
575 TEST "Move in correct mini-grid is now legal"
576 result=$(scribe_move '[0,8]')
577 test "$result" = '{"legal":true}'
578 TEST_END
579
580 TEST "Several moves to fill up the mini-grid 0"
581 scribe_set_player_team o
582 scribe_move '[3,0]' >/dev/null
583 scribe_set_player_team +
584 scribe_move '[8,0]' >/dev/null
585 scribe_set_player_team o
586 scribe_move '[0,0]' >/dev/null
587 scribe_set_player_team +
588 scribe_move '[0,1]' >/dev/null
589 scribe_set_player_team o
590 scribe_move '[0,2]' >/dev/null
591 scribe_set_player_team +
592 scribe_move '[1,0]' >/dev/null
593 scribe_set_player_team o
594 scribe_move '[2,0]' >/dev/null
595 scribe_set_player_team +
596 scribe_move '[0,5]' >/dev/null
597 scribe_set_player_team o
598 scribe_move '[0,6]' >/dev/null
599 scribe_set_player_team +
600 scribe_move '[5,0]' >/dev/null
601 scribe_set_player_team o
602 scribe_move '[6,0]' >/dev/null
603 scribe_set_player_team +
604 scribe_move '[0,7]' >/dev/null
605 scribe_set_player_team o
606 scribe_move '[0,4]' >/dev/null
607 scribe_set_player_team +
608 result=$(scribe_move '[7,0]')
609 test "$result" = '{"legal":true}'
610 TEST_END
611
612 TEST "Full mini grid allows a free move"
613 scribe_set_player_team o
614 scribe_move '[4,1]' >/dev/null
615 scribe_set_player_team +
616 result=$(scribe_move '[1,1]')
617 test "$result" = '{"legal":true}'
618 TEST_END
619
620 TEST "Player o forms Cross glyph"
621 scribe_set_player_team o
622 scribe_move '[1,4]' >/dev/null
623 scribe_set_player_team +
624 scribe_move '[1,2]' >/dev/null
625 scribe_set_player_team o
626 scribe_move '[4,4]' >/dev/null
627 scribe_set_player_team +
628 scribe_move '[2,1]' >/dev/null
629 scribe_set_player_team o
630 scribe_move '[4,7]' >/dev/null
631 scribe_set_player_team +
632 scribe_move '[1,5]' >/dev/null
633 scribe_set_player_team o
634 scribe_move '[7,4]' >/dev/null
635 scribe_set_player_team +
636 scribe_move '[5,1]' >/dev/null
637 scribe_set_player_team o
638 scribe_move '[4,3]' >/dev/null
639 scribe_set_player_team +
640 scribe_move '[1,8]' >/dev/null
641 scribe_set_player_team o
642 scribe_move '[3,4]' >/dev/null
643 scribe_set_player_team +
644 scribe_move '[8,1]' >/dev/null
645 scribe_set_player_team o
646 result=$(scribe_move '[4,5]')
647 test "$result" = '{"legal":true}'
648 TEST_END
649
650 TEST "Player + forms J glyph"
651 scribe_set_player_team +
652 result=$(scribe_move '[1,7]')
653 test "$result" = '{"legal":true}'
654 TEST_END
655
656 TEST "Player + forms Earring glyph"
657 scribe_set_player_team o
658 scribe_move '[5,4]' >/dev/null
659 scribe_set_player_team +
660 scribe_move '[7,1]' >/dev/null
661 scribe_set_player_team o
662 scribe_move '[4,6]' >/dev/null
663 scribe_set_player_team +
664 result=$(scribe_move '[1,3]')
665 test "$result" = '{"legal":true}'
666 TEST_END
667
668 TEST "Player o forms House glyph"
669 scribe_set_player_team o
670 scribe_move '[6,4]' >/dev/null
671 scribe_set_player_team +
672 scribe_move '[3,1]' >/dev/null
673 scribe_set_player_team o
674 result=$(scribe_move '[4,8]')
675 test "$result" = '{"legal":true}'
676 TEST_END
677
678 TEST "Player + forms O glyph"
679 scribe_set_player_team +
680 result=$(scribe_move '[1,6]')
681 test "$result" = '{"legal":true}'
682 TEST_END
683
684 TEST "Player o forms T glyph"
685 scribe_set_player_team o
686 scribe_move '[8,6]' >/dev/null
687 scribe_set_player_team +
688 scribe_move '[6,7]' >/dev/null
689 scribe_set_player_team o
690 scribe_move '[6,3]' >/dev/null
691 scribe_set_player_team +
692 scribe_move '[7,8]' >/dev/null
693 scribe_set_player_team o
694 scribe_move '[3,6]' >/dev/null
695 scribe_set_player_team +
696 scribe_move '[8,2]' >/dev/null
697 scribe_set_player_team o
698 scribe_move '[6,6]' >/dev/null
699 scribe_set_player_team +
700 scribe_move '[2,8]' >/dev/null
701 scribe_set_player_team o
702 result=$(scribe_move '[6,5]')
703 test "$result" = '{"legal":true}'
704 TEST_END
705
706 TEST "Player o forms Chair glyph"
707 scribe_set_player_team +
708 scribe_move '[8,5]' >/dev/null
709 scribe_set_player_team o
710 scribe_move '[5,6]' >/dev/null
711 scribe_set_player_team +
712 scribe_move '[5,8]' >/dev/null
713 scribe_set_player_team o
714 result=$(scribe_move '[6,2]')
715 test "$result" = '{"legal":true}'
716 TEST_END
717
718 TEST "Player + forms Bomber glyph"
719 scribe_set_player_team +
720 scribe_move '[8,8]' >/dev/null
721 scribe_set_player_team o
722 scribe_move '[2,6]' >/dev/null
723 scribe_set_player_team +
724 result=$(scribe_move '[8,4]')
725 test "$result" = '{"legal":true}'
726 TEST_END
727
728 TEST "Player o forms H glyph"
729 scribe_set_player_team o
730 result=$(scribe_move '[6,8]')
731 test "$result" = '{"legal":true}'
732 TEST_END
733
734 TEST "Player + forms 6-Block glyph"
735 scribe_set_player_team +
736 scribe_move '[4,2]' >/dev/null
737 scribe_set_player_team o
738 scribe_move '[8,3]' >/dev/null
739 scribe_set_player_team +
740 scribe_move '[2,4]' >/dev/null
741 scribe_set_player_team o
742 scribe_move '[3,3]' >/dev/null
743 scribe_set_player_team +
744 scribe_move '[2,2]' >/dev/null
745 scribe_set_player_team o
746 scribe_move '[3,7]' >/dev/null
747 scribe_set_player_team +
748 scribe_move '[2,5]' >/dev/null
749 scribe_set_player_team o
750 scribe_move '[7,3]' >/dev/null
751 scribe_set_player_team +
752 scribe_move '[5,2]' >/dev/null
753 scribe_set_player_team o
754 scribe_move '[3,2]' >/dev/null
755 scribe_set_player_team +
756 result=$(scribe_move '[2,7]')
757 test "$result" = '{"legal":true}'
758 TEST_END
759
760 TEST "Player o forms Ottoman glyph"
761 scribe_set_player_team o
762 scribe_move '[2,3]' >/dev/null
763 scribe_set_player_team +
764 scribe_move '[7,7]' >/dev/null
765 scribe_set_player_team o
766 scribe_move '[3,5]' >/dev/null
767 scribe_set_player_team +
768 scribe_move '[7,2]' >/dev/null
769 scribe_set_player_team o
770 scribe_move '[5,3]' >/dev/null
771 scribe_set_player_team +
772 scribe_move '[7,6]' >/dev/null
773 scribe_set_player_team o
774 result=$(scribe_move '[3,8]')
775 test "$result"='{"legal":true}'
776 TEST_END
777
778 TEST "Player + forms J glyph"
779 scribe_set_player_team +
780 scribe_move '[6,1]' >/dev/null
781 scribe_set_player_team o
782 scribe_move '[8,7]' >/dev/null
783 scribe_set_player_team +
784 scribe_move '[5,5]' >/dev/null
785 scribe_set_player_team o
786 scribe_move '[7,5]' >/dev/null
787 scribe_set_player_team +
788 result=$(scribe_move '[5,7]')
789 test "$result"='{"legal":true}'
790 TEST_END
791
792 TEST_SECTION "Empathy game"
793
794 TEST_SUBSECTION "Create a game and register 3 players"
795
796 TEST "Create the game"
797 empathy_game_id=$(new_game empathy)
798 test "$empathy_game_id" != ""
799 TEST_END $empathy_game_id
800
801 empathy_game_path=empathy/$empathy_game_id
802
803 # Usage: empathy_get <player_name> <endpoint> [curl_options]
804 empathy_get()
805 {
806     curl_get_cookie $1 $empathy_game_path/$2 "${3:-}"
807 }
808
809 # Usage: empathy_post <player_name> <endpoint> [data]
810 empathy_post()
811 {
812     curl_post_cookie $1 $empathy_game_path/$2 "${3:-}"
813 }
814
815 # Given a player name as $1 (eg. "empathy_player_activate alice") set both
816 # $1_cookie and $1_pid (that is $alice_cookie and $alice_pid) to
817 # a filename containing a cookie and the PID of a running event-streaming
818 # process.
819 empathy_player_activate()
820 {
821     player="$1"
822     player_cookie=${player}_cookie
823     player_pid=${player}_pid
824
825     eval ${player_cookie}=".cookie-empathy-$player"
826     curl_put /profile "{ \"nickname\": \"$player\" }" "-c ${!player_cookie}"
827     empathy_get $player events >/dev/null 2>&1 &
828     eval ${player_pid}=$!
829     empathy_players+=($player)
830 }
831
832 empathy_player_reactivate()
833 {
834     player="$1"
835     player_pid=${player}_pid
836
837     empathy_get $player events >/dev/null 2>&1 &
838     eval ${player_pid}=$!
839 }
840
841 # Usage: empathy_player_deactivate <player_name>
842 empathy_player_deactivate()
843 {
844     player="$1"
845     player_pid=${player}_pid
846     if [ "${!player_pid}" != "" ]; then
847         pkill -P ${!player_pid}
848     fi
849     eval ${player_pid}=""
850 }
851
852 empathy_deactivate_all()
853 {
854     for player in ${empathy_players[*]}; do
855         empathy_player_deactivate $player
856     done
857 }
858
859 # Pulls a single named event out of the empathy event stream
860 #
861 # Usage: empathy_get_event <player_name> <event_name>
862 empathy_get_event()
863 {
864     empathy_get $1 events "-m 0.1" 2>&1 \
865         | grep "^event: $2" -A 1 \
866         | grep ^data: \
867         | sed -e 's,^data: *,,'
868 }
869
870 # Usage: empathy_player_name <player_name>
871 empathy_player_name()
872 {
873     empathy_get_event $1 player-info | jq -r .name
874 }
875
876 TEST "Set 'alice' in session"
877 empathy_player_activate alice
878 test "$alice_cookie" = ".cookie-empathy-alice"
879 TEST_END
880
881 TEST "Register alice and verify name"
882 result=$(empathy_player_name alice)
883 test "$result" = "alice"
884 TEST_END
885
886 TEST "Register bob"
887 empathy_player_activate bob
888 result=$(empathy_player_name bob)
889 test "$result" = "bob"
890 TEST_END
891
892 TEST "Register charlie"
893 empathy_player_activate charlie
894 result=$(empathy_player_name charlie)
895 test "$result" = "charlie"
896 TEST_END
897
898 TEST_SUBSECTION "Category selection"
899
900 # Usage: empathy_submit_prompt <player_name> <count> <prompt_string>
901 empathy_submit_prompt()
902 {
903     empathy_post $1 prompts "{ \"items\": $2, \"prompt\": \"$3\"}"
904 }
905
906 TEST "Huge numbers are rejected"
907 result=$(empathy_submit_prompt alice 10000 "10,000 Maniacs")
908 test "$result" = '{"valid":false,"message":"Maximum number of items is 20"}'
909 TEST_END
910
911 TEST "Submit a category"
912 prompt_id=$(empathy_submit_prompt alice 4 "4 things on a beach" | jq .id)
913 test "$prompt_id" = "1"
914 TEST_END
915
916 # Usage: empathy_vote <player_name> <prompt_id>
917 empathy_vote()
918 {
919     empathy_post $1 vote/$2
920 }
921
922 TEST "Vote on this category"
923 empathy_vote alice $prompt_id
924 test "$?" = "0"
925 TEST_END
926
927 # Usage: empathy_start <player_name> <prompt_id>
928 empathy_start()
929 {
930     empathy_post $1 start/$2
931 }
932
933 TEST "Start the game with this category"
934 empathy_start alice $prompt_id
935 test "$?" = "0"
936 TEST_END
937
938 # Usage: empathy_answer <player_name> <prompt_id> <answers_string>
939 empathy_answer()
940 {
941     empathy_post $1 answer/$2 "{ \"answers\": [$3]}"
942 }
943
944 TEST_SUBSECTION "Submitting answers"
945
946 TEST "Submit from a non-player fails"
947 bogus_cookie=/dev/null
948 result=$(empathy_answer bogus $prompt_id '"Sun", "Sand", "Water", "People"')
949 test "$result" = '{"valid":false,"message":"Player not found"}'
950 TEST_END
951
952 TEST "Submit from alice succeeds"
953 result=$(empathy_answer alice $prompt_id '"sun", "sand", "water", "people"')
954 test "$result" = '{"valid":true}'
955 TEST_END
956
957 TEST "Submit from bob succeeds"
958 result=$(empathy_answer bob $prompt_id '"sand", "sands", "SunLight", "towels"')
959 test "$result" = '{"valid":true}'
960 TEST_END
961
962 # Usage: empathy_ambiguities <player_name>
963 empathy_ambiguities()
964 {
965     empathy_get_event $1 game-state | jq .ambiguities
966 }
967
968 TEST "Judging hasn't started with player unsubmitted"
969 result=$(echo $(empathy_ambiguities alice))
970 test "$result" = "null"
971 TEST_END
972
973 TEST "Submit from charlie succeeds"
974 result=$(empathy_answer charlie $prompt_id '"SunShine", "Grains of Sand", "wafer", "people"')
975 test "$result" = '{"valid":true}'
976 TEST_END
977
978 TEST_SUBSECTION "Transition from answering to judging (no voting needed)"
979
980 TEST "Judging already started"
981 result=$(echo $(empathy_ambiguities alice))
982 test "$result" != "null"
983 TEST_END
984
985 TEST_SUBSECTION "Judging answers"
986
987 # Usage: empathy_ambiguities_list <player_name>
988 empathy_ambiguities_list()
989 {
990     empathy_get_event $1 game-state | jq .ambiguities[]
991 }
992
993 TEST "Received all unique words"
994 # echo here is to strip newlines
995 result=$(echo $(empathy_ambiguities_list alice))
996 test "$result" = '"Grains of Sand" "people" "sand" "sands" "sun" "SunLight" "SunShine" "towels" "wafer" "water"'
997 TEST_END
998
999 # Usage: empathy_judged <player_name> <prompt_id> <word_groups_string>
1000 empathy_judged()
1001 {
1002     empathy_post $1 judged/$2 "{ \"word_groups\": $3}"
1003 }
1004
1005 TEST "Submit word groups from alice"
1006 result=$(empathy_judged alice $prompt_id '[{"words":["sun","SunLight","SunShine"],"kudos":false},{"words":["sand","sands","Grains of Sand"],"kudos":false},{"words":["water","wafer"],"kudos":false}]')
1007 test "$result" = '{"valid":true}'
1008 TEST_END
1009
1010 TEST "Submit word groups from bob"
1011 result=$(empathy_judged bob $prompt_id '[{"words":["sands","grains of sand"],"kudos":false},{"words":["water","wafer"],"kudos":false}]')
1012 test "$result" = '{"valid":true}'
1013 TEST_END
1014
1015 # Usage: empathy_scores <player_name>
1016 empathy_scores()
1017 {
1018     empathy_get_event $1 game-state | jq .scores
1019 }
1020
1021 TEST "Scoring hasn't started with player unsubmitted"
1022 result=$(echo $(empathy_scores alice))
1023 test "$result" = "null"
1024 TEST_END
1025
1026 TEST "Submit word groups from charlie"
1027 result=$(empathy_judged charlie $prompt_id '[{"words":["SunLight","SunShine"],"kudos":false},{"words":["sand","Grains of Sand"],"kudos":false}]')
1028 test "$result" = '{"valid":true}'
1029 TEST_END
1030
1031 TEST_SUBSECTION "Transition from judging to scoring (no voting needed)"
1032
1033 TEST "Scoring already started"
1034 result=$(echo $(empathy_scores alice))
1035 test "$result" != "null"
1036 TEST_END
1037
1038 # Usage: empathy_scores_names_numbers <player_name>
1039 empathy_scores_names_numbers()
1040 {
1041     empathy_get_event $1 game-state | jq '.scores.scores[]|.players[],.score'
1042 }
1043
1044 TEST_SUBSECTION "Scoring"
1045
1046 TEST "Verify final scores as expected"
1047 # echo here is to strip newlines
1048 result=$(echo $(empathy_scores_names_numbers alice))
1049 test "$result" = '"charlie" 9 "alice" 8 "bob" 6'
1050 TEST_END
1051
1052 # Usage: empathy_words_submitted <player_name>
1053 empathy_words_submitted()
1054 {
1055     empathy_get_event $1 game-state | jq '.scores.words[].word'
1056 }
1057
1058 TEST "Verify final list of words submitted"
1059 # echo here is to strip newlines
1060 result=$(echo $(empathy_words_submitted alice))
1061 test "$result" = '"Grains of Sand/sand/sands" "SunLight/SunShine" "wafer/water" "people" "sun" "towels"'
1062 TEST_END
1063
1064 TEST_SUBSECTION "New game (using voting to advance phases)"
1065
1066 empathy_reset()
1067 {
1068     curl_post $empathy_game_path/reset
1069 }
1070
1071 TEST "Any post to /reset resets the game"
1072 empathy_reset
1073 test "$?" = "0"
1074 TEST_END
1075
1076 TEST "Verify scoring is over"
1077 result=$(echo $(empathy_scores alice))
1078 test "$result" = "null"
1079 TEST_END
1080
1081 # Usage: empathy_answering <player_name> <prompt_id>
1082 empathy_answering()
1083 {
1084     empathy_post $1 answering/$2
1085 }
1086
1087 TEST "Start 4-player game, 3 submissions"
1088 empathy_player_activate dale
1089 result=$(empathy_player_name dale)
1090 test "$result" = "dale"
1091 prompt_id=$(empathy_submit_prompt alice 4 "3 little words" | jq .id)
1092 empathy_start alice $prompt_id
1093 empathy_answer alice   $prompt_id '"I",    "love", "you"' >/dev/null
1094 empathy_answer bob     $prompt_id '"I",    "love", "food"' >/dev/null
1095 empathy_answer charlie $prompt_id '"food", "is",   "good"' >/dev/null
1096 result=$(empathy_answering dale $prompt_id)
1097 test "$result" = '{"valid":true}'
1098 TEST_END
1099
1100 TEST "Judging hasn't started with player unsubmitted"
1101 result=$(echo $(empathy_ambiguities alice))
1102 test "$result" = "null"
1103 TEST_END
1104
1105 # Usage: empathy_end_answers <player_name> <prompt_id>
1106 empathy_end_answers()
1107 {
1108     empathy_post $1 end-answers/$2
1109 }
1110
1111 TEST "Minority of players vote to end answering"
1112 empathy_end_answers alice $prompt_id
1113 empathy_end_answers bob $prompt_id
1114 test "$?" = "0"
1115 TEST_END
1116
1117 TEST "Judging still hasn't started"
1118 result=$(echo $(empathy_ambiguities alice))
1119 test "$result" = "null"
1120 TEST_END
1121
1122 TEST "Majority of players vote to end answering"
1123 empathy_end_answers charlie $prompt_id
1124 test "$?" = "0"
1125 TEST_END
1126
1127 TEST "Judging has now started"
1128 result=$(echo $(empathy_ambiguities alice))
1129 test "$result" != "null"
1130 TEST_END
1131
1132 # Usage: empathy_players_judging <player_name>
1133 empathy_players_judging()
1134 {
1135     empathy_get_event $1 game-state | jq .players_judging[]
1136 }
1137
1138 TEST "Verify active players listed as judging"
1139 # echo here is to strip newlines
1140 result=$(echo $(empathy_players_judging alice))
1141 test "$result" = '"alice" "bob" "charlie"'
1142 TEST_END
1143
1144 TEST "Submit word groups from majority"
1145 empathy_judged alice $prompt_id '[]' >/dev/null
1146 result=$(empathy_judged bob $prompt_id '[]')
1147 test "$result" = '{"valid":true}'
1148 TEST_END
1149
1150 TEST "Scoring hasn't started with player unsubmitted"
1151 result=$(echo $(empathy_scores alice))
1152 test "$result" = "null"
1153 TEST_END
1154
1155 # Usage: empathy_end_judging <player_name> <prompt_id>
1156 empathy_end_judging()
1157 {
1158     empathy_post $1 end-judging/$2
1159 }
1160
1161 TEST "Minority of players vote to end judging"
1162 empathy_end_judging alice $prompt_id
1163 test "$?" = "0"
1164 TEST_END
1165
1166 TEST "Scoring still hasn't started"
1167 result=$(echo $(empathy_scores alice))
1168 test "$result" = "null"
1169 TEST_END
1170
1171 TEST "Majority of players vote to end judging"
1172 empathy_end_judging bob $prompt_id
1173 test "$?" = "0"
1174 TEST_END
1175
1176 TEST "Scoring has now started"
1177 result=$(echo $(empathy_scores alice))
1178 test "$result" != "null"
1179 TEST_END
1180
1181 TEST_SUBSECTION "New game (no voting needed when all answered players judge)"
1182
1183 TEST "Start 4-player game, 3 submissions"
1184 empathy_reset
1185 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
1186 empathy_start alice $prompt_id
1187 empathy_answer alice   $prompt_id '"truth"' >/dev/null
1188 empathy_answer bob     $prompt_id '"truth"' >/dev/null
1189 empathy_answer charlie $prompt_id '"dare"' >/dev/null
1190 empathy_end_answers alice $prompt_id
1191 empathy_end_answers bob $prompt_id
1192 empathy_end_answers charlie $prompt_id
1193 test "$?" = "0"
1194 TEST_END
1195
1196 TEST "Submit word groups from 2 players"
1197 empathy_judged alice $prompt_id '[]' >/dev/null
1198 result=$(empathy_judged bob $prompt_id '[]')
1199 test "$result" = '{"valid":true}'
1200 TEST_END
1201
1202 TEST "Scoring hasn't started with player unsubmitted"
1203 result=$(echo $(empathy_scores alice))
1204 test "$result" = "null"
1205 TEST_END
1206
1207 TEST "Submit word groups from a non-answering player"
1208 result=$(empathy_judged dale $prompt_id '[]')
1209 test "$result" = '{"valid":true}'
1210 TEST_END
1211
1212 TEST "Scoring still hasn't started"
1213 result=$(echo $(empathy_scores alice))
1214 test "$result" = "null"
1215 TEST_END
1216
1217 TEST "Submit word groups from last answering player"
1218 result=$(empathy_judged charlie $prompt_id '[]')
1219 test "$result" = '{"valid":true}'
1220 TEST_END
1221
1222 TEST "Scoring has now started"
1223 result=$(echo $(empathy_scores alice))
1224 test "$result" != "null"
1225 TEST_END
1226
1227 TEST_SUBSECTION "Non players don't affect judging requirements"
1228
1229 TEST "Start 2-player game with 6 registered players"
1230 empathy_reset
1231 empathy_player_activate eric
1232 empathy_player_activate fred
1233 prompt_id=$(empathy_submit_prompt alice 4 "1 truth or dare" | jq .id)
1234 empathy_start alice $prompt_id
1235 empathy_answer alice $prompt_id '"truth"' >/dev/null
1236 empathy_answer bob   $prompt_id '"true"' >/dev/null
1237 empathy_end_answers alice $prompt_id
1238 empathy_end_answers bob $prompt_id
1239 test "$?" = "0"
1240 TEST_END
1241
1242 TEST "1 player votes for a match"
1243 empathy_judged alice $prompt_id '[{"words":["truth","true"],"kudos":false}]' >/dev/null
1244 result=$(empathy_judged bob $prompt_id '[]')
1245 test "$result" = '{"valid":true}'
1246 TEST_END
1247
1248 TEST "Verify the match passed the vote"
1249 # echo here is to strip newlines
1250 result=$(echo $(empathy_scores_names_numbers alice))
1251 test "$result" = '"alice" "bob" 2 "charlie" "dale" "eric" "fred" 0'
1252 TEST_END
1253
1254 echo ""
1255 echo "NOTE: Slow tests ahead!"
1256 echo "If you are impatient and somehow \"know\" you don't care about the"
1257 echo "tests below then you can interrupt the test suite with Control-C"
1258 echo "to get a summary report on the tests that have already been run."
1259
1260 TEST_SUBSECTION "Inactive players don't appear in scores"
1261
1262 TEST "Start 2-player game with 6 registered players"
1263 empathy_reset
1264 prompt_id=$(empathy_submit_prompt alice 4 "1 best pet" | jq .id)
1265 empathy_start alice $prompt_id
1266 empathy_answer alice $prompt_id '"cats"' >/dev/null
1267 empathy_answer bob   $prompt_id '"dogs"' >/dev/null
1268 empathy_end_answers alice $prompt_id
1269 empathy_end_answers bob $prompt_id
1270 test "$?" = "0"
1271 TEST_END
1272
1273 TEST "Deactivate 3 players"
1274 empathy_player_deactivate dale
1275 empathy_player_deactivate eric
1276 empathy_player_deactivate fred
1277 sleep 30
1278 test "$?" = "0"
1279 TEST_END
1280
1281 TEST "Finish game with 2 active players"
1282 empathy_judged alice $prompt_id '[]' >/dev/null
1283 result=$(empathy_judged bob $prompt_id '[]')
1284 test "$result" = '{"valid":true}'
1285 TEST_END
1286
1287 TEST "Verify scores don't include inactive players"
1288 # echo here is to strip newlines
1289 result=$(echo $(empathy_scores_names_numbers alice))
1290 test "$result" = '"alice" "bob" 1 "charlie" 0'
1291 TEST_END
1292
1293 TEST_SUBSECTION "Deactivated players don't block future game phase advances"
1294
1295 TEST "New 3-player game, 2 submit right away"
1296 empathy_reset
1297 prompt_id=$(empathy_submit_prompt charlie 4 "2 legit 2 quit" | jq .id)
1298 empathy_start alice $prompt_id
1299 empathy_answer alice $prompt_id '"what", "gives?"' >/dev/null
1300 empathy_answer bob   $prompt_id '"so", "confused"' >/dev/null
1301 test "$?" = "0"
1302 TEST_END
1303
1304 TEST "Judging hasn't started with player unsubmitted"
1305 result=$(echo $(empathy_ambiguities alice))
1306 test "$result" = "null"
1307 TEST_END
1308
1309 TEST "Final active player submits"
1310 result=$(empathy_answer charlie $prompt_id '"best", "category"')
1311 test "$result" = '{"valid":true}'
1312 TEST_END
1313
1314 TEST "Judging has started (don't need inactive players)"
1315 result=$(echo $(empathy_ambiguities alice))
1316 test "$result" != "null"
1317 TEST_END
1318
1319 TEST "Submit word groups from 2 players"
1320 empathy_judged alice $prompt_id '[]' >/dev/null
1321 result=$(empathy_judged bob $prompt_id '[]')
1322 test "$result" = '{"valid":true}'
1323 TEST_END
1324
1325 TEST "Scoring hasn't started with player unsubmitted"
1326 result=$(echo $(empathy_scores alice))
1327 test "$result" = "null"
1328 TEST_END
1329
1330 TEST "Submit word groups from last answering player"
1331 result=$(empathy_judged charlie $prompt_id '[]')
1332 test "$result" = '{"valid":true}'
1333 TEST_END
1334
1335 TEST "Scoring has now started"
1336 result=$(echo $(empathy_scores alice))
1337 test "$result" != "null"
1338 TEST_END
1339
1340 TEST_SUBSECTION "Reactivated player is fully active"
1341
1342 TEST "The dale player is currently deactivated"
1343 test "$dale_pid" = ""
1344 TEST_END
1345
1346 TEST "Reactivate dale"
1347 empathy_player_reactivate dale
1348 test "$dale_pid" != ""
1349 TEST_END
1350
1351 TEST "New 4-player game, 3 submit right away"
1352 empathy_reset
1353 prompt_id=$(empathy_submit_prompt alice 1 "favorite letter" | jq .id)
1354 empathy_start alice $prompt_id
1355 empathy_answer alice   $prompt_id '"A"' >/dev/null
1356 empathy_answer bob     $prompt_id '"B"' >/dev/null
1357 empathy_answer charlie $prompt_id '"C"' >/dev/null
1358 test "$?" = "0"
1359 TEST_END
1360
1361 TEST "Judging hasn't started with player unsubmitted"
1362 result=$(echo $(empathy_ambiguities alice))
1363 test "$result" = "null"
1364 TEST_END
1365
1366 TEST "Final active player submits"
1367 result=$(empathy_answer dale $prompt_id '"D"')
1368 test "$result" = '{"valid":true}'
1369 TEST_END
1370
1371 TEST "Judging has started now"
1372 result=$(echo $(empathy_ambiguities alice))
1373 test "$result" != "null"
1374 TEST_END
1375
1376 cleanup_and_report