tercul-backend/internal/app/translation/commands_test.go
google-labs-jules[bot] fa90dd79da feat: Complete large-scale refactor and prepare for production
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production.

The key changes include:

- **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model.

- **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior.

- **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies.

- **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime.

- **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors.

- **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph.

- **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability.

This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
2025-10-07 11:09:37 +00:00

126 lines
4.0 KiB
Go

package translation_test
import (
"context"
"testing"
"tercul/internal/app/authz"
"tercul/internal/app/translation"
"tercul/internal/domain"
platform_auth "tercul/internal/platform/auth"
"tercul/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
type TranslationCommandsTestSuite struct {
suite.Suite
mockWorkRepo *testutil.MockWorkRepository
mockTranslationRepo *testutil.MockTranslationRepository
authzSvc *authz.Service
cmd *translation.TranslationCommands
adminCtx context.Context
userCtx context.Context
adminUser *domain.User
regularUser *domain.User
}
func (s *TranslationCommandsTestSuite) SetupTest() {
s.mockWorkRepo = new(testutil.MockWorkRepository)
s.mockTranslationRepo = new(testutil.MockTranslationRepository)
s.authzSvc = authz.NewService(s.mockWorkRepo, s.mockTranslationRepo)
s.cmd = translation.NewTranslationCommands(s.mockTranslationRepo, s.authzSvc)
s.adminUser = &domain.User{BaseModel: domain.BaseModel{ID: 1}, Role: domain.UserRoleAdmin}
s.regularUser = &domain.User{BaseModel: domain.BaseModel{ID: 2}, Role: domain.UserRoleContributor}
s.adminCtx = context.WithValue(context.Background(), platform_auth.ClaimsContextKey, &platform_auth.Claims{
UserID: s.adminUser.ID,
Role: string(s.adminUser.Role),
})
s.userCtx = context.WithValue(context.Background(), platform_auth.ClaimsContextKey, &platform_auth.Claims{
UserID: s.regularUser.ID,
Role: string(s.regularUser.Role),
})
}
func (s *TranslationCommandsTestSuite) TestCreateOrUpdateTranslation() {
testWork := &domain.Work{
TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}},
}
input := translation.CreateOrUpdateTranslationInput{
Title: "Test Title",
Content: "Test content",
Language: "es",
TranslatableID: testWork.ID,
TranslatableType: "works",
}
s.Run("should create translation for admin", func() {
// Arrange
s.mockWorkRepo.On("GetByID", mock.Anything, testWork.ID).Return(testWork, nil).Once()
s.mockTranslationRepo.On("Upsert", mock.Anything, mock.AnythingOfType("*domain.Translation")).Return(nil).Once()
// Act
result, err := s.cmd.CreateOrUpdateTranslation(s.adminCtx, input)
// Assert
s.NoError(err)
s.NotNil(result)
s.Equal(input.Title, result.Title)
s.Equal(s.adminUser.ID, *result.TranslatorID)
s.mockWorkRepo.AssertExpectations(s.T())
s.mockTranslationRepo.AssertExpectations(s.T())
})
s.Run("should create translation for author", func() {
// Arrange
s.mockWorkRepo.On("GetByID", mock.Anything, testWork.ID).Return(testWork, nil).Once()
s.mockWorkRepo.On("IsAuthor", mock.Anything, testWork.ID, s.regularUser.ID).Return(true, nil).Once()
s.mockTranslationRepo.On("Upsert", mock.Anything, mock.AnythingOfType("*domain.Translation")).Return(nil).Once()
// Act
result, err := s.cmd.CreateOrUpdateTranslation(s.userCtx, input)
// Assert
s.NoError(err)
s.NotNil(result)
s.Equal(input.Title, result.Title)
s.Equal(s.regularUser.ID, *result.TranslatorID)
s.mockWorkRepo.AssertExpectations(s.T())
s.mockTranslationRepo.AssertExpectations(s.T())
})
s.Run("should fail if user is not authorized", func() {
// Arrange
s.mockWorkRepo.On("GetByID", mock.Anything, testWork.ID).Return(testWork, nil).Once()
s.mockWorkRepo.On("IsAuthor", mock.Anything, testWork.ID, s.regularUser.ID).Return(false, nil).Once()
// Act
_, err := s.cmd.CreateOrUpdateTranslation(s.userCtx, input)
// Assert
s.Error(err)
s.ErrorIs(err, domain.ErrForbidden)
s.mockWorkRepo.AssertExpectations(s.T())
})
s.Run("should fail on validation error for empty language", func() {
// Arrange
invalidInput := input
invalidInput.Language = ""
// Act
_, err := s.cmd.CreateOrUpdateTranslation(s.userCtx, invalidInput)
// Assert
s.Error(err)
assert.ErrorContains(s.T(), err, "language cannot be empty")
})
}
func TestTranslationCommands(t *testing.T) {
suite.Run(t, new(TranslationCommandsTestSuite))
}