package handlers import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "git.mchus.pro/mchus/quoteforge/internal/configfeatures" "github.com/gin-gonic/gin" ) func TestClientFacingErrorMessageKeepsDomain4xx(t *testing.T) { t.Parallel() got := clientFacingErrorMessage(400, "invalid request", &json.SyntaxError{Offset: 1}) if got != "invalid request" { t.Fatalf("expected fallback for decode error, got %q", got) } } func TestClientFacingErrorMessagePreservesBusinessMessage(t *testing.T) { t.Parallel() err := errString("main project variant cannot be deleted") got := clientFacingErrorMessage(400, "invalid request", err) if got != err.Error() { t.Fatalf("expected business message, got %q", got) } } func TestClientFacingErrorMessageHidesTechnical4xx(t *testing.T) { t.Parallel() err := errString("sql: no rows in result set") got := clientFacingErrorMessage(404, "resource not found", err) if got != "resource not found" { t.Fatalf("expected fallback for technical error, got %q", got) } } type errString string func (e errString) Error() string { return string(e) } func TestRespondErrorUnsupportedFeaturesIs409(t *testing.T) { gin.SetMode(gin.TestMode) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) err := fmt.Errorf("wrapped: %w", configfeatures.Check([]string{"future_x"})) RespondError(c, http.StatusInternalServerError, "internal server error", err) if w.Code != http.StatusConflict { t.Fatalf("status = %d, want 409", w.Code) } var body struct { Code string `json:"code"` Features []string `json:"unsupported_features"` } if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatalf("decode body: %v", err) } if body.Code != "unsupported_config_features" || len(body.Features) != 1 || body.Features[0] != "future_x" { t.Fatalf("unexpected body %s", w.Body.String()) } }