灵隙(synapse) v1.0.0 版本
This commit is contained in:
512
types/types_test.go
Normal file
512
types/types_test.go
Normal file
@@ -0,0 +1,512 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// =============================================================================
|
||||
// 基础类型测试
|
||||
// =============================================================================
|
||||
|
||||
func TestGenerateMessageID(t *testing.T) {
|
||||
id := GenerateMessageID()
|
||||
|
||||
if id == "" {
|
||||
t.Error("Message ID should not be empty")
|
||||
}
|
||||
|
||||
// 验证格式(msg-前缀)
|
||||
if !startsWith(id, "msg-") {
|
||||
t.Errorf("Message ID should have msg- prefix: %s", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateTaskID(t *testing.T) {
|
||||
id := GenerateTaskID()
|
||||
|
||||
if id == "" {
|
||||
t.Error("Task ID should not be empty")
|
||||
}
|
||||
|
||||
// 验证格式(task-前缀)
|
||||
if !startsWith(id, "task-") {
|
||||
t.Errorf("Task ID should have task- prefix: %s", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNowTimestamp(t *testing.T) {
|
||||
ts := NowTimestamp()
|
||||
if ts == "" {
|
||||
t.Error("Timestamp should not be empty")
|
||||
}
|
||||
|
||||
// 验证可以解析为时间
|
||||
parsed, err := ts.Time()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse timestamp: %v", err)
|
||||
}
|
||||
|
||||
// 验证时间接近当前时间
|
||||
now := time.Now().UTC()
|
||||
diff := now.Sub(parsed)
|
||||
if diff > time.Minute || diff < -time.Minute {
|
||||
t.Errorf("Timestamp difference too large: %v", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimestampTime(t *testing.T) {
|
||||
ts := Timestamp("2026-06-27T10:00:00Z")
|
||||
parsed, err := ts.Time()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse timestamp: %v", err)
|
||||
}
|
||||
|
||||
expected := time.Date(2026, 6, 27, 10, 0, 0, 0, time.UTC)
|
||||
if !parsed.Equal(expected) {
|
||||
t.Errorf("Parsed time mismatch: got %v, want %v", parsed, expected)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// AgentEndpoint 测试
|
||||
// =============================================================================
|
||||
|
||||
func TestAgentEndpointJSON(t *testing.T) {
|
||||
ep := AgentEndpoint{
|
||||
AgentID: "agent-001",
|
||||
AgentName: "TestAgent",
|
||||
SystemID: "system-001",
|
||||
Endpoint: "http://localhost:8080",
|
||||
Version: "1.0.0",
|
||||
}
|
||||
|
||||
// 序列化
|
||||
data, err := json.Marshal(ep)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal AgentEndpoint: %v", err)
|
||||
}
|
||||
|
||||
// 反序列化
|
||||
var parsed AgentEndpoint
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal AgentEndpoint: %v", err)
|
||||
}
|
||||
|
||||
if parsed.AgentID != ep.AgentID {
|
||||
t.Errorf("AgentID mismatch: got %s, want %s", parsed.AgentID, ep.AgentID)
|
||||
}
|
||||
if parsed.Endpoint != ep.Endpoint {
|
||||
t.Errorf("Endpoint mismatch: got %s, want %s", parsed.Endpoint, ep.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// UnifiedSynapseMessage 测试
|
||||
// =============================================================================
|
||||
|
||||
func TestUnifiedSynapseMessageJSON(t *testing.T) {
|
||||
receiver := &AgentEndpoint{
|
||||
AgentID: "agent-002",
|
||||
Endpoint: "http://localhost:8081",
|
||||
}
|
||||
|
||||
msg := UnifiedSynapseMessage{
|
||||
ID: GenerateMessageID(),
|
||||
Version: "1.0.0",
|
||||
Type: string(P2PTaskRequest),
|
||||
Sender: AgentEndpoint{AgentID: "agent-001", Endpoint: "http://localhost:8080"},
|
||||
Receiver: receiver,
|
||||
Payload: json.RawMessage(`{"task": {"id": "task-001"}}`),
|
||||
Timestamp: NowTimestamp(),
|
||||
Priority: PriorityNormal,
|
||||
}
|
||||
|
||||
// 序列化
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal message: %v", err)
|
||||
}
|
||||
|
||||
// 反序列化
|
||||
var parsed UnifiedSynapseMessage
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal message: %v", err)
|
||||
}
|
||||
|
||||
if parsed.ID != msg.ID {
|
||||
t.Errorf("Message ID mismatch")
|
||||
}
|
||||
if parsed.Type != msg.Type {
|
||||
t.Errorf("Message type mismatch: got %s, want %s", parsed.Type, msg.Type)
|
||||
}
|
||||
if parsed.Receiver.AgentID != receiver.AgentID {
|
||||
t.Errorf("Receiver AgentID mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnifiedSynapseMessageGroupContext(t *testing.T) {
|
||||
msg := UnifiedSynapseMessage{
|
||||
ID: GenerateMessageID(),
|
||||
Version: "1.0.0",
|
||||
Type: string(GroupBroadcast),
|
||||
Sender: AgentEndpoint{AgentID: "agent-001"},
|
||||
GroupID: "group-001",
|
||||
TargetType: "all",
|
||||
Broadcast: true,
|
||||
Payload: json.RawMessage(`{"message": "hello"}`),
|
||||
Timestamp: NowTimestamp(),
|
||||
}
|
||||
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal group message: %v", err)
|
||||
}
|
||||
|
||||
var parsed UnifiedSynapseMessage
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal group message: %v", err)
|
||||
}
|
||||
|
||||
if parsed.GroupID != "group-001" {
|
||||
t.Errorf("GroupID mismatch: got %s, want group-001", parsed.GroupID)
|
||||
}
|
||||
if !parsed.Broadcast {
|
||||
t.Error("Broadcast should be true")
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// P2P Payload 测试
|
||||
// =============================================================================
|
||||
|
||||
func TestTaskRequestPayloadJSON(t *testing.T) {
|
||||
payload := TaskRequestPayload{}
|
||||
payload.Task.ID = "task-001"
|
||||
payload.Task.Type = "text_generation"
|
||||
payload.Task.Priority = PriorityNormal
|
||||
payload.Task.Payload = map[string]interface{}{
|
||||
"prompt": "写一首诗",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal TaskRequestPayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed TaskRequestPayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal TaskRequestPayload: %v", err)
|
||||
}
|
||||
|
||||
if parsed.Task.ID != "task-001" {
|
||||
t.Errorf("Task ID mismatch")
|
||||
}
|
||||
if parsed.Task.Type != "text_generation" {
|
||||
t.Errorf("Task type mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskConfirmPayloadJSON(t *testing.T) {
|
||||
payload := TaskConfirmPayload{
|
||||
TaskID: "task-001",
|
||||
Confirmed: true,
|
||||
EstimatedDuration: 60,
|
||||
ExpectedCompletion: NowTimestamp(),
|
||||
CapabilitiesUsed: []string{"text_gen"},
|
||||
}
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal TaskConfirmPayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed TaskConfirmPayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal TaskConfirmPayload: %v", err)
|
||||
}
|
||||
|
||||
if parsed.TaskID != "task-001" {
|
||||
t.Errorf("TaskID mismatch")
|
||||
}
|
||||
if !payload.Confirmed {
|
||||
t.Error("Confirmed should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskStatusUpdatePayloadJSON(t *testing.T) {
|
||||
payload := TaskStatusUpdatePayload{
|
||||
TaskID: "task-001",
|
||||
Status: "executing",
|
||||
Progress: 50,
|
||||
Message: "Processing...",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal TaskStatusUpdatePayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed TaskStatusUpdatePayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal TaskStatusUpdatePayload: %v", err)
|
||||
}
|
||||
|
||||
if parsed.Progress != 50 {
|
||||
t.Errorf("Progress mismatch: got %d, want 50", parsed.Progress)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Group Payload 测试
|
||||
// =============================================================================
|
||||
|
||||
func TestGroupCreatePayloadJSON(t *testing.T) {
|
||||
payload := GroupCreatePayload{}
|
||||
payload.Group.Name = "TestGroup"
|
||||
payload.Group.Topology = GroupTopologyStar
|
||||
payload.Group.Config = GroupConfig{
|
||||
AllowSelfLeave: true,
|
||||
ConsensusRequired: false,
|
||||
ConsensusThreshold: 0.5,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal GroupCreatePayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed GroupCreatePayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal GroupCreatePayload: %v", err)
|
||||
}
|
||||
|
||||
if parsed.Group.Name != "TestGroup" {
|
||||
t.Errorf("Group name mismatch")
|
||||
}
|
||||
if parsed.Group.Topology != GroupTopologyStar {
|
||||
t.Errorf("Topology mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupTaskSplitPayloadJSON(t *testing.T) {
|
||||
payload := GroupTaskSplitPayload{}
|
||||
payload.OriginalTask.ID = "task-001"
|
||||
payload.OriginalTask.Type = "video_generation"
|
||||
payload.Subtasks = []Subtask{
|
||||
{
|
||||
ID: "sub-001",
|
||||
Description: "Generate scene 1",
|
||||
AssignedTo: "agent-002",
|
||||
Priority: 1,
|
||||
},
|
||||
{
|
||||
ID: "sub-002",
|
||||
Description: "Generate scene 2",
|
||||
AssignedTo: "agent-003",
|
||||
Priority: 2,
|
||||
},
|
||||
}
|
||||
payload.MergeStrategy = MergeMajority
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal GroupTaskSplitPayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed GroupTaskSplitPayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal GroupTaskSplitPayload: %v", err)
|
||||
}
|
||||
|
||||
if len(parsed.Subtasks) != 2 {
|
||||
t.Errorf("Subtasks count mismatch: got %d, want 2", len(parsed.Subtasks))
|
||||
}
|
||||
if parsed.MergeStrategy != MergeMajority {
|
||||
t.Errorf("MergeStrategy mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupVotePayloadJSON(t *testing.T) {
|
||||
payload := GroupVotePayload{
|
||||
VoteID: "vote-001",
|
||||
Question: "Which approach is better?",
|
||||
Options: []string{"Option A", "Option B", "Option C"},
|
||||
Voters: []string{"agent-001", "agent-002", "agent-003"},
|
||||
Threshold: 0.5,
|
||||
Status: "open",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to marshal GroupVotePayload: %v", err)
|
||||
}
|
||||
|
||||
var parsed GroupVotePayload
|
||||
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||
t.Errorf("Failed to unmarshal GroupVotePayload: %v", err)
|
||||
}
|
||||
|
||||
if len(parsed.Options) != 3 {
|
||||
t.Errorf("Options count mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 错误码测试
|
||||
// =============================================================================
|
||||
|
||||
func TestSynapseErrorCategory(t *testing.T) {
|
||||
tests := []struct {
|
||||
code SynapseErrorCode
|
||||
expected string
|
||||
}{
|
||||
{ErrSystemError, "SYSTEM_ERROR"},
|
||||
{ErrTaskNotFound, "TASK_ERROR"},
|
||||
{ErrRequiredFieldMissing, "INFO_ERROR"},
|
||||
{ErrUnsupportedTaskType, "CAPABILITY_ERROR"},
|
||||
{ErrAuthFailed, "SECURITY_ERROR"},
|
||||
{ErrEndpointUnreachable, "COMMUNICATION_ERROR"},
|
||||
{ErrGroupNotFound, "GROUP_ERROR"},
|
||||
{ErrHumanTimeout, "HIL_ERROR"},
|
||||
{ErrInvalidParams, "VALIDATION_ERROR"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := tt.code.Category(); got != tt.expected {
|
||||
t.Errorf("Category for %d: got %s, want %s", tt.code, got, tt.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSynapseError(t *testing.T) {
|
||||
err := NewSynapseError(ErrTaskNotFound, "Task not found", map[string]string{"task_id": "task-001"})
|
||||
|
||||
if err.Code != ErrTaskNotFound {
|
||||
t.Errorf("Error code mismatch")
|
||||
}
|
||||
if err.Message != "Task not found" {
|
||||
t.Errorf("Error message mismatch")
|
||||
}
|
||||
if err.Category != "TASK_ERROR" {
|
||||
t.Errorf("Error category mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSynapseErrorToMap(t *testing.T) {
|
||||
err := NewSynapseError(ErrTaskNotFound, "Task not found", nil)
|
||||
m := err.ToMap()
|
||||
|
||||
if m["code"] != ErrTaskNotFound {
|
||||
t.Errorf("Map code mismatch")
|
||||
}
|
||||
if m["category"] != "TASK_ERROR" {
|
||||
t.Errorf("Map category mismatch")
|
||||
}
|
||||
if m["message"] != "Task not found" {
|
||||
t.Errorf("Map message mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 状态机测试
|
||||
// =============================================================================
|
||||
|
||||
func TestP2PTaskStatusValues(t *testing.T) {
|
||||
statuses := []P2PTaskStatus{
|
||||
P2PStatusIdle,
|
||||
P2PStatusPending,
|
||||
P2PStatusConfirmed,
|
||||
P2PStatusExecuting,
|
||||
P2PStatusCompleted,
|
||||
P2PStatusFailed,
|
||||
P2PStatusCancelled,
|
||||
}
|
||||
|
||||
for _, s := range statuses {
|
||||
if s == "" {
|
||||
t.Error("Status should not be empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupStatusValues(t *testing.T) {
|
||||
statuses := []GroupStatus{
|
||||
GroupStatusCreating,
|
||||
GroupStatusActive,
|
||||
GroupStatusFrozen,
|
||||
GroupStatusDisbanded,
|
||||
}
|
||||
|
||||
for _, s := range statuses {
|
||||
if s == "" {
|
||||
t.Error("Status should not be empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 消息类型测试
|
||||
// =============================================================================
|
||||
|
||||
func TestP2PMessageTypes(t *testing.T) {
|
||||
types := []P2PMessageType{
|
||||
P2PTaskRequest,
|
||||
P2PTaskConfirm,
|
||||
P2PTaskReject,
|
||||
P2PTaskNeedInfo,
|
||||
P2PTaskInfoProvided,
|
||||
P2PTaskStatusUpdate,
|
||||
P2PTaskComplete,
|
||||
P2PTaskFailed,
|
||||
P2PTaskCancel,
|
||||
P2PTaskAck,
|
||||
P2PAck,
|
||||
P2PNack,
|
||||
P2PHeartbeat,
|
||||
P2PCapabilityQuery,
|
||||
P2PCapabilityResponse,
|
||||
P2PTaskNeedHuman,
|
||||
P2PHumanDecision,
|
||||
}
|
||||
|
||||
for _, mt := range types {
|
||||
if mt == "" {
|
||||
t.Error("Message type should not be empty")
|
||||
}
|
||||
// 验证前缀
|
||||
if !startsWith(string(mt), "p2p:") {
|
||||
t.Errorf("P2P message type should have p2p: prefix: %s", mt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupMessageTypes(t *testing.T) {
|
||||
groupTypes := []GroupMessageType{
|
||||
GroupCreate,
|
||||
GroupDisband,
|
||||
GroupJoin,
|
||||
GroupLeave,
|
||||
GroupBroadcast,
|
||||
GroupTaskSplit,
|
||||
GroupTaskAssign,
|
||||
GroupTaskMerge,
|
||||
GroupVote,
|
||||
GroupVoteSubmit,
|
||||
}
|
||||
|
||||
for _, mt := range groupTypes {
|
||||
if mt == "" {
|
||||
t.Error("Message type should not be empty")
|
||||
}
|
||||
// 验证前缀
|
||||
if !startsWith(string(mt), "group:") {
|
||||
t.Errorf("Group message type should have group: prefix: %s", mt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func startsWith(s, prefix string) bool {
|
||||
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
||||
}
|
||||
Reference in New Issue
Block a user