tercul-backend/internal/app/localization/service_test.go
google-labs-jules[bot] 781b313bf1 feat: Complete all pending tasks from TASKS.md
This commit addresses all the high-priority tasks outlined in the TASKS.md file, significantly improving the application's observability, completing key features, and refactoring critical parts of the codebase.

### Observability

- **Centralized Logging:** Implemented a new structured, context-aware logging system using `zerolog`. A new logging middleware injects request-specific information (request ID, user ID, trace ID) into the logger, and all application logging has been refactored to use this new system.
- **Prometheus Metrics:** Added Prometheus metrics for database query performance by creating a GORM plugin that automatically records query latency and totals.
- **OpenTelemetry Tracing:** Fully instrumented all application services in `internal/app` and data repositories in `internal/data/sql` with OpenTelemetry tracing, providing deep visibility into application performance.

### Features

- **Analytics:** Implemented like, comment, and bookmark counting. The respective command handlers now call the analytics service to increment counters when these actions are performed.
- **Enrichment Tool:** Built a new, extensible `enrich` command-line tool to fetch data from external sources. The initial implementation enriches author data using the Open Library API.

### Refactoring & Fixes

- **Decoupled Testing:** Refactored the testing utilities in `internal/testutil` to be database-agnostic, promoting the use of mock-based unit tests and improving test speed and reliability.
- **Build Fixes:** Resolved numerous build errors, including a critical import cycle between the logging, observability, and authentication packages.
- **Search Service:** Fixed the search service integration by implementing the `GetWorkContent` method in the localization service, allowing the search indexer to correctly fetch and index work content.
2025-10-05 05:26:27 +00:00

93 lines
2.6 KiB
Go

package localization
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockLocalizationRepository struct {
mock.Mock
}
func (m *mockLocalizationRepository) GetTranslation(ctx context.Context, key string, language string) (string, error) {
args := m.Called(ctx, key, language)
return args.String(0), args.Error(1)
}
func (m *mockLocalizationRepository) GetTranslations(ctx context.Context, keys []string, language string) (map[string]string, error) {
args := m.Called(ctx, keys, language)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(map[string]string), args.Error(1)
}
func (m *mockLocalizationRepository) GetAuthorBiography(ctx context.Context, authorID uint, language string) (string, error) {
args := m.Called(ctx, authorID, language)
return args.String(0), args.Error(1)
}
func (m *mockLocalizationRepository) GetWorkContent(ctx context.Context, workID uint, language string) (string, error) {
args := m.Called(ctx, workID, language)
return args.String(0), args.Error(1)
}
func TestLocalizationService_GetTranslation(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
key := "test_key"
language := "en"
expectedTranslation := "Test Translation"
repo.On("GetTranslation", ctx, key, language).Return(expectedTranslation, nil)
translation, err := service.Queries.GetTranslation(ctx, key, language)
assert.NoError(t, err)
assert.Equal(t, expectedTranslation, translation)
repo.AssertExpectations(t)
}
func TestLocalizationService_GetTranslations(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
keys := []string{"key1", "key2"}
language := "en"
expectedTranslations := map[string]string{
"key1": "Translation 1",
"key2": "Translation 2",
}
repo.On("GetTranslations", ctx, keys, language).Return(expectedTranslations, nil)
translations, err := service.Queries.GetTranslations(ctx, keys, language)
assert.NoError(t, err)
assert.Equal(t, expectedTranslations, translations)
repo.AssertExpectations(t)
}
func TestLocalizationService_GetAuthorBiography(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
authorID := uint(1)
language := "en"
expectedBiography := "This is a test biography."
repo.On("GetAuthorBiography", ctx, authorID, language).Return(expectedBiography, nil)
biography, err := service.Queries.GetAuthorBiography(ctx, authorID, language)
assert.NoError(t, err)
assert.Equal(t, expectedBiography, biography)
repo.AssertExpectations(t)
}