package service

import (
	"go_dreamfactory/cmd/v2/lib"
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/comm"
	"go_dreamfactory/pb"
	"testing"
	"time"

	"github.com/Pallinder/go-randomdata"
	"google.golang.org/protobuf/proto"
)

func loginReq() ([]byte, error) {
	head := &pb.UserMessage{MainType: "user", SubType: "login"}
	sid := "dfz"
	account := randomdata.SillyName()
	head.Sec = common.BuildSecStr(sid, account)
	if comm.ProtoMarshal(&pb.UserLoginReq{
		Sid:     sid,
		Account: account,
	}, head) {
		data, err := proto.Marshal(head)
		if err != nil {
			return nil, err
		}

		return data, nil
	}
	return nil, nil
}

func TestStart(t *testing.T) {
	wsAddr := "ws://10.0.0.238:7891/gateway"
	h, err := NewWsCli(wsAddr, 2*time.Second)
	if err != nil {
		panic(err)
	}

	b, err := loginReq()
	if err != nil {
		t.Fatal(err)
	}
	h.SetReq(b, false)
	param := lib.ParamMgr{
		Caller:   h,
		Timeout:  50 * time.Millisecond,
		Lps:      uint32(1),
		Duration: 5 * time.Second,
		ResultCh: make(chan *lib.CallResult, 50),
	}
	a, err := lib.NewAssistant(nil, param)
	if err != nil {
		t.Fatalf("AI助手初始化错误: %v", err)
		t.FailNow()
	}

	a.Start()

	a.ShowResult()
}
