|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package google |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestDetectMimeType(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + data []byte |
| 18 | + expected string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "plain text", |
| 22 | + data: []byte("Hello, World!"), |
| 23 | + expected: "text/plain", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "empty file", |
| 27 | + data: []byte{}, |
| 28 | + expected: "text/plain", |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.name, func(t *testing.T) { |
| 34 | + result := detectMimeType(tt.data) |
| 35 | + if !containsMimeType(result, tt.expected) { |
| 36 | + t.Errorf("detectMimeType() = %v, want to contain %v", result, tt.expected) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func containsMimeType(got, want string) bool { |
| 43 | + // DetectContentType may return variations like "text/plain; charset=utf-8" |
| 44 | + return got == want || (want == "text/plain" && got == "text/plain; charset=utf-8") |
| 45 | +} |
| 46 | + |
| 47 | +func TestGetMaxFileSize(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + mimeType string |
| 51 | + expectedSize int |
| 52 | + expectedStr string |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "PDF file", |
| 56 | + mimeType: "application/pdf", |
| 57 | + expectedSize: 5 * 1024 * 1024, |
| 58 | + expectedStr: "5MB", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "PNG image", |
| 62 | + mimeType: "image/png", |
| 63 | + expectedSize: 7 * 1024 * 1024, |
| 64 | + expectedStr: "7MB", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "JPEG image", |
| 68 | + mimeType: "image/jpeg", |
| 69 | + expectedSize: 7 * 1024 * 1024, |
| 70 | + expectedStr: "7MB", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "text file", |
| 74 | + mimeType: "text/plain", |
| 75 | + expectedSize: 200 * 1024, |
| 76 | + expectedStr: "200KB", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + size, sizeStr := getMaxFileSize(tt.mimeType) |
| 83 | + if size != tt.expectedSize { |
| 84 | + t.Errorf("getMaxFileSize() size = %v, want %v", size, tt.expectedSize) |
| 85 | + } |
| 86 | + if sizeStr != tt.expectedStr { |
| 87 | + t.Errorf("getMaxFileSize() sizeStr = %v, want %v", sizeStr, tt.expectedStr) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestSummarizeFile_FileNotFound(t *testing.T) { |
| 94 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 95 | + defer cancel() |
| 96 | + |
| 97 | + _, _, err := SummarizeFile(ctx, "/nonexistent/file.txt", "fake-api-key") |
| 98 | + if err == nil { |
| 99 | + t.Error("SummarizeFile() expected error for nonexistent file, got nil") |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestSummarizeFile_BinaryFile(t *testing.T) { |
| 104 | + // Create a temporary binary file |
| 105 | + tmpDir := t.TempDir() |
| 106 | + binFile := filepath.Join(tmpDir, "test.bin") |
| 107 | + |
| 108 | + // Create binary data (not text, image, or PDF) |
| 109 | + binaryData := []byte{0x00, 0x01, 0x02, 0x03, 0x7F, 0x80, 0xFF} |
| 110 | + if err := os.WriteFile(binFile, binaryData, 0644); err != nil { |
| 111 | + t.Fatalf("Failed to create test file: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 115 | + defer cancel() |
| 116 | + |
| 117 | + _, _, err := SummarizeFile(ctx, binFile, "fake-api-key") |
| 118 | + if err == nil { |
| 119 | + t.Error("SummarizeFile() expected error for binary file, got nil") |
| 120 | + } |
| 121 | + if err != nil && !containsString(err.Error(), "binary data") { |
| 122 | + t.Errorf("SummarizeFile() error = %v, want error containing 'binary data'", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestSummarizeFile_FileTooLarge(t *testing.T) { |
| 127 | + // Create a temporary text file that exceeds the limit |
| 128 | + tmpDir := t.TempDir() |
| 129 | + textFile := filepath.Join(tmpDir, "large.txt") |
| 130 | + |
| 131 | + // Create a file larger than 200KB (text file limit) |
| 132 | + largeData := make([]byte, 201*1024) |
| 133 | + for i := range largeData { |
| 134 | + largeData[i] = 'a' |
| 135 | + } |
| 136 | + if err := os.WriteFile(textFile, largeData, 0644); err != nil { |
| 137 | + t.Fatalf("Failed to create test file: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 141 | + defer cancel() |
| 142 | + |
| 143 | + _, _, err := SummarizeFile(ctx, textFile, "fake-api-key") |
| 144 | + if err == nil { |
| 145 | + t.Error("SummarizeFile() expected error for file too large, got nil") |
| 146 | + } |
| 147 | + if err != nil && !containsString(err.Error(), "exceeds maximum size") { |
| 148 | + t.Errorf("SummarizeFile() error = %v, want error containing 'exceeds maximum size'", err) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func containsString(s, substr string) bool { |
| 153 | + return len(s) >= len(substr) && (s == substr || len(substr) == 0 || |
| 154 | + (len(s) > 0 && len(substr) > 0 && stringContains(s, substr))) |
| 155 | +} |
| 156 | + |
| 157 | +func stringContains(s, substr string) bool { |
| 158 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 159 | + if s[i:i+len(substr)] == substr { |
| 160 | + return true |
| 161 | + } |
| 162 | + } |
| 163 | + return false |
| 164 | +} |
| 165 | + |
| 166 | +// Note: We don't test the actual API call without a real API key |
| 167 | +// Integration tests would require setting GOOGLE_API_KEY environment variable |
0 commit comments