package config import ( "strings" "testing" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" ) func TestValidateConcurrencyRules(t *testing.T) { for _, workers := range []int{0, -1, 1, 64, 1000000} { cfg := Default() cfg.Concurrency.Workers = workers err := cfg.Validate() if (err != nil) != (workers <= 0) { t.Fatalf("workers %d: %v", workers, err) } } } func TestValidateStateSurfaceRules(t *testing.T) { for _, test := range []struct { name string setup func(*Config) }{ {"output", func(c *Config) { c.Output.Directory = "" }}, {"cache", func(c *Config) { c.CacheDirectory = "\\x00" }}, {"debug", func(c *Config) { c.Debug.Directory = "\\x00" }}, } { t.Run(test.name, func(t *testing.T) { c := Default() test.setup(&c) if c.Validate() == nil { t.Fatal("accepted invalid path") } }) } } func TestValidateIdentifiersAfterTrimming(t *testing.T) { tests := []struct { name string setup func(*Config) want string }{ { name: "empty pipeline id", setup: func(cfg *Config) { cfg.Pipelines = map[string]pipeline.PipelineProfile{" ": {}} }, want: "pipeline id must not be empty", }, { name: "duplicate pipeline ids", setup: func(cfg *Config) { cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": {}, " main ": {}} }, want: "pipeline id \"main\" is duplicated after trimming", }, { name: "empty lane id", setup: func(cfg *Config) { profile := validationProfile() profile.Artifacts = map[string]pipeline.ArtifactLaneProfile{" ": {}} cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "artifact lane id must not be empty", }, { name: "duplicate lane ids", setup: func(cfg *Config) { profile := validationProfile() profile.Artifacts = map[string]pipeline.ArtifactLaneProfile{"spells": {}, " spells ": {}} cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "artifact lane id \"spells\" is duplicated after trimming", }, { name: "empty reference slot", setup: func(cfg *Config) { profile := validationProfile() profile.References = pipeline.ExternalReferenceMap(map[string]string{" ": "source.txt"}) cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "reference slot name must not be empty", }, { name: "duplicate reference slots", setup: func(cfg *Config) { profile := validationProfile() profile.References = pipeline.ExternalReferenceMap(map[string]string{"slot": "one.txt", " slot ": "two.txt"}) cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "reference slot \"slot\" is duplicated after trimming", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := Default() tt.setup(&cfg) assertValidationContains(t, cfg, tt.want) }) } } func TestValidateBindingControlsAndProfiles(t *testing.T) { tests := []struct { name string setup func(*Config) want string }{ { name: "negative turns", setup: func(cfg *Config) { profile := validationProfile() value := -1 profile.Input.MaxAdditionalTurns = &value cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "input: max_additional_turns must not be negative", }, { name: "whitespace-only input profile", setup: func(cfg *Config) { profile := validationProfile() profile.Input.LLMProfile = " " cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} }, want: "input llm_profile must not be empty when set", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := Default() tt.setup(&cfg) assertValidationContains(t, cfg, tt.want) }) } } func TestValidateReferencesAreUnsupportedOnInputAndOutput(t *testing.T) { tests := []struct { name string set func(*pipeline.PipelineProfile) want string }{ { name: "input references", set: func(profile *pipeline.PipelineProfile) { profile.Input.References = pipeline.ExternalReferenceMap(map[string]string{"slot": "source.txt"}) }, want: "input references are not supported", }, { name: "output references", set: func(profile *pipeline.PipelineProfile) { profile.Output.References = pipeline.ExternalReferenceMap(map[string]string{"slot": "source.txt"}) }, want: "output references are not supported", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { profile := validationProfile() tt.set(&profile) cfg := Default() cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} assertValidationContains(t, cfg, tt.want) }) } } func TestValidateValidatorBindingRules(t *testing.T) { tests := []struct { name string setup func(*pipeline.PipelineProfile) want string }{ { name: "empty validator module", setup: func(profile *pipeline.PipelineProfile) { profile.Chunk.Validators = pipeline.ValidatorOverride{ Set: true, Validators: []pipeline.ModuleBinding{{}}, } }, want: "chunk validators[0] module must not be empty", }, { name: "negative validator retries", setup: func(profile *pipeline.PipelineProfile) { profile.Chunk.Validators = pipeline.ValidatorOverride{ Set: true, Validators: []pipeline.ModuleBinding{{ Module: "validator", MaxAdditionalTurns: documentValue(-1), }}, } }, want: "chunk validators[0] producer controls are not supported for validators", }, { name: "validator references", setup: func(profile *pipeline.PipelineProfile) { profile.Chunk.Validators = pipeline.ValidatorOverride{ Set: true, Validators: []pipeline.ModuleBinding{{ Module: "validator", References: pipeline.ExternalReferenceMap(map[string]string{"slot": "source.txt"}), }}, } }, want: "chunk validators[0] references are not supported", }, { name: "nested validators", setup: func(profile *pipeline.PipelineProfile) { profile.Chunk.Validators = pipeline.ValidatorOverride{ Set: true, Validators: []pipeline.ModuleBinding{{ Module: "validator", Validators: pipeline.ValidatorOverride{Set: true}, }}, } }, want: "chunk validators[0] nested validators are not supported", }, { name: "input validator chain", setup: func(profile *pipeline.PipelineProfile) { profile.Input.Validators = pipeline.ValidatorOverride{Set: true} }, want: "input validators are not supported", }, { name: "output validator chain", setup: func(profile *pipeline.PipelineProfile) { profile.Output.Validators = pipeline.ValidatorOverride{Set: true} }, want: "output validators are not supported", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { profile := validationProfile() tt.setup(&profile) cfg := Default() cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} assertValidationContains(t, cfg, tt.want) }) } } func TestValidateLaneValidatorCompatibility(t *testing.T) { tests := []struct { name string lane func(*pipeline.ArtifactLaneProfile) want string }{ { name: "deprecated non-empty lane validators", lane: func(lane *pipeline.ArtifactLaneProfile) { lane.Validators = []pipeline.ModuleBinding{{Module: "old-validator"}} }, want: "validators are not supported at artifact lane level", }, { name: "stage validators omitted", lane: func(lane *pipeline.ArtifactLaneProfile) { }, }, { name: "stage validators explicitly empty", lane: func(lane *pipeline.ArtifactLaneProfile) { lane.Extract.Validators = pipeline.ValidatorOverride{Set: true} }, }, { name: "stage validators configured", lane: func(lane *pipeline.ArtifactLaneProfile) { lane.Extract.Validators = pipeline.ValidatorOverride{ Set: true, Validators: []pipeline.ModuleBinding{{Module: "validator"}}, } }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { profile := validationProfile() lane := profile.Artifacts["lane"] tt.lane(&lane) profile.Artifacts["lane"] = lane cfg := Default() cfg.Pipelines = map[string]pipeline.PipelineProfile{"main": profile} err := cfg.Validate() if tt.want == "" { if err != nil { t.Fatalf("Validate() error = %v", err) } return } if err == nil || !strings.Contains(err.Error(), tt.want) { t.Fatalf("Validate() error = %v, want context %q", err, tt.want) } }) } } func validationProfile() pipeline.PipelineProfile { return pipeline.PipelineProfile{ ID: "main", Input: pipeline.Binding("input"), Artifacts: map[string]pipeline.ArtifactLaneProfile{ "lane": {}, }, } } func assertValidationContains(t *testing.T, cfg Config, want string) { t.Helper() err := cfg.Validate() if err == nil || !strings.Contains(err.Error(), want) { t.Fatalf("Validate() error = %v, want context %q", err, want) } }