From 8136b4814f2172491d022c8eaed840e5ae0c37e6 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Tue, 17 Nov 2026 20:53:07 +0000 Subject: [PATCH] Verify configuration discovery and provenance --- docs/roadmap/implementation.md | 5 ++ internal/cli/configkit_load_test.go | 74 +++++++++++++++++++++ internal/cli/run_contract_test.go | 35 ++++++++++ internal/core/config/document_paths_test.go | 31 +++++++++ 4 files changed, 145 insertions(+) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 7b5266af..61c8217c 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -329,6 +329,11 @@ Prompt size: shared CLI loading boundary, not the full discovery test matrix. ## Stage 7 — Verify Discovery, Inspection, And Failure Semantics +**Complete.** Controlled discovery tests cover precedence, filename ordering, +terminal failures, canonical symlinks, required exhaustion, reports, and the +selected path recorded by an injected-client run. Focused tests pass; the broader +CLI suite still awaits the maintained-example migration. + Goal: protect the new operator contract with bounded offline coverage. Inspect CLI command_contract/configkit_load/configkit_commands tests and diff --git a/internal/cli/configkit_load_test.go b/internal/cli/configkit_load_test.go index fb17daf2..4a619493 100644 --- a/internal/cli/configkit_load_test.go +++ b/internal/cli/configkit_load_test.go @@ -4,6 +4,7 @@ import ( "bytes" "os" "path/filepath" + "runtime" "strconv" "strings" "testing" @@ -59,6 +60,79 @@ func TestLoadConfigKitUsesDiscoveryOverlayAndOverrides(t *testing.T) { } } +func TestNativeDiscoverySelectionAndTerminalFailures(t *testing.T) { + root := t.TempDir() + work := filepath.Join(root, "work") + user := filepath.Join(root, "user") + if err := os.MkdirAll(work, 0700); err != nil { + t.Fatal(err) + } + switch runtime.GOOS { + case "darwin": + t.Setenv("HOME", user) + user = filepath.Join(user, "Library", "Application Support") + case "windows": + t.Setenv("AppData", user) + default: + t.Setenv("XDG_CONFIG_HOME", user) + } + previous, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(work); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := os.Chdir(previous); err != nil { + t.Error(err) + } + }) + userFile := filepath.Join(user, "notarius", "config.yml") + writeConfigKitFile(t, userFile, "version: 5\npipelines: {user: {input: {module: input}}}\n") + writeConfigKitFile(t, filepath.Join(work, "config.yml"), "version: 5\npipelines: {generic: {input: {module: input}}}\n") + opts := commandContractOptions(t) + loaded, path, err := loadConfigKit("", "", nil, opts) + if err != nil || path != userFile || loaded.Config.Pipelines["user"].Input.Module != "input" { + t.Fatalf("user discovery = %q, %v", path, err) + } + workingYAML := filepath.Join(work, "notarius.yaml") + workingYML := filepath.Join(work, "notarius.yml") + writeConfigKitFile(t, workingYAML, "version: 5\npipelines: {yaml: {input: {module: input}}}\n") + writeConfigKitFile(t, workingYML, "version: 5\npipelines: {yml: {input: {module: input}}}\n") + loaded, path, err = loadConfigKit("", "", nil, opts) + if err != nil || path != workingYML || loaded.Config.Pipelines["yml"].Input.Module != "input" || loaded.Config.Output.Directory != filepath.Join(work, "notarius-output") { + t.Fatalf("working discovery = %q, %v", path, err) + } + for _, args := range [][]string{{"pipelines", "list"}, {"config", "validate"}, {"config", "effective"}, {"config", "explain", "/version"}} { + var stdout, stderr bytes.Buffer + if code := RunWithOptions(args, &stdout, &stderr, opts); code != 0 || stderr.Len() != 0 || stdout.Len() == 0 { + t.Fatalf("%v: code=%d stdout=%q stderr=%q", args, code, stdout.String(), stderr.String()) + } + } + missingEnv := opts + missingEnv.LookupEnv = func(name string) (string, bool) { return filepath.Join(root, "missing.yml"), name == "NOTARIUS_CONFIG" } + if _, _, err := loadConfigKit("", "", nil, missingEnv); err == nil { + t.Fatal("missing environment selection fell back") + } + if err := os.WriteFile(workingYML, []byte("version: [\n"), 0600); err != nil { + t.Fatal(err) + } + if _, _, err := loadConfigKit("", "", nil, opts); err == nil { + t.Fatal("invalid selected file fell back") + } + if _, _, err := loadConfigKit(filepath.Join(root, "missing.yml"), "", nil, opts); err == nil { + t.Fatal("missing explicit file fell back") + } + if err := os.Remove(workingYML); err != nil { + t.Fatal(err) + } + loaded, path, err = loadConfigKit("", "", nil, opts) + if err != nil || path != workingYAML || loaded.Config.Pipelines["yaml"].Input.Module != "input" { + t.Fatalf("alternate named file = %q, %v", path, err) + } +} + func TestWriteConfigLoadErrorUsesSafeDiagnostics(t *testing.T) { t.Parallel() path := filepath.Join(t.TempDir(), "invalid.yml") diff --git a/internal/cli/run_contract_test.go b/internal/cli/run_contract_test.go index 008f07d4..e31a7a13 100644 --- a/internal/cli/run_contract_test.go +++ b/internal/cli/run_contract_test.go @@ -271,6 +271,41 @@ func TestRunAppliesConfigurationOverlayAndRecordsSelection(t *testing.T) { } } +func TestRunRecordsDiscoveredConfigurationPath(t *testing.T) { + roots := newStateTestRoots(t) + work := t.TempDir() + selected := filepath.Join(work, "notarius.yml") + data, err := os.ReadFile(roots.config) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(selected, data, 0600); err != nil { + t.Fatal(err) + } + previous, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(work); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := os.Chdir(previous); err != nil { + t.Error(err) + } + }) + var stdout, stderr bytes.Buffer + code := RunWithOptions([]string{"run", "sample", "--input", roots.input, "--chunk_cache", "bypass", "--debug"}, &stdout, &stderr, newStateTestHarness().options()) + if code != 0 { + t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String()) + } + var invocation debugbundle.Invocation + readStateTestSummaryJSON(t, onlyChildDir(t, roots.debug), "invocation.json", &invocation) + if invocation.ConfigPath != selected { + t.Fatalf("recorded config path = %q, want %q", invocation.ConfigPath, selected) + } +} + func TestEffectiveConfigOverlayOmitsBaseSelection(t *testing.T) { for _, overlay := range []string{"", "default"} { if got := effectiveConfigOverlay(overlay); got != "" { diff --git a/internal/core/config/document_paths_test.go b/internal/core/config/document_paths_test.go index a22e5ceb..686706d3 100644 --- a/internal/core/config/document_paths_test.go +++ b/internal/core/config/document_paths_test.go @@ -122,6 +122,37 @@ func TestDocumentPathsUseInvocationDirectoryForOverrides(t *testing.T) { } } +func TestDiscoveredSymlinkUsesCanonicalMainDirectory(t *testing.T) { + root := t.TempDir() + realDir := filepath.Join(root, "real") + if err := os.Mkdir(realDir, 0700); err != nil { + t.Fatal(err) + } + main := filepath.Join(realDir, "main.yml") + if err := os.WriteFile(main, []byte("version: 5\n"), 0600); err != nil { + t.Fatal(err) + } + link := filepath.Join(root, "notarius.yml") + if err := os.Symlink(main, link); err != nil { + t.Skipf("symlink unavailable: %v", err) + } + loaded, err := LoadSource(configkit.SearchConfig("", configkit.SearchPaths(link), configkit.SearchOptions{Required: true}), LoadOptions{}) + if err != nil { + t.Fatal(err) + } + if loaded.MainSourcePath() != main || loaded.Config.Output.Directory != filepath.Join(realDir, "notarius-output") { + t.Fatalf("canonical source = %q, output = %q", loaded.MainSourcePath(), loaded.Config.Output.Directory) + } +} + +func TestRequiredSearchExhaustion(t *testing.T) { + missing := filepath.Join(t.TempDir(), "missing.yml") + _, err := LoadSource(configkit.SearchConfig("", configkit.SearchPaths(missing), configkit.SearchOptions{Required: true}), LoadOptions{}) + if err == nil { + t.Fatal("missing required search succeeded") + } +} + func TestDocumentPathsUseOverlayDeclarationDirectory(t *testing.T) { t.Parallel()