package infra

import (
	"net/http"
	"testing"
)

func TestSessionTokenExtract(t *testing.T) {
	const cookieName = "test_session"

	t.Run("cookie present retorna token de cookie", func(t *testing.T) {
		r, _ := http.NewRequest("GET", "/", nil)
		r.AddCookie(&http.Cookie{Name: cookieName, Value: "tok_cookie"})
		got := SessionTokenExtract(r, cookieName)
		if got != "tok_cookie" {
			t.Errorf("got %q, want %q", got, "tok_cookie")
		}
	})

	t.Run("bearer header retorna token de header", func(t *testing.T) {
		r, _ := http.NewRequest("GET", "/", nil)
		r.Header.Set("Authorization", "Bearer tok_bearer")
		got := SessionTokenExtract(r, cookieName)
		if got != "tok_bearer" {
			t.Errorf("got %q, want %q", got, "tok_bearer")
		}
	})

	t.Run("cookie gana sobre bearer header", func(t *testing.T) {
		r, _ := http.NewRequest("GET", "/", nil)
		r.AddCookie(&http.Cookie{Name: cookieName, Value: "tok_cookie"})
		r.Header.Set("Authorization", "Bearer tok_bearer")
		got := SessionTokenExtract(r, cookieName)
		if got != "tok_cookie" {
			t.Errorf("got %q, want %q (cookie should win)", got, "tok_cookie")
		}
	})

	t.Run("sin token retorna cadena vacia", func(t *testing.T) {
		r, _ := http.NewRequest("GET", "/", nil)
		got := SessionTokenExtract(r, cookieName)
		if got != "" {
			t.Errorf("got %q, want empty string", got)
		}
	})
}
