package mcp

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewToken_FormatAndUniqueness(t *testing.T) {
	t.Parallel()

	tok1, hash1, err := NewToken()
	require.NoError(t, err)
	tok2, hash2, err := NewToken()
	require.NoError(t, err)

	// 前缀正确
	assert.True(t, strings.HasPrefix(tok1, "mcpt_"))
	assert.True(t, strings.HasPrefix(tok2, "mcpt_"))

	// 两次生成不重复（碰撞概率 ≈ 2^-128）
	assert.NotEqual(t, tok1, tok2)
	assert.NotEqual(t, hash1, hash2)

	// hash 是 32 字节
	assert.Len(t, hash1, 32)
	assert.Len(t, hash2, 32)
}

func TestHashToken_Deterministic(t *testing.T) {
	t.Parallel()

	plain := "mcpt_someplaintext"
	h1 := HashToken(plain)
	h2 := HashToken(plain)

	assert.Equal(t, h1, h2)
	assert.Len(t, h1, 32)
}

func TestNewToken_HashMatchesPlaintext(t *testing.T) {
	t.Parallel()

	tok, hash, err := NewToken()
	require.NoError(t, err)

	expected := HashToken(tok)
	assert.Equal(t, expected, hash)
}
