using System.Text.Json.Nodes;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.DeviceForms;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
///
/// Guards for the MQTT device model. MQTT holds ONE broker connection per driver, so — like
/// Galaxy — the device carries no endpoint of its own and the model round-trips its DeviceConfig
/// verbatim. The one behaviour it does add is detecting legacy connection keys left on a
/// DeviceConfig by a pre-form (hand-authored / SQL-seeded) deployment, because
/// DriverDeviceConfigMerger merges a sole device's keys UP over the driver's and they would
/// otherwise silently win over what the driver form shows.
///
public sealed class MqttDeviceModelTests
{
[Fact]
public void Round_trips_every_key_verbatim()
{
const string inbound = """{"Host":"10.100.0.35","Port":8883,"anything":{"nested":true}}""";
var json = MqttDeviceModel.FromJson(inbound).ToJson();
var o = (JsonObject)JsonNode.Parse(json)!;
o["Host"]!.GetValue().ShouldBe("10.100.0.35");
o["Port"]!.GetValue().ShouldBe(8883);
o["anything"]!["nested"]!.GetValue().ShouldBeTrue();
}
[Fact]
public void A_new_device_serializes_to_an_empty_object()
=> MqttDeviceModel.FromJson(null).ToJson().ShouldBe("{}");
[Fact]
public void A_malformed_blob_degrades_to_an_empty_object_rather_than_throwing()
=> MqttDeviceModel.FromJson("}{ not json").ToJson().ShouldBe("{}");
[Fact]
public void Validate_always_passes_because_there_is_no_per_device_endpoint()
=> MqttDeviceModel.FromJson("""{"Host":"x"}""").Validate().ShouldBeNull();
[Fact]
public void Legacy_connection_keys_are_reported_case_insensitively()
{
var model = MqttDeviceModel.FromJson("""{"host":"h","PORT":1883,"useTls":false,"unrelated":1}""");
model.LegacyConnectionKeys.ShouldBe(["host", "PORT", "useTls"], ignoreOrder: true);
}
[Fact]
public void An_empty_device_reports_no_legacy_connection_keys()
=> MqttDeviceModel.FromJson("{}").LegacyConnectionKeys.ShouldBeEmpty();
}