Unify inline and bound schema preparation
This commit is contained in:
103
internal/validate/preparation_test.go
Normal file
103
internal/validate/preparation_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
)
|
||||
|
||||
func TestPrepareInlineAndBoundDocuments(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
document any
|
||||
file string
|
||||
instance string
|
||||
status domain.ValidationStatus
|
||||
}{
|
||||
{"object", map[string]any{"type": "object", "required": []any{"x"}}, `{"type":"object","required":["x"]}`, `{"x":1}`, domain.ValidationPassed},
|
||||
{"reject object", map[string]any{"type": "object"}, `{"type":"object"}`, `1`, domain.ValidationFailed},
|
||||
{"true", true, `true`, `null`, domain.ValidationPassed},
|
||||
{"false", false, `false`, `null`, domain.ValidationFailed},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
validator := NewValidator()
|
||||
contract := domain.OutputContract{ValidationMode: domain.ValidationJSONSchema, Schema: tc.document}
|
||||
inline, err := validator.PrepareValidation(context.Background(), Input{Contract: contract})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
contract.Schema = nil
|
||||
file := &domain.ResourceBinding{FS: fstest.MapFS{"schema.json": {Data: []byte(tc.file)}}, Path: "schema.json"}
|
||||
bound, err := validator.PrepareValidation(context.Background(), Input{Contract: contract, File: file})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(inline.SchemaDocument(), bound.SchemaDocument()) {
|
||||
t.Fatalf("documents differ: %#v %#v", inline.SchemaDocument(), bound.SchemaDocument())
|
||||
}
|
||||
for _, plan := range []PreparedValidation{inline, bound} {
|
||||
result, err := plan.Validate(context.Background(), &domain.Artifact{Body: []byte(tc.instance)})
|
||||
if err != nil || result.Status != tc.status {
|
||||
t.Fatalf("result = %+v, %v", result, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareSchemaReferencesAndDialects(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
document any
|
||||
valid bool
|
||||
}{
|
||||
{"internal ref", map[string]any{"$defs": map[string]any{"n": map[string]any{"type": "integer"}}, "$ref": "#/$defs/n"}, true},
|
||||
{"external ref", map[string]any{"$ref": "https://example.invalid/schema"}, false},
|
||||
{"invalid schema", map[string]any{"type": "invalid"}, false},
|
||||
{"invalid dialect", map[string]any{"$schema": "https://invalid.example/schema"}, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := NewValidator().PrepareValidation(context.Background(), Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema, Schema: tc.document}})
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("preparation error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareBoundSchemaPreservesNumbersAndCancellation(t *testing.T) {
|
||||
file := &domain.ResourceBinding{FS: fstest.MapFS{"schema.json": {Data: []byte(`{"const":9007199254740993}`)}}, Path: "schema.json"}
|
||||
input := Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema}, File: file}
|
||||
plan, err := NewValidator().PrepareValidation(context.Background(), input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := plan.SchemaDocument().(map[string]any)["const"]; got != json.Number("9007199254740993") {
|
||||
t.Fatalf("number = %#v", got)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := NewValidator().PrepareValidation(ctx, input); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancellation = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalReferencesDoNotOpenAdditionalResources(t *testing.T) {
|
||||
files := &countingSchemaFS{FS: fstest.MapFS{
|
||||
"root.json": {Data: []byte(`{"$ref":"child.json"}`)},
|
||||
"child.json": {Data: []byte(`{"type":"string"}`)},
|
||||
}}
|
||||
input := Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema}, File: &domain.ResourceBinding{FS: files, Path: "root.json"}}
|
||||
_, err := NewValidator().PrepareValidation(context.Background(), input)
|
||||
if err == nil {
|
||||
t.Fatal("external reference was accepted")
|
||||
}
|
||||
if opens := files.opens.Load(); opens != 1 {
|
||||
t.Fatalf("schema file opens = %d, want one", opens)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user