2024-10-20 09:23:53 +00:00
|
|
|
package nex_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleCommand(t *testing.T) {
|
|
|
|
conn, err := nex.NewNexusConn("127.0.0.1:7001", &proto.ServiceInfo{
|
|
|
|
Id: "echo01",
|
|
|
|
Type: "echo",
|
|
|
|
Label: "Echo",
|
|
|
|
GrpcAddr: "127.0.0.1:6001",
|
|
|
|
HttpAddr: nil,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("unable to connect nexus: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.RegisterService(); err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("unable to register service: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-20 09:42:51 +00:00
|
|
|
err = conn.AddCommand("say.hi", "all", nil, func(ctx *nex.CommandCtx) error {
|
2024-10-20 10:13:07 +00:00
|
|
|
return ctx.Write([]byte("Hello, World!"), "text/plain", http.StatusOK)
|
2024-10-20 09:42:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("unable to add command: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = conn.AddCommand("echo", "all", nil, func(ctx *nex.CommandCtx) error {
|
|
|
|
t.Log("Received command: ", string(ctx.Read()))
|
2024-10-20 10:13:07 +00:00
|
|
|
return ctx.Write(ctx.Read(), "text/plain", http.StatusOK)
|
|
|
|
})
|
|
|
|
err = conn.AddCommand("echo.details", "all", nil, func(ctx *nex.CommandCtx) error {
|
|
|
|
return ctx.JSON(map[string]any{
|
|
|
|
"values": ctx.Values(),
|
|
|
|
"body": ctx.Read(),
|
|
|
|
}, http.StatusOK)
|
2024-10-20 09:23:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("unable to add command: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-10-20 09:42:51 +00:00
|
|
|
err := conn.RunCommands("0.0.0.0:6001")
|
2024-10-20 09:23:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(fmt.Errorf("unable to run commands: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-10-20 09:42:51 +00:00
|
|
|
t.Log("Waiting 60 seconds for calling command...")
|
|
|
|
time.Sleep(time.Second * 60)
|
2024-10-20 09:23:53 +00:00
|
|
|
}
|