Add comprehensive test suite (216 tests)
Test coverage for: - Task package: models, repository (memory + SQLite), service, API handlers - Agent package: registry, lifecycle manager, credentials manager, API handlers - Orchestrator package: queue, scheduler, watcher, ACP message store - Events package: in-memory event bus with pub/sub - ACP protocol: message types, helpers, JSON marshaling All tests pass with 'go test ./...'
This commit is contained in:
@@ -0,0 +1,275 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewProgressMessage(t *testing.T) {
|
||||
data := ProgressData{
|
||||
Progress: 50,
|
||||
Message: "Processing files",
|
||||
CurrentFile: "main.go",
|
||||
FilesProcessed: 5,
|
||||
TotalFiles: 10,
|
||||
}
|
||||
|
||||
msg := NewProgressMessage("agent-1", "task-1", data)
|
||||
|
||||
if msg.Type != MessageTypeProgress {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeProgress, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-1" {
|
||||
t.Errorf("Expected agentID 'agent-1', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-1" {
|
||||
t.Errorf("Expected taskID 'task-1', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["progress"] != 50 {
|
||||
t.Errorf("Expected progress 50, got %v", msg.Data["progress"])
|
||||
}
|
||||
if msg.Data["message"] != "Processing files" {
|
||||
t.Errorf("Expected message 'Processing files', got %v", msg.Data["message"])
|
||||
}
|
||||
if msg.Data["current_file"] != "main.go" {
|
||||
t.Errorf("Expected current_file 'main.go', got %v", msg.Data["current_file"])
|
||||
}
|
||||
if msg.Data["files_processed"] != 5 {
|
||||
t.Errorf("Expected files_processed 5, got %v", msg.Data["files_processed"])
|
||||
}
|
||||
if msg.Data["total_files"] != 10 {
|
||||
t.Errorf("Expected total_files 10, got %v", msg.Data["total_files"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLogMessage(t *testing.T) {
|
||||
data := LogData{
|
||||
Level: "info",
|
||||
Message: "Task started",
|
||||
Metadata: map[string]interface{}{
|
||||
"component": "scanner",
|
||||
},
|
||||
}
|
||||
|
||||
msg := NewLogMessage("agent-2", "task-2", data)
|
||||
|
||||
if msg.Type != MessageTypeLog {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeLog, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-2" {
|
||||
t.Errorf("Expected agentID 'agent-2', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-2" {
|
||||
t.Errorf("Expected taskID 'task-2', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["level"] != "info" {
|
||||
t.Errorf("Expected level 'info', got %v", msg.Data["level"])
|
||||
}
|
||||
if msg.Data["message"] != "Task started" {
|
||||
t.Errorf("Expected message 'Task started', got %v", msg.Data["message"])
|
||||
}
|
||||
metadata, ok := msg.Data["metadata"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatal("Expected metadata to be a map")
|
||||
}
|
||||
if metadata["component"] != "scanner" {
|
||||
t.Errorf("Expected component 'scanner', got %v", metadata["component"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewResultMessage(t *testing.T) {
|
||||
data := ResultData{
|
||||
Status: "completed",
|
||||
Summary: "All checks passed",
|
||||
Artifacts: []Artifact{
|
||||
{Type: "report", Path: "/reports/scan.json", URL: "https://example.com/report"},
|
||||
{Type: "log", Path: "/logs/scan.log", URL: ""},
|
||||
},
|
||||
}
|
||||
|
||||
msg := NewResultMessage("agent-3", "task-3", data)
|
||||
|
||||
if msg.Type != MessageTypeResult {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeResult, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-3" {
|
||||
t.Errorf("Expected agentID 'agent-3', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-3" {
|
||||
t.Errorf("Expected taskID 'task-3', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["status"] != "completed" {
|
||||
t.Errorf("Expected status 'completed', got %v", msg.Data["status"])
|
||||
}
|
||||
if msg.Data["summary"] != "All checks passed" {
|
||||
t.Errorf("Expected summary 'All checks passed', got %v", msg.Data["summary"])
|
||||
}
|
||||
|
||||
artifacts, ok := msg.Data["artifacts"].([]interface{})
|
||||
if !ok {
|
||||
t.Fatal("Expected artifacts to be a slice")
|
||||
}
|
||||
if len(artifacts) != 2 {
|
||||
t.Errorf("Expected 2 artifacts, got %d", len(artifacts))
|
||||
}
|
||||
|
||||
artifact1, ok := artifacts[0].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatal("Expected first artifact to be a map")
|
||||
}
|
||||
if artifact1["type"] != "report" {
|
||||
t.Errorf("Expected first artifact type 'report', got %v", artifact1["type"])
|
||||
}
|
||||
if artifact1["path"] != "/reports/scan.json" {
|
||||
t.Errorf("Expected first artifact path '/reports/scan.json', got %v", artifact1["path"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewErrorMessage(t *testing.T) {
|
||||
data := ErrorData{
|
||||
Error: "Failed to parse file",
|
||||
File: "config.yaml",
|
||||
Details: "Invalid YAML syntax on line 42",
|
||||
}
|
||||
|
||||
msg := NewErrorMessage("agent-4", "task-4", data)
|
||||
|
||||
if msg.Type != MessageTypeError {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeError, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-4" {
|
||||
t.Errorf("Expected agentID 'agent-4', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-4" {
|
||||
t.Errorf("Expected taskID 'task-4', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["error"] != "Failed to parse file" {
|
||||
t.Errorf("Expected error 'Failed to parse file', got %v", msg.Data["error"])
|
||||
}
|
||||
if msg.Data["file"] != "config.yaml" {
|
||||
t.Errorf("Expected file 'config.yaml', got %v", msg.Data["file"])
|
||||
}
|
||||
if msg.Data["details"] != "Invalid YAML syntax on line 42" {
|
||||
t.Errorf("Expected details, got %v", msg.Data["details"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStatusMessage(t *testing.T) {
|
||||
data := StatusData{
|
||||
Status: "running",
|
||||
Message: "Processing task",
|
||||
}
|
||||
|
||||
msg := NewStatusMessage("agent-5", "task-5", data)
|
||||
|
||||
if msg.Type != MessageTypeStatus {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeStatus, msg.Type)
|
||||
}
|
||||
if msg.Data["status"] != "running" {
|
||||
t.Errorf("Expected status 'running', got %v", msg.Data["status"])
|
||||
}
|
||||
if msg.Data["message"] != "Processing task" {
|
||||
t.Errorf("Expected message 'Processing task', got %v", msg.Data["message"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHeartbeatMessage(t *testing.T) {
|
||||
msg := NewHeartbeatMessage("agent-6", "task-6")
|
||||
|
||||
if msg.Type != MessageTypeHeartbeat {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeHeartbeat, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-6" {
|
||||
t.Errorf("Expected agentID 'agent-6', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-6" {
|
||||
t.Errorf("Expected taskID 'task-6', got %s", msg.TaskID)
|
||||
}
|
||||
if len(msg.Data) != 0 {
|
||||
t.Errorf("Expected empty data map, got %v", msg.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewControlMessage(t *testing.T) {
|
||||
data := ControlData{
|
||||
Action: "pause",
|
||||
Reason: "User requested pause",
|
||||
}
|
||||
|
||||
msg := NewControlMessage("agent-7", "task-7", data)
|
||||
|
||||
if msg.Type != MessageTypeControl {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeControl, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-7" {
|
||||
t.Errorf("Expected agentID 'agent-7', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-7" {
|
||||
t.Errorf("Expected taskID 'task-7', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["action"] != "pause" {
|
||||
t.Errorf("Expected action 'pause', got %v", msg.Data["action"])
|
||||
}
|
||||
if msg.Data["reason"] != "User requested pause" {
|
||||
t.Errorf("Expected reason 'User requested pause', got %v", msg.Data["reason"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewProgressMessage_ZeroValues(t *testing.T) {
|
||||
data := ProgressData{
|
||||
Progress: 0,
|
||||
Message: "",
|
||||
}
|
||||
|
||||
msg := NewProgressMessage("agent-1", "task-1", data)
|
||||
|
||||
if msg.Type != MessageTypeProgress {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeProgress, msg.Type)
|
||||
}
|
||||
if msg.Data["progress"] != 0 {
|
||||
t.Errorf("Expected progress 0, got %v", msg.Data["progress"])
|
||||
}
|
||||
if msg.Data["message"] != "" {
|
||||
t.Errorf("Expected empty message, got %v", msg.Data["message"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewResultMessage_NoArtifacts(t *testing.T) {
|
||||
data := ResultData{
|
||||
Status: "completed",
|
||||
Summary: "No files generated",
|
||||
Artifacts: []Artifact{},
|
||||
}
|
||||
|
||||
msg := NewResultMessage("agent-1", "task-1", data)
|
||||
|
||||
if msg.Type != MessageTypeResult {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeResult, msg.Type)
|
||||
}
|
||||
artifacts, ok := msg.Data["artifacts"].([]interface{})
|
||||
if !ok {
|
||||
t.Fatal("Expected artifacts to be a slice")
|
||||
}
|
||||
if len(artifacts) != 0 {
|
||||
t.Errorf("Expected 0 artifacts, got %d", len(artifacts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewLogMessage_AllLevels(t *testing.T) {
|
||||
levels := []string{"debug", "info", "warn", "error"}
|
||||
|
||||
for _, level := range levels {
|
||||
t.Run(level, func(t *testing.T) {
|
||||
data := LogData{
|
||||
Level: level,
|
||||
Message: "Test message",
|
||||
}
|
||||
|
||||
msg := NewLogMessage("agent-1", "task-1", data)
|
||||
|
||||
if msg.Data["level"] != level {
|
||||
t.Errorf("Expected level '%s', got %v", level, msg.Data["level"])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMessageType_Constants(t *testing.T) {
|
||||
tests := []struct {
|
||||
msgType MessageType
|
||||
expected string
|
||||
}{
|
||||
{MessageTypeProgress, "progress"},
|
||||
{MessageTypeLog, "log"},
|
||||
{MessageTypeResult, "result"},
|
||||
{MessageTypeError, "error"},
|
||||
{MessageTypeStatus, "status"},
|
||||
{MessageTypeHeartbeat, "heartbeat"},
|
||||
{MessageTypeControl, "control"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if string(tt.msgType) != tt.expected {
|
||||
t.Errorf("Expected MessageType %s, got %s", tt.expected, tt.msgType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessage(t *testing.T) {
|
||||
agentID := "agent-123"
|
||||
taskID := "task-456"
|
||||
data := map[string]interface{}{"key": "value"}
|
||||
|
||||
before := time.Now().UTC()
|
||||
msg := NewMessage(MessageTypeProgress, agentID, taskID, data)
|
||||
after := time.Now().UTC()
|
||||
|
||||
if msg.Type != MessageTypeProgress {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeProgress, msg.Type)
|
||||
}
|
||||
if msg.AgentID != agentID {
|
||||
t.Errorf("Expected agentID %s, got %s", agentID, msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != taskID {
|
||||
t.Errorf("Expected taskID %s, got %s", taskID, msg.TaskID)
|
||||
}
|
||||
if msg.Data["key"] != "value" {
|
||||
t.Error("Expected data to contain key=value")
|
||||
}
|
||||
if msg.Timestamp.Before(before) || msg.Timestamp.After(after) {
|
||||
t.Error("Expected timestamp to be set to current time")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessage_MarshalJSON(t *testing.T) {
|
||||
fixedTime := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC)
|
||||
msg := &Message{
|
||||
Type: MessageTypeLog,
|
||||
Timestamp: fixedTime,
|
||||
AgentID: "agent-001",
|
||||
TaskID: "task-001",
|
||||
Data: map[string]interface{}{"level": "info"},
|
||||
}
|
||||
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
t.Fatalf("MarshalJSON failed: %v", err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
if result["type"] != "log" {
|
||||
t.Errorf("Expected type 'log', got %v", result["type"])
|
||||
}
|
||||
if result["agent_id"] != "agent-001" {
|
||||
t.Errorf("Expected agent_id 'agent-001', got %v", result["agent_id"])
|
||||
}
|
||||
if result["task_id"] != "task-001" {
|
||||
t.Errorf("Expected task_id 'task-001', got %v", result["task_id"])
|
||||
}
|
||||
|
||||
// Check timestamp is in RFC3339Nano format
|
||||
ts, ok := result["timestamp"].(string)
|
||||
if !ok {
|
||||
t.Fatal("Expected timestamp to be a string")
|
||||
}
|
||||
_, err = time.Parse(time.RFC3339Nano, ts)
|
||||
if err != nil {
|
||||
t.Errorf("Timestamp not in RFC3339Nano format: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
jsonData := []byte(`{
|
||||
"type": "progress",
|
||||
"timestamp": "2024-01-15T10:30:00Z",
|
||||
"agent_id": "agent-test",
|
||||
"task_id": "task-test",
|
||||
"data": {"progress": 50, "message": "Processing..."}
|
||||
}`)
|
||||
|
||||
msg, err := Parse(jsonData)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
if msg.Type != MessageTypeProgress {
|
||||
t.Errorf("Expected type %s, got %s", MessageTypeProgress, msg.Type)
|
||||
}
|
||||
if msg.AgentID != "agent-test" {
|
||||
t.Errorf("Expected agentID 'agent-test', got %s", msg.AgentID)
|
||||
}
|
||||
if msg.TaskID != "task-test" {
|
||||
t.Errorf("Expected taskID 'task-test', got %s", msg.TaskID)
|
||||
}
|
||||
if msg.Data["progress"] != float64(50) {
|
||||
t.Errorf("Expected progress 50, got %v", msg.Data["progress"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse_InvalidJSON(t *testing.T) {
|
||||
invalidJSON := []byte(`{invalid json}`)
|
||||
|
||||
_, err := Parse(invalidJSON)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessage_IsValid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
msg *Message
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "valid message",
|
||||
msg: &Message{
|
||||
Type: MessageTypeProgress,
|
||||
AgentID: "agent-1",
|
||||
TaskID: "task-1",
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "missing type",
|
||||
msg: &Message{
|
||||
Type: "",
|
||||
AgentID: "agent-1",
|
||||
TaskID: "task-1",
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "missing agent_id",
|
||||
msg: &Message{
|
||||
Type: MessageTypeProgress,
|
||||
AgentID: "",
|
||||
TaskID: "task-1",
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "missing task_id",
|
||||
msg: &Message{
|
||||
Type: MessageTypeProgress,
|
||||
AgentID: "agent-1",
|
||||
TaskID: "",
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.msg.IsValid(); got != tt.expected {
|
||||
t.Errorf("IsValid() = %v, expected %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user