package infra

import (
	"net/http/httptest"
	"strings"
	"testing"
)

func TestSessionCookieClear(t *testing.T) {
	t.Run("cookie clear setea MaxAge negativo", func(t *testing.T) {
		w := httptest.NewRecorder()
		SessionCookieClear(w, "my_session")

		cookies := w.Result().Cookies()
		if len(cookies) != 1 {
			t.Fatalf("expected 1 cookie, got %d", len(cookies))
		}
		c := cookies[0]
		if c.Name != "my_session" {
			t.Errorf("Name: got %q, want %q", c.Name, "my_session")
		}
		if c.MaxAge >= 0 {
			t.Errorf("MaxAge: got %d, want negative", c.MaxAge)
		}
	})

	t.Run("cookie clear valor es vacio", func(t *testing.T) {
		w := httptest.NewRecorder()
		SessionCookieClear(w, "sess")
		cookies := w.Result().Cookies()
		if len(cookies) == 0 {
			t.Fatal("no cookie set")
		}
		if cookies[0].Value != "" {
			t.Errorf("Value: got %q, want empty", cookies[0].Value)
		}
	})

	t.Run("header Set-Cookie contiene HttpOnly y SameSite=Lax", func(t *testing.T) {
		w := httptest.NewRecorder()
		SessionCookieClear(w, "sess")
		header := w.Header().Get("Set-Cookie")
		if !strings.Contains(header, "HttpOnly") {
			t.Errorf("Set-Cookie header missing HttpOnly: %s", header)
		}
		if !strings.Contains(header, "SameSite=Lax") {
			t.Errorf("Set-Cookie header missing SameSite=Lax: %s", header)
		}
	})
}
