diff --git a/sparkplug_plugin/unit_test.go b/sparkplug_plugin/unit_test.go index 93fea42..81fc8cd 100644 --- a/sparkplug_plugin/unit_test.go +++ b/sparkplug_plugin/unit_test.go @@ -110,6 +110,37 @@ var _ = Describe("AliasCache Unit Tests", func() { Expect(count).To(Equal(2)) Expect(*dataMetrics[0].Name).To(Equal("Temperature")) Expect(*dataMetrics[1].Name).To(Equal("Pressure")) + //TODO: test on type + }) + + // ENG-5126: the BIRTH-cached datatype must be restored on DATA metrics + // even when the publisher already sent the metric name alongside the + // alias. Without the datatype, extractMetricValue cannot reinterpret + // signed integer wire values (two's-complement packed into the unsigned + // int_value/long_value fields) and Int32(-12) surfaces as 4294967284. + It("restores the datatype when DATA carries the name alongside the alias", func() { + birthMetrics := []*sparkplugb.Payload_Metric{ + { + Name: stringPtr("Temperature"), + Alias: uint64Ptr(100), + Datatype: uint32Ptr(sparkplugplugin.SparkplugDataTypeInt32), + }, + } + Expect(cache.CacheAliases("TestFactory/Line1", birthMetrics)).To(Equal(1)) + + dataMetrics := []*sparkplugb.Payload_Metric{ + { + Name: stringPtr("Temperature"), + Alias: uint64Ptr(100), + Value: &sparkplugb.Payload_Metric_IntValue{IntValue: 4294967284}, // -12 as Int32 + }, + } + + cache.ResolveAliases("TestFactory/Line1", dataMetrics) + + Expect(dataMetrics[0].Datatype).NotTo(BeNil(), + "datatype from the BIRTH certificate must be restored even when DATA already carries the name") + Expect(*dataMetrics[0].Datatype).To(Equal(sparkplugplugin.SparkplugDataTypeInt32)) }) It("should handle alias collisions in NBIRTH", func() {