diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-01 14:06:30 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-01 14:06:30 +0300 |
| commit | a892b4913c233d0ef85dd67e3d1c415e383bd6fd (patch) | |
| tree | 198aa3d727a9ae937391f2880cc248d9c22f9498 /internal/phonetic | |
| parent | 9dbf30e9eacccfc23d980c94c3b4d91b5cdb3459 (diff) | |
zu: switch GUI phonetics to shared fetcher
Diffstat (limited to 'internal/phonetic')
| -rw-r--r-- | internal/phonetic/fetcher.go | 13 | ||||
| -rw-r--r-- | internal/phonetic/fetcher_test.go | 56 |
2 files changed, 65 insertions, 4 deletions
diff --git a/internal/phonetic/fetcher.go b/internal/phonetic/fetcher.go index a59d1f4..e6f9694 100644 --- a/internal/phonetic/fetcher.go +++ b/internal/phonetic/fetcher.go @@ -131,10 +131,7 @@ func NewFetcher(config *Config) *Fetcher { // FetchAndSave fetches phonetic information for a word and saves it to the word directory. func (f *Fetcher) FetchAndSave(word, wordDir string) error { - ctx, cancel := context.WithTimeout(context.Background(), phoneticTimeout) - defer cancel() - - phoneticInfo, err := f.fetchPhoneticInfo(ctx, word) + phoneticInfo, err := f.Fetch(word) if err != nil { return err } @@ -147,6 +144,14 @@ func (f *Fetcher) FetchAndSave(word, wordDir string) error { return nil } +// Fetch fetches phonetic information for a word. +func (f *Fetcher) Fetch(word string) (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), phoneticTimeout) + defer cancel() + + return f.fetchPhoneticInfo(ctx, word) +} + // Provider reports the configured phonetic backend. func (f *Fetcher) Provider() Provider { return f.provider diff --git a/internal/phonetic/fetcher_test.go b/internal/phonetic/fetcher_test.go index fedc5c2..c577d5d 100644 --- a/internal/phonetic/fetcher_test.go +++ b/internal/phonetic/fetcher_test.go @@ -64,6 +64,30 @@ func TestFetchAndSave_UnknownProvider(t *testing.T) { } } +func TestFetch_OpenAIProvider(t *testing.T) { + originalFetch := fetchOpenAIPhonetic + fetchOpenAIPhonetic = func(context.Context, *openai.Client, string) (string, error) { + return "[ˈjɤbɐlkɐ]", nil + } + t.Cleanup(func() { + fetchOpenAIPhonetic = originalFetch + }) + + fetcher := NewFetcher(&Config{ + Provider: ProviderOpenAI, + OpenAIKey: "test-openai-key", + }) + + got, err := fetcher.Fetch("ябълка") + if err != nil { + t.Fatalf("Fetch failed: %v", err) + } + + if got != "[ˈjɤbɐlkɐ]" { + t.Fatalf("unexpected phonetic content %q", got) + } +} + func TestFetchAndSave_OpenAIProvider_WritesFile(t *testing.T) { originalFetch := fetchOpenAIPhonetic fetchOpenAIPhonetic = func(context.Context, *openai.Client, string) (string, error) { @@ -93,6 +117,38 @@ func TestFetchAndSave_OpenAIProvider_WritesFile(t *testing.T) { } } +func TestFetch_GeminiProvider(t *testing.T) { + originalFetch := fetchGeminiPhonetic + fetchGeminiPhonetic = func(context.Context, *genai.Client, string) (string, error) { + return "[ˈkotka]", nil + } + t.Cleanup(func() { + fetchGeminiPhonetic = originalFetch + }) + + originalNewGeminiClient := newGeminiClient + newGeminiClient = func(context.Context, *genai.ClientConfig) (*genai.Client, error) { + return &genai.Client{}, nil + } + t.Cleanup(func() { + newGeminiClient = originalNewGeminiClient + }) + + fetcher := NewFetcher(&Config{ + Provider: ProviderGemini, + GoogleAPIKey: "test-google-key", + }) + + got, err := fetcher.Fetch("котка") + if err != nil { + t.Fatalf("Fetch failed: %v", err) + } + + if got != "[ˈkotka]" { + t.Fatalf("unexpected phonetic content %q", got) + } +} + func TestFetchAndSave_GeminiProvider_WritesFile(t *testing.T) { originalFetch := fetchGeminiPhonetic fetchGeminiPhonetic = func(context.Context, *genai.Client, string) (string, error) { |
