using System.Text.Json; using ZB.MOM.WW.ScadaBridge.CLI.Commands; namespace ZB.MOM.WW.ScadaBridge.CLI.Tests; /// /// Tests for the compact template list/get table projection (followup #6): /// the full per-template attribute/alarm/script dumps are collapsed to counts so table /// output is usable in a terminal, while the array/object shape is preserved. /// public class TemplateTableProjectionTests { private const string ListJson = """ [ { "id": 3, "name": "MESReceiver", "description": "base", "parentTemplateId": null, "isDerived": false, "attributes": [ {"id":1},{"id":2},{"id":3} ], "alarms": [ {"id":10} ], "scripts": [ {"id":20},{"id":21} ], "compositions": [], "nativeAlarmSources": [] }, { "id": 5, "name": "LeftMESReceiver", "description": null, "parentTemplateId": 3, "isDerived": false, "attributes": [ {"id":1} ], "alarms": [], "scripts": [], "compositions": [ {"id":99} ], "nativeAlarmSources": [ {"id":7} ] } ] """; [Fact] public void ProjectSummary_Array_DropsMemberArraysAndKeepsCounts() { var compact = TemplateTableProjection.ProjectSummary(ListJson); using var doc = JsonDocument.Parse(compact); var root = doc.RootElement; Assert.Equal(JsonValueKind.Array, root.ValueKind); Assert.Equal(2, root.GetArrayLength()); var first = root[0]; Assert.Equal(3, first.GetProperty("id").GetInt32()); Assert.Equal("MESReceiver", first.GetProperty("name").GetString()); Assert.Equal("base", first.GetProperty("description").GetString()); Assert.Equal(JsonValueKind.Null, first.GetProperty("parentTemplateId").ValueKind); Assert.Equal(3, first.GetProperty("#attrs").GetInt32()); Assert.Equal(1, first.GetProperty("#alarms").GetInt32()); Assert.Equal(2, first.GetProperty("#scripts").GetInt32()); Assert.Equal(0, first.GetProperty("#comps").GetInt32()); Assert.Equal(0, first.GetProperty("#nativeAlarms").GetInt32()); // The full member arrays must NOT survive — that is the whole point of the projection. Assert.False(first.TryGetProperty("attributes", out _)); Assert.False(first.TryGetProperty("scripts", out _)); var second = root[1]; Assert.Equal(5, second.GetProperty("id").GetInt32()); Assert.Equal(3, second.GetProperty("parentTemplateId").GetInt32()); Assert.Equal(1, second.GetProperty("#comps").GetInt32()); Assert.Equal(1, second.GetProperty("#nativeAlarms").GetInt32()); // A null description stays null (it is not invented). Assert.Equal(JsonValueKind.Null, second.GetProperty("description").ValueKind); } [Fact] public void ProjectSummary_SingleObject_ProducesCompactObject() { const string getJson = """ { "id": 7, "name": "ReactorSide", "description": "d", "parentTemplateId": null, "isDerived": false, "attributes": [ {"id":1},{"id":2} ], "alarms": [], "scripts": [], "compositions": [], "nativeAlarmSources": [] } """; var compact = TemplateTableProjection.ProjectSummary(getJson); using var doc = JsonDocument.Parse(compact); var root = doc.RootElement; Assert.Equal(JsonValueKind.Object, root.ValueKind); Assert.Equal(7, root.GetProperty("id").GetInt32()); Assert.Equal(2, root.GetProperty("#attrs").GetInt32()); Assert.False(root.TryGetProperty("attributes", out _)); } [Fact] public void ProjectSummary_NonJson_ReturnedVerbatim() { const string notJson = "proxy error"; Assert.Equal(notJson, TemplateTableProjection.ProjectSummary(notJson)); } [Fact] public void ProjectSummary_IsSubstantiallySmallerThanFullDump() { // Sanity check that the projection actually shrinks output (the reported symptom // was ~171 KB table dumps). A template with a fat attribute array should collapse. var fatAttributes = string.Join(",", Enumerable.Range(0, 200).Select(i => $"{{\"id\":{i},\"name\":\"Attr{i}\",\"dataType\":\"String\",\"value\":\"some long-ish default value {i}\"}}")); var fullJson = $$""" [ { "id": 1, "name": "T", "description": "d", "parentTemplateId": null, "isDerived": false, "attributes": [ {{fatAttributes}} ], "alarms": [], "scripts": [], "compositions": [], "nativeAlarmSources": [] } ] """; var compact = TemplateTableProjection.ProjectSummary(fullJson); Assert.True(compact.Length * 4 < fullJson.Length, $"Expected compact ({compact.Length}) to be far smaller than full ({fullJson.Length})."); using var doc = JsonDocument.Parse(compact); Assert.Equal(200, doc.RootElement[0].GetProperty("#attrs").GetInt32()); } }