package tencentim

import (
	"testing"
	"time"
)

// TestGenerateUserSigUsesInternalUserID 验证 gateway 签发给客户端的腾讯云 IM 标识只使用不可变长 user_id。
func TestGenerateUserSigUsesInternalUserID(t *testing.T) {
	now := time.Unix(1_700_000_000, 0)

	result, err := GenerateUserSig(UserSigConfig{
		SDKAppID:  1400000000,
		SecretKey: "secret",
		TTL:       time.Hour,
	}, FormatUserID(10001), now)
	if err != nil {
		t.Fatalf("GenerateUserSig failed: %v", err)
	}

	if result.SDKAppID != 1400000000 {
		t.Fatalf("sdk_app_id mismatch: got %d", result.SDKAppID)
	}
	if result.UserID != "10001" {
		t.Fatalf("user_id mismatch: got %q", result.UserID)
	}
	if result.UserSig == "" {
		t.Fatalf("usersig should not be empty")
	}
	if result.ExpireAtMS != now.Add(time.Hour).UnixMilli() {
		t.Fatalf("expire_at_ms mismatch: got %d", result.ExpireAtMS)
	}
}

// TestGenerateUserSigFailsClosed 验证配置缺失时不会给客户端返回假票据。
func TestGenerateUserSigFailsClosed(t *testing.T) {
	_, err := GenerateUserSig(UserSigConfig{
		SDKAppID:  0,
		SecretKey: "",
		TTL:       time.Hour,
	}, "10001", time.Unix(1_700_000_000, 0))
	if err == nil {
		t.Fatalf("GenerateUserSig should reject incomplete config")
	}
}
