package handlers

import (
	"encoding/json"
	"testing"
)

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)
}
