183 lines
3.8 KiB
Go
183 lines
3.8 KiB
Go
package v01_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"mayhem-party/src/device/input/button"
|
|
v01 "mayhem-party/src/device/input/parse/v01"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestV01(t *testing.T) {
|
|
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50))
|
|
t.Logf("(%v) %s", len(src), src.Read())
|
|
v01 := v01.NewV01(context.Background(), src)
|
|
defer v01.Close()
|
|
got := v01.Read()
|
|
want := []button.Button{
|
|
{Down: true, Char: 'a'},
|
|
{Down: true, Char: 'b'},
|
|
{Down: true, Char: 'c'},
|
|
{Down: false, Char: 'c'},
|
|
{Down: false, Char: 'd'},
|
|
{Down: false, Char: 'e'},
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatal(len(want), len(got))
|
|
}
|
|
for i := range got {
|
|
if got[i] != want[i] {
|
|
t.Errorf("[%d] want %+v got %+v", i, want[i], got[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestV01WithCfg(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := path.Join(d, "cfg.yaml")
|
|
os.WriteFile(p, []byte(`
|
|
users:
|
|
bel:
|
|
player: 2
|
|
players:
|
|
- transformation:
|
|
w: t
|
|
- transformation:
|
|
w: i
|
|
`), os.ModePerm)
|
|
v01.FlagParseV01Config = p
|
|
|
|
t.Run("unknown user ignored", func(t *testing.T) {
|
|
v01 := v01.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`))
|
|
defer v01.Close()
|
|
got := v01.Read()
|
|
if len(got) != 0 {
|
|
t.Error(got)
|
|
}
|
|
})
|
|
|
|
t.Run("player2", func(t *testing.T) {
|
|
v01 := v01.NewV01(context.Background(), constSrc(`{"U":"bel","Y":"w","N":"w"}`))
|
|
defer v01.Close()
|
|
got := v01.Read()
|
|
if len(got) != 2 {
|
|
t.Error(got)
|
|
}
|
|
if got[0] != (button.Button{Char: 'i', Down: true}) {
|
|
t.Error(got[0])
|
|
}
|
|
if got[1] != (button.Button{Char: 'i', Down: false}) {
|
|
t.Error(got[1])
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestV01Feedback(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := path.Join(d, "cfg.yaml")
|
|
os.WriteFile(p, []byte(`
|
|
feedback:
|
|
addr: :27071
|
|
ttsurl: http://localhost:15002
|
|
users:
|
|
bel:
|
|
player: 2
|
|
message: to bel
|
|
broadcast:
|
|
message: to everyone
|
|
players:
|
|
- transformation:
|
|
w: t
|
|
- transformation:
|
|
w: i
|
|
`), os.ModePerm)
|
|
v01.FlagParseV01Config = p
|
|
ctx, can := context.WithCancel(context.Background())
|
|
defer can()
|
|
|
|
v01 := v01.NewV01(ctx, constSrc(`{"U":"qt","Y":"w"}`))
|
|
defer v01.Close()
|
|
|
|
for {
|
|
time.Sleep(time.Millisecond * 100)
|
|
resp, err := http.Get("http://localhost:27071?user=bel")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
resp.Body.Close()
|
|
break
|
|
}
|
|
|
|
t.Run("specific user", func(t *testing.T) {
|
|
resp, err := http.Get("http://localhost:27071?user=bel")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, _ := io.ReadAll(resp.Body)
|
|
if string(b) != "to bel" {
|
|
t.Error(b)
|
|
}
|
|
})
|
|
|
|
t.Run("broadcast", func(t *testing.T) {
|
|
resp, err := http.Get("http://localhost:27071")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, _ := io.ReadAll(resp.Body)
|
|
if string(b) != "to everyone" {
|
|
t.Error(b)
|
|
}
|
|
})
|
|
|
|
t.Run("change broadcast", func(t *testing.T) {
|
|
want := `my new broadcast`
|
|
r, _ := http.NewRequest(http.MethodPut, "http://localhost:27071/broadcast", strings.NewReader(want))
|
|
resp, err := http.DefaultClient.Do(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
|
|
resp, err = http.Get("http://localhost:27071")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, _ := io.ReadAll(resp.Body)
|
|
if string(b) != want {
|
|
t.Error(string(b))
|
|
}
|
|
})
|
|
|
|
t.Run("tts", func(t *testing.T) {
|
|
if os.Getenv("INTEGRATION_TTS") != "true" {
|
|
t.Skip("$INTEGRATION_TTS is not true")
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
resp, err := http.Get("http://localhost:27071/?say=hello%20world")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
}
|
|
time.Sleep(time.Second * 3)
|
|
})
|
|
}
|
|
|
|
type constSrc string
|
|
|
|
func (c constSrc) Close() {}
|
|
|
|
func (c constSrc) Read() []byte {
|
|
return []byte(c)
|
|
}
|