230 lines
6.0 KiB
Go
230 lines
6.0 KiB
Go
package inputregistry
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func TestRegisterAndBuild(t *testing.T) {
|
|
registry := New()
|
|
|
|
if err := registry.Register("generic-input", fakeConstructor("generic-input")); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
adapter, err := registry.Build("generic-input")
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v, want nil", err)
|
|
}
|
|
if adapter.Key() != "generic-input" {
|
|
t.Fatalf("adapter.Key() = %q, want generic-input", adapter.Key())
|
|
}
|
|
}
|
|
|
|
func TestRegisterAndBuildTrimKeys(t *testing.T) {
|
|
registry := New()
|
|
|
|
if err := registry.Register(" generic-input ", fakeConstructor("generic-input")); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
adapter, err := registry.Build("\tgeneric-input\n")
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v, want nil", err)
|
|
}
|
|
if adapter.Key() != "generic-input" {
|
|
t.Fatalf("adapter.Key() = %q, want generic-input", adapter.Key())
|
|
}
|
|
}
|
|
|
|
func TestRegisterRejectsEmptyKey(t *testing.T) {
|
|
registry := New()
|
|
|
|
err := registry.Register(" \t", fakeConstructor("generic-input"))
|
|
|
|
if err == nil {
|
|
t.Fatal("Register() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "key must not be empty") {
|
|
t.Fatalf("Register() error = %q, want empty key error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestRegisterRejectsDuplicateKey(t *testing.T) {
|
|
registry := New()
|
|
if err := registry.Register("generic-input", fakeConstructor("generic-input")); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
err := registry.Register(" generic-input ", fakeConstructor("generic-input"))
|
|
|
|
if err == nil {
|
|
t.Fatal("Register() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "already registered") {
|
|
t.Fatalf("Register() error = %q, want duplicate key error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestRegisterRejectsNilConstructor(t *testing.T) {
|
|
registry := New()
|
|
|
|
err := registry.Register("generic-input", nil)
|
|
|
|
if err == nil {
|
|
t.Fatal("Register() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "constructor") {
|
|
t.Fatalf("Register() error = %q, want constructor error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsUnknownKey(t *testing.T) {
|
|
registry := New()
|
|
|
|
_, err := registry.Build("missing-input")
|
|
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "not registered") {
|
|
t.Fatalf("Build() error = %q, want unknown key error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBuildWrapsConstructorError(t *testing.T) {
|
|
registry := New()
|
|
constructorErr := errors.New("constructor failed")
|
|
if err := registry.Register("generic-input", func() (contracts.InputAdapter, error) {
|
|
return nil, constructorErr
|
|
}); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
_, err := registry.Build("generic-input")
|
|
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if !errors.Is(err, constructorErr) {
|
|
t.Fatalf("Build() error = %v, want wrapped constructor error", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "generic-input") {
|
|
t.Fatalf("Build() error = %q, want key context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsNilAdapter(t *testing.T) {
|
|
registry := New()
|
|
if err := registry.Register("generic-input", func() (contracts.InputAdapter, error) {
|
|
return nil, nil
|
|
}); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
_, err := registry.Build("generic-input")
|
|
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "returned nil") {
|
|
t.Fatalf("Build() error = %q, want nil adapter error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsAdapterKeyMismatch(t *testing.T) {
|
|
registry := New()
|
|
if err := registry.Register("generic-input", fakeConstructor("other-input")); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
_, err := registry.Build("generic-input")
|
|
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "returned key") {
|
|
t.Fatalf("Build() error = %q, want key mismatch error", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestRegisteredKeysReturnsSortedCopy(t *testing.T) {
|
|
registry := New()
|
|
for _, key := range []string{"zeta", "alpha", "middle"} {
|
|
if err := registry.Register(key, fakeConstructor(key)); err != nil {
|
|
t.Fatalf("Register(%q) error = %v, want nil", key, err)
|
|
}
|
|
}
|
|
|
|
keys := registry.RegisteredKeys()
|
|
|
|
want := []string{"alpha", "middle", "zeta"}
|
|
if !reflect.DeepEqual(keys, want) {
|
|
t.Fatalf("RegisteredKeys() = %#v, want %#v", keys, want)
|
|
}
|
|
|
|
keys[0] = "changed"
|
|
if got := registry.RegisteredKeys(); !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("RegisteredKeys() after caller mutation = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestNilRegistryBehavior(t *testing.T) {
|
|
var registry *Registry
|
|
|
|
if err := registry.Register("generic-input", fakeConstructor("generic-input")); err == nil {
|
|
t.Fatal("Register() error = nil, want error")
|
|
}
|
|
if _, err := registry.Build("generic-input"); err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if keys := registry.RegisteredKeys(); keys != nil {
|
|
t.Fatalf("RegisteredKeys() = %#v, want nil", keys)
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsEmptyKey(t *testing.T) {
|
|
registry := New()
|
|
|
|
_, err := registry.Build(" \n")
|
|
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "key must not be empty") {
|
|
t.Fatalf("Build() error = %q, want empty key error", err.Error())
|
|
}
|
|
}
|
|
|
|
type fakeAdapter struct {
|
|
key string
|
|
}
|
|
|
|
func fakeConstructor(key string) Constructor {
|
|
return func() (contracts.InputAdapter, error) {
|
|
return fakeAdapter{key: key}, nil
|
|
}
|
|
}
|
|
|
|
func (adapter fakeAdapter) Key() string {
|
|
return adapter.key
|
|
}
|
|
|
|
func (adapter fakeAdapter) Parse(ctx context.Context, req contracts.ParseRequest) (*source.SourceDocument, error) {
|
|
return &source.SourceDocument{
|
|
ID: req.SourceID,
|
|
Kind: "document",
|
|
Format: "text/plain",
|
|
Digest: "sha256:abc123",
|
|
Units: []source.SourceUnit{
|
|
{ID: "u1", Kind: "unit", Text: "Source unit."},
|
|
},
|
|
}, nil
|
|
}
|