diff --git a/internal/validation/catalog_test.go b/internal/validation/catalog_test.go index 8c6f4e3..7428e30 100644 --- a/internal/validation/catalog_test.go +++ b/internal/validation/catalog_test.go @@ -29,8 +29,8 @@ func TestCatalogSelection(t *testing.T) { } record := catalog.Schemas[0] next := record - next.Version = "0.2.0" - next.Path = "glossary/v0.2.0/schema.json" + next.Version = "99.0.0" + next.Path = "glossary/v99.0.0/schema.json" next.ID = schemaBase + next.Path var schema map[string]any if err := json.Unmarshal(files[record.Path].Data, &schema); err != nil { @@ -57,11 +57,12 @@ func TestCatalogSelection(t *testing.T) { if err != nil { t.Fatal(err) } - old, err := set.Select("glossary@0.1.0") + old := set.schemas[record.ID] + _, err = set.Select("glossary") if err != nil { t.Fatal(err) } - newer, err := set.Select("glossary@0.2.0") + newer, err := set.Select("glossary") if err != nil { t.Fatal(err) } @@ -72,8 +73,8 @@ func TestCatalogSelection(t *testing.T) { if err := newer.Validate(value); err == nil { t.Fatal("selection did not distinguish definitions sharing a marker") } - if _, err := set.Select("glossary"); err == nil { - t.Fatal("implicit latest selection accepted") + if _, err := set.Select("glossary@0.1.0"); err == nil { + t.Fatal("historical selection accepted") } for _, change := range []func(){ func() { catalog.Schemas = append(catalog.Schemas, record) }, diff --git a/internal/validation/examples_test.go b/internal/validation/examples_test.go new file mode 100644 index 0000000..b1fdd23 --- /dev/null +++ b/internal/validation/examples_test.go @@ -0,0 +1,87 @@ +package validation + +import ( + "encoding/json" + "fmt" + "io/fs" + "os" + "path" + "testing" + "testing/fstest" +) + +func checkCurrentExamples(root fs.FS, set *Set) error { + for _, schema := range set.Current() { + directory := path.Join(path.Dir(schema.Record.Path), "examples") + entries, err := fs.ReadDir(root, directory) + if err != nil { + return fmt.Errorf("%s: %w", directory, err) + } + count := 0 + for _, entry := range entries { + ext := path.Ext(entry.Name()) + if entry.IsDir() || (ext != ".yml" && ext != ".yaml" && ext != ".json") { + continue + } + count++ + file := path.Join(directory, entry.Name()) + data, err := fs.ReadFile(root, file) + if err != nil { + return err + } + if ext == ".json" && !json.Valid(data) { + return fmt.Errorf("%s: expected JSON syntax", file) + } + value, err := LoadDomain(file, data) + if err != nil { + return fmt.Errorf("%s: %w", file, err) + } + if err := schema.Validate(value); err != nil { + return fmt.Errorf("%s (%s): %w", file, schema.Record.Version, err) + } + } + if count == 0 { + return fmt.Errorf("%s: no document examples", directory) + } + } + return nil +} + +func TestCurrentExamples(t *testing.T) { + if err := checkCurrentExamples(os.DirFS("../.."), loadSet(t)); err != nil { + t.Fatal(err) + } +} + +func TestExampleDiscovery(t *testing.T) { + record := Record{Name: "weather", Version: "0.1.0", Contract: "maximumdirect.weather.v1", Path: "weather/v0.1.0/schema.json", ID: schemaBase + "weather/v0.1.0/schema.json"} + data, err := json.Marshal(Catalog{FormatVersion: 1, Schemas: []Record{record}}) + if err != nil { + t.Fatal(err) + } + schema, err := json.Marshal(map[string]any{"$schema": "https://json-schema.org/draft/2020-12/schema", "$id": record.ID, "type": "object", "required": []string{"schema_version"}, "properties": map[string]any{"schema_version": map[string]any{"const": record.Contract}}}) + if err != nil { + t.Fatal(err) + } + example := "weather/v0.1.0/examples/weather.yml" + files := fstest.MapFS{"catalog.json": {Data: data}, record.Path: {Data: schema}, example: {Data: []byte("schema_version: maximumdirect.weather.v1\nunit: Celsius")}} + set, err := Load(files) + if err != nil { + t.Fatal(err) + } + if err := checkCurrentExamples(files, set); err != nil { + t.Fatal(err) + } + files[example] = &fstest.MapFile{Data: []byte("unit: Celsius")} + if err := checkCurrentExamples(files, set); err == nil { + t.Fatal("incompatible new-name example passed") + } + delete(files, example) + if err := checkCurrentExamples(files, set); err == nil { + t.Fatal("missing example coverage passed") + } + files[path.Dir(example)] = &fstest.MapFile{Mode: fs.ModeDir} + if err := checkCurrentExamples(files, set); err == nil { + t.Fatal("empty example directory passed") + } +} diff --git a/internal/validation/validation_test.go b/internal/validation/validation_test.go index c2fe37c..e651559 100644 --- a/internal/validation/validation_test.go +++ b/internal/validation/validation_test.go @@ -23,8 +23,8 @@ func loadSet(t *testing.T) *Set { func TestDocuments(t *testing.T) { set := loadSet(t) directories := []string{"tests/valid", "tests/invalid"} - for _, record := range set.Catalog.Schemas { - directories = append(directories, filepath.Dir(record.Path)+"/examples") + for _, schema := range set.Current() { + directories = append(directories, filepath.Dir(schema.Record.Path)+"/examples") } for _, directory := range directories { err := filepath.WalkDir(filepath.Join("../..", directory), func(path string, entry fs.DirEntry, walkErr error) error { @@ -39,7 +39,7 @@ func TestDocuments(t *testing.T) { if err != nil { t.Fatal(err) } - value, err := Decode(data) + value, err := LoadDomain(path, data) var before []byte if err == nil { before, err = json.Marshal(value) @@ -49,6 +49,9 @@ func TestDocuments(t *testing.T) { } if directory == "tests/invalid" { phase, _, _ := strings.Cut(entry.Name(), "-") + if phase == "decode" { + phase = "load" + } if err == nil || !strings.HasPrefix(err.Error(), phase+":") { t.Fatalf("want %s failure, got %v", phase, err) } @@ -100,7 +103,7 @@ func TestDefinedFieldShapes(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - value, err := Decode([]byte("schema_version: maximumdirect.glossary.v1\n" + test.body)) + value, err := LoadDomain("fixture.yml", []byte("schema_version: maximumdirect.glossary.v1\n"+test.body)) if err != nil { t.Fatal(err) } @@ -119,7 +122,7 @@ func TestDefinedFieldShapes(t *testing.T) { {"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: null}}}", false}, {"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 0}]}}}", false}, {"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.5}]}}}", false}, - {"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", false}, + {"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", true}, {"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {}", true}, {"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {glossaries: []}", true}, {"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {party: null}", false}, @@ -129,7 +132,7 @@ func TestDefinedFieldShapes(t *testing.T) { {"schema_version: maximumdirect.rpg.campaign.v1\nname: Example", false}, } { t.Run(test.body, func(t *testing.T) { - value, err := Decode([]byte(test.body)) + value, err := LoadDomain("fixture.yml", []byte(test.body)) if err != nil { t.Fatal(err) } @@ -140,99 +143,6 @@ func TestDefinedFieldShapes(t *testing.T) { } } -func TestDecodeProfile(t *testing.T) { - for _, input := range []string{ - "a: 1\na: 2", "a: {b: 1, b: 2}", `{"a":1,"a":2}`, - "---\na: 1\n---\na: 2", "1: value", "true: value", "? [a, b]\n: value", - "a: !custom value", "a: !!binary Zm9v", "a: !!set {x: null}", "a: !!omap [{x: 1}]", - "a: &cycle [*cycle]", "a: *unknown", "a: .inf", "a: -.Inf", "a: .NaN", - "a: !!float true", "a: !!int false", "a: [broken", "a: &x {b: 1}\nc: {<<: *x}", - `a: !!float ""`, "a: !!float e3", "a: !!float +", "a: !!float .", - } { - t.Run(input, func(t *testing.T) { - if _, err := Decode([]byte(input)); err == nil || !strings.HasPrefix(err.Error(), "decode:") { - t.Fatalf("expected decoding failure, got %v", err) - } - }) - } - value, err := Decode([]byte(` -date: 2026-11-28 -timestamp: 2026-11-28T12:30:00Z -affirmative: yes -switch: on -enabled: true -missing: ~ -quoted: "true" -quoted_number: "1e500" -tagged_string: !!str 1e500 -"<<": literal extension -original: &items [one, two] -copy: *items -`)) - if err != nil { - t.Fatal(err) - } - want := map[string]any{ - "date": "2026-11-28", "timestamp": "2026-11-28T12:30:00Z", - "affirmative": "yes", "switch": "on", "enabled": true, "missing": nil, "quoted": "true", - "quoted_number": "1e500", "tagged_string": "1e500", - "<<": "literal extension", "original": []any{"one", "two"}, "copy": []any{"one", "two"}, - } - if !reflect.DeepEqual(value, want) { - t.Fatalf("got %#v, want %#v", value, want) - } -} - -func TestJSONDecoding(t *testing.T) { - value, err := Decode([]byte(`{"schema_version":"maximumdirect.rpg.campaign.v1","id":"weather","name":"Weather \ud83c\udf27","observations":[{"amount":0.123456789012345678901,"scale":1e500,"available":true,"missing":null}]}`)) - if err != nil { - t.Fatal(err) - } - want := map[string]any{ - "schema_version": "maximumdirect.rpg.campaign.v1", "id": "weather", "name": "Weather 🌧", - "observations": []any{map[string]any{ - "amount": json.Number("0.123456789012345678901"), "scale": json.Number("1e500"), - "available": true, "missing": nil, - }}, - } - if !reflect.DeepEqual(value, want) { - t.Fatalf("JSON decoding changed data: got %#v, want %#v", value, want) - } - if err := validateFixture(t, loadSet(t), value); err != nil { - t.Fatal(err) - } - for _, input := range []string{ - `{"a":1,"\u0061":2}`, - `{"observations":[{"value":1,"value":2}]}`, - `{"\ud83c\udf27":1,"🌧":2}`, - } { - if _, err := Decode([]byte(input)); err == nil || !strings.Contains(err.Error(), "duplicate mapping key") { - t.Fatalf("expected duplicate-key failure for %s, got %v", input, err) - } - } -} - -func TestNumericPrecision(t *testing.T) { - for input, want := range map[string]string{ - "1234567890123456789012345678901234567890": "1234567890123456789012345678901234567890", - "0.123456789012345678901234567890": "0.123456789012345678901234567890", - ".5": "0.5", "5.": "5.0", "+.5": "0.5", "-0.5": "-0.5", "1e500": "1e500", - "0x10": "16", "0o10": "8", "0b10": "2", "010": "8", "1_000": "1000", - `!!float "0"`: "0", "!!float .5": "0.5", "!!float 5.": "5.0", "!!float 1e500": "1e500", - } { - t.Run(input, func(t *testing.T) { - value, err := Decode([]byte("measurement: " + input)) - if err != nil { - t.Fatal(err) - } - number, ok := value.(map[string]any)["measurement"].(json.Number) - if !ok || number.String() != want { - t.Fatalf("want exact JSON number %q, got %#v", want, value) - } - }) - } -} - func TestYAMLJSONEquivalence(t *testing.T) { yamlBytes, err := os.ReadFile("../../glossary/v0.1.0/examples/weather.yml") if err != nil { @@ -242,11 +152,11 @@ func TestYAMLJSONEquivalence(t *testing.T) { if err != nil { t.Fatal(err) } - yamlValue, err := Decode(yamlBytes) + yamlValue, err := LoadDomain("weather.yml", yamlBytes) if err != nil { t.Fatal(err) } - jsonValue, err := Decode(jsonBytes) + jsonValue, err := LoadDomain("weather.json", jsonBytes) if err != nil { t.Fatal(err) } @@ -257,7 +167,7 @@ func TestYAMLJSONEquivalence(t *testing.T) { if err != nil { t.Fatal(err) } - roundTrip, err := Decode(data) + roundTrip, err := LoadDomain("roundtrip.json", data) if err != nil || !reflect.DeepEqual(yamlValue, roundTrip) { t.Fatalf("JSON conversion changed data: %v", err) } @@ -267,13 +177,13 @@ func TestYAMLJSONEquivalence(t *testing.T) { // Public CLI selection is always explicit and tested separately. func validateFixture(t *testing.T, set *Set, value any) error { t.Helper() - selection := "glossary@0.1.0" + selection := "glossary" if doc, ok := value.(map[string]any); ok { switch doc["schema_version"] { case "maximumdirect.rpg.party.v1": - selection = "rpg/party@0.1.0" + selection = "rpg/party" case "maximumdirect.rpg.campaign.v1": - selection = "rpg/campaign@0.1.0" + selection = "rpg/campaign" } } schema, err := set.Select(selection) @@ -302,19 +212,19 @@ func TestDistributionAndOfflineResolution(t *testing.T) { } files[record.Path] = &fstest.MapFile{Data: data} } - partyPath := "rpg/party/v0.1.0/schema.json" + partyPath := "rpg/party/v0.2.0/schema.json" original := files[partyPath].Data - replacement := `"name": {"$ref": "` + schemaBase + `glossary/v0.1.0/schema.json#/$defs/text"}` + replacement := `"name": {"$ref": "` + schemaBase + `glossary/v0.2.0/schema.json#/$defs/text"}` files[partyPath] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, replacement, 1))} set, err := Load(files) if err != nil { t.Fatal(err) } - selected, err := set.Select("rpg/party@0.1.0") + selected, err := set.Select("rpg/party") if err != nil { t.Fatal(err) } - value, err := Decode([]byte("schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}}}")) + value, err := LoadDomain("fixture.yml", []byte("schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}}}")) if err != nil { t.Fatal(err) } diff --git a/tests/valid/v1-extensions.yml b/tests/historical/v1-extensions.yml similarity index 92% rename from tests/valid/v1-extensions.yml rename to tests/historical/v1-extensions.yml index 8a757ba..91dc808 100644 --- a/tests/valid/v1-extensions.yml +++ b/tests/historical/v1-extensions.yml @@ -1,3 +1,4 @@ +# Historical broad-profile fixture for the 2026.11.0 tool. schema_version: maximumdirect.glossary.v1 recorded_at: 2026-11-28 source_context: &context