52 lines
2.2 KiB
Go
52 lines
2.2 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"go.maximumdirect.net/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestValidationBindingUsesEffectiveContractAndPromptRoot(t *testing.T) {
|
|
fileName := "configured.json"
|
|
def := &domain.PromptDefinition{
|
|
ID: "collection/prompt",
|
|
SchemaRoot: &domain.ResourceBinding{FS: fstest.MapFS{}, Path: "collection/schemas"},
|
|
SchemaFile: &domain.ResourceBinding{FS: fstest.MapFS{}, Path: "origin/configured.json"},
|
|
Validation: domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, SchemaFile: &fileName},
|
|
}
|
|
configured, err := bindValidationInput(def, def.Validation, false)
|
|
if err != nil || configured.File == nil || configured.File.Path != "origin/configured.json" {
|
|
t.Fatalf("configured binding = %+v, %v", configured, err)
|
|
}
|
|
inline := &domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, Schema: false}
|
|
contract, err := resolveOutputContract(context.Background(), def, inline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
selected, err := bindValidationInput(def, contract, true)
|
|
if err != nil || selected.File != nil || selected.Contract.Schema != false || selected.Contract.SchemaFile != nil {
|
|
t.Fatalf("inline replacement = %+v, %v", selected, err)
|
|
}
|
|
path := &domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, SchemaPath: "runtime.json"}
|
|
contract, err = resolveOutputContract(context.Background(), def, path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
selected, err = bindValidationInput(def, contract, true)
|
|
if err != nil || selected.File == nil || selected.File.Path != "collection/schemas/runtime.json" || selected.Contract.SchemaFile != nil {
|
|
t.Fatalf("path replacement = %+v, %v", selected, err)
|
|
}
|
|
def.SchemaRoot = nil
|
|
if _, err := bindValidationInput(def, contract, true); err == nil || !strings.Contains(err.Error(), "schema_root") {
|
|
t.Fatalf("missing root = %v", err)
|
|
}
|
|
def.SchemaRoot = &domain.ResourceBinding{FS: fstest.MapFS{}, Path: "collection/schemas"}
|
|
contract.SchemaPath = "../outside.json"
|
|
if _, err := bindValidationInput(def, contract, true); err == nil {
|
|
t.Fatal("escaping path accepted")
|
|
}
|
|
}
|