Files
apocapoc-api/internal/application/commands/unmark_habit_test.go
T
david aa8f7af55d feat: implement offline sync endpoints with Last-Write-Wins strategy
Add comprehensive offline synchronization support for habits and entries:

## Infrastructure (Phase 1)
- Add UpdatedAt and DeletedAt timestamps to Habit and HabitEntry entities
- Implement soft delete with Delete(), Touch(), and IsDeleted() methods
- Create SQL migration with optimized composite indexes for sync queries
- Add GetChangesSince() and SoftDelete() to both repositories
- Update all Find* methods to exclude soft-deleted records
- 13 comprehensive TDD tests for sync repository methods

## HTTP Endpoints (Phase 2)
- GET /api/v1/sync/changes: retrieve all changes since timestamp
- POST /api/v1/sync/batch: apply client changes with conflict resolution
- Implement Last-Write-Wins strategy using UpdatedAt timestamps
- Add authentication and rate limiting (100 req/min)
- Validate user ownership for all sync operations
- 9 tests for sync handlers (3 queries + 6 commands)

## Technical Details
- Composite indexes: (user_id, updated_at) for optimal query performance
- No pagination: atomic sync operations for data consistency
- Upsert behavior: create resources if not found on server
- DTOs with full entity state including timestamps
- Swagger documentation updated for new endpoints

All 220+ tests passing ✓
2025-12-12 00:11:46 +01:00

170 lines
4.6 KiB
Go

package commands
import (
"apocapoc-api/internal/domain/repositories"
"apocapoc-api/internal/shared/pagination"
"context"
"testing"
"time"
"apocapoc-api/internal/domain/entities"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/errors"
)
type mockEntryRepoForUnmark struct {
mockEntryRepo
entries []*entities.HabitEntry
deletedEntryID string
errorOnDelete error
}
func (m *mockEntryRepoForUnmark) FindByHabitIDAndDateRange(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
return m.entries, nil
}
func (m *mockEntryRepoForUnmark) Delete(ctx context.Context, id string) error {
if m.errorOnDelete != nil {
return m.errorOnDelete
}
m.deletedEntryID = id
return nil
}
func TestUnmarkHabitHandler_UnmarksSuccessfully(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
scheduledDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
entry := entities.NewHabitEntry("habit-1", scheduledDate, nil)
entry.ID = "entry-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
entryRepo := &mockEntryRepoForUnmark{
entries: []*entities.HabitEntry{entry},
}
handler := NewUnmarkHabitHandler(habitRepo, entryRepo)
cmd := UnmarkHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
ScheduledDate: scheduledDate,
}
err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if entryRepo.deletedEntryID != "entry-1" {
t.Errorf("Expected entry entry-1 to be deleted, got %s", entryRepo.deletedEntryID)
}
}
func TestUnmarkHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
habitRepo := &mockHabitRepoForUpdate{
errorOnFind: errors.ErrNotFound,
}
entryRepo := &mockEntryRepoForUnmark{}
handler := NewUnmarkHabitHandler(habitRepo, entryRepo)
cmd := UnmarkHabitCommand{
HabitID: "non-existent",
UserID: "user-123",
ScheduledDate: time.Now(),
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrNotFound {
t.Errorf("Expected ErrNotFound, got %v", err)
}
}
func TestUnmarkHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
entryRepo := &mockEntryRepoForUnmark{}
handler := NewUnmarkHabitHandler(habitRepo, entryRepo)
cmd := UnmarkHabitCommand{
HabitID: "habit-1",
UserID: "user-456", // Different user
ScheduledDate: time.Now(),
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrUnauthorized {
t.Errorf("Expected ErrUnauthorized, got %v", err)
}
}
func TestUnmarkHabitHandler_ReturnsErrorWhenEntryNotFound(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
entryRepo := &mockEntryRepoForUnmark{
entries: []*entities.HabitEntry{},
}
handler := NewUnmarkHabitHandler(habitRepo, entryRepo)
cmd := UnmarkHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrNotFound {
t.Errorf("Expected ErrNotFound for missing entry, got %v", err)
}
}
func (m *mockEntryRepoForUnmark) FindActiveByUserIDWithPagination(ctx context.Context, userID string, params pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockEntryRepoForUnmark) CountActiveByUserID(ctx context.Context, userID string) (int, error) {
return 0, nil
}
func (m *mockEntryRepoForUnmark) FindByUserIDFiltered(ctx context.Context, userID string, filter repositories.HabitFilter, paginationParams *pagination.Params) ([]*entities.Habit, error) {
return nil, nil
}
func (m *mockEntryRepoForUnmark) CountByUserIDFiltered(ctx context.Context, userID string, filter repositories.HabitFilter) (int, error) {
return 0, nil
}
func (m *mockEntryRepoForUnmark) GetChangesSince(ctx context.Context, userID string, since time.Time) (*repositories.HabitEntryChanges, error) {
return &repositories.HabitEntryChanges{
Created: []*entities.HabitEntry{},
Updated: []*entities.HabitEntry{},
Deleted: []string{},
}, nil
}
func (m *mockEntryRepoForUnmark) SoftDelete(ctx context.Context, id string) error {
return nil
}