2026-11-21 22:56:14 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-22 06:00:52 +00:00
|
|
|
"errors"
|
2026-11-21 22:56:14 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestNativeLoadPreparesOperatorSchemaSelections(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
name, schema, root string
|
|
|
|
|
missing bool
|
|
|
|
|
}{
|
2026-11-22 06:00:52 +00:00
|
|
|
{"inline", `schema: {type: object, required: [operator_value], properties: {operator_value: {const: selected}}, additionalProperties: false}`, "", false},
|
2026-11-21 22:56:14 +00:00
|
|
|
{"source file", "schema_file: response.json", "", false},
|
|
|
|
|
{"external root", "schema_path: response.json", "schema_root: external-schemas", false},
|
|
|
|
|
{"replaced root missing embedded schema", "schema_path: dnd/scenes/schemas/dnd_scenes_llm.v1.json", "schema_root: external-schemas", true},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
main := filepath.Join(dir, "config.yml")
|
|
|
|
|
writeConfigKitFile(t, main, "version: 6\n")
|
2026-11-22 06:00:52 +00:00
|
|
|
schema := `{"type":"object","required":["operator_value"],"properties":{"operator_value":{"const":"selected"}},"additionalProperties":false}`
|
|
|
|
|
writeConfigKitFile(t, filepath.Join(dir, "response.json"), schema)
|
|
|
|
|
writeConfigKitFile(t, filepath.Join(dir, "external-schemas", "response.json"), schema)
|
2026-11-21 22:56:14 +00:00
|
|
|
writeConfigKitFile(t, filepath.Join(dir, "prompts.yml"), "collections:\n notarius:\n "+test.root+"\n prompts:\n - id: dnd.scenes\n default_profile: notarius/dnd-extraction\n messages: [{role: user, content: operator-message}]\n output:\n format: json\n validation_mode: json_schema\n "+test.schema+"\n")
|
2026-11-22 06:00:52 +00:00
|
|
|
calls := 0
|
|
|
|
|
loaders := &nativeLoaders{engineOptions: []promptkit.Option{promptkit.WithLLMClient(captureClient(func(_ context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
|
|
|
calls++
|
|
|
|
|
if !productionSchemaHasProperty(request, "operator_value") {
|
|
|
|
|
return nil, errors.New("operator schema did not reach provider preparation")
|
|
|
|
|
}
|
|
|
|
|
return &promptkit.GenerateResponse{Content: `{"operator_value":"selected"}`}, nil
|
|
|
|
|
}))}}
|
2026-11-21 22:56:14 +00:00
|
|
|
engine, err := loaders.engine(context.Background(), nativeSelection(main))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
request := promptkit.RunRequest{PromptID: "notarius/dnd.scenes", APIKey: "fixture"}
|
|
|
|
|
prepared, err := engine.PrepareExecution(context.Background(), request)
|
|
|
|
|
if test.missing {
|
|
|
|
|
if err == nil {
|
|
|
|
|
prepared.Discard()
|
|
|
|
|
t.Fatal("root replacement silently retained embedded schema root")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer prepared.Discard()
|
|
|
|
|
details := prepared.Details()
|
|
|
|
|
if len(details.Messages) != 1 || details.Messages[0].Content != "operator-message" || details.StructuredOutput == nil {
|
|
|
|
|
t.Fatalf("replacement contract = %#v", details)
|
|
|
|
|
}
|
2026-11-22 06:00:52 +00:00
|
|
|
result, err := engine.RunPrepared(context.Background(), prepared)
|
|
|
|
|
if err != nil || result == nil || !result.Validation.IsValid || calls != 1 {
|
|
|
|
|
t.Fatalf("operator contract execution=%#v error=%v calls=%d", result, err, calls)
|
|
|
|
|
}
|
2026-11-21 22:56:14 +00:00
|
|
|
// Replacing one identified prompt must not discard unrelated embedded prompts.
|
|
|
|
|
if _, err := engine.InspectPrompt(context.Background(), "notarius/dnd.spells"); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|