package allowlist

import (
	"sync"
	"testing"
)

func TestLockList(t *testing.T) {
	lockList := &LockList{
		OrgIds:         []uint64{1, 2, 3},
		SuperUsers:     []string{"test@pingcap.com"},
		LockAll:        false,
		lockCache:      make(map[uint64]bool),
		superUserCache: make(map[string]bool),
		lock:           &sync.Mutex{},
	}

	for _, i := range []uint64{11, 22, 33} {
		if lockList.IsLockOrgId(i) {
			t.Errorf("lockList.IsLockOrgID(%d) = %t; want false", i, lockList.IsLockOrgId(i))
		}
	}

	for _, i := range []uint64{1, 2, 3} {
		if !lockList.IsLockOrgId(i) {
			t.Errorf("lockList.IsLockOrgID(%d) = %t; want true", i, lockList.IsLockOrgId(i))
		}
	}

	// super user
	if !lockList.IsSuperUser("test@pingcap.com") {
		t.Errorf(
			"lockList.Dedicated.IsSuperUser(`test@pingcap.com`) = %t; want true",
			lockList.IsSuperUser("test@pingcap.com"),
		)
	}
}

func TestLockAll(t *testing.T) {
	lockList := &LockList{
		OrgIds:         []uint64{1, 2, 3},
		SuperUsers:     []string{"test@pingcap.com"},
		LockAll:        true,
		lockCache:      make(map[uint64]bool),
		superUserCache: make(map[string]bool),
		lock:           &sync.Mutex{},
	}

	for _, i := range []uint64{222, 777, 999, 999, 1, 2, 3, 45, 6} {
		if !lockList.IsLockOrgId(i) {
			t.Errorf("lockList.IsLockOrgID(%d) = %t; want true", i, lockList.IsLockOrgId(i))
		}
	}
}
