package store

import (
	"strings"
	"testing"
)

func TestCapacityKindForTask(t *testing.T) {
	tests := map[string]string{
		"images.generations": "image",
		"images.edits":       "image",
		"images.vectorize":   "image",
		"videos.generations": "video",
		"videos.upscales":    "video",
		"chat.completions":   "",
	}
	for taskKind, expected := range tests {
		if actual := capacityKindForTask(taskKind); actual != expected {
			t.Fatalf("capacityKindForTask(%q)=%q, want %q", taskKind, actual, expected)
		}
	}
}

func TestCapacityProfileValidationAndStableHash(t *testing.T) {
	profiles := []CapacityProfileInput{
		{
			Kind: "image", Model: "gemini-image",
			StableThroughputPerSecond: 10, AdmittedThroughputPerSecond: 8,
			MaxRunning: 48, MaxQueued: 100, MaxPredictedWaitSeconds: 60,
			Metadata: map[string]any{"profile": "P24"},
		},
		{
			Kind: "video", Model: "seedance",
			StableThroughputPerSecond: 2, AdmittedThroughputPerSecond: 1.6,
			MaxRunning: 48, MaxQueued: 100, MaxPredictedWaitSeconds: 120,
		},
	}
	if err := validateCapacityProfileInputs(profiles); err != nil {
		t.Fatalf("valid profiles were rejected: %v", err)
	}
	firstHash := capacityProfileInputsHash(profiles)
	secondHash := capacityProfileInputsHash(profiles)
	if len(firstHash) != 64 || firstHash != secondHash {
		t.Fatalf("capacity profile hash is unstable: %q %q", firstHash, secondHash)
	}
	duplicate := append([]CapacityProfileInput(nil), profiles...)
	duplicate = append(duplicate, profiles[0])
	if err := validateCapacityProfileInputs(duplicate); err == nil || !strings.Contains(err.Error(), "duplicate") {
		t.Fatalf("duplicate profiles validation error=%v", err)
	}
}
