package rosa_test

import (
	"testing"

	"hakurei.app/internal/rosa"
)

func TestLoad(t *testing.T) {
	t.Parallel()

	for i := range rosa.PresetEnd {
		p := rosa.PArtifact(i)
		t.Run(rosa.GetMetadata(p).Name, func(t *testing.T) {
			t.Parallel()

			rosa.Std.Load(p)
		})
	}
}

func BenchmarkAll(b *testing.B) {
	arch, flags := rosa.Arch(), rosa.Flags()
	b.Cleanup(func() { rosa.DropCaches(arch, flags) })

	for b.Loop() {
		for i := range rosa.PresetEnd {
			rosa.Std.Load(rosa.PArtifact(i))
		}

		b.StopTimer()
		rosa.DropCaches("", 0)
		b.StartTimer()
	}
}

func TestResolveName(t *testing.T) {
	t.Parallel()

	for i := range rosa.PresetUnexportedStart {
		p := i
		name := rosa.GetMetadata(p).Name
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			if got, ok := rosa.ResolveName(name); !ok {
				t.Fatal("ResolveName: ok = false")
			} else if got != p {
				t.Fatalf("ResolveName: %d, want %d", got, p)
			}
		})
	}
}
func TestResolveNameUnexported(t *testing.T) {
	t.Parallel()

	for i := rosa.PresetUnexportedStart; i < rosa.PresetEnd; i++ {
		p := i
		name := rosa.GetMetadata(p).Name
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			if got, ok := rosa.ResolveName(name); ok {
				t.Fatalf("ResolveName: resolved unexported preset %d", got)
			}
		})
	}
}

func TestUnique(t *testing.T) {
	t.Parallel()

	names := make(map[string]struct{})
	for i := range rosa.PresetEnd {
		name := rosa.GetMetadata(rosa.PArtifact(i)).Name
		if _, ok := names[name]; ok {
			t.Fatalf("name %s is not unique", name)
		}
		names[name] = struct{}{}
	}
}
