package runtime

import (
	"testing"

	"github.com/google/uuid"
)

func TestCacheRuntimeRoundTrip(t *testing.T) {
	c := NewCache()
	gameID := uuid.New()
	rec := RuntimeRecord{GameID: gameID, Status: RuntimeStatusRunning}

	c.PutRuntime(rec)
	got, ok := c.GetRuntime(gameID)
	if !ok {
		t.Fatal("expected cache hit")
	}
	if got.Status != RuntimeStatusRunning {
		t.Fatalf("status = %s, want running", got.Status)
	}

	rec.Status = RuntimeStatusFinished
	c.PutRuntime(rec)
	if _, ok := c.GetRuntime(gameID); ok {
		t.Fatal("terminal status must evict")
	}
}

func TestCacheEngineVersionRoundTrip(t *testing.T) {
	c := NewCache()
	v := EngineVersion{Version: "0.1.0", ImageRef: "img", Enabled: true}
	c.PutEngineVersion(v)
	got, ok := c.GetEngineVersion("0.1.0")
	if !ok {
		t.Fatal("expected hit")
	}
	if got.ImageRef != "img" {
		t.Fatalf("image_ref = %s, want img", got.ImageRef)
	}
	if list := c.ListEngineVersions(); len(list) != 1 {
		t.Fatalf("list size = %d, want 1", len(list))
	}
}

func TestCacheActiveRuntimes(t *testing.T) {
	c := NewCache()
	c.PutRuntime(RuntimeRecord{GameID: uuid.New(), Status: RuntimeStatusRunning})
	c.PutRuntime(RuntimeRecord{GameID: uuid.New(), Status: RuntimeStatusStarting})
	c.PutRuntime(RuntimeRecord{GameID: uuid.New(), Status: RuntimeStatusFinished}) // evicted
	if got := c.ActiveRuntimes(); len(got) != 2 {
		t.Fatalf("active = %d, want 2", len(got))
	}
}
