package auth

import (
	"testing"
	"time"
)

func TestTokenMakerCreateAndParse(t *testing.T) {
	maker := NewTokenMaker("test-secret", 30, 7)

	token, jti, err := maker.Create("123", "admin", "access")
	if err != nil {
		t.Fatalf("create token: %v", err)
	}
	if jti == "" {
		t.Fatal("expected jti")
	}

	claims, err := maker.Parse(token)
	if err != nil {
		t.Fatalf("parse token: %v", err)
	}
	if claims.Sub != "123" || claims.Role != "admin" || claims.Type != "access" || claims.JTI == "" {
		t.Fatalf("unexpected claims: %#v", claims)
	}
}

func TestTokenMakerCreateImpersonation(t *testing.T) {
	maker := NewTokenMaker("test-secret", 30, 7)
	token, jti, expiresAt, err := maker.CreateImpersonation("123", "user", 9, 10*time.Minute)
	if err != nil {
		t.Fatalf("create impersonation token: %v", err)
	}
	claims, err := maker.Parse(token)
	if err != nil {
		t.Fatalf("parse impersonation token: %v", err)
	}
	if jti == "" || claims.JTI != jti || claims.Type != "access" || claims.ImpersonatorID == nil || *claims.ImpersonatorID != 9 {
		t.Fatalf("unexpected impersonation claims: %#v", claims)
	}
	remaining := time.Until(expiresAt)
	if remaining < 9*time.Minute || remaining > 11*time.Minute {
		t.Fatalf("unexpected impersonation ttl: %s", remaining)
	}
}
