| 1 | package anthropic |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func TestBuildRequestEmbedsImageBlockForVisionModel(t *testing.T) { |
| 12 | c := &client{model: "claude-opus-4-8", vision: true} |
| 13 | req := c.buildRequest(context.Background(), provider.Request{ |
| 14 | Messages: []provider.Message{ |
| 15 | {Role: provider.RoleUser, Content: "describe", Images: []string{"data:image/jpeg;base64,ZZZZ"}}, |
| 16 | }, |
| 17 | }) |
| 18 | blocks := req.Messages[0].Content |
| 19 | if len(blocks) != 2 || blocks[0].Type != "text" || blocks[1].Type != "image" { |
| 20 | t.Fatalf("blocks = %+v, want [text, image]", blocks) |
| 21 | } |
| 22 | src := blocks[1].Source |
| 23 | if src == nil || src.Type != "base64" || src.MediaType != "image/jpeg" || src.Data != "ZZZZ" { |
| 24 | t.Fatalf("image source = %+v, want base64 / image/jpeg / ZZZZ", src) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestBuildRequestSkipsImageBlockWithoutVision(t *testing.T) { |
| 29 | c := &client{model: "claude-opus-4-8"} // vision unset |
| 30 | req := c.buildRequest(context.Background(), provider.Request{ |
| 31 | Messages: []provider.Message{ |
| 32 | {Role: provider.RoleUser, Content: "describe", Images: []string{"data:image/jpeg;base64,ZZZZ"}}, |
| 33 | }, |
| 34 | }) |
| 35 | blocks := req.Messages[0].Content |
| 36 | if len(blocks) != 1 || blocks[0].Type != "text" { |
| 37 | t.Fatalf("blocks = %+v, want [text] only when vision is off", blocks) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // toolMessages is a paired history whose tool result carries an image: the |
| 42 | // shape parseToolResult produces for an MCP screenshot tool. |
| 43 | func toolMessages(images []string) []provider.Message { |
| 44 | return []provider.Message{ |
| 45 | {Role: provider.RoleUser, Content: "screenshot please"}, |
| 46 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "c1", Name: "shot", Arguments: "{}"}}}, |
| 47 | {Role: provider.RoleTool, ToolCallID: "c1", Name: "shot", Content: "[image: image/png]", Images: images}, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestBuildRequestEmbedsToolResultImagesForVisionModel(t *testing.T) { |
| 52 | c := &client{model: "claude-opus-4-8", vision: true} |
| 53 | req := c.buildRequest(context.Background(), provider.Request{Messages: toolMessages([]string{"data:image/png;base64,QUFB"})}) |
| 54 | last := req.Messages[len(req.Messages)-1] |
| 55 | if last.Role != "user" || len(last.Content) != 1 || last.Content[0].Type != "tool_result" { |
| 56 | t.Fatalf("last message = %+v, want a single tool_result block", last) |
| 57 | } |
| 58 | blocks, ok := last.Content[0].Content.([]contentBlock) |
| 59 | if !ok { |
| 60 | t.Fatalf("tool_result content = %T, want []contentBlock", last.Content[0].Content) |
| 61 | } |
| 62 | if len(blocks) != 2 || blocks[0].Type != "text" || blocks[1].Type != "image" { |
| 63 | t.Fatalf("tool_result blocks = %+v, want [text, image]", blocks) |
| 64 | } |
| 65 | if blocks[0].Text != "[image: image/png]" { |
| 66 | t.Fatalf("text block = %q, want the placeholder text", blocks[0].Text) |
| 67 | } |
| 68 | src := blocks[1].Source |
| 69 | if src == nil || src.Type != "base64" || src.MediaType != "image/png" || src.Data != "QUFB" { |
| 70 | t.Fatalf("image source = %+v, want base64 / image/png / QUFB", src) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestBuildRequestDropsToolResultImagesWithoutVision(t *testing.T) { |
| 75 | c := &client{model: "claude-opus-4-8"} // vision unset |
| 76 | req := c.buildRequest(context.Background(), provider.Request{Messages: toolMessages([]string{"data:image/png;base64,QUFB"})}) |
| 77 | last := req.Messages[len(req.Messages)-1] |
| 78 | if s, ok := last.Content[0].Content.(string); !ok || s != "[image: image/png]" { |
| 79 | t.Fatalf("non-vision tool_result content = %#v, want the plain placeholder string", last.Content[0].Content) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // A text-only tool result must keep serializing exactly as before the image |
| 84 | // channel existed: plain string content, no array — the prompt-cache prefix of |
| 85 | // existing sessions depends on those bytes. |
| 86 | func TestBuildRequestToolResultTextOnlyKeepsStringContent(t *testing.T) { |
| 87 | c := &client{model: "claude-opus-4-8", vision: true} |
| 88 | msgs := toolMessages(nil) |
| 89 | msgs[2].Content = "plain output" |
| 90 | // Trailing user turn merges after the tool_result in the same user message |
| 91 | // and takes the cache breakpoint, so the tool_result block keeps its |
| 92 | // pre-image-channel bytes. |
| 93 | msgs = append(msgs, provider.Message{Role: provider.RoleUser, Content: "next"}) |
| 94 | req := c.buildRequest(context.Background(), provider.Request{Messages: msgs}) |
| 95 | last := req.Messages[len(req.Messages)-1] |
| 96 | if len(last.Content) != 2 || last.Content[0].Type != "tool_result" { |
| 97 | t.Fatalf("last message blocks = %+v, want [tool_result, text]", last.Content) |
| 98 | } |
| 99 | if s, ok := last.Content[0].Content.(string); !ok || s != "plain output" { |
| 100 | t.Fatalf("tool_result content = %#v, want plain string", last.Content[0].Content) |
| 101 | } |
| 102 | body, err := json.Marshal(last.Content[0]) |
| 103 | if err != nil { |
| 104 | t.Fatalf("marshal: %v", err) |
| 105 | } |
| 106 | want := `{"type":"tool_result","tool_use_id":"c1","content":"plain output"}` |
| 107 | if string(body) != want { |
| 108 | t.Fatalf("serialized tool_result = %s, want %s", body, want) |
| 109 | } |
| 110 | } |
| 111 |