package config import ( "strings" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/diagnostics" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" ) func TestValidateSuccessForValidConfig(t *testing.T) { cfg := validConfig() if err := cfg.Validate(); err != nil { t.Fatalf("Validate: %v", err) } } func TestValidateAllowsExplicitScriptoriumProfileIDOnBinding(t *testing.T) { cfg := validConfig() lane := cfg.Pipelines["example"].Artifacts["events"] lane.Extract.LLMProfile = "scriptorium-profile" cfg.Pipelines["example"].Artifacts["events"] = lane if err := cfg.Validate(); err != nil { t.Fatalf("Validate() error = %v, want nil", err) } } func TestValidateRejectsWhitespaceOnlyExplicitLLMProfile(t *testing.T) { cfg := validConfig() lane := cfg.Pipelines["example"].Artifacts["events"] lane.Extract.LLMProfile = " " cfg.Pipelines["example"].Artifacts["events"] = lane err := cfg.Validate() if err == nil || !strings.Contains(err.Error(), "llm_profile") || !strings.Contains(err.Error(), "events") { t.Fatalf("expected llm_profile error with lane context, got %v", err) } } func TestValidateRejectsInvalidNumericFields(t *testing.T) { tests := []struct { name string mutate func(Config) Config want string }{ { name: "total concurrency", mutate: func(cfg Config) Config { cfg.Concurrency.TotalLLM = 0 return cfg }, want: "total LLM concurrency", }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.mutate(validConfig()).Validate() if err == nil || !strings.Contains(err.Error(), tc.want) { t.Fatalf("expected error containing %q, got %v", tc.want, err) } }) } } func TestValidateRejectsMutuallyExclusiveScriptoriumProfileSources(t *testing.T) { cfg := validConfig() cfg.Scriptorium.ProfileDir = "./profiles" cfg.Scriptorium.ProfileFile = "./profiles.yml" err := cfg.Validate() if err == nil || !strings.Contains(err.Error(), "mutually exclusive") { t.Fatalf("expected Scriptorium source conflict, got %v", err) } } func TestValidateRejectsInvalidDiagnosticsRetention(t *testing.T) { cfg := validConfig() cfg.Diagnostics.Retention = diagnostics.RetentionMode("sometimes") err := cfg.Validate() if err == nil || !strings.Contains(err.Error(), "retention") { t.Fatalf("expected retention error, got %v", err) } } func TestValidateRejectsInvalidReferenceMaps(t *testing.T) { tests := []struct { name string mutate func(Config) Config want []string }{ { name: "empty chunk slot", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.Chunk.References = map[string]string{" ": "./roster.yml"} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "chunk", "reference slot", "empty"}, }, { name: "empty chunk source", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.Chunk.References = map[string]string{"roster": " "} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "chunk", "roster", "source", "empty"}, }, { name: "empty extract slot", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Extract.References = map[string]string{" ": "./roster.yml"} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "extract", "reference slot", "empty"}, }, { name: "empty extract source", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Extract.References = map[string]string{"roster": " "} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "extract", "roster", "source", "empty"}, }, { name: "empty normalize slot", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Normalize.References = map[string]string{" ": "./roster.yml"} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "normalize", "reference slot", "empty"}, }, { name: "empty normalize source", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Normalize.References = map[string]string{"roster": " "} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "normalize", "roster", "source", "empty"}, }, { name: "empty pipeline slot", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.References = map[string]string{" ": "./roster.yml"} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "reference slot", "empty"}, }, { name: "empty pipeline source", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.References = map[string]string{"roster": " "} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "roster", "source", "empty"}, }, { name: "empty lane slot", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.References = map[string]string{" ": "./roster.yml"} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "reference slot", "empty"}, }, { name: "empty lane source", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.References = map[string]string{"roster": " "} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "roster", "source", "empty"}, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.mutate(validConfig()).Validate() if err == nil { t.Fatal("Validate() error = nil, want error") } for _, want := range tc.want { if !strings.Contains(err.Error(), want) { t.Fatalf("Validate() error = %q, want substring %q", err.Error(), want) } } }) } } func TestValidateRejectsReferencesOnUnsupportedBindings(t *testing.T) { tests := []struct { name string mutate func(Config) Config want []string }{ { name: "input", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.Input.References = map[string]string{"roster": "./roster.yml"} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "input", "references", "not supported"}, }, { name: "merge", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Merge.References = map[string]string{"roster": "./roster.yml"} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "merge", "references", "not supported"}, }, { name: "validator", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] lane := profile.Artifacts["events"] lane.Validators = []pipeline.ModuleBinding{{ Module: "fake/validator", References: map[string]string{"roster": "./roster.yml"}, }} profile.Artifacts["events"] = lane cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "events", "validator[0]", "references", "not supported"}, }, { name: "output", mutate: func(cfg Config) Config { profile := cfg.Pipelines["example"] profile.Output.References = map[string]string{"roster": "./roster.yml"} cfg.Pipelines["example"] = profile return cfg }, want: []string{"example", "output", "references", "not supported"}, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.mutate(validConfig()).Validate() if err == nil { t.Fatal("Validate() error = nil, want error") } for _, want := range tc.want { if !strings.Contains(err.Error(), want) { t.Fatalf("Validate() error = %q, want substring %q", err.Error(), want) } } }) } } func TestValidateRejectsEmptyIDs(t *testing.T) { tests := []struct { name string mutate func(Config) Config want string }{ { name: "pipeline", mutate: func(cfg Config) Config { cfg.Pipelines[" "] = pipeline.PipelineProfile{} return cfg }, want: "pipeline id", }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.mutate(validConfig()).Validate() if err == nil || !strings.Contains(err.Error(), tc.want) { t.Fatalf("expected error containing %q, got %v", tc.want, err) } }) } } func TestValidateRejectsIDsDuplicatedAfterTrimming(t *testing.T) { tests := []struct { name string mutate func(Config) Config want string }{ { name: "pipeline", mutate: func(cfg Config) Config { cfg.Pipelines[" example "] = cfg.Pipelines["example"] return cfg }, want: "duplicated", }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.mutate(validConfig()).Validate() if err == nil || !strings.Contains(err.Error(), tc.want) { t.Fatalf("expected error containing %q, got %v", tc.want, err) } }) } } func validConfig() Config { cfg := Default() cfg.Pipelines["example"] = pipeline.PipelineProfile{ Input: pipeline.Binding("fake/input"), Artifacts: map[string]pipeline.ArtifactLaneProfile{ "events": { Extract: pipeline.Binding("fake/extract"), Validators: []pipeline.ModuleBinding{pipeline.Binding("fake/validator")}, }, "notes": { Extract: pipeline.Binding("fake/extract"), }, }, } return cfg } func fakeCatalog(t *testing.T, overrides ...pipeline.ModuleSpec) pipeline.ModuleCatalog { t.Helper() specs := map[string]pipeline.ModuleSpec{ "fake/input": { Key: "fake/input", Stage: pipeline.StageInput, Provides: []string{"source"}, }, "generic": { Key: "generic", Stage: pipeline.StageChunk, Requires: []string{"source"}, Provides: []string{"chunks"}, }, "fake/extract": { Key: "fake/extract", Stage: pipeline.StageExtract, Requires: []string{"chunks"}, Provides: []string{"artifact"}, }, "appendorder": { Key: "appendorder", Stage: pipeline.StageMerge, Requires: []string{"artifact"}, Provides: []string{"merged"}, }, "noop": { Key: "noop", Stage: pipeline.StageNormalize, Requires: []string{"merged"}, Provides: []string{"normalized"}, }, "fake/validator": { Key: "fake/validator", Stage: pipeline.StageValidate, Requires: []string{"normalized"}, Provides: []string{"validated"}, }, "json": { Key: "json", Stage: pipeline.StageOutput, Requires: []string{"normalized"}, }, } for _, override := range overrides { specs[override.Key] = override } inputs := pipeline.NewInputAdapterRegistry() chunkers := pipeline.NewChunkerRegistry() extractors := pipeline.NewExtractorRegistry() mergers := pipeline.NewMergerRegistry() normalizers := pipeline.NewNormalizerRegistry() validators := pipeline.NewValidatorRegistry() outputs := pipeline.NewOutputEncoderRegistry() mustRegisterInput(t, inputs, specs["fake/input"]) mustRegisterChunker(t, chunkers, specs["generic"]) mustRegisterExtractor(t, extractors, specs["fake/extract"]) mustRegisterMerger(t, mergers, specs["appendorder"]) mustRegisterNormalizer(t, normalizers, specs["noop"]) mustRegisterValidator(t, validators, specs["fake/validator"]) mustRegisterOutput(t, outputs, specs["json"]) return pipeline.ModuleCatalog{ Inputs: inputs, Chunkers: chunkers, Extractors: extractors, Mergers: mergers, Normalizers: normalizers, Validators: validators, Outputs: outputs, } } func mustRegisterInput(t *testing.T, registry *pipeline.InputAdapterRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.InputAdapter, error) { return nil, nil }); err != nil { t.Fatalf("register input: %v", err) } } func mustRegisterChunker(t *testing.T, registry *pipeline.ChunkerRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.Chunker, error) { return nil, nil }); err != nil { t.Fatalf("register chunker: %v", err) } } func mustRegisterExtractor(t *testing.T, registry *pipeline.ExtractorRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.Extractor, error) { return nil, nil }); err != nil { t.Fatalf("register extractor: %v", err) } } func mustRegisterMerger(t *testing.T, registry *pipeline.MergerRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.Merger, error) { return nil, nil }); err != nil { t.Fatalf("register merger: %v", err) } } func mustRegisterNormalizer(t *testing.T, registry *pipeline.NormalizerRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.Normalizer, error) { return nil, nil }); err != nil { t.Fatalf("register normalizer: %v", err) } } func mustRegisterValidator(t *testing.T, registry *pipeline.ValidatorRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.Validator, error) { return nil, nil }); err != nil { t.Fatalf("register validator: %v", err) } } func mustRegisterOutput(t *testing.T, registry *pipeline.OutputEncoderRegistry, spec pipeline.ModuleSpec) { t.Helper() if err := registry.RegisterWithSpec(spec, func() (contracts.OutputEncoder, error) { return nil, nil }); err != nil { t.Fatalf("register output: %v", err) } }