package infra

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

func TestSessionCookieSet(t *testing.T) {
	t.Run("cookie set con nombre y token correctos", func(t *testing.T) {
		w := httptest.NewRecorder()
		expires := time.Now().Add(24 * time.Hour).Unix()
		SessionCookieSet(w, "my_session", "tok_abc", expires)

		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.Value != "tok_abc" {
			t.Errorf("Value: got %q, want %q", c.Value, "tok_abc")
		}
		if c.Path != "/" {
			t.Errorf("Path: got %q, want %q", c.Path, "/")
		}
		if !c.HttpOnly {
			t.Errorf("expected HttpOnly=true")
		}
	})

	t.Run("header Set-Cookie contiene HttpOnly y SameSite=Lax", func(t *testing.T) {
		w := httptest.NewRecorder()
		SessionCookieSet(w, "s", "v", time.Now().Add(time.Hour).Unix())
		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)
		}
	})
}
