summaryrefslogtreecommitdiff
path: root/internal/translation
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-20 23:10:50 +0300
committerPaul Buetow <paul@buetow.org>2025-07-20 23:10:50 +0300
commit9c12e879c5d6833ce50f5b6d646ccce03a78db31 (patch)
tree206906b551d595b35d00586b6cc5bf9e1f3fe7f8 /internal/translation
parente580fb57a29ec3c3f3e180b20cfa6ec28687689b (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.go156
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)
+ }
+}