Correct engine construction edge cases

This commit is contained in:
2026-08-11 21:54:47 +00:00
parent 57f2ce1ce4
commit 58ac3ce298
7 changed files with 161 additions and 22 deletions

View File

@@ -2374,6 +2374,116 @@ func TestRunStructuredOutputWorksWithSchemaFile(t *testing.T) {
}
}
func TestSingleFileOptionsPreserveWhitespaceInPaths(t *testing.T) {
profile := promptkit.Profile{
ID: "exact-path-profile",
Endpoint: "http://example.test/v1",
Model: "exact-path-model",
}
promptDocument := []byte(`
id: exact-path-prompt
version: "1.0.0"
default_profile: exact-path-profile
messages:
- role: user
content: exact path
output:
format: text
validation_mode: none
`)
profileDocument := []byte(`
id: exact-path-profile
endpoint: http://example.test/v1
model: exact-path-model
`)
tests := []struct {
name string
baseName string
content []byte
engine func(string) (*promptkit.Engine, error)
}{
{
name: "prompt",
baseName: "prompt.yaml",
content: promptDocument,
engine: func(path string) (*promptkit.Engine, error) {
return promptkit.NewEngine(promptkit.Config{},
promptkit.WithPromptFile(path),
promptkit.WithProfiles(profile),
)
},
},
{
name: "profile",
baseName: "profile.yaml",
content: profileDocument,
engine: func(path string) (*promptkit.Engine, error) {
return promptkit.NewEngine(promptkit.Config{},
promptkit.WithPromptFS(contractPromptFS("exact-path-prompt", "exact-path-profile", "exact path"), "."),
promptkit.WithProfileFile(path),
)
},
},
{
name: "schema",
baseName: "schema.json",
content: []byte(publicSchemaJSON()),
engine: func(path string) (*promptkit.Engine, error) {
promptSource := fstest.MapFS{
"prompt.yaml": &fstest.MapFile{Data: []byte(fmt.Sprintf(`
id: exact-path-prompt
version: "1.0.0"
default_profile: exact-path-profile
messages:
- role: user
content: exact path
output:
format: json
validation_mode: json_schema
schema_path: %q
`, filepath.Base(path)))},
}
return promptkit.NewEngine(promptkit.Config{},
promptkit.WithPromptFS(promptSource, "."),
promptkit.WithProfiles(profile),
promptkit.WithSchemaFile(path),
)
},
},
}
positions := []struct {
name string
prefix string
suffix string
}{
{name: "leading whitespace", prefix: " "},
{name: "trailing whitespace", suffix: " "},
}
for _, tc := range tests {
for _, position := range positions {
t.Run(tc.name+"/"+position.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), position.prefix+tc.baseName+position.suffix)
if err := os.WriteFile(path, tc.content, 0o644); err != nil {
t.Fatalf("write source file: %v", err)
}
engine, err := tc.engine(path)
if err != nil {
t.Fatalf("construct engine from exact path %q: %v", path, err)
}
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "exact-path-prompt"})
if err != nil {
t.Fatalf("prepare from exact path %q: %v", path, err)
}
if prepared.EffectiveModelParams.Model != "exact-path-model" {
t.Fatalf("prepared model = %q, want exact-path-model", prepared.EffectiveModelParams.Model)
}
})
}
}
}
func TestSourceOptionsRejectInvalidInputs(t *testing.T) {
missingFile := filepath.Join(t.TempDir(), "missing.yaml")
directoryPath := t.TempDir()
@@ -2405,6 +2515,16 @@ func TestSourceOptionsRejectInvalidInputs(t *testing.T) {
}
})
}
t.Run("later valid replacement does not hide invalid option", func(t *testing.T) {
_, err := promptkit.NewEngine(promptkit.Config{PromptDir: frameworkPromptDir},
promptkit.WithProfileFS(nil, "profiles"),
promptkit.WithProfileFS(contractProfileFS("profile", "model"), "."),
)
if !errors.Is(err, promptkit.ErrInvalidConfig) {
t.Fatalf("expected ErrInvalidConfig, got %v", err)
}
})
}
func TestPackageOptionsComposeFromSlice(t *testing.T) {