package config import ( "bytes" "errors" "os" "path/filepath" "testing" configkit "go.maximumdirect.net/configkit" ) func TestDocumentWorkerCount(t *testing.T) { t.Parallel() for _, test := range []struct { name, body string want int invalid bool }{ {"omitted", "", 64, false}, {"serial", "concurrency: {workers: 1}", 1, false}, {"large", "concurrency: {workers: 1000000}", 1000000, false}, {"zero", "concurrency: {workers: 0}", 0, true}, {"negative", "concurrency: {workers: -1}", 0, true}, } { t.Run(test.name, func(t *testing.T) { loaded, err := loadConfigSource(configkit.Bytes("workers.yml", []byte("version: 6\n"+test.body+"\n")), LoadOptions{}) if test.invalid { assertDocumentDiagnosticAt(t, err, "/concurrency/workers") return } if err != nil { t.Fatal(err) } if loaded.Config.Concurrency.Workers != test.want { t.Fatalf("workers = %d", loaded.Config.Concurrency.Workers) } }) } } func TestDocumentStateDirectorySelection(t *testing.T) { t.Parallel() userRoot, err := os.UserCacheDir() if err != nil { t.Fatal(err) } for _, test := range []struct{ name, body, cache, debug string }{ {"omitted", "", filepath.Join(userRoot, "notarius"), "/tmp/notarius-debug"}, {"empty", "cache_directory: ''\ndebug: {directory: ''}", filepath.Join(userRoot, "notarius"), ""}, {"explicit", "cache_directory: /var/cache/fixture\ndebug: {directory: /tmp/fixture-debug}", "/var/cache/fixture", "/tmp/fixture-debug"}, } { t.Run(test.name, func(t *testing.T) { loaded, err := loadConfigSource(configkit.Bytes("directories.yml", []byte("version: 6\n"+test.body+"\n")), LoadOptions{}) if err != nil { t.Fatal(err) } if loaded.Config.CacheDirectory != test.cache || loaded.Config.Debug.Directory != test.debug { t.Fatalf("directories = %q, %q", loaded.Config.CacheDirectory, loaded.Config.Debug.Directory) } var effective bytes.Buffer if err := loaded.WriteEffective(&effective); err != nil { t.Fatal(err) } }) } } func TestDirectoryPathsFollowImportedSource(t *testing.T) { t.Parallel() dir := t.TempDir() fragmentDir := filepath.Join(dir, "fragments") if err := os.Mkdir(fragmentDir, 0755); err != nil { t.Fatal(err) } main := filepath.Join(dir, "config.yml") if err := os.WriteFile(main, []byte("version: 6\nconfig:\n imports:\n - path: fragments/state.yml\n required: true\n"), 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(fragmentDir, "state.yml"), []byte("cache_directory: selected\ndebug: {directory: evidence}\noutput: {directory: deliverables}\n"), 0600); err != nil { t.Fatal(err) } loaded, err := Load(main, LoadOptions{}) if err != nil { t.Fatal(err) } if loaded.Config.CacheDirectory != filepath.Join(fragmentDir, "selected") || loaded.Config.Debug.Directory != filepath.Join(fragmentDir, "evidence") || loaded.Config.Output.Directory != filepath.Join(fragmentDir, "deliverables") { t.Fatalf("source-relative directories lost: %#v", loaded.Config) } } func TestDocumentRejectsRetiredControls(t *testing.T) { t.Parallel() for _, body := range []string{ "concurrency: {total_llm: 4}", "concurrency: {stage_workers: {extract: 4}}", "cache: {chunk_plans: {mode: auto}}", "pipelines: {main: {input: {module: input}, retries: 1}}", "pipelines: {main: {input: {module: input}, structured_output_repair_attempts: 0}}", "pipelines: {main: {input: {module: input}, validation_policy: {validator_failure: fail_run}}}", "pipelines: {main: {input: {module: input}, chunk: {module: chunk, retries: 1}}}", "pipelines: {main: {input: {module: input}, chunk: {module: chunk, validators: [{module: validate, max_additional_turns: 1}]}}}", } { _, err := loadConfigSource(configkit.Bytes("retired.yml", []byte("version: 6\n"+body+"\n")), LoadOptions{}) if err == nil { t.Fatalf("accepted retired controls: %s", body) } } } func assertDocumentDiagnostic(t *testing.T, err error, code configkit.Code, path string) { t.Helper() var loadError *configkit.LoadError if !errors.As(err, &loadError) { t.Fatalf("error = %v, want ConfigKit load error", err) } for _, diagnostic := range loadError.Diagnostics() { if diagnostic.Code == code && diagnostic.Path.String() == path { return } } t.Fatalf("diagnostics = %#v, want %s at %s", loadError.Diagnostics(), code, path) }