package example_chat_test

import (
	"testing"
	"time"

	"git.ali33.ru/fcg-xvii/go-tools/json"
	"git.ali33.ru/fcg-xvii/rest"
	"git.ali33.ru/fcg-xvii/rest/example_chat/chat"
	"git.ali33.ru/fcg-xvii/rest/rest_websocket"
)

func TestChat(t *testing.T) {

	addr := "127.0.0.1:40000"
	clAddr := "ws://127.0.0.1:40000/ws"

	core := chat.New()

	server := rest.NewServer(addr, []byte("top-secret"))
	if err := server.Listen(time.Second); err != nil {
		t.Fatal(err)
	}

	app := NewApplication(server.Context())

	restEngine := rest_websocket.New(app, core)
	restEngine.Prepare(server, "/ws")

	//ch := make(chan struct{})
	//<-ch

	cl, err := rest_websocket.NewClient(clAddr)
	if err != nil {
		t.Fatal(err)
	}

	t.Log(cl)

	mes := rest_websocket.NewMessage(
		"/users/register",
		json.Map{
			"name":     "LETO",
			"password": "my-pass-99",
		},
		nil,
		time.Second*10,
		rest.RequestTypeMessage,
	)

	if err := cl.SendMessage(mes); err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Second * 5)
}
