Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build and test

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up .NET SDK
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json

- name: Restore
run: dotnet restore .\ModbusLab.slnx

- name: Build
run: dotnet build .\ModbusLab.slnx -c Release --no-restore /warnaserror

- name: Test
run: dotnet test .\ModbusLab.slnx -c Release --no-build --no-restore
233 changes: 233 additions & 0 deletions ModbusLab.Tests/DomainValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using ModbusLab.Models;
using ModbusLab.Validation;
using ModbusLab.ViewModels;

namespace ModbusLab.Tests;

public sealed class DomainValidatorTests
{
[Fact]
public void WatchNamesMustBeUniqueIgnoringCase()
{
var existing = new WatchItem { Name = "Temperature" };
var candidate = new WatchItem { Name = "temperature" };

var result = DomainValidator.ValidateWatch(candidate, [existing]);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(WatchItem.Name));
}

[Fact]
public void EditingOneLegacyDuplicateRequiresResolvingTheDuplicateName()
{
var first = new WatchItem { Name = "Duplicate", Address = 1 };
var second = new WatchItem { Name = "duplicate", Address = 2 };
var candidate = first.Clone();
candidate.Address = 3;

var result = DomainValidator.ValidateWatch(candidate, [first, second], first);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(WatchItem.Name));
Assert.Equal("Duplicate", first.Name);
Assert.Equal("duplicate", second.Name);
}

[Fact]
public void NonFiniteWatchAndReactionValuesAreRejected()
{
var floatWatch = new WatchItem
{
Name = "Float",
Area = MemoryArea.HoldingRegisters,
DataType = WatchDataTypes.Float32,
Count = 2
};
var watchValue = DomainValidator.ValidateWatchValue(
floatWatch,
"NaN",
ByteOrderMode.BigEndian,
WordOrderMode.HighWordFirst);
var reaction = DomainValidator.ValidateReaction(new WriteReactionRule
{
TriggerArea = MemoryArea.HoldingRegisters,
MatchValue = "1",
Action = WriteReactionAction.WriteValue,
TargetArea = MemoryArea.HoldingRegisters,
TargetAddress = 1,
Value = "70000"
});

Assert.False(watchValue.IsValid);
Assert.False(reaction.IsValid);
Assert.Contains(reaction.Issues, issue => issue.Field == nameof(WriteReactionRule.Value));
}

[Fact]
public void OrderingOperatorsAreRejectedForStringTriggers()
{
var watch = new WatchItem
{
Name = "Text",
Area = MemoryArea.HoldingRegisters,
DataType = WatchDataTypes.String,
Count = 2
};
var reaction = new WriteReactionRule
{
TriggerKind = WriteReactionEndpointKind.Watch,
TriggerWatchName = watch.Name,
MatchOperator = WriteReactionMatchOperator.GreaterThan,
MatchValue = "abc",
Action = WriteReactionAction.ResetTrigger
};

var result = DomainValidator.ValidateReaction(reaction, [watch]);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(WriteReactionRule.MatchOperator));
}

[Fact]
public void InvalidLegacyWatchFieldsAreReportedWithoutChangingTheSource()
{
var watch = new WatchItem
{
Name = "",
Area = MemoryArea.Coils,
DataType = WatchDataTypes.UInt32,
Count = 4,
StringFormat = "unsupported"
};

var result = DomainValidator.ValidateWatch(watch);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(WatchItem.Name));
Assert.Contains(result.Issues, issue => issue.Field == nameof(WatchItem.DataType));
Assert.Contains(result.Issues, issue => issue.Field == nameof(WatchItem.Count));
Assert.Equal("", watch.Name);
Assert.Equal(WatchDataTypes.UInt32, watch.DataType);
Assert.Equal(4, watch.Count);
Assert.Equal(WatchDataTypes.Bool, result.NormalizedValue.DataType);
Assert.Equal(1, result.NormalizedValue.Count);
}

[Fact]
public void InvalidImportedServerIsRejectedBeforeItCanBeAdded()
{
var imported = ServerProfile.CreateDefault("Imported", "127.0.0.1", 1502);
imported.WatchItems =
[
new WatchItem { Name = "Duplicate" },
new WatchItem { Name = "duplicate", Address = 1 }
];
var viewModel = new MainWindowViewModel(ProjectModel.CreateDefault());
viewModel.InitializeRuntimes();

Assert.Throws<DomainValidationException>(() => viewModel.PrepareImportedServer(imported));
Assert.Single(viewModel.Project.Servers);
}

[Fact]
public void MissingWatchTargetMakesSimulationInvalid()
{
var rule = new SimulationRule
{
TargetKind = SimulationTargetKind.Watch,
TargetWatchName = "Missing",
Mode = SimulationMode.Counter,
PeriodMs = 1000
};

var result = DomainValidator.ValidateSimulationRule(rule, []);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(SimulationRule.TargetWatchName));
}

[Fact]
public void NumericSimulationModeIsRejectedForBitTargets()
{
var rule = new SimulationRule
{
Area = MemoryArea.Coils,
Mode = SimulationMode.Sine,
PeriodMs = 1000
};

var result = DomainValidator.ValidateSimulationRule(rule);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(SimulationRule.Mode));
}

[Fact]
public void MaskWriteRegisterIsAValidFaultFunction()
{
var rule = new FaultRule
{
FunctionCode = 22,
StartAddress = 0,
EndAddress = 10
};

Assert.True(DomainValidator.ValidateFaultRule(rule).IsValid);
}

[Fact]
public void StringReactionCannotIncrementItsTarget()
{
var watch = new WatchItem
{
Name = "Status",
Area = MemoryArea.HoldingRegisters,
Address = 0,
DataType = WatchDataTypes.String,
Count = 4
};
var reaction = new WriteReactionRule
{
TriggerArea = MemoryArea.HoldingRegisters,
TriggerAddress = 0,
MatchValue = "",
Action = WriteReactionAction.IncrementRegister,
TargetKind = WriteReactionEndpointKind.Watch,
TargetWatchName = watch.Name,
Value = "1"
};

var result = DomainValidator.ValidateReaction(reaction, [watch]);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(WriteReactionRule.Action));
}

[Fact]
public void ProjectValidationReportsLegacyDuplicateWatchesWithoutChangingThem()
{
var profile = ServerProfile.CreateDefault("Server", "127.0.0.1", 1502);
profile.WatchItems.Add(new WatchItem { Name = "Duplicate" });
profile.WatchItems.Add(new WatchItem { Name = "duplicate" });
var project = new ProjectModel { Servers = [profile] };

var issues = DomainValidator.ValidateProject(project);

Assert.Contains(issues, issue => issue.Message.Contains("Duplicate watch name", StringComparison.Ordinal));
Assert.Equal(new[] { "Duplicate", "duplicate" }, profile.WatchItems.Select(watch => watch.Name));
}

[Fact]
public void ServerSettingsRequireAValidBindAddressAndSnapshot()
{
var profile = ServerProfile.CreateDefault("Server", "not-an-ip", 1502);
profile.StartupMemoryMode = StartupMemoryMode.RestoreSnapshot;

var result = DomainValidator.ValidateServerSettings(profile);

Assert.False(result.IsValid);
Assert.Contains(result.Issues, issue => issue.Field == nameof(ServerProfile.BindAddress));
Assert.Contains(result.Issues, issue => issue.Field == nameof(ServerProfile.StartupSnapshot));
}
}
Loading
Loading