using ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis; namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.ScriptAnalysis; public class JsonSchemaShapeParserTests { // ── JSON Schema (post-migration) ───────────────────────────────────────── /// Verifies that a JSON schema parses into scalar parameters with their required flags. [Fact] public void Parameters_JsonSchema_ScalarsAndRequired() { const string json = """ {"type":"object","properties":{ "id":{"type":"integer"}, "label":{"type":"string"}, "active":{"type":"boolean"} },"required":["id","active"]} """; var result = JsonSchemaShapeParser.ParseParameters(json); Assert.Collection(result, p => { Assert.Equal("id", p.Name); Assert.Equal("Integer", p.Type); Assert.True(p.Required); }, p => { Assert.Equal("label", p.Name); Assert.Equal("String", p.Type); Assert.False(p.Required); }, p => { Assert.Equal("active", p.Name); Assert.Equal("Boolean", p.Type); Assert.True(p.Required); }); } /// Verifies that a JSON schema array of strings becomes a list of strings. [Fact] public void Parameters_JsonSchema_ArrayOfStringsBecomesListString() { const string json = """ {"type":"object","properties":{ "tags":{"type":"array","items":{"type":"string"}} }} """; var result = JsonSchemaShapeParser.ParseParameters(json); var tags = Assert.Single(result); Assert.Equal("tags", tags.Name); Assert.Equal("List", tags.Type); Assert.False(tags.Required); } /// Verifies that a JSON schema number return type parses as a number. [Fact] public void Return_JsonSchema_Number() { Assert.Equal("Float", JsonSchemaShapeParser.ParseReturnType(@"{""type"":""number""}")); } /// Verifies that a JSON schema array of integers return type parses as a list of integers. [Fact] public void Return_JsonSchema_ArrayOfIntegers() { Assert.Equal("List", JsonSchemaShapeParser.ParseReturnType(@"{""type"":""array"",""items"":{""type"":""integer""}}")); } // ── Legacy flat shape (pre-migration safety net) ───────────────────────── /// Verifies that the legacy flat array parameter format still parses. [Fact] public void Parameters_Legacy_FlatArrayStillParses() { const string json = """[{"name":"x","type":"Integer"},{"name":"y","type":"String","required":false}]"""; var result = JsonSchemaShapeParser.ParseParameters(json); Assert.Collection(result, p => { Assert.Equal("x", p.Name); Assert.Equal("Integer", p.Type); Assert.True(p.Required); }, p => { Assert.Equal("y", p.Name); Assert.Equal("String", p.Type); Assert.False(p.Required); }); } /// Verifies that the legacy list sentinel return format still parses. [Fact] public void Return_Legacy_ListSentinelStillParses() { Assert.Equal("List", JsonSchemaShapeParser.ParseReturnType(@"{""type"":""List"",""itemType"":""String""}")); } // ── Edge cases ──────────────────────────────────────────────────────────── /// Verifies that null parameters yield an empty list. [Fact] public void Parameters_Null_ReturnsEmpty() { Assert.Empty(JsonSchemaShapeParser.ParseParameters(null)); Assert.Empty(JsonSchemaShapeParser.ParseParameters("")); Assert.Empty(JsonSchemaShapeParser.ParseParameters(" ")); } /// Verifies that malformed parameters yield an empty list. [Fact] public void Parameters_Malformed_ReturnsEmpty() { Assert.Empty(JsonSchemaShapeParser.ParseParameters("{not json")); Assert.Empty(JsonSchemaShapeParser.ParseParameters("42")); } /// Verifies that a null return definition yields null. [Fact] public void Return_Null_ReturnsNull() { Assert.Null(JsonSchemaShapeParser.ParseReturnType(null)); Assert.Null(JsonSchemaShapeParser.ParseReturnType("")); } /// Verifies that a schema with no properties yields an empty parameter list. [Fact] public void Parameters_SchemaWithNoProperties_ReturnsEmpty() { Assert.Empty(JsonSchemaShapeParser.ParseParameters(@"{""type"":""object""}")); Assert.Empty(JsonSchemaShapeParser.ParseParameters(@"{""type"":""object"",""properties"":{}}")); } }