package service import ( "context" "errors" "strings" "testing" "github.com/charawein/mtusi2026lab2/internal/repository/memory" ) func newUserService() *UserService { return NewUserService(memory.NewUserRepository()) } func TestUserService_Register_OK(t *testing.T) { svc := newUserService() ctx := context.Background() u, err := svc.Register(ctx, "alice", "Алиса") if err != nil { t.Fatalf("Register failed: %v", err) } if u.ID == "" { t.Error("expected non-empty ID") } if u.Username != "alice" { t.Errorf("username mismatch: got %q", u.Username) } if u.CreatedAt.IsZero() { t.Error("expected non-zero CreatedAt") } } func TestUserService_Register_InvalidUsername(t *testing.T) { svc := newUserService() ctx := context.Background() cases := []struct { name string username string }{ {"too short", "ab"}, {"too long", strings.Repeat("a", 33)}, {"uppercase", "Alice"}, {"with dash", "al-ice"}, {"with space", "al ice"}, {"empty", ""}, {"only spaces", " "}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { _, err := svc.Register(ctx, tc.username, "Test") if !errors.Is(err, ErrInvalidInput) { t.Errorf("expected ErrInvalidInput, got %v", err) } }) } } func TestUserService_Register_InvalidDisplayName(t *testing.T) { svc := newUserService() ctx := context.Background() cases := []struct { name string displayName string }{ {"empty", ""}, {"only spaces", " "}, {"too long", strings.Repeat("я", 101)}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { _, err := svc.Register(ctx, "alice", tc.displayName) if !errors.Is(err, ErrInvalidInput) { t.Errorf("expected ErrInvalidInput, got %v", err) } }) } } func TestUserService_Register_UnicodeDisplayName_OK(t *testing.T) { svc := newUserService() ctx := context.Background() // 100 code points ровно — должно пройти. name := strings.Repeat("я", 100) if _, err := svc.Register(ctx, "alice", name); err != nil { t.Fatalf("expected OK for 100 code points, got %v", err) } } func TestUserService_Register_DuplicateUsername(t *testing.T) { svc := newUserService() ctx := context.Background() if _, err := svc.Register(ctx, "alice", "Алиса"); err != nil { t.Fatalf("first Register failed: %v", err) } _, err := svc.Register(ctx, "alice", "Another") if !errors.Is(err, ErrConflict) { t.Fatalf("expected ErrConflict, got %v", err) } } func TestUserService_GetByID_NotFound(t *testing.T) { svc := newUserService() ctx := context.Background() _, err := svc.GetByID(ctx, "nonexistent") if !errors.Is(err, ErrNotFound) { t.Fatalf("expected ErrNotFound, got %v", err) } }