package domain import ( "testing" ) func TestNewUser(t *testing.T) { tests := []struct { name string id UserID email string password string wantErr error }{ { name: "valid user", id: "user-123", email: "user@example.com", password: "password123", wantErr: nil, }, { name: "empty email", id: "user-123", email: "", password: "password123", wantErr: ErrInvalidEmail, }, { name: "email without @", id: "user-123", email: "not-an-email", password: "password123", wantErr: ErrInvalidEmail, }, { name: "email with @ at start", id: "user-123", email: "@example.com", password: "password123", wantErr: ErrInvalidEmail, }, { name: "email with @ at end", id: "user-123", email: "user@", password: "password123", wantErr: ErrInvalidEmail, }, { name: "email with multiple @", id: "user-123", email: "user@@example.com", password: "password123", wantErr: ErrInvalidEmail, }, { name: "short password", id: "user-123", email: "user@example.com", password: "short", wantErr: ErrInvalidPassword, }, { name: "empty password", id: "user-123", email: "user@example.com", password: "", wantErr: ErrInvalidPassword, }, { name: "minimum valid password", id: "user-123", email: "user@example.com", password: "12345678", wantErr: nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { user, err := NewUser(tt.id, tt.email, tt.password) if tt.wantErr != nil { if err != tt.wantErr { t.Errorf("expected error %v, got %v", tt.wantErr, err) } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if user.ID != tt.id { t.Errorf("expected ID %s, got %s", tt.id, user.ID) } if user.Email != tt.email { t.Errorf("expected email %s, got %s", tt.email, user.Email) } if user.PasswordHash == "" { t.Error("expected password hash to be set") } if user.PasswordHash == tt.password { t.Error("password hash should not equal plaintext password") } if user.CreatedAt.IsZero() { t.Error("expected CreatedAt to be set") } if user.UpdatedAt.IsZero() { t.Error("expected UpdatedAt to be set") } }) } } func TestUser_CheckPassword(t *testing.T) { user, err := NewUser("user-123", "user@example.com", "correctpassword") if err != nil { t.Fatalf("failed to create user: %v", err) } tests := []struct { name string password string want bool }{ { name: "correct password", password: "correctpassword", want: true, }, { name: "wrong password", password: "wrongpassword", want: false, }, { name: "empty password", password: "", want: false, }, { name: "similar password", password: "correctPassword", want: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := user.CheckPassword(tt.password) if got != tt.want { t.Errorf("CheckPassword() = %v, want %v", got, tt.want) } }) } } func TestUserID(t *testing.T) { t.Run("String", func(t *testing.T) { id := UserID("user-123") if id.String() != "user-123" { t.Errorf("String() = %s, want user-123", id.String()) } }) t.Run("IsZero", func(t *testing.T) { var zeroID UserID if !zeroID.IsZero() { t.Error("expected zero ID to be zero") } nonZeroID := UserID("user-123") if nonZeroID.IsZero() { t.Error("expected non-zero ID to not be zero") } }) }