Make PromptKit profile handling safer and more consistent
This commit is contained in:
@@ -24,10 +24,6 @@ func NewChunkerRegistry() *ChunkerRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) Register(key string, constructor ChunkerConstructor) error {
|
||||
return r.RegisterWithSpec(defaultModuleSpec(key, StageChunk), constructor)
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) RegisterWithSpec(spec ModuleSpec, constructor ChunkerConstructor) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("chunker constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
|
||||
@@ -29,6 +29,10 @@ type registryBehaviorCase[M any] struct {
|
||||
moduleKey func(M) string
|
||||
}
|
||||
|
||||
func testModuleSpec(key string, stage ModuleStage) ModuleSpec {
|
||||
return ModuleSpec{Key: key, Stage: stage, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func TestChunkerRegistryBehavior(t *testing.T) {
|
||||
runRegistryBehaviorTests(t, registryBehaviorCase[contracts.Chunker]{
|
||||
name: "ChunkerRegistry",
|
||||
@@ -39,7 +43,9 @@ func TestChunkerRegistryBehavior(t *testing.T) {
|
||||
return NewChunkerRegistry()
|
||||
},
|
||||
register: func(registry any, key string, constructor func() (contracts.Chunker, error)) error {
|
||||
return registry.(*ChunkerRegistry).Register(key, constructor)
|
||||
return registry.(*ChunkerRegistry).RegisterWithSpec(ModuleSpec{
|
||||
Key: key, Stage: StageChunk, ExecutionClass: contracts.ExecutionClassDeterministic,
|
||||
}, constructor)
|
||||
},
|
||||
registerWithSpec: func(registry any, spec ModuleSpec, constructor func() (contracts.Chunker, error)) error {
|
||||
return registry.(*ChunkerRegistry).RegisterWithSpec(spec, constructor)
|
||||
@@ -55,7 +61,9 @@ func TestChunkerRegistryBehavior(t *testing.T) {
|
||||
},
|
||||
nilRegister: func(key string, constructor func() (contracts.Chunker, error)) error {
|
||||
var registry *ChunkerRegistry
|
||||
return registry.Register(key, constructor)
|
||||
return registry.RegisterWithSpec(ModuleSpec{
|
||||
Key: key, Stage: StageChunk, ExecutionClass: contracts.ExecutionClassDeterministic,
|
||||
}, constructor)
|
||||
},
|
||||
nilBuild: func(key string) (contracts.Chunker, error) {
|
||||
var registry *ChunkerRegistry
|
||||
@@ -136,7 +144,7 @@ func runRegistryBehaviorTests[M any](t *testing.T, testCase registryBehaviorCase
|
||||
}
|
||||
})
|
||||
|
||||
t.Run(testCase.name+"/default spec from register", func(t *testing.T) {
|
||||
t.Run(testCase.name+"/minimal explicit spec registration", func(t *testing.T) {
|
||||
registry := testCase.newRegistry()
|
||||
if err := testCase.register(registry, " "+testCase.key+" ", testCase.constructor(testCase.key)); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
|
||||
@@ -108,7 +108,7 @@ func registerTestEvidenceOutput(t *testing.T, registries *Registries, policy Evi
|
||||
func registerTestEvidenceOutputWithProfileValidation(t *testing.T, registries *Registries, policy EvidenceContextPolicy, validateProfile OutputProfileOptionValidator) {
|
||||
t.Helper()
|
||||
registry := NewOutputEncoderRegistry()
|
||||
if err := registry.RegisterBuilderWithProfileValidation(defaultModuleSpec("output", StageOutput), func(options map[string]any) error {
|
||||
if err := registry.RegisterBuilderWithProfileValidation(testModuleSpec("output", StageOutput), func(options map[string]any) error {
|
||||
return RejectUnknownOptions(options, "known")
|
||||
}, validateProfile, func(BuildRequest) (contracts.OutputEncoder, error) {
|
||||
return testEvidenceOutput{policy: cloneEvidenceContextPolicy(policy)}, nil
|
||||
|
||||
@@ -24,10 +24,6 @@ func NewInputAdapterRegistry() *InputAdapterRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *InputAdapterRegistry) Register(key string, constructor InputAdapterConstructor) error {
|
||||
return r.RegisterWithSpec(defaultModuleSpec(key, StageInput), constructor)
|
||||
}
|
||||
|
||||
func (r *InputAdapterRegistry) RegisterWithSpec(spec ModuleSpec, constructor InputAdapterConstructor) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("input adapter constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
|
||||
@@ -11,10 +11,14 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func registerTestInput(registry *InputAdapterRegistry, key string, constructor InputAdapterConstructor) error {
|
||||
return registry.RegisterWithSpec(testModuleSpec(key, StageInput), constructor)
|
||||
}
|
||||
|
||||
func TestInputAdapterRegistryRegisterAndBuild(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
|
||||
if err := registry.Register("generic-input", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
if err := registerTestInput(registry, "generic-input", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
@@ -30,7 +34,7 @@ func TestInputAdapterRegistryRegisterAndBuild(t *testing.T) {
|
||||
func TestInputAdapterRegistryRegisterAndBuildTrimKeys(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
|
||||
if err := registry.Register(" generic-input ", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
if err := registerTestInput(registry, " generic-input ", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
@@ -82,10 +86,10 @@ func TestInputAdapterRegistryRegisterWithSpecStoresMetadata(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInputAdapterRegistryRegisterStoresDefaultSpec(t *testing.T) {
|
||||
func TestInputAdapterRegistryRegisterWithSpecStoresMinimalMetadata(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
|
||||
if err := registry.Register(" generic-input ", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
if err := registerTestInput(registry, " generic-input ", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
@@ -123,7 +127,7 @@ func TestInputAdapterRegistrySpecRejectsUnknownKey(t *testing.T) {
|
||||
func TestInputAdapterRegistryRegisterRejectsEmptyKey(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
|
||||
err := registry.Register(" \t", fakeInputAdapterConstructor("generic-input"))
|
||||
err := registerTestInput(registry, " \t", fakeInputAdapterConstructor("generic-input"))
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
@@ -135,11 +139,11 @@ func TestInputAdapterRegistryRegisterRejectsEmptyKey(t *testing.T) {
|
||||
|
||||
func TestInputAdapterRegistryRegisterRejectsDuplicateKey(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
if err := registry.Register("generic-input", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
if err := registerTestInput(registry, "generic-input", fakeInputAdapterConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
err := registry.Register(" generic-input ", fakeInputAdapterConstructor("generic-input"))
|
||||
err := registerTestInput(registry, " generic-input ", fakeInputAdapterConstructor("generic-input"))
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
@@ -152,7 +156,7 @@ func TestInputAdapterRegistryRegisterRejectsDuplicateKey(t *testing.T) {
|
||||
func TestInputAdapterRegistryRegisterRejectsNilConstructor(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
|
||||
err := registry.Register("generic-input", nil)
|
||||
err := registerTestInput(registry, "generic-input", nil)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
@@ -178,7 +182,7 @@ func TestInputAdapterRegistryBuildRejectsUnknownKey(t *testing.T) {
|
||||
func TestInputAdapterRegistryBuildWrapsConstructorError(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
constructorErr := errors.New("constructor failed")
|
||||
if err := registry.Register("generic-input", func() (contracts.InputAdapter, error) {
|
||||
if err := registerTestInput(registry, "generic-input", func() (contracts.InputAdapter, error) {
|
||||
return nil, constructorErr
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
@@ -199,7 +203,7 @@ func TestInputAdapterRegistryBuildWrapsConstructorError(t *testing.T) {
|
||||
|
||||
func TestInputAdapterRegistryBuildRejectsNilAdapter(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
if err := registry.Register("generic-input", func() (contracts.InputAdapter, error) {
|
||||
if err := registerTestInput(registry, "generic-input", func() (contracts.InputAdapter, error) {
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
@@ -217,7 +221,7 @@ func TestInputAdapterRegistryBuildRejectsNilAdapter(t *testing.T) {
|
||||
|
||||
func TestInputAdapterRegistryBuildRejectsAdapterKeyMismatch(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
if err := registry.Register("generic-input", fakeInputAdapterConstructor("other-input")); err != nil {
|
||||
if err := registerTestInput(registry, "generic-input", fakeInputAdapterConstructor("other-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
@@ -234,7 +238,7 @@ func TestInputAdapterRegistryBuildRejectsAdapterKeyMismatch(t *testing.T) {
|
||||
func TestInputAdapterRegistryRegisteredKeysReturnsSortedCopy(t *testing.T) {
|
||||
registry := NewInputAdapterRegistry()
|
||||
for _, key := range []string{"zeta", "alpha", "middle"} {
|
||||
if err := registry.Register(key, fakeInputAdapterConstructor(key)); err != nil {
|
||||
if err := registerTestInput(registry, key, fakeInputAdapterConstructor(key)); err != nil {
|
||||
t.Fatalf("Register(%q) error = %v, want nil", key, err)
|
||||
}
|
||||
}
|
||||
@@ -255,7 +259,7 @@ func TestInputAdapterRegistryRegisteredKeysReturnsSortedCopy(t *testing.T) {
|
||||
func TestInputAdapterRegistryNilRegistryBehavior(t *testing.T) {
|
||||
var registry *InputAdapterRegistry
|
||||
|
||||
if err := registry.Register("generic-input", fakeInputAdapterConstructor("generic-input")); err == nil {
|
||||
if err := registerTestInput(registry, "generic-input", fakeInputAdapterConstructor("generic-input")); err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if _, err := registry.Build("generic-input"); err == nil {
|
||||
|
||||
@@ -30,14 +30,6 @@ type ModuleSpec struct {
|
||||
ReferenceSlots []contracts.ReferenceSlot
|
||||
}
|
||||
|
||||
func defaultModuleSpec(key string, stage ModuleStage) ModuleSpec {
|
||||
return ModuleSpec{
|
||||
Key: key,
|
||||
Stage: stage,
|
||||
ExecutionClass: contracts.ExecutionClassDeterministic,
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeModuleSpec(spec ModuleSpec) ModuleSpec {
|
||||
executionClass := contracts.ExecutionClass(strings.TrimSpace(string(spec.ExecutionClass)))
|
||||
return ModuleSpec{
|
||||
|
||||
@@ -32,10 +32,6 @@ func NewOutputEncoderRegistry() *OutputEncoderRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *OutputEncoderRegistry) Register(key string, constructor OutputEncoderConstructor) error {
|
||||
return r.RegisterWithSpec(defaultModuleSpec(key, StageOutput), constructor)
|
||||
}
|
||||
|
||||
func (r *OutputEncoderRegistry) RegisterWithSpec(spec ModuleSpec, constructor OutputEncoderConstructor) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("output encoder constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
|
||||
@@ -17,7 +17,9 @@ func TestOutputEncoderRegistryBehavior(t *testing.T) {
|
||||
return NewOutputEncoderRegistry()
|
||||
},
|
||||
register: func(registry any, key string, constructor func() (contracts.OutputEncoder, error)) error {
|
||||
return registry.(*OutputEncoderRegistry).Register(key, constructor)
|
||||
return registry.(*OutputEncoderRegistry).RegisterWithSpec(ModuleSpec{
|
||||
Key: key, Stage: StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic,
|
||||
}, constructor)
|
||||
},
|
||||
registerWithSpec: func(registry any, spec ModuleSpec, constructor func() (contracts.OutputEncoder, error)) error {
|
||||
return registry.(*OutputEncoderRegistry).RegisterWithSpec(spec, constructor)
|
||||
@@ -33,7 +35,9 @@ func TestOutputEncoderRegistryBehavior(t *testing.T) {
|
||||
},
|
||||
nilRegister: func(key string, constructor func() (contracts.OutputEncoder, error)) error {
|
||||
var registry *OutputEncoderRegistry
|
||||
return registry.Register(key, constructor)
|
||||
return registry.RegisterWithSpec(ModuleSpec{
|
||||
Key: key, Stage: StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic,
|
||||
}, constructor)
|
||||
},
|
||||
nilBuild: func(key string) (contracts.OutputEncoder, error) {
|
||||
var registry *OutputEncoderRegistry
|
||||
@@ -60,7 +64,7 @@ func TestOutputEncoderRegistryBehavior(t *testing.T) {
|
||||
|
||||
func TestOutputProfileValidationReceivesOwnedOptionsAndLaneIDs(t *testing.T) {
|
||||
registry := NewOutputEncoderRegistry()
|
||||
if err := registry.RegisterBuilderWithProfileValidation(defaultModuleSpec("profile-output", StageOutput), func(options map[string]any) error {
|
||||
if err := registry.RegisterBuilderWithProfileValidation(testModuleSpec("profile-output", StageOutput), func(options map[string]any) error {
|
||||
options["nested"].(map[string]any)["value"] = "changed"
|
||||
return nil
|
||||
}, func(context OutputProfileOptionContext, options map[string]any) error {
|
||||
|
||||
@@ -480,19 +480,19 @@ func constructionRegistriesWithHooks(t *testing.T, built *[]string, failure *con
|
||||
if err := RegisterArtifactCodec(registries.ArtifactCodecs, notesCodec()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registries.Inputs.RegisterBuilderWithSpec(defaultModuleSpec("input", StageInput), strict, func(request BuildRequest) (contracts.InputAdapter, error) {
|
||||
if err := registries.Inputs.RegisterBuilderWithSpec(testModuleSpec("input", StageInput), strict, func(request BuildRequest) (contracts.InputAdapter, error) {
|
||||
record("input", &request)
|
||||
return input, nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registries.Chunkers.RegisterBuilderWithSpec(defaultModuleSpec("chunk", StageChunk), strict, func(request BuildRequest) (contracts.Chunker, error) {
|
||||
if err := registries.Chunkers.RegisterBuilderWithSpec(testModuleSpec("chunk", StageChunk), strict, func(request BuildRequest) (contracts.Chunker, error) {
|
||||
record("chunk", &request)
|
||||
return &typedTestChunker{key: "chunk"}, nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
extractSpec := defaultModuleSpec("extract", StageExtract)
|
||||
extractSpec := testModuleSpec("extract", StageExtract)
|
||||
extractSpec.ArtifactKind = "test/notes"
|
||||
if err := RegisterExtractorBuilder(registries.Extractors, extractSpec, strict, func(request BuildRequest) (contracts.Extractor[codecNotes], error) {
|
||||
record("extract", &request)
|
||||
@@ -503,7 +503,7 @@ func constructionRegistriesWithHooks(t *testing.T, built *[]string, failure *con
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mergeSpec := defaultModuleSpec("merge", StageMerge)
|
||||
mergeSpec := testModuleSpec("merge", StageMerge)
|
||||
mergeSpec.ArtifactKind = "test/notes"
|
||||
if err := RegisterMergerBuilder(registries.Mergers, mergeSpec, strict, func(request BuildRequest) (contracts.Merger[codecNotes], error) {
|
||||
record("merge", &request)
|
||||
@@ -511,7 +511,7 @@ func constructionRegistriesWithHooks(t *testing.T, built *[]string, failure *con
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
normalizeSpec := defaultModuleSpec("normalize", StageNormalize)
|
||||
normalizeSpec := testModuleSpec("normalize", StageNormalize)
|
||||
normalizeSpec.ArtifactKind = "test/notes"
|
||||
if err := RegisterNormalizerBuilder(registries.Normalizers, normalizeSpec, strict, func(request BuildRequest) (contracts.Normalizer[codecNotes], error) {
|
||||
record("normalize", &request)
|
||||
@@ -532,7 +532,7 @@ func constructionRegistriesWithHooks(t *testing.T, built *[]string, failure *con
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registries.Outputs.RegisterBuilderWithSpec(defaultModuleSpec("output", StageOutput), strict, func(request BuildRequest) (contracts.OutputEncoder, error) {
|
||||
if err := registries.Outputs.RegisterBuilderWithSpec(testModuleSpec("output", StageOutput), strict, func(request BuildRequest) (contracts.OutputEncoder, error) {
|
||||
record("output", &request)
|
||||
if failure.output != nil {
|
||||
return nil, failure.output
|
||||
|
||||
Reference in New Issue
Block a user