package response

import (
	"bytes"
	"io"
	"net/http"
	"testing"

	"github.com/gofiber/fiber/v2"

	"wucher/internal/transport/http/jsonapi"
)

func TestWriteErrorsSanitizesDetail(t *testing.T) {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) error {
		return WriteErrors(c, http.StatusUnprocessableEntity, []jsonapi.ErrorObject{{
			Title:  "Validation error",
			Detail: "raw validation detail",
		}})
	})

	req, _ := http.NewRequest(http.MethodGet, "/", nil)
	resp, err := app.Test(req)
	if err != nil {
		t.Fatalf("app.Test: %v", err)
	}
	body, _ := io.ReadAll(resp.Body)
	if bytes.Contains(body, []byte("raw validation detail")) {
		t.Fatalf("expected sanitized detail, got %s", string(body))
	}
	if !bytes.Contains(body, []byte(`"detail":"validation failed"`)) {
		t.Fatalf("expected generic validation detail, got %s", string(body))
	}
}
