|
| 1 | +package aibridge_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/anthropics/anthropic-sdk-go" |
| 7 | + "github.com/coder/aibridge" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAnthropicLastUserPrompt(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + wrapper *aibridge.MessageNewParamsWrapper |
| 17 | + expected string |
| 18 | + expectError bool |
| 19 | + errorMsg string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "nil struct", |
| 23 | + expectError: true, |
| 24 | + errorMsg: "nil struct", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "no messages", |
| 28 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 29 | + MessageNewParams: anthropic.MessageNewParams{ |
| 30 | + Messages: []anthropic.MessageParam{}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + expectError: true, |
| 34 | + errorMsg: "no messages", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "last message not from user", |
| 38 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 39 | + MessageNewParams: anthropic.MessageNewParams{ |
| 40 | + Messages: []anthropic.MessageParam{ |
| 41 | + { |
| 42 | + Role: anthropic.MessageParamRoleUser, |
| 43 | + Content: []anthropic.ContentBlockParamUnion{ |
| 44 | + anthropic.NewTextBlock("user message"), |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Role: anthropic.MessageParamRoleAssistant, |
| 49 | + Content: []anthropic.ContentBlockParamUnion{ |
| 50 | + anthropic.NewTextBlock("assistant message"), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "last user message with empty content", |
| 59 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 60 | + MessageNewParams: anthropic.MessageNewParams{ |
| 61 | + Messages: []anthropic.MessageParam{ |
| 62 | + { |
| 63 | + Role: anthropic.MessageParamRoleUser, |
| 64 | + Content: []anthropic.ContentBlockParamUnion{}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "last user message with single text content", |
| 72 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 73 | + MessageNewParams: anthropic.MessageNewParams{ |
| 74 | + Messages: []anthropic.MessageParam{ |
| 75 | + { |
| 76 | + Role: anthropic.MessageParamRoleUser, |
| 77 | + Content: []anthropic.ContentBlockParamUnion{ |
| 78 | + anthropic.NewTextBlock("Hello, world!"), |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + expected: "Hello, world!", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "last user message with multiple content blocks - text at end", |
| 88 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 89 | + MessageNewParams: anthropic.MessageNewParams{ |
| 90 | + Messages: []anthropic.MessageParam{ |
| 91 | + { |
| 92 | + Role: anthropic.MessageParamRoleUser, |
| 93 | + Content: []anthropic.ContentBlockParamUnion{ |
| 94 | + anthropic.NewImageBlockBase64("image/png", "base64data"), |
| 95 | + anthropic.NewTextBlock("First text"), |
| 96 | + anthropic.NewImageBlockBase64("image/jpeg", "moredata"), |
| 97 | + anthropic.NewTextBlock("Last text"), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + expected: "Last text", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "last user message with only non-text content", |
| 107 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 108 | + MessageNewParams: anthropic.MessageNewParams{ |
| 109 | + Messages: []anthropic.MessageParam{ |
| 110 | + { |
| 111 | + Role: anthropic.MessageParamRoleUser, |
| 112 | + Content: []anthropic.ContentBlockParamUnion{ |
| 113 | + anthropic.NewImageBlockBase64("image/png", "base64data"), |
| 114 | + anthropic.NewImageBlockBase64("image/jpeg", "moredata"), |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "multiple messages with last being user", |
| 123 | + wrapper: &aibridge.MessageNewParamsWrapper{ |
| 124 | + MessageNewParams: anthropic.MessageNewParams{ |
| 125 | + Messages: []anthropic.MessageParam{ |
| 126 | + { |
| 127 | + Role: anthropic.MessageParamRoleUser, |
| 128 | + Content: []anthropic.ContentBlockParamUnion{ |
| 129 | + anthropic.NewTextBlock("First user message"), |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + Role: anthropic.MessageParamRoleAssistant, |
| 134 | + Content: []anthropic.ContentBlockParamUnion{ |
| 135 | + anthropic.NewTextBlock("Assistant response"), |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + Role: anthropic.MessageParamRoleUser, |
| 140 | + Content: []anthropic.ContentBlockParamUnion{ |
| 141 | + anthropic.NewTextBlock("Second user message"), |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + expected: "Second user message", |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tt := range tests { |
| 152 | + t.Run(tt.name, func(t *testing.T) { |
| 153 | + result, err := tt.wrapper.LastUserPrompt() |
| 154 | + |
| 155 | + if tt.expectError { |
| 156 | + require.Error(t, err) |
| 157 | + require.Contains(t, err.Error(), tt.errorMsg) |
| 158 | + require.Nil(t, result) |
| 159 | + } else { |
| 160 | + require.NoError(t, err) |
| 161 | + // Check pointer equality - both nil or both non-nil |
| 162 | + if tt.expected == "" { |
| 163 | + require.Nil(t, result) |
| 164 | + } else { |
| 165 | + require.NotNil(t, result) |
| 166 | + // The result should point to the same string from the content block |
| 167 | + require.Equal(t, tt.expected, *result) |
| 168 | + } |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
0 commit comments