package ai

import (
	"strings"
	"testing"
)

func TestRedisModelsCacheKeysAreNamespacedAndCoLocated(t *testing.T) {
	cache := &RedisModelsCache{namespace: normalizeRedisNamespace("tenant:v2")}
	key := ModelsCacheKey{Provider: ProviderXAI, CredentialFingerprint: strings.Repeat("a", 64)}
	valueKey := cache.valueKey(key)
	lockKey := cache.lockKey(key)

	wantTag := "{" + ProviderXAI + ":" + strings.Repeat("a", 64) + "}"
	if !strings.HasPrefix(valueKey, "tenant:v2:ai:models:") || !strings.Contains(valueKey, wantTag) {
		t.Fatalf("unexpected value key %q", valueKey)
	}
	if !strings.Contains(lockKey, wantTag) {
		t.Fatalf("lock key is not colocated with value key: %q", lockKey)
	}
}

func TestRedisModelsCacheDefaultNamespace(t *testing.T) {
	if got := normalizeRedisNamespace(""); got != "haixun:dev:v1" {
		t.Fatalf("namespace=%q", got)
	}
}
