diff options
Diffstat (limited to 'internal/image/gemini_test.go')
| -rw-r--r-- | internal/image/gemini_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/image/gemini_test.go b/internal/image/gemini_test.go index aae8014..91e0ce4 100644 --- a/internal/image/gemini_test.go +++ b/internal/image/gemini_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "strings" "testing" + "unicode/utf8" "google.golang.org/genai" ) @@ -189,6 +190,55 @@ func TestGeminiProvider_Search_CustomPromptIsTruncated(t *testing.T) { } } +func TestGeminiProvider_Search_CustomPromptTruncatesUTF8Safely(t *testing.T) { + originalText := geminiGenerateText + originalImage := geminiGenerateImage + t.Cleanup(func() { + geminiGenerateText = originalText + geminiGenerateImage = originalImage + }) + + geminiGenerateText = func(context.Context, *GeminiProvider, string, string, string, float32, int32) (string, error) { + t.Fatal("unexpected text generation for truncated custom prompt") + return "", nil + } + + var gotPrompt string + geminiGenerateImage = func(_ context.Context, _ *GeminiProvider, prompt, _ string) ([]byte, string, error) { + gotPrompt = prompt + return mustJPEGBytes(t), "image/jpeg", nil + } + + client := NewGeminiProvider(&GeminiConfig{APIKey: "test-key"}) + longPrompt := strings.Repeat("яa", 2500) + if len(longPrompt) <= maxCustomPrompt { + t.Fatal("test prompt did not exceed truncation limit") + } + + results, err := client.Search(context.Background(), &SearchOptions{ + Query: "ябълка", + CustomPrompt: longPrompt, + }) + if err != nil { + t.Fatalf("Search() unexpected error: %v", err) + } + if !utf8.ValidString(gotPrompt) { + t.Fatalf("Search() returned invalid UTF-8 prompt: %q", gotPrompt) + } + if len(gotPrompt) > maxCustomPrompt { + t.Fatalf("prompt length = %d, want <= %d", len(gotPrompt), maxCustomPrompt) + } + if !strings.HasSuffix(gotPrompt, "...") { + t.Fatalf("prompt = %q, want ellipsis suffix", gotPrompt) + } + if client.LastPrompt() != gotPrompt { + t.Fatalf("LastPrompt() = %q, want %q", client.LastPrompt(), gotPrompt) + } + if len(results) != 1 { + t.Fatalf("expected 1 result, got %d", len(results)) + } +} + func TestGeminiProvider_GenerateImage_UsesCustomPrompt(t *testing.T) { originalText := geminiGenerateText originalImage := geminiGenerateImage |
