|
| 1 | +package bridge |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestBuildUserMessageWithImages(t *testing.T) { |
| 10 | + msg := BuildUserMessageWithImages("看图", []string{"data:image/png;base64,AAAA"}) |
| 11 | + data, _ := json.Marshal(msg) |
| 12 | + s := string(data) |
| 13 | + if !strings.Contains(s, `"type":"image_url"`) { |
| 14 | + t.Fatalf("missing image part: %s", s) |
| 15 | + } |
| 16 | + if !strings.Contains(s, `"url":"data:image/png;base64,AAAA"`) { |
| 17 | + t.Fatalf("missing image url: %s", s) |
| 18 | + } |
| 19 | + if !strings.Contains(s, `"type":"text"`) { |
| 20 | + t.Fatalf("missing text part: %s", s) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestBuildUserMessageTextOnlyUnchanged(t *testing.T) { |
| 25 | + msg := BuildUserMessage("hi") |
| 26 | + contents, _ := msg["contents"].([]interface{}) |
| 27 | + if len(contents) != 1 { |
| 28 | + t.Fatalf("text-only should have exactly 1 part, got %d", len(contents)) |
| 29 | + } |
| 30 | + first, _ := contents[0].(map[string]interface{}) |
| 31 | + if first["type"] != "text" || first["text"] != "hi" { |
| 32 | + t.Fatalf("unexpected text part: %v", first) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestConvertIncomingMessageImageOnly(t *testing.T) { |
| 37 | + msg := map[string]interface{}{ |
| 38 | + "role": "user", |
| 39 | + "content": []interface{}{ |
| 40 | + map[string]interface{}{"type": "image_url", "image_url": map[string]interface{}{"url": "data:image/jpeg;base64,BBBB"}}, |
| 41 | + }, |
| 42 | + } |
| 43 | + out := ConvertIncomingMessage(msg, false) |
| 44 | + if out == nil { |
| 45 | + t.Fatal("image-only user message was dropped") |
| 46 | + } |
| 47 | + contents, _ := out["contents"].([]interface{}) |
| 48 | + if len(contents) != 1 { |
| 49 | + t.Fatalf("want 1 part (image), got %d: %v", len(contents), contents) |
| 50 | + } |
| 51 | + first, _ := contents[0].(map[string]interface{}) |
| 52 | + if first["type"] != "image_url" { |
| 53 | + t.Fatalf("want image part, got %v", first) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestConvertIncomingMessageTextAndImage(t *testing.T) { |
| 58 | + msg := map[string]interface{}{ |
| 59 | + "role": "user", |
| 60 | + "content": []interface{}{ |
| 61 | + map[string]interface{}{"type": "text", "text": "这是什么"}, |
| 62 | + map[string]interface{}{"type": "image_url", "image_url": map[string]interface{}{"url": "https://example.com/a.png"}}, |
| 63 | + }, |
| 64 | + } |
| 65 | + out := ConvertIncomingMessage(msg, false) |
| 66 | + contents, _ := out["contents"].([]interface{}) |
| 67 | + if len(contents) != 2 { |
| 68 | + t.Fatalf("want 2 parts, got %d: %v", len(contents), contents) |
| 69 | + } |
| 70 | + first, _ := contents[0].(map[string]interface{}) |
| 71 | + if first["type"] != "image_url" { |
| 72 | + t.Fatalf("image should come first, got %v", first) |
| 73 | + } |
| 74 | + last, _ := contents[1].(map[string]interface{}) |
| 75 | + if last["type"] != "text" || last["text"] != "这是什么" { |
| 76 | + t.Fatalf("text should follow image, got %v", last) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestConvertIncomingMessageClaudeImage(t *testing.T) { |
| 81 | + msg := map[string]interface{}{ |
| 82 | + "role": "user", |
| 83 | + "content": []interface{}{ |
| 84 | + map[string]interface{}{ |
| 85 | + "type": "image", |
| 86 | + "source": map[string]interface{}{ |
| 87 | + "type": "base64", "media_type": "image/png", "data": "CCCC", |
| 88 | + }, |
| 89 | + }, |
| 90 | + map[string]interface{}{"type": "text", "text": "hi"}, |
| 91 | + }, |
| 92 | + } |
| 93 | + out := ConvertIncomingMessage(msg, false) |
| 94 | + contents, _ := out["contents"].([]interface{}) |
| 95 | + if len(contents) != 2 { |
| 96 | + t.Fatalf("want 2 parts, got %d: %v", len(contents), contents) |
| 97 | + } |
| 98 | + first, _ := contents[0].(map[string]interface{}) |
| 99 | + if first["type"] != "image_url" { |
| 100 | + t.Fatalf("claude image not converted: %v", first) |
| 101 | + } |
| 102 | + iu, _ := first["image_url"].(map[string]interface{}) |
| 103 | + if iu["url"] != "data:image/png;base64,CCCC" { |
| 104 | + t.Fatalf("bad data url: %v", iu) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestConvertIncomingMessageRejectsJunkImageURL(t *testing.T) { |
| 109 | + msg := map[string]interface{}{ |
| 110 | + "role": "user", |
| 111 | + "content": []interface{}{ |
| 112 | + map[string]interface{}{"type": "image_url", "image_url": map[string]interface{}{"url": "not-a-url"}}, |
| 113 | + }, |
| 114 | + } |
| 115 | + if out := ConvertIncomingMessage(msg, false); out != nil { |
| 116 | + t.Fatalf("junk image should be dropped, got %v", out) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestExtractMessageImagesInputImageString(t *testing.T) { |
| 121 | + msg := map[string]interface{}{ |
| 122 | + "content": []interface{}{ |
| 123 | + map[string]interface{}{"type": "input_image", "image_url": "data:image/webp;base64,DDDD"}, |
| 124 | + }, |
| 125 | + } |
| 126 | + imgs := ExtractMessageImages(msg) |
| 127 | + if len(imgs) != 1 || imgs[0] != "data:image/webp;base64,DDDD" { |
| 128 | + t.Fatalf("want 1 data url, got %v", imgs) |
| 129 | + } |
| 130 | +} |
0 commit comments