feat: Adding very preliminar and broken test harness.

This commit is contained in:
James Wells 2023-04-03 18:39:01 -07:00
parent ac8c225752
commit 7f9a5777bd
Signed by: jwells
GPG key ID: 73196D10B8E65666
14 changed files with 678 additions and 10 deletions

View file

@ -0,0 +1,35 @@
package gstructs
import (
"fmt"
)
type AgentStatus []string
var AgentStatuses = AgentStatus{
"ERROR",
"INIT",
"SENDING",
"RECEIVING",
"ROUTING",
"PROCESSING",
"COMPLETED",
"RETURNING",
"ERROR",
}
func (a AgentStatus) GetByIndex(index int) (string, error) {
if index < 0 || index >= len(a) {
return "", fmt.Errorf("invalid index: %d", index)
}
return a[index], nil
}
func (a AgentStatus) GetByName(name string) (byte, error) {
for i, status := range a {
if status == name {
return byte(i), nil
}
}
return 0, fmt.Errorf("value not found: %s", name)
}

View file

@ -0,0 +1,59 @@
package gstructs_test
import (
"testing"
"github.com/dragonheim/gagent/internal/gstructs"
"github.com/stretchr/testify/assert"
)
func TestGetByIndex(t *testing.T) {
agentStatuses := gstructs.AgentStatuses
tests := []struct {
index int
expected string
shouldReturn bool
}{
{0, "ERROR", true},
{1, "INIT", true},
{8, "ERROR", true},
{9, "", false},
{-1, "", false},
}
for _, test := range tests {
res, err := agentStatuses.GetByIndex(test.index)
if test.shouldReturn {
assert.NoError(t, err)
assert.Equal(t, test.expected, res)
} else {
assert.Error(t, err)
}
}
}
func TestGetByName(t *testing.T) {
agentStatuses := gstructs.AgentStatuses
tests := []struct {
name string
expected byte
shouldReturn bool
}{
{"ERROR", 0, true},
{"INIT", 1, true},
{"COMPLETED", 6, true},
{"INVALID", 0, false},
}
for _, test := range tests {
res, err := agentStatuses.GetByName(test.name)
if test.shouldReturn {
assert.NoError(t, err)
assert.Equal(t, test.expected, res)
} else {
assert.Error(t, err)
}
}
}

View file

@ -111,10 +111,10 @@ type WorkerDetails struct {
}
type AgentDetails struct {
Status byte `hcl:"status"`
Client string `hcl:"client"`
Shasum string `hcl:"shasum"`
Hints []string
Script []byte
Answer []byte
Status byte `hcl:"status"`
Client string `hcl:"client"`
Shasum string `hcl:"shasum"`
Hints []string `hcl:"hints"`
Script []byte `hcl:"script"`
Answer []byte `hcl:"answer"`
}

View file

@ -0,0 +1,76 @@
package gstructs_test
import (
"testing"
"github.com/dragonheim/gagent/internal/gstructs"
)
func TestGagentConfig(t *testing.T) {
config := gstructs.GagentConfig{
Name: "test-config",
Mode: "client",
UUID: "test-uuid",
ListenAddr: "127.0.0.1",
ChainDBPath: "/tmp/chaindb",
MonitorPort: 8888,
ClientPort: 1234,
RouterPort: 5678,
WorkerPort: 9012,
Clients: []*gstructs.ClientDetails{
{
ClientName: "test-client",
ClientID: "client-id",
},
},
Routers: []*gstructs.RouterDetails{
{
RouterName: "test-router",
RouterID: "router-id",
RouterAddr: "192.168.1.1",
RouterTags: []string{"tag1", "tag2"},
ClientPort: 1234,
RouterPort: 5678,
WorkerPort: 9012,
},
},
Workers: []*gstructs.WorkerDetails{
{
WorkerName: "test-worker",
WorkerID: "worker-id",
WorkerTags: []string{"tag3", "tag4"},
},
},
Version: "1.0.0",
File: "config.hcl",
Agent: "agent.gagent",
CMode: true,
}
if config.Name != "test-config" {
t.Errorf("Expected config name to be 'test-config', got %s", config.Name)
}
if config.Mode != "client" {
t.Errorf("Expected config mode to be 'client', got %s", config.Mode)
}
// TODO: add more assertions for other config fields
}
func TestAgentDetails(t *testing.T) {
agent := gstructs.AgentDetails{
Status: 1,
Client: "test-client",
Shasum: "123456789abcdef",
Hints: []string{"tag1", "tag2", "tag3"},
Script: []byte("sample script content"),
Answer: []byte("sample answer content"),
}
if agent.Status != 1 {
t.Errorf("Expected agent status to be 1, got %d", agent.Status)
}
if agent.Client != "test-client" {
t.Errorf("Expected agent client to be 'test-client', got %s", agent.Client)
}
// TODO: add more assertions for other agent fields
}