using System; using System.Collections.Generic; using Junction.Domain.Models; using Xunit; namespace Junction.Tests.Unit { public class MachineTests { [Fact] public void Ctor_HoldsGenericConfigBag() { var id = Guid.NewGuid(); var config = new Dictionary { ["url"] = "http://host:5000", ["deviceName"] = "M1" }; var m = new Machine(id, "Mill", "mtconnect", config, TimeSpan.FromSeconds(2)); Assert.Equal(id, m.Id); Assert.Equal("Mill", m.Name); Assert.Equal("mtconnect", m.ProtocolId); Assert.Equal("http://host:5000", m.ConnectionConfig["url"]); Assert.Equal("M1", m.ConnectionConfig["deviceName"]); Assert.Equal(TimeSpan.FromSeconds(2), m.PollInterval); } [Fact] public void Ctor_NullConfig_DefaultsToEmptyNeverNull() { var m = new Machine(Guid.NewGuid(), "M", "p", null, TimeSpan.Zero); Assert.NotNull(m.ConnectionConfig); Assert.Empty(m.ConnectionConfig); } [Fact] public void Ctor_NullStrings_DefaultToEmpty() { var m = new Machine(Guid.NewGuid(), null!, null!, null, TimeSpan.Zero); Assert.Equal("", m.Name); Assert.Equal("", m.ProtocolId); } [Fact] public void Properties_AreGetOnly() { var props = typeof(Machine).GetProperties(); Assert.All(props, p => Assert.Null(p.SetMethod)); } [Fact] public void With_OverridesGivenFields_KeepsRest() { var id = Guid.NewGuid(); var m = new Machine(id, "Mill", "mtconnect", null, TimeSpan.FromSeconds(1)); var updated = m.With(name: "Lathe", pollInterval: TimeSpan.FromSeconds(5)); Assert.Equal(id, updated.Id); Assert.Equal("Lathe", updated.Name); Assert.Equal("mtconnect", updated.ProtocolId); Assert.Equal(TimeSpan.FromSeconds(5), updated.PollInterval); // original unchanged (immutable) Assert.Equal("Mill", m.Name); Assert.Equal(TimeSpan.FromSeconds(1), m.PollInterval); } } }