package yuhticket

import (
	"testing"
	"time"
)

func TestSignVerifyRoundTrip(t *testing.T) {
	secret := "test-secret"
	now := time.Unix(1_700_000_000, 0).UTC()
	c := Claims{
		Iss: "yuheng", Aud: "aijianzhan", Phone: "13531041944",
		HostKey: "host-1", Exp: now.Add(90 * time.Second).Unix(), JTI: "jti-ok", Scope: "sync_bind",
	}
	tok, err := Sign(secret, c)
	if err != nil {
		t.Fatal(err)
	}
	got, err := Verify(tok, VerifyOpts{Secret: secret, Now: now})
	if err != nil {
		t.Fatal(err)
	}
	if got.Phone != c.Phone || got.HostKey != c.HostKey {
		t.Fatalf("claims mismatch: %+v", got)
	}
}

func TestVerifyBadSig(t *testing.T) {
	now := time.Unix(1_700_000_000, 0).UTC()
	tok, _ := Sign("a", Claims{
		Phone: "13800000001", HostKey: "h", Exp: now.Add(60 * time.Second).Unix(), JTI: "j1",
	})
	if _, err := Verify(tok, VerifyOpts{Secret: "b", Now: now}); err == nil {
		t.Fatal("expected bad sig")
	}
}

func TestJTIConsume(t *testing.T) {
	dir := t.TempDir()
	st, err := NewJTIStore(dir)
	if err != nil {
		t.Fatal(err)
	}
	exp := time.Now().Add(time.Minute).Unix()
	if err := st.Consume("x", exp); err != nil {
		t.Fatal(err)
	}
	if err := st.Consume("x", exp); err == nil {
		t.Fatal("expected replay")
	}
}
