summaryrefslogtreecommitdiff
path: root/internal/phonetic
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/phonetic
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/phonetic')
-rw-r--r--internal/phonetic/fetcher_test.go91
1 files changed, 91 insertions, 0 deletions
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")
+ }
+}