|
| 1 | +package anthropic |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/cagent/pkg/chat" |
| 8 | + "github.com/docker/cagent/pkg/tools" |
| 9 | +) |
| 10 | + |
| 11 | +func TestConvertMessages_SkipEmptySystemText(t *testing.T) { |
| 12 | + msgs := []chat.Message{{ |
| 13 | + Role: chat.MessageRoleSystem, |
| 14 | + Content: " \n\t ", |
| 15 | + }} |
| 16 | + |
| 17 | + out := convertMessages(msgs) |
| 18 | + if len(out) != 0 { |
| 19 | + t.Fatalf("expected 0 messages, got %d", len(out)) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestConvertMessages_SkipEmptyUserText_NoMultiContent(t *testing.T) { |
| 24 | + msgs := []chat.Message{{ |
| 25 | + Role: chat.MessageRoleUser, |
| 26 | + Content: " \n\t ", |
| 27 | + }} |
| 28 | + |
| 29 | + out := convertMessages(msgs) |
| 30 | + if len(out) != 0 { |
| 31 | + t.Fatalf("expected 0 messages, got %d", len(out)) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestConvertMessages_UserMultiContent_SkipEmptyText_KeepImage(t *testing.T) { |
| 36 | + msgs := []chat.Message{{ |
| 37 | + Role: chat.MessageRoleUser, |
| 38 | + MultiContent: []chat.MessagePart{ |
| 39 | + {Type: chat.MessagePartTypeText, Text: " "}, |
| 40 | + {Type: chat.MessagePartTypeImageURL, ImageURL: &chat.MessageImageURL{URL: "data:image/png;base64,AAAA"}}, |
| 41 | + }, |
| 42 | + }} |
| 43 | + |
| 44 | + out := convertMessages(msgs) |
| 45 | + if len(out) != 1 { |
| 46 | + t.Fatalf("expected 1 message, got %d", len(out)) |
| 47 | + } |
| 48 | + |
| 49 | + b, err := json.Marshal(out[0]) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("marshal error: %v", err) |
| 52 | + } |
| 53 | + // Basic JSON structure checks |
| 54 | + var m map[string]any |
| 55 | + if err := json.Unmarshal(b, &m); err != nil { |
| 56 | + t.Fatalf("unmarshal error: %v", err) |
| 57 | + } |
| 58 | + // role should be user |
| 59 | + if role, _ := m["role"].(string); role != "user" { |
| 60 | + t.Fatalf("expected role 'user', got %v", m["role"]) |
| 61 | + } |
| 62 | + // content should contain exactly one block (the image) |
| 63 | + if content, _ := m["content"].([]any); len(content) != 1 { |
| 64 | + t.Fatalf("expected 1 content block, got %d", len(content)) |
| 65 | + } |
| 66 | + // and it should be an image block |
| 67 | + if content, _ := m["content"].([]any); len(content) == 1 { |
| 68 | + cb, _ := content[0].(map[string]any) |
| 69 | + if typ, _ := cb["type"].(string); typ != "image" { |
| 70 | + t.Fatalf("expected content block type 'image', got %v", typ) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestConvertMessages_SkipEmptyAssistantText_NoToolCalls(t *testing.T) { |
| 76 | + msgs := []chat.Message{{ |
| 77 | + Role: chat.MessageRoleAssistant, |
| 78 | + Content: " \t\n ", |
| 79 | + }} |
| 80 | + |
| 81 | + out := convertMessages(msgs) |
| 82 | + if len(out) != 0 { |
| 83 | + t.Fatalf("expected 0 messages, got %d", len(out)) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestConvertMessages_AssistantToolCalls_NoText_IncludesToolUse(t *testing.T) { |
| 88 | + msgs := []chat.Message{{ |
| 89 | + Role: chat.MessageRoleAssistant, |
| 90 | + Content: " ", |
| 91 | + ToolCalls: []tools.ToolCall{ |
| 92 | + {ID: "tool-1", Function: tools.FunctionCall{Name: "do_thing", Arguments: "{\"x\":1}"}}, |
| 93 | + }, |
| 94 | + }} |
| 95 | + |
| 96 | + out := convertMessages(msgs) |
| 97 | + if len(out) != 1 { |
| 98 | + t.Fatalf("expected 1 message, got %d", len(out)) |
| 99 | + } |
| 100 | + |
| 101 | + b, err := json.Marshal(out[0]) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("marshal error: %v", err) |
| 104 | + } |
| 105 | + var m map[string]any |
| 106 | + if err := json.Unmarshal(b, &m); err != nil { |
| 107 | + t.Fatalf("unmarshal error: %v", err) |
| 108 | + } |
| 109 | + if role, _ := m["role"].(string); role != "assistant" { |
| 110 | + t.Fatalf("expected role 'assistant', got %v", m["role"]) |
| 111 | + } |
| 112 | + content, _ := m["content"].([]any) |
| 113 | + if len(content) != 1 { |
| 114 | + t.Fatalf("expected 1 content block, got %d", len(content)) |
| 115 | + } |
| 116 | + cb, _ := content[0].(map[string]any) |
| 117 | + if typ, _ := cb["type"].(string); typ != "tool_use" { |
| 118 | + t.Fatalf("expected content block type 'tool_use', got %v", typ) |
| 119 | + } |
| 120 | +} |
0 commit comments