package kernelcat

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

func TestParseCatalogEmpty(t *testing.T) {
	t.Parallel()
	cat, err := ParseCatalog(nil)
	if err != nil {
		t.Fatalf("ParseCatalog(nil): %v", err)
	}
	if len(cat.Entries) != 0 {
		t.Fatalf("entries = %d, want 0", len(cat.Entries))
	}
}

func TestParseCatalogValid(t *testing.T) {
	t.Parallel()
	cat, err := ParseCatalog([]byte(`{"version":1,"entries":[{"name":"void-6.12","distro":"void","tarball_url":"https://example/v.tar.zst","tarball_sha256":"abc"}]}`))
	if err != nil {
		t.Fatalf("ParseCatalog: %v", err)
	}
	if cat.Version != 1 || len(cat.Entries) != 1 || cat.Entries[0].Name != "void-6.12" {
		t.Fatalf("catalog = %+v", cat)
	}
}

func TestCatalogLookup(t *testing.T) {
	t.Parallel()
	cat := Catalog{Entries: []CatEntry{{Name: "a"}, {Name: "b"}}}
	if entry, err := cat.Lookup("b"); err != nil || entry.Name != "b" {
		t.Fatalf("Lookup(b) = %+v, %v", entry, err)
	}
	if _, err := cat.Lookup("c"); !errors.Is(err, os.ErrNotExist) {
		t.Fatalf("Lookup(missing) err = %v, want ErrNotExist", err)
	}
}

func TestLoadEmbeddedReturnsValidCatalog(t *testing.T) {
	t.Parallel()
	cat, err := LoadEmbedded()
	if err != nil {
		t.Fatalf("LoadEmbedded: %v", err)
	}
	if cat.Version != 1 {
		t.Fatalf("embedded catalog.Version = %d, want 1", cat.Version)
	}
	// Embedded catalog starts empty; Phase 5 CI populates it.
}
