package register

import (
	"strings"
	"testing"

	"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)

func TestRegisterAddsSeriatimFamily(t *testing.T) {
	registries := pipeline.Registries{Inputs: pipeline.NewInputAdapterRegistry()}
	if err := Register(registries, nil); err != nil {
		t.Fatalf("Register() error = %v, want nil", err)
	}
	assertContainsKeys(t, "inputs", registries.Inputs.RegisteredKeys(), []string{"seriatim"})
	if adapter, err := registries.Inputs.Build("seriatim"); err != nil || adapter.Key() != "seriatim" {
		t.Fatalf("build seriatim input = %v, %v; want seriatim implementation", adapter, err)
	}
}

func assertContainsKeys(t *testing.T, name string, got, want []string) {
	t.Helper()
	seen := make(map[string]struct{}, len(got))
	for _, key := range got {
		seen[key] = struct{}{}
	}
	for _, key := range want {
		if _, ok := seen[key]; !ok {
			t.Fatalf("%s keys = %#v, want required key %q", name, got, key)
		}
	}
}

func TestRegisterRejectsNilInputRegistry(t *testing.T) {
	err := Register(pipeline.Registries{}, nil)
	if err == nil || !strings.Contains(err.Error(), "input registry must not be nil") {
		t.Fatalf("Register() error = %v, want nil input registry error", err)
	}
}

func TestRegisterReportsDuplicateSeriatimRegistration(t *testing.T) {
	registries := pipeline.Registries{Inputs: pipeline.NewInputAdapterRegistry()}
	if err := Register(registries, nil); err != nil {
		t.Fatalf("first Register() error = %v, want nil", err)
	}
	err := Register(registries, nil)
	if err == nil || !strings.Contains(err.Error(), "register transcript input") || !strings.Contains(err.Error(), "already registered") {
		t.Fatalf("second Register() error = %v, want contextual duplicate error", err)
	}
}
