diff options
| author | Paul Buetow <paul@buetow.org> | 2025-07-20 23:10:50 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-07-20 23:10:50 +0300 |
| commit | 9c12e879c5d6833ce50f5b6d646ccce03a78db31 (patch) | |
| tree | 206906b551d595b35d00586b6cc5bf9e1f3fe7f8 /internal/translation | |
| parent | e580fb57a29ec3c3f3e180b20cfa6ec28687689b (diff) | |
test: add comprehensive test coverage for refactored packages
Add test suites for all newly created packages from the main.go refactoring:
- batch: 100% coverage - file reading, parsing, edge cases
- cli: 96.7% coverage - command setup, flags, configuration
- translation: 92% coverage - API integration, caching, errors
- phonetic: 87.5% coverage - API fetching, file operations
- models: 77.3% coverage - model listing functionality
- processor: 18% coverage - basic tests (limited by API dependencies)
Total: 1159 lines of test code across 7 new test files
π€ Generated with [opencode](https://opencode.ai)
Co-Authored-By: opencode <noreply@opencode.ai>
Diffstat (limited to 'internal/translation')
| -rw-r--r-- | internal/translation/translator_test.go | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/internal/translation/translator_test.go b/internal/translation/translator_test.go new file mode 100644 index 0000000..3688311 --- /dev/null +++ b/internal/translation/translator_test.go @@ -0,0 +1,156 @@ +package translation + +import ( + "os" + "path/filepath" + "reflect" + "testing" +) + +func TestNewTranslator(t *testing.T) { + translator := NewTranslator("test-api-key") + + if translator == nil { + t.Fatal("NewTranslator returned nil") + } + + if translator.apiKey != "test-api-key" { + t.Errorf("Expected API key 'test-api-key', got '%s'", translator.apiKey) + } + + if translator.client == nil { + t.Error("OpenAI client not initialized") + } +} + +func TestTranslateWord_NoAPIKey(t *testing.T) { + translator := NewTranslator("") + + _, err := translator.TranslateWord("ΡΠ±ΡΠ»ΠΊΠ°") + if err == nil { + t.Error("Expected error for missing API key") + } + + if err.Error() != "OpenAI API key not found" { + t.Errorf("Expected 'OpenAI API key not found' error, got: %v", err) + } +} + +func TestTranslateWord_Integration(t *testing.T) { + // Skip if no API key + apiKey := os.Getenv("OPENAI_API_KEY") + if apiKey == "" { + t.Skip("Skipping integration test: OPENAI_API_KEY not set") + } + + translator := NewTranslator(apiKey) + + // Test with a simple word + translation, err := translator.TranslateWord("ΡΠ±ΡΠ»ΠΊΠ°") + if err != nil { + t.Errorf("TranslateWord failed: %v", err) + } + + // Check that we got a reasonable translation + // The exact translation might vary, but it should contain "apple" + if translation == "" { + t.Error("Got empty translation") + } + + t.Logf("Translation of 'ΡΠ±ΡΠ»ΠΊΠ°': %s", translation) +} + +func TestSaveTranslation(t *testing.T) { + tmpDir := t.TempDir() + + err := SaveTranslation(tmpDir, "ΡΠ±ΡΠ»ΠΊΠ°", "apple") + if err != nil { + t.Errorf("SaveTranslation failed: %v", err) + } + + // Check file was created + translationFile := filepath.Join(tmpDir, "translation.txt") + content, err := os.ReadFile(translationFile) + if err != nil { + t.Errorf("Failed to read translation file: %v", err) + } + + expected := "ΡΠ±ΡΠ»ΠΊΠ° = apple\n" + if string(content) != expected { + t.Errorf("Expected content '%s', got '%s'", expected, string(content)) + } +} + +func TestSaveTranslation_InvalidPath(t *testing.T) { + err := SaveTranslation("/nonexistent/path", "ΡΠ±ΡΠ»ΠΊΠ°", "apple") + if err == nil { + t.Error("Expected error for invalid path") + } +} + +func TestTranslationCache(t *testing.T) { + cache := NewTranslationCache() + + // Test empty cache + _, found := cache.Get("ΡΠ±ΡΠ»ΠΊΠ°") + if found { + t.Error("Expected not found in empty cache") + } + + // Test adding and retrieving + cache.Add("ΡΠ±ΡΠ»ΠΊΠ°", "apple") + cache.Add("ΠΊΠΎΡΠΊΠ°", "cat") + + translation, found := cache.Get("ΡΠ±ΡΠ»ΠΊΠ°") + if !found { + t.Error("Expected to find 'ΡΠ±ΡΠ»ΠΊΠ°' in cache") + } + if translation != "apple" { + t.Errorf("Expected 'apple', got '%s'", translation) + } + + // Test overwriting + cache.Add("ΡΠ±ΡΠ»ΠΊΠ°", "apple (fruit)") + translation, found = cache.Get("ΡΠ±ΡΠ»ΠΊΠ°") + if !found || translation != "apple (fruit)" { + t.Errorf("Expected 'apple (fruit)', got '%s'", translation) + } +} + +func TestTranslationCache_GetAll(t *testing.T) { + cache := NewTranslationCache() + + // Add some translations + cache.Add("ΡΠ±ΡΠ»ΠΊΠ°", "apple") + cache.Add("ΠΊΠΎΡΠΊΠ°", "cat") + cache.Add("ΠΊΡΡΠ΅", "dog") + + all := cache.GetAll() + + expected := map[string]string{ + "ΡΠ±ΡΠ»ΠΊΠ°": "apple", + "ΠΊΠΎΡΠΊΠ°": "cat", + "ΠΊΡΡΠ΅": "dog", + } + + if !reflect.DeepEqual(all, expected) { + t.Errorf("GetAll() = %v, want %v", all, expected) + } + + // Test that modifying returned map doesn't affect cache + all["ΡΠ±ΡΠ»ΠΊΠ°"] = "modified" + + translation, _ := cache.Get("ΡΠ±ΡΠ»ΠΊΠ°") + if translation != "apple" { + t.Error("Cache was modified through returned map") + } +} + +func TestTranslationCache_EmptyCache(t *testing.T) { + cache := NewTranslationCache() + + all := cache.GetAll() + if len(all) != 0 { + t.Errorf("Expected empty map, got %v", all) + } +} |
