package storaged

import (
	"errors"
	"testing"
)

func TestRegistry_RegisterAndResolve(t *testing.T) {
	r := NewRegistry()
	b := &Bucket{name: "primary"}
	if err := r.Register(b); err != nil {
		t.Fatalf("Register: %v", err)
	}
	got, err := r.Resolve("primary")
	if err != nil {
		t.Fatalf("Resolve: %v", err)
	}
	if got != b {
		t.Errorf("Resolve returned different bucket")
	}
}

func TestRegistry_DuplicateRegisterRejected(t *testing.T) {
	r := NewRegistry()
	if err := r.Register(&Bucket{name: "primary"}); err != nil {
		t.Fatal(err)
	}
	if err := r.Register(&Bucket{name: "primary"}); err == nil {
		t.Fatal("expected error on duplicate register")
	}
}

func TestRegistry_UnknownReturnsErrBucketUnknown(t *testing.T) {
	r := NewRegistry()
	_, err := r.Resolve("nope")
	if !errors.Is(err, ErrBucketUnknown) {
		t.Fatalf("expected ErrBucketUnknown, got %v", err)
	}
}

func TestRegistry_Names(t *testing.T) {
	r := NewRegistry()
	_ = r.Register(&Bucket{name: "primary"})
	_ = r.Register(&Bucket{name: "archive"})
	names := r.Names()
	if len(names) != 2 {
		t.Fatalf("expected 2 names, got %d", len(names))
	}
}
