From 9c12e879c5d6833ce50f5b6d646ccce03a78db31 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 20 Jul 2025 23:10:50 +0300 Subject: test: add comprehensive test coverage for refactored packages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/phonetic/fetcher_test.go | 91 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 internal/phonetic/fetcher_test.go (limited to 'internal/phonetic') diff --git a/internal/phonetic/fetcher_test.go b/internal/phonetic/fetcher_test.go new file mode 100644 index 0000000..0c9916b --- /dev/null +++ b/internal/phonetic/fetcher_test.go @@ -0,0 +1,91 @@ +package phonetic + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestNewFetcher(t *testing.T) { + fetcher := NewFetcher("test-api-key") + + if fetcher == nil { + t.Fatal("NewFetcher returned nil") + } + + if fetcher.apiKey != "test-api-key" { + t.Errorf("Expected API key 'test-api-key', got '%s'", fetcher.apiKey) + } + + if fetcher.client == nil { + t.Error("OpenAI client not initialized") + } +} + +func TestFetchAndSave_NoAPIKey(t *testing.T) { + fetcher := NewFetcher("") + tmpDir := t.TempDir() + + err := fetcher.FetchAndSave("ябълка", tmpDir) + if err == nil { + t.Error("Expected error for missing API key") + } + + if err.Error() != "OpenAI API key not configured" { + t.Errorf("Expected 'OpenAI API key not configured' error, got: %v", err) + } +} + +func TestFetchAndSave_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") + } + + fetcher := NewFetcher(apiKey) + tmpDir := t.TempDir() + + // Test with a simple word + err := fetcher.FetchAndSave("ябълка", tmpDir) + if err != nil { + t.Errorf("FetchAndSave failed: %v", err) + } + + // Check file was created + phoneticFile := filepath.Join(tmpDir, "phonetic.txt") + content, err := os.ReadFile(phoneticFile) + if err != nil { + t.Errorf("Failed to read phonetic file: %v", err) + } + + // Check content is reasonable + if len(content) < 50 { + t.Error("Phonetic content seems too short") + } + + // Should contain IPA symbols or phonetic information + contentStr := string(content) + if !strings.Contains(contentStr, "/") && !strings.Contains(contentStr, "[") { + t.Error("Content doesn't appear to contain IPA transcription") + } + + t.Logf("Phonetic info for 'ябълка':\n%s", contentStr) +} + +func TestFetchAndSave_InvalidDirectory(t *testing.T) { + // Skip if no API key + apiKey := os.Getenv("OPENAI_API_KEY") + if apiKey == "" { + t.Skip("Skipping test: OPENAI_API_KEY not set") + } + + fetcher := NewFetcher(apiKey) + + // Try to save to a non-existent directory + err := fetcher.FetchAndSave("ябълка", "/nonexistent/path") + if err == nil { + t.Error("Expected error for invalid directory") + } +} -- cgit v1.2.3