Files
notarius/internal/core/config/native_settings_test.go

115 lines
3.3 KiB
Go

package config
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
func TestLoadedConfigHandsNativeSettingsAndOriginsToEngine(t *testing.T) {
t.Parallel()
dir := t.TempDir()
resources := filepath.Join(dir, "resources")
if err := os.Mkdir(resources, 0755); err != nil {
t.Fatal(err)
}
write := func(path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
t.Fatal(err)
}
}
main := filepath.Join(dir, "config.yml")
fragment := filepath.Join(resources, "prompts.yml")
write(main, "version: 6\nconfig:\n imports:\n - path: resources/prompts.yml\n required: true\n")
write(filepath.Join(resources, "message.txt"), "Imported message for {{.topic}}")
write(fragment, `output:
directory: delivered
promptkit:
backends:
- id: local
endpoint: https://example.invalid/v1
profiles:
- id: base
backend: local
model: fixture-model
extra_params:
token: fixture-secret
- id: child
extends: {id: base}
temperature: 0
prompts:
- id: imported
default_profile: child
messages:
- role: user
content_file: message.txt
output: {format: text, validation_mode: none}
`)
loaded, err := Load(main, LoadOptions{})
if err != nil {
t.Fatal(err)
}
result := loaded.Result()
value, ok := result.Value(configkit.Keys("promptkit"))
if _, typed := value.(promptkit.Settings); !ok || !typed {
t.Fatalf("native settings handoff = %T, present %t", value, ok)
}
if loaded.Config.Output.Directory != filepath.Join(resources, "delivered") {
t.Fatalf("application source origin lost: %s", loaded.Config.Output.Directory)
}
var effective bytes.Buffer
if err := loaded.WriteEffective(&effective); err != nil {
t.Fatal(err)
}
if bytes.Contains(effective.Bytes(), []byte("fixture-secret")) {
t.Fatal("native secret exposed in inspection")
}
// Construction must use the captured result, not reload either YAML source.
if err := os.Remove(main); err != nil {
t.Fatal(err)
}
if err := os.Remove(fragment); err != nil {
t.Fatal(err)
}
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{})
if err != nil {
t.Fatal(err)
}
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
PromptID: "imported", Vars: map[string]string{"topic": "testing"},
})
if err != nil {
t.Fatal(err)
}
if len(prepared.Messages) != 1 || prepared.Messages[0].Content != "Imported message for testing" {
t.Fatalf("source-relative native messages = %#v", prepared.Messages)
}
profile, err := engine.InspectProfile(context.Background(), "child")
if err != nil {
t.Fatal(err)
}
if profile.Model == nil || *profile.Model != "fixture-model" || profile.Temperature == nil || *profile.Temperature != 0 {
t.Fatalf("native identity/inheritance/presence lost: %#v", profile)
}
if loaded.Result() != result {
t.Fatal("load evidence was replaced")
}
}
func TestDocumentUsesNativeResourceValidation(t *testing.T) {
t.Parallel()
_, err := loadDocumentSource(configkit.Bytes("invalid.yml", []byte(`version: 6
promptkit:
backends:
- id: local
concurrency_limit: -1
`)), LoadOptions{})
assertDocumentDiagnosticAt(t, err, "/promptkit/backends/0/concurrency_limit")
}