diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index f001038..a081bfd 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -92,6 +92,14 @@ message, a main setting, and an overlay override. Main/companion behavior with no selected main is covered. Neither assembly nor these tests contacts a provider. The former `WithSchemaFS` users will be removed in Stages 3 and 4. +**Complete.** The matched modules and combined application definition are in +place. Focused core and assembly tests pass, including main-relative glossary +imports, replacement and peer conflicts, prompt content files, overlay +selection, and absent-main discovery. The CLI and remaining runtime test +callers cannot yet compile against the new result and removed PromptKit APIs. +Their migration and integration checks are deferred to the following stages. +The superseded runtime companion assembly remains until its removal in Stage 3. + ## Stage 2: Bootstrap Secrets and Wire Shared CLI Acquisitions **Outcome:** Processing and native configuration commands use the same diff --git a/go.mod b/go.mod index 8fac2d7..157d94d 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,11 @@ go 1.27.0 require ( github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 - go.maximumdirect.net/ckconfig v0.9.0 - go.maximumdirect.net/ckprompt v0.10.0 - go.maximumdirect.net/commandkit v0.7.0 - go.maximumdirect.net/configkit v1.2.1 - go.maximumdirect.net/promptkit v0.23.2 + go.maximumdirect.net/ckconfig v0.11.0 + go.maximumdirect.net/ckprompt v0.12.0 + go.maximumdirect.net/commandkit v0.8.0 + go.maximumdirect.net/configkit v1.4.0 + go.maximumdirect.net/promptkit v0.25.0 go.maximumdirect.net/releasekit v0.10.0 go.yaml.in/yaml/v4 v4.0.0 ) diff --git a/go.sum b/go.sum index f43bc7a..851df7c 100644 --- a/go.sum +++ b/go.sum @@ -15,14 +15,24 @@ github.com/yuin/goldmark/v2 v2.1.16 h1:MXvCVo+Cb2SVJ5cwJUzXetDjI7HECRJCkcqylEQtc github.com/yuin/goldmark/v2 v2.1.16/go.mod h1:G6M4/qOFtfn01/o14BU1UR2Lo5N3S9Qo7xCuz/sHjGQ= go.maximumdirect.net/ckconfig v0.9.0 h1:s9Ikga4wt4K1NpLCX9bLhm5vbcm1Fgqy9yk2lmO4Sss= go.maximumdirect.net/ckconfig v0.9.0/go.mod h1:yGjFVr2UzjJjiwDdMwsKehuQfvvtv7rTEOIk5NQOY00= +go.maximumdirect.net/ckconfig v0.11.0 h1:oTPW5R4vW5TNs97rqgABC/dir723rbGBz0FUyZ9QiNk= +go.maximumdirect.net/ckconfig v0.11.0/go.mod h1:9phvXeLAgQSBKpJWqPyIQASKVJr0nRhiPMIQkfNhaRo= go.maximumdirect.net/ckprompt v0.10.0 h1:t2UyWSke+bWH9aTDrw5/bteD+TO0y5LD0NWw0y2KniQ= go.maximumdirect.net/ckprompt v0.10.0/go.mod h1:vx8lZnelASctQtyxY2yV4PuTZfLpE9EqDa+LPj3+SHM= +go.maximumdirect.net/ckprompt v0.12.0 h1:TPx4c0ohnG51F5eXk9SLHvkSKGNnWBhzkJAC5LdZCrE= +go.maximumdirect.net/ckprompt v0.12.0/go.mod h1:6DsEfIbKsFFzdAAV2TZD2LiwMvEG//s7zFOVe2wDvN0= go.maximumdirect.net/commandkit v0.7.0 h1:toLDGhHoVe6gBX5HWuzQ/Joz0cBqEhJXolW21m3axBk= go.maximumdirect.net/commandkit v0.7.0/go.mod h1:sVp3JXKpm2YrP4Ie8Q3nWmQgwBdPN8uO+wQU72erDns= +go.maximumdirect.net/commandkit v0.8.0 h1:EBDjr7SvfgvVDsA2S95hkOhgZe7oGZdDvURDJxHyX48= +go.maximumdirect.net/commandkit v0.8.0/go.mod h1:sVp3JXKpm2YrP4Ie8Q3nWmQgwBdPN8uO+wQU72erDns= go.maximumdirect.net/configkit v1.2.1 h1:f+/6WjNl6CULbMr6ZSVwi4aty9rmuDfJlvpeU7I+HZM= go.maximumdirect.net/configkit v1.2.1/go.mod h1:ewu6blSokUDCMovy8JBXAdXRikGmlHXAQpFghnPWRfs= +go.maximumdirect.net/configkit v1.4.0 h1:TfOupZbQ23/ZMs+xt5lmyukCF11ekqKep8KbmFQD63o= +go.maximumdirect.net/configkit v1.4.0/go.mod h1:ewu6blSokUDCMovy8JBXAdXRikGmlHXAQpFghnPWRfs= go.maximumdirect.net/promptkit v0.23.2 h1:nE+Qqqbw5GN4PHD2e5OEWUHXcYkESZm3ryZVBsKWwFQ= go.maximumdirect.net/promptkit v0.23.2/go.mod h1:vPFg76VXcffP0JBFMS2WADCHwV5FYvO2O4bvIoiDlwc= +go.maximumdirect.net/promptkit v0.25.0 h1:vJOfxauwASllOSDceR2oypJ4NEwIh2s6F8dDexEzdM8= +go.maximumdirect.net/promptkit v0.25.0/go.mod h1:o2axdkV11KTPDgk81i2vrJ66dtcedXRhau/Ko3YSZ4A= go.maximumdirect.net/releasekit v0.10.0 h1:3GOgaIigXTkvYaRkntWZASE//2NTR1+IhkoKmt64aJo= go.maximumdirect.net/releasekit v0.10.0/go.mod h1:cT5NZNEcf7SKDx4MPG40YNK9V4bNyIxcLLFuNlbGQ90= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= diff --git a/internal/appconfig/config.go b/internal/appconfig/config.go new file mode 100644 index 0000000..21bf23f --- /dev/null +++ b/internal/appconfig/config.go @@ -0,0 +1,89 @@ +// Package appconfig composes Audita and PromptKit configuration sources. +package appconfig + +import ( + "fmt" + + "gitea.maximumdirect.net/eric/audita/v4/internal/core/config" + "gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema" + "gitea.maximumdirect.net/eric/audita/v4/internal/prompts" + ck "go.maximumdirect.net/configkit" + pk "go.maximumdirect.net/promptkit" +) + +// Config combines Audita's root settings with native PromptKit settings. +type Config struct { + Application config.Config `yaml:",inline"` + PromptKit pk.Settings `yaml:"promptkit"` +} + +const glossaryImports = `config: + imports: + - path: glossary.yml + required: false + destination: [glossary_collections] + - dir: glossary + required: false + recursive: true + pattern: ["*.yml", "*.yaml"] + destination: [glossary_collections] +` + +const operatorImports = `config: + imports: + - path: prompts.yml + required: false + - dir: prompts + required: false + recursive: true + pattern: ["*.yml", "*.yaml"] +` + +// Definition combines application rules with PromptKit's native rules. +func Definition() ck.Definition[Config] { + domain := config.Definition() + definition := ck.Definition[Config]{ + Defaults: func() Config { return Config{Application: domain.Defaults()} }, + Version: domain.Version, + SequenceIdentities: domain.SequenceIdentities, + Sensitive: domain.Sensitive, + Resolve: func(value *Config, resolution *ck.Resolution) error { + return domain.Resolve(&value.Application, resolution) + }, + Validate: func(value *Config, validation *ck.Validation) { + domain.Validate(&value.Application, validation) + }, + } + return pk.Configure(definition, func(value *Config) *pk.Settings { return &value.PromptKit }) +} + +// Load composes built-in resources and optional companions with the selected input. +func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) { + embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot) + if err != nil { + return nil, fmt.Errorf("discover embedded prompts: %w", err) + } + for i := range embedded { + embedded[i].Destination = ck.Keys("promptkit") + } + schemas := ck.NewFilesystem("Audita embedded schemas", responseschema.Filesystem()) + embedded = append(embedded, ck.Input{ + Source: pk.SchemaRootSource("Audita embedded schemas", responseschema.AssetRoot), + Destination: ck.Keys("promptkit"), + Filesystem: &ck.FilesystemContext{FS: &schemas, Root: ".", BaseDir: "."}, + }) + input.Imports = true + return ck.LoadLayered(input, ck.LayeredOptions[Config]{ + Definition: Definition(), + Overlay: overlay, + Overrides: overrides, + Underlays: []ck.Underlay{ + pk.BundledOpenRouterUnderlay(), + {Inputs: embedded}, + {Inputs: []ck.Input{ + {Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()}, + {Source: ck.Bytes("Audita automatic prompt imports", []byte(operatorImports)), Destination: ck.Keys("promptkit"), Imports: true, MainRelative: ck.MainRelative()}, + }}, + }, + }) +} diff --git a/internal/appconfig/config_test.go b/internal/appconfig/config_test.go new file mode 100644 index 0000000..45463f7 --- /dev/null +++ b/internal/appconfig/config_test.go @@ -0,0 +1,77 @@ +package appconfig + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + ck "go.maximumdirect.net/configkit" + pk "go.maximumdirect.net/promptkit" +) + +func TestLoadCombinesApplicationAndPromptSettings(t *testing.T) { + dir := t.TempDir() + if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil { + t.Fatal(err) + } + for name, data := range map[string]string{ + "config.yml": "version: 3\nllm:\n transient_retries: 4\n", + "prompts.yml": "profiles:\n - id: local\n model: fixture\ncollections:\n example:\n prompts:\n - id: sample\n messages:\n - role: user\n content_file: message.txt\n output:\n format: text\n validation_mode: none\n", + "message.txt": "A source-local message.", + "overlays/selected.yml": "context:\n description: overlay\n", + } { + if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { + t.Fatal(err) + } + } + result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected") + if err != nil { + t.Fatal(err) + } + if result.Config.Application.Version != 3 || result.Config.Application.LLM.TransientRetries != 4 { + t.Fatalf("application settings: %+v", result.Config.Application) + } + if result.Config.Application.Context.Description != "overlay" { + t.Fatalf("overlay description = %q", result.Config.Application.Context.Description) + } + settings, ok := result.Value(ck.Keys("promptkit")) + if !ok { + t.Fatal("missing PromptKit settings") + } + if _, ok := settings.(pk.Settings); !ok { + t.Fatalf("PromptKit settings have type %T", settings) + } + engine, err := pk.NewFromConfig(result, pk.RuntimeConfig{}) + if err != nil { + t.Fatal(err) + } + prompt, err := engine.InspectPrompt(context.Background(), "example/sample") + if err != nil { + t.Fatalf("inspect source-local prompt: %v", err) + } + if len(prompt.Messages) != 1 || prompt.Messages[0].Content != "A source-local message." { + t.Fatalf("captured messages = %+v", prompt.Messages) + } +} + +func TestLoadWithoutMainDoesNotDiscoverCompanions(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "prompts.yml"), []byte("profiles: [\n"), 0600); err != nil { + t.Fatal(err) + } + t.Chdir(dir) + result, err := Load(ck.Input{Source: ck.NoSource()}, "") + if err != nil { + t.Fatal(err) + } + if result.Config.Application.Version != 3 { + t.Fatalf("version = %d", result.Config.Application.Version) + } + for _, source := range result.Sources() { + if strings.HasSuffix(source.Path, "prompts.yml") || strings.HasSuffix(source.Path, "glossary.yml") { + t.Fatalf("unexpected companion source: %+v", source) + } + } +} diff --git a/internal/core/config/glossary_test.go b/internal/appconfig/glossary_test.go similarity index 88% rename from internal/core/config/glossary_test.go rename to internal/appconfig/glossary_test.go index 910d307..5bb3484 100644 --- a/internal/core/config/glossary_test.go +++ b/internal/appconfig/glossary_test.go @@ -1,4 +1,4 @@ -package config +package appconfig import ( "os" @@ -52,21 +52,21 @@ party: if err != nil { t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics()) } - party := result.Config.GlossaryCollections["party"].Entries + party := result.Config.Application.GlossaryCollections["party"].Entries if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" { t.Fatalf("party=%+v", party) } if party[0].Extra["level"] == nil { t.Fatalf("extension missing: %+v", party[0]) } - if got := result.Config.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" { + if got := result.Config.Application.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" { t.Fatalf("lore=%+v", got) } discovered, err := Load(ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "") - if err != nil || len(discovered.Config.GlossaryCollections["party"].Entries) != 2 { + if err != nil || len(discovered.Config.Application.GlossaryCollections["party"].Entries) != 2 { t.Fatalf("discovered glossary: %+v %v", discovered, err) } - spells := result.Config.GlossaryCollections["spells"].Entries + spells := result.Config.Application.GlossaryCollections["spells"].Entries if len(spells) != 1 || spells[0].Plural == nil || *spells[0].Plural != "Bolts" || spells[0].Extra["extra"] == nil { t.Fatalf("spells=%+v", spells) } @@ -133,7 +133,7 @@ func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) { if err != nil { t.Fatal(err) } - if len(result.Config.GlossaryCollections) != 0 { - t.Fatalf("unanchored glossary loaded: %+v", result.Config.GlossaryCollections) + if len(result.Config.Application.GlossaryCollections) != 0 { + t.Fatalf("unanchored glossary loaded: %+v", result.Config.Application.GlossaryCollections) } } diff --git a/internal/appconfig/sources_test.go b/internal/appconfig/sources_test.go new file mode 100644 index 0000000..5b226e6 --- /dev/null +++ b/internal/appconfig/sources_test.go @@ -0,0 +1,113 @@ +package appconfig + +import ( + ck "go.maximumdirect.net/configkit" + "io/fs" + "os" + "path" + "path/filepath" + "testing" +) + +func TestMainContextImportsAndOverlay(t *testing.T) { + dir := t.TempDir() + if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil { + t.Fatal(err) + } + for name, contents := range map[string]string{ + "main.yml": "version: 3\nconfig:\n imports:\n - path: fragment.yml\noutput:\n schema: bare-segments\n", + "fragment.yml": "context:\n description: imported\n", + "overlays/selected.yml": "context:\n description: overlay\n", + } { + if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil { + t.Fatal(err) + } + } + result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"}) + if err != nil { + t.Fatal(err) + } + if info, ok := result.Metadata().Lookup(ck.Keys("context", "description")); !ok || info.Origin.Layer != ck.LayerCLI { + t.Fatalf("override origin = %+v", info) + } + if result.Config.Application.Context.Description != "override" { + t.Fatalf("description = %q", result.Config.Application.Context.Description) + } + found := false + for _, source := range result.Sources() { + if source.Layer == ck.LayerMain && source.ImportedFrom == nil { + context, ok := result.SourceContext(source.ID) + if !ok { + t.Fatal("main context missing") + } + data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "fragment.yml")) + if err != nil || len(data) == 0 { + t.Fatalf("main context cannot access companion: %v", err) + } + found = true + } + } + if !found { + t.Fatal("main source missing") + } + if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil { + t.Fatal("missing overlay accepted") + } + if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil { + t.Fatal("overlay without context accepted") + } +} + +func TestDiscoveryAndSelectedFailure(t *testing.T) { + first, second := t.TempDir(), t.TempDir() + search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second)) + load := func() (*ck.Result[Config], error) { + return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "") + } + result, err := load() + if err != nil { + t.Fatal(err) + } + for _, source := range result.Sources() { + if source.Layer == ck.LayerMain { + t.Fatalf("unexpected main source: %+v", source) + } + } + if err := os.WriteFile(filepath.Join(second, "config.yml"), []byte("context:\n description: selected\n"), 0644); err != nil { + t.Fatal(err) + } + result, err = load() + if err != nil { + t.Fatal(err) + } + if result.Config.Application.Context.Description != "selected" { + t.Fatalf("description = %q", result.Config.Application.Context.Description) + } + if err := os.WriteFile(filepath.Join(first, "config.yml"), []byte("version: 2\n"), 0644); err != nil { + t.Fatal(err) + } + if _, err := load(); err == nil { + t.Fatal("invalid selected file fell through") + } +} + +func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) { + root := t.TempDir() + dir := filepath.Join(root, "project") + if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil { + t.Fatal(err) + } + result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local") + if err != nil { + t.Fatal(err) + } + if result.Config.Application.Context.Description != "project" { + t.Fatalf("description = %q", result.Config.Application.Context.Description) + } +} diff --git a/internal/cli/app_wiring.go b/internal/cli/app_wiring.go index 83dd31f..ac43cb1 100644 --- a/internal/cli/app_wiring.go +++ b/internal/cli/app_wiring.go @@ -7,7 +7,7 @@ import ( "reflect" "strings" - "gitea.maximumdirect.net/eric/audita/v4/internal/core/config" + "gitea.maximumdirect.net/eric/audita/v4/internal/appconfig" "gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm" "gitea.maximumdirect.net/eric/audita/v4/internal/prompts" "go.maximumdirect.net/ckconfig" @@ -27,7 +27,7 @@ type engineEntry struct { engine *pk.Engine } -func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[config.Config], error) { +func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct{}, _ *invocationState) (*ck.Result[appconfig.Config], error) { if err := ctx.Err(); err != nil { return nil, err } @@ -40,7 +40,7 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, _ struct } else { source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories())) } - return config.Load(ck.Input{Source: source}, selection.Overlay.Value, selection.Overrides...) + return appconfig.Load(ck.Input{Source: source}, selection.Overlay.Value, selection.Overrides...) } func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input struct{}, state *invocationState) (*pk.Engine, error) { diff --git a/internal/core/config/config.go b/internal/core/config/config.go index dba057f..945c25d 100644 --- a/internal/core/config/config.go +++ b/internal/core/config/config.go @@ -107,37 +107,20 @@ func defaults() Config { } } -const glossaryImports = `config: - imports: - - path: glossary.yml - required: false - destination: [glossary_collections] - - dir: glossary - required: false - recursive: true - pattern: ["*.yml", "*.yaml"] - destination: [glossary_collections] -` - -// Load resolves the selected main source and preserves native source evidence. -func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) { - input.Imports = true - return ck.LoadLayered(input, ck.LayeredOptions[Config]{ - Overlay: overlay, Overrides: overrides, - Underlays: []ck.Underlay{{Inputs: []ck.Input{{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()}}}}, - Definition: ck.Definition[Config]{ - Defaults: defaults, - SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")}, - Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error { - if version.Present && (version.Null || version.Text != "3") { - v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md") - } - return nil - }}, - Resolve: resolveSettings, - Validate: validate, - }, - }) +// Definition describes the application settings independently of their sources. +func Definition() ck.Definition[Config] { + return ck.Definition[Config]{ + Defaults: defaults, + SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")}, + Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error { + if version.Present && (version.Null || version.Text != "3") { + v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md") + } + return nil + }}, + Resolve: resolveSettings, + Validate: validate, + } } func validate(c *Config, v *ck.Validation) { diff --git a/internal/core/config/config_test.go b/internal/core/config/config_test.go index fa0ffc5..881165e 100644 --- a/internal/core/config/config_test.go +++ b/internal/core/config/config_test.go @@ -1,9 +1,7 @@ package config import ( - "io/fs" "os" - "path" "path/filepath" "testing" @@ -40,109 +38,6 @@ func TestDefaultsAndVersion(t *testing.T) { } -func TestMainContextImportsAndOverlay(t *testing.T) { - dir := t.TempDir() - if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil { - t.Fatal(err) - } - for name, contents := range map[string]string{ - "main.yml": "version: 3\nconfig:\n imports:\n - path: fragment.yml\noutput:\n schema: bare-segments\n", - "fragment.yml": "context:\n description: imported\n", - "overlays/selected.yml": "context:\n description: overlay\n", - } { - if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil { - t.Fatal(err) - } - } - result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"}) - if err != nil { - t.Fatal(err) - } - if info, ok := result.Metadata().Lookup(ck.Keys("context", "description")); !ok || info.Origin.Layer != ck.LayerCLI { - t.Fatalf("override origin = %+v", info) - } - if result.Config.Context.Description != "override" { - t.Fatalf("description = %q", result.Config.Context.Description) - } - found := false - for _, source := range result.Sources() { - if source.Layer == ck.LayerMain && source.ImportedFrom == nil { - context, ok := result.SourceContext(source.ID) - if !ok { - t.Fatal("main context missing") - } - data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "fragment.yml")) - if err != nil || len(data) == 0 { - t.Fatalf("main context cannot access companion: %v", err) - } - found = true - } - } - if !found { - t.Fatal("main source missing") - } - if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil { - t.Fatal("missing overlay accepted") - } - if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil { - t.Fatal("overlay without context accepted") - } -} - -func TestDiscoveryAndSelectedFailure(t *testing.T) { - first, second := t.TempDir(), t.TempDir() - search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second)) - load := func() (*ck.Result[Config], error) { - return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "") - } - result, err := load() - if err != nil { - t.Fatal(err) - } - for _, source := range result.Sources() { - if source.Layer == ck.LayerMain { - t.Fatalf("unexpected main source: %+v", source) - } - } - if err := os.WriteFile(filepath.Join(second, "config.yml"), []byte("context:\n description: selected\n"), 0644); err != nil { - t.Fatal(err) - } - result, err = load() - if err != nil { - t.Fatal(err) - } - if result.Config.Context.Description != "selected" { - t.Fatalf("description = %q", result.Config.Context.Description) - } - if err := os.WriteFile(filepath.Join(first, "config.yml"), []byte("version: 2\n"), 0644); err != nil { - t.Fatal(err) - } - if _, err := load(); err == nil { - t.Fatal("invalid selected file fell through") - } -} - -func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) { - root := t.TempDir() - dir := filepath.Join(root, "project") - if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil { - t.Fatal(err) - } - result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local") - if err != nil { - t.Fatal(err) - } - if result.Config.Context.Description != "project" { - t.Fatalf("description = %q", result.Config.Context.Description) - } -} - func TestSchemaConstraintsAndExplicitValues(t *testing.T) { path := filepath.Join(t.TempDir(), "config.yml") for _, tc := range []struct { diff --git a/internal/core/config/loader_test.go b/internal/core/config/loader_test.go new file mode 100644 index 0000000..fbcc027 --- /dev/null +++ b/internal/core/config/loader_test.go @@ -0,0 +1,12 @@ +package config + +import ck "go.maximumdirect.net/configkit" + +func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) { + input.Imports = true + return ck.LoadLayered(input, ck.LayeredOptions[Config]{ + Definition: Definition(), + Overlay: overlay, + Overrides: overrides, + }) +}