package imagecat

import (
	"errors"
	"os"
	"testing"
)

func TestLoadEmbeddedReturnsVersion1(t *testing.T) {
	cat, err := LoadEmbedded()
	if err != nil {
		t.Fatalf("LoadEmbedded: %v", err)
	}
	if cat.Version != 1 {
		t.Fatalf("Version = %d, want 1", cat.Version)
	}
}

func TestParseCatalogAcceptsNilAndEmpty(t *testing.T) {
	for _, data := range [][]byte{nil, {}} {
		cat, err := ParseCatalog(data)
		if err != nil {
			t.Fatalf("ParseCatalog(%q): %v", data, err)
		}
		if cat.Version != 0 || len(cat.Entries) != 0 {
			t.Fatalf("ParseCatalog returned non-zero catalog: %+v", cat)
		}
	}
}

func TestParseCatalogRejectsMalformed(t *testing.T) {
	if _, err := ParseCatalog([]byte("not json")); err == nil {
		t.Fatal("want parse error for malformed catalog")
	}
}

func TestLookupHitAndMiss(t *testing.T) {
	cat := Catalog{
		Version: 1,
		Entries: []CatEntry{
			{Name: "debian-bookworm", TarballURL: "https://example.com/a.tar.zst", TarballSHA256: "deadbeef"},
		},
	}
	hit, err := cat.Lookup("debian-bookworm")
	if err != nil {
		t.Fatalf("Lookup hit: %v", err)
	}
	if hit.TarballURL != "https://example.com/a.tar.zst" {
		t.Fatalf("unexpected entry: %+v", hit)
	}
	if _, err := cat.Lookup("nope"); !errors.Is(err, os.ErrNotExist) {
		t.Fatalf("Lookup miss error = %v, want ErrNotExist", err)
	}
}

func TestValidateName(t *testing.T) {
	cases := []struct {
		name string
		ok   bool
	}{
		{"debian-bookworm", true},
		{"alpine-3.20", true},
		{"generic-6.12", true},
		{"a", true},
		{"", false},
		{"   ", false},
		{"-starts-with-hyphen", false},
		{"has spaces", false},
		{"has/slash", false},
	}
	for _, tc := range cases {
		err := ValidateName(tc.name)
		if tc.ok && err != nil {
			t.Errorf("ValidateName(%q): unexpected error %v", tc.name, err)
		}
		if !tc.ok && err == nil {
			t.Errorf("ValidateName(%q): expected error", tc.name)
		}
	}
}
