A few gaming features added, e.g. rolling dice, flipping coin, eightball.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package rtpconn
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChooseCoinSide(t *testing.T) {
|
||||
for i, want := range []string{"heads", "tails"} {
|
||||
got, err := chooseCoinSide(bytes.NewReader([]byte{byte(i)}))
|
||||
if err != nil {
|
||||
t.Fatalf("chooseCoinSide(%d): %v", i, err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("chooseCoinSide(%d) = %q, want %q", i, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoinRejectsArguments(t *testing.T) {
|
||||
for _, value := range []any{nil, "", " "} {
|
||||
if err := validateCoinRequest(value); err != nil {
|
||||
t.Fatalf("validateCoinRequest(%#v): %v", value, err)
|
||||
}
|
||||
}
|
||||
for _, value := range []any{"twice", 2} {
|
||||
if err := validateCoinRequest(value); err == nil {
|
||||
t.Fatalf("validateCoinRequest(%#v) unexpectedly succeeded", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoinFlipIsServerGeneratedAndStoredInHistory(t *testing.T) {
|
||||
hallName := newChalkboardTestHall(t)
|
||||
flipper := addTestWebClient(t, hallName, "Participant", "message")
|
||||
observer := addTestWebClient(t, hallName, "Operator", "op")
|
||||
|
||||
if err := handleClientMessage(flipper, clientMessage{
|
||||
Type: "hallaction", Kind: "coin", Value: "",
|
||||
}); err != nil {
|
||||
t.Fatalf("handleClientMessage(coin): %v", err)
|
||||
}
|
||||
|
||||
for name, messages := range map[string][]clientMessage{
|
||||
"flipper": drainMessages(flipper),
|
||||
"observer": drainMessages(observer),
|
||||
} {
|
||||
if len(messages) != 1 {
|
||||
t.Fatalf("%s received %d messages, want 1: %#v", name, len(messages), messages)
|
||||
}
|
||||
message := messages[0]
|
||||
if message.Type != "chat" || message.Kind != "coin" || !message.Privileged {
|
||||
t.Fatalf("%s received non-authoritative coin message: %#v", name, message)
|
||||
}
|
||||
if message.Source != "" || message.Username == nil || *message.Username != "Coin" {
|
||||
t.Fatalf("%s received coin message with unexpected identity: %#v", name, message)
|
||||
}
|
||||
text, ok := message.Value.(string)
|
||||
if !ok || (text != "Participant flips a coin: heads." &&
|
||||
text != "Participant flips a coin: tails.") {
|
||||
t.Fatalf("%s received malformed coin announcement %q", name, text)
|
||||
}
|
||||
}
|
||||
|
||||
history := flipper.hall.GetChatHistory()
|
||||
if len(history) != 1 || history[0].Kind != "coin" {
|
||||
t.Fatalf("coin history = %#v", history)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoinFlipRequiresMessagePermission(t *testing.T) {
|
||||
hallName := newChalkboardTestHall(t)
|
||||
flipper := addTestWebClient(t, hallName, "Participant", "message")
|
||||
flipper.permissions = remove("message", flipper.permissions)
|
||||
|
||||
if err := handleClientMessage(flipper, clientMessage{
|
||||
Type: "hallaction", Kind: "coin", Value: "",
|
||||
}); err != nil {
|
||||
t.Fatalf("handleClientMessage(coin): %v", err)
|
||||
}
|
||||
if got := flipper.hall.GetChatHistory(); len(got) != 0 {
|
||||
t.Fatalf("unauthorised coin flip entered history: %#v", got)
|
||||
}
|
||||
messages := drainMessages(flipper)
|
||||
if len(messages) != 1 || messages[0].Kind != "error" ||
|
||||
!strings.Contains(messages[0].Value.(string), "not authorised") {
|
||||
t.Fatalf("unauthorised coin response = %#v", messages)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user