package commerce

import "testing"

func TestStableDownstreamTokenRequiresSecret(t *testing.T) {
	instance := Instance{ID: 1, OrderID: 2, ProductID: 3}

	t.Setenv("JWT_SECRET_KEY", "secret-a")
	tokenA := stableDownstreamToken(instance)

	t.Setenv("JWT_SECRET_KEY", "secret-b")
	tokenB := stableDownstreamToken(instance)

	if tokenA == tokenB {
		t.Fatalf("expected token to depend on JWT_SECRET_KEY, got same value for different secrets: %s", tokenA)
	}
	if len(tokenA) != 32 {
		t.Fatalf("expected 32-char token, got %d chars: %s", len(tokenA), tokenA)
	}
}

func TestStableDownstreamTokenIsDeterministic(t *testing.T) {
	t.Setenv("JWT_SECRET_KEY", "matching-secret")
	instance := Instance{ID: 10, OrderID: 20, ProductID: 30}

	first := stableDownstreamToken(instance)
	second := stableDownstreamToken(instance)
	if first != second {
		t.Fatalf("expected deterministic token for same inputs, got %s vs %s", first, second)
	}
}
