package mining import ( "errors" "net/http" "testing" ) // TestErrors_Error_Good verifies the error string format with no cause func TestErrors_Error_Good(t *testing.T) { err := NewMiningError(ErrCodeMinerNotFound, "miner not found") expected := "MINER_NOT_FOUND: miner not found" if err.Error() != expected { t.Errorf("Expected %q, got %q", expected, err.Error()) } } // TestErrors_Error_Bad verifies the error string includes the underlying cause func TestErrors_Error_Bad(t *testing.T) { cause := errors.New("underlying error") err := NewMiningError(ErrCodeStartFailed, "failed to start").WithCause(cause) if err.Cause != cause { t.Error("Cause was not set") } if errors.Unwrap(err) != cause { t.Error("Unwrap did not return cause") } } // TestErrors_WithDetails_Good verifies details are attached to the error func TestErrors_WithDetails_Good(t *testing.T) { err := NewMiningError(ErrCodeInvalidConfig, "invalid config"). WithDetails("port must be between 1024 and 65535") if err.Details != "port must be between 1024 and 65535" { t.Errorf("Details not set correctly: %s", err.Details) } } // TestErrors_WithSuggestion_Good verifies suggestions are attached to the error func TestErrors_WithSuggestion_Good(t *testing.T) { err := NewMiningError(ErrCodeConnectionFailed, "connection failed"). WithSuggestion("check your network") if err.Suggestion != "check your network" { t.Errorf("Suggestion not set correctly: %s", err.Suggestion) } } // TestErrors_StatusCode_Good verifies each error constructor returns the correct HTTP status func TestErrors_StatusCode_Good(t *testing.T) { cases := []struct { name string err *MiningError expected int }{ {"default", NewMiningError("TEST", "test"), http.StatusInternalServerError}, {"not found", ErrMinerNotFound("test"), http.StatusNotFound}, {"conflict", ErrMinerExists("test"), http.StatusConflict}, {"bad request", ErrInvalidConfig("bad"), http.StatusBadRequest}, {"service unavailable", ErrConnectionFailed("pool"), http.StatusServiceUnavailable}, {"timeout", ErrTimeout("operation"), http.StatusGatewayTimeout}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if c.err.StatusCode() != c.expected { t.Errorf("Expected status %d, got %d", c.expected, c.err.StatusCode()) } }) } } // TestErrors_IsRetryable_Good verifies retryable vs non-retryable classifications func TestErrors_IsRetryable_Good(t *testing.T) { cases := []struct { name string err *MiningError retryable bool }{ {"not found", ErrMinerNotFound("test"), false}, {"exists", ErrMinerExists("test"), false}, {"invalid config", ErrInvalidConfig("bad"), false}, {"install failed", ErrInstallFailed("xmrig"), true}, {"start failed", ErrStartFailed("test"), true}, {"connection failed", ErrConnectionFailed("pool"), true}, {"timeout", ErrTimeout("operation"), true}, {"database error", ErrDatabaseError("query"), true}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if c.err.IsRetryable() != c.retryable { t.Errorf("Expected retryable=%v, got %v", c.retryable, c.err.IsRetryable()) } }) } } // TestErrors_PredefinedErrors_Good verifies each error constructor sets the correct code and message func TestErrors_PredefinedErrors_Good(t *testing.T) { cases := []struct { name string err *MiningError code string }{ {"ErrMinerNotFound", ErrMinerNotFound("test"), ErrCodeMinerNotFound}, {"ErrMinerExists", ErrMinerExists("test"), ErrCodeMinerExists}, {"ErrMinerNotRunning", ErrMinerNotRunning("test"), ErrCodeMinerNotRunning}, {"ErrInstallFailed", ErrInstallFailed("xmrig"), ErrCodeInstallFailed}, {"ErrStartFailed", ErrStartFailed("test"), ErrCodeStartFailed}, {"ErrStopFailed", ErrStopFailed("test"), ErrCodeStopFailed}, {"ErrInvalidConfig", ErrInvalidConfig("bad port"), ErrCodeInvalidConfig}, {"ErrUnsupportedMiner", ErrUnsupportedMiner("unknown"), ErrCodeUnsupportedMiner}, {"ErrConnectionFailed", ErrConnectionFailed("pool:3333"), ErrCodeConnectionFailed}, {"ErrTimeout", ErrTimeout("GetStats"), ErrCodeTimeout}, {"ErrDatabaseError", ErrDatabaseError("insert"), ErrCodeDatabaseError}, {"ErrProfileNotFound", ErrProfileNotFound("abc123"), ErrCodeProfileNotFound}, {"ErrProfileExists", ErrProfileExists("My Profile"), ErrCodeProfileExists}, {"ErrInternal", ErrInternal("unexpected error"), ErrCodeInternalError}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if c.err.Code != c.code { t.Errorf("Expected code %s, got %s", c.code, c.err.Code) } if c.err.Message == "" { t.Error("Message should not be empty") } }) } } // TestErrors_Chaining_Ugly verifies error chaining survives multiple WithCause/WithDetails/WithSuggestion calls func TestErrors_Chaining_Ugly(t *testing.T) { cause := errors.New("network timeout") err := ErrConnectionFailed("pool:3333"). WithCause(cause). WithDetails("timeout after 30s"). WithSuggestion("check firewall settings") if err.Code != ErrCodeConnectionFailed { t.Errorf("Code changed: %s", err.Code) } if err.Cause != cause { t.Error("Cause not set") } if err.Details != "timeout after 30s" { t.Errorf("Details not set: %s", err.Details) } if err.Suggestion != "check firewall settings" { t.Errorf("Suggestion not set: %s", err.Suggestion) } }