package flyto import "testing" func TestUserText(t *testing.T) { msg := UserText("hello") if msg.Role != RoleUser { t.Errorf("Role = %q, want user", msg.Role) } if len(msg.Blocks) != 1 { t.Fatalf("应有 1 个 block,实际: %d", len(msg.Blocks)) } if msg.Blocks[0].Type != BlockText { t.Errorf("Type = %q, want %q", msg.Blocks[0].Type, BlockText) } if msg.Blocks[0].Text != "hello" { t.Errorf("Text = %q", msg.Blocks[0].Text) } } func TestAssistantText(t *testing.T) { msg := AssistantText("hi there") if msg.Role != RoleAssistant { t.Errorf("Role = %q, want assistant", msg.Role) } if len(msg.Blocks) != 1 || msg.Blocks[0].Text != "hi there" { t.Errorf("Blocks: %+v", msg.Blocks) } } func TestTextBlock(t *testing.T) { b := TextBlock("text content") if b.Type != BlockText { t.Errorf("Type = %q", b.Type) } if b.Text != "text content" { t.Errorf("Text = %q", b.Text) } } func TestToolUseBlock(t *testing.T) { input := map[string]any{"command": "ls", "timeout": 5000} b := ToolUseBlock("tool_abc", "Bash", input) if b.Type != BlockToolUse { t.Errorf("Type = %q", b.Type) } if b.ToolUseID != "tool_abc" { t.Errorf("ToolUseID = %q", b.ToolUseID) } if b.ToolName != "Bash" { t.Errorf("ToolName = %q", b.ToolName) } if b.ToolInput["command"] != "ls" { t.Error("ToolInput map 未正确保存") } } func TestToolResultBlock(t *testing.T) { b := ToolResultBlock("tool_abc", "success output", false) if b.Type != BlockToolResult { t.Errorf("Type = %q", b.Type) } if b.ToolUseID != "tool_abc" { t.Errorf("ToolUseID = %q", b.ToolUseID) } if b.ResultText != "success output" { t.Errorf("ResultText = %q", b.ResultText) } if b.IsError { t.Error("IsError 应为 false") } // 错误结果 errBlock := ToolResultBlock("tool_xyz", "error message", true) if !errBlock.IsError { t.Error("IsError 应为 true") } } func TestThinkingBlock(t *testing.T) { meta := map[string]string{"thinking_signature": "sig_12345"} b := ThinkingBlock("step by step thought", meta) if b.Type != BlockThinking { t.Errorf("Type = %q", b.Type) } if b.ThinkingText != "step by step thought" { t.Errorf("ThinkingText = %q", b.ThinkingText) } if b.ProviderMetadata["thinking_signature"] != "sig_12345" { t.Errorf("ProviderMetadata 未保留") } } func TestThinkingBlock_NilMeta(t *testing.T) { // nil metadata 应允许 b := ThinkingBlock("thought", nil) if b.ProviderMetadata != nil { t.Errorf("nil meta 应保持 nil,实际: %v", b.ProviderMetadata) } } func TestRole_Constants(t *testing.T) { if string(RoleUser) != "user" { t.Errorf("RoleUser = %q", RoleUser) } if string(RoleAssistant) != "assistant" { t.Errorf("RoleAssistant = %q", RoleAssistant) } } func TestBlockType_Constants(t *testing.T) { if string(BlockText) != "text" { t.Errorf("BlockText = %q", BlockText) } if string(BlockToolUse) != "tool_use" { t.Errorf("BlockToolUse = %q", BlockToolUse) } if string(BlockToolResult) != "tool_result" { t.Errorf("BlockToolResult = %q", BlockToolResult) } if string(BlockThinking) != "thinking" { t.Errorf("BlockThinking = %q", BlockThinking) } }