Files
notarius/internal/cli/native_load_test.go

174 lines
5.9 KiB
Go
Raw Normal View History

package cli
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
func nativeSelection(path string) ckconfig.Selection {
return ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true, Value: path}}
}
func TestNativeLoadUsesOptionalCompanionsAndStrictPeers(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
files map[string]string
invalid bool
}{
{"absent", nil, false},
{"recursive", map[string]string{"prompts/nested/resources.yaml": "profiles: [{id: partial, model: fixture}]\n"}, false},
{"conflicting", map[string]string{
"prompts.yml": "profiles: [{id: collision, model: first}]\n",
"prompts/nested/resources.yaml": "profiles: [{id: collision, model: second}]\n",
}, true},
{"malformed", map[string]string{"prompts.yml": "profiles: [\n"}, true},
} {
t.Run(test.name, func(t *testing.T) {
dir := t.TempDir()
main := filepath.Join(dir, "config.yml")
writeConfigKitFile(t, main, "version: 6\n")
for file, contents := range test.files {
writeConfigKitFile(t, filepath.Join(dir, file), contents)
}
loaders := &nativeLoaders{}
result, err := loaders.application(context.Background(), nativeSelection(main))
if test.invalid {
if err == nil {
t.Fatal("invalid companion group fell back")
}
return
}
if err != nil {
t.Fatal(err)
}
if len(result.Config.PromptKit.Collections["notarius"].Prompts) == 0 {
t.Fatal("embedded collection missing")
}
if test.name == "recursive" {
if len(result.Config.PromptKit.Profiles) != 1 || result.Config.PromptKit.Profiles[0].ID != "partial" {
t.Fatal("recursive companion not loaded")
}
}
for _, surface := range []string{"notarius-output", "notarius-debug", "notarius-cache"} {
if _, err := os.Stat(filepath.Join(dir, surface)); !os.IsNotExist(err) {
t.Fatalf("loader created run storage %q: %v", surface, err)
}
}
})
}
}
func TestNativeLoadRetainsImportsInheritanceAndSourceRelativeMessages(t *testing.T) {
t.Parallel()
dir := t.TempDir()
main := filepath.Join(dir, "config.yml")
writeConfigKitFile(t, main, "version: 6\n")
writeConfigKitFile(t, filepath.Join(dir, "prompts.yml"), `config:
imports:
- path: resources/custom.yml
required: true
`)
writeConfigKitFile(t, filepath.Join(dir, "resources", "message.md"), "Imported native message")
writeConfigKitFile(t, filepath.Join(dir, "resources", "custom.yml"), `collections:
notarius:
profiles:
- id: child
extends: {id: dnd-extraction}
timeout_seconds: 7
prompts:
- id: custom
default_profile: notarius/child
messages: [{role: user, content_file: message.md}]
output: {format: text, validation_mode: none}
`)
loaders := &nativeLoaders{}
selection := nativeSelection(main)
engine, err := loaders.engine(context.Background(), selection)
if err != nil {
t.Fatal(err)
}
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "notarius/custom", APIKey: "fixture"})
if err != nil {
t.Fatal(err)
}
if len(prepared.Messages) != 1 || prepared.Messages[0].Content != "Imported native message" || prepared.EffectiveModelParams.TimeoutSeconds != 7 {
t.Fatalf("native context lost: %#v", prepared)
}
writeConfigKitFile(t, filepath.Join(dir, "resources", "message.md"), "Changed message")
same, err := loaders.engine(context.Background(), selection)
if err != nil || same != engine {
t.Fatalf("invocation engine was replaced: %v", err)
}
again, err := same.Prepare(context.Background(), promptkit.RunRequest{PromptID: "notarius/custom", APIKey: "fixture"})
if err != nil || again.Messages[0].Content != "Imported native message" {
t.Fatalf("captured engine changed: %v", err)
}
}
func TestNativeLoadDistinguishesOverlayAndOverrideSelections(t *testing.T) {
t.Parallel()
dir := t.TempDir()
main := filepath.Join(dir, "config.yml")
writeConfigKitFile(t, main, "version: 6\n")
writeConfigKitFile(t, filepath.Join(dir, "overlays", "alternate.yml"), "concurrency: {workers: 3}\n")
loaders := &nativeLoaders{}
base := nativeSelection(main)
left, err := loaders.application(context.Background(), base)
if err != nil {
t.Fatal(err)
}
rightSelection := base
rightSelection.Overlay = commandkit.Supplied[string]{Set: true, Value: "alternate"}
right, err := loaders.application(context.Background(), rightSelection)
if err != nil {
t.Fatal(err)
}
if left == right || *right.Config.Concurrency.Workers != 3 {
t.Fatal("diff selections were collapsed")
}
rightSelection.Overrides = []configkit.Override{{Path: configkit.Keys("concurrency", "workers"), Value: 1, Flag: "--set"}}
overridden, err := loaders.application(context.Background(), rightSelection)
if err != nil {
t.Fatal(err)
}
if overridden == right || *overridden.Config.Concurrency.Workers != 1 {
t.Fatal("override selection was collapsed")
}
same, err := loaders.application(context.Background(), base)
if err != nil || same != left {
t.Fatal("complete selection was not reused")
}
}
func TestNativeSourceRejectsExplicitEmptyAndHonorsEnvironment(t *testing.T) {
t.Parallel()
main := filepath.Join(t.TempDir(), "config.yml")
writeConfigKitFile(t, main, "version: 6\n")
loaders := &nativeLoaders{lookupEnv: func(key string) (string, bool) { return main, key == "NOTARIUS_CONFIG" }}
if _, err := loaders.application(context.Background(), nativeSelection("")); err == nil || !strings.Contains(err.Error(), "--config") {
t.Fatalf("explicit empty config discovered another file: %v", err)
}
result, err := loaders.application(context.Background(), ckconfig.Selection{})
if err != nil {
t.Fatal(err)
}
found := false
for _, source := range result.Sources() {
if source.Layer == configkit.LayerMain && source.Path == main {
found = true
}
}
if !found {
t.Fatal("environment selection lost")
}
}