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
160 changes: 160 additions & 0 deletions EvenBetterFastSim.Tests/ConnectionNodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System.Linq;
using System.Text.Json;
using System.Windows;
using EvenBetterFastSim.WPF.ViewModels;
using EvenBetterFastSim.WPF.ViewModels.Graph;
using MessagePack;
using SecsGemScenarioEngine.Models;
using Xunit;

namespace EvenBetterFastSim.Tests;

/// <summary>
/// The nodes Drop connection and Wait for communication on the canvas (GitLab secsgem/evencoolerfastsim#25). Their
/// parameter is a time in ms as text in TransactionName. The engine reads it as digits only (white space around them
/// ignored) and lets the node fail for anything else, so the title has to say when the value is not a time.
/// </summary>
public class ConnectionNodeTests
{
[Theory]
[InlineData("5000", "Drop connection · 5000 ms")]
[InlineData("0", "Drop connection · 0 ms")]
[InlineData("007", "Drop connection · 7 ms")]
[InlineData(" 250 ", "Drop connection · 250 ms")]
[InlineData("\t250\r\n", "Drop connection · 250 ms")]
// The engine trims every kind of white space here (string.Trim), also a non-breaking space.
[InlineData(" 250", "Drop connection · 250 ms")]
[InlineData("2147483647", "Drop connection · 2147483647 ms")]
[InlineData("2147483648", "Drop connection (invalid: 2147483648)")]
[InlineData("+5", "Drop connection (invalid: +5)")]
[InlineData("-1", "Drop connection (invalid: -1)")]
[InlineData("5 s", "Drop connection (invalid: 5 s)")]
[InlineData("1,5", "Drop connection (invalid: 1,5)")]
[InlineData("15​00", "Drop connection (invalid: 15[U+200B]00)")]
[InlineData("500​", "Drop connection (invalid: 500[U+200B])")]
[InlineData("", "Drop connection (no downtime set)")]
[InlineData(" ", "Drop connection (no downtime set)")]
[InlineData(null, "Drop connection (no downtime set)")]
public void Drop_connection_shows_its_downtime_or_what_is_wrong_with_it(string? text, string title)
{
var node = new ScenarioNodeViewModel { Type = NodeType.DropConnection, TransactionName = text };

Assert.Equal(title, node.Title);
}

[Theory]
[InlineData("20000", "Wait for communication · max 20000 ms")]
[InlineData("1", "Wait for communication · max 1 ms")]
[InlineData(" 250 ", "Wait for communication · max 250 ms")]
[InlineData("0", "Wait for communication · no timeout")]
[InlineData("000", "Wait for communication · no timeout")]
[InlineData("2147483648", "Wait for communication (invalid: 2147483648)")]
[InlineData("+5", "Wait for communication (invalid: +5)")]
[InlineData("-0", "Wait for communication (invalid: -0)")]
[InlineData("20 s", "Wait for communication (invalid: 20 s)")]
[InlineData("20​", "Wait for communication (invalid: 20[U+200B])")]
[InlineData("", "Wait for communication (no timeout set)")]
[InlineData(null, "Wait for communication (no timeout set)")]
public void Wait_for_communication_shows_its_timeout_or_what_is_wrong_with_it(string? text, string title)
{
var node = new ScenarioNodeViewModel { Type = NodeType.WaitForCommunication, TransactionName = text };

Assert.Equal(title, node.Title);
}

/// <summary>The title follows the value, and a DisplayName from a file does not replace it.</summary>
[Theory]
[InlineData(NodeType.DropConnection, "Drop connection · 100 ms", "Drop connection · 0 ms")]
[InlineData(NodeType.WaitForCommunication, "Wait for communication · max 100 ms", "Wait for communication · no timeout")]
public void Title_follows_the_value_and_is_not_replaced_by_a_display_name(NodeType type, string first, string then)
{
var node = new ScenarioNodeViewModel { Type = type, TransactionName = "100", DisplayName = "Pause" };
Assert.Equal(first, node.Title);

node.TransactionName = "0";

Assert.Equal(then, node.Title);
}

/// <summary>
/// Both nodes fail in normal use (no connection to drop, communication not back in time). Without a failure
/// output such a failure could not be handled in the scenario and would end the whole run.
/// </summary>
[Theory]
[InlineData(NodeType.DropConnection, "5000", "Drop connection · 5000 ms")]
[InlineData(NodeType.WaitForCommunication, "20000", "Wait for communication · max 20000 ms")]
public void Node_from_the_palette_has_its_default_and_a_failure_output(NodeType type, string value, string title)
{
var node = ScenarioNodeViewModel.CreateStructural(type, new Point(120, 80));

Assert.Equal(type, node.Type);
Assert.Equal(value, node.TransactionName);
Assert.Equal(title, node.Title);
Assert.Equal(new Point(120, 80), node.Location);
Assert.Equal(["In"], node.Input.Select(connector => connector.Title));
Assert.Equal(["Success", "Failure"], node.Output.Select(connector => connector.Title));
Assert.True(node.IsDeletable);
Assert.False(node.CanToggleMode);
Assert.False(node.IsSendNode);
Assert.False(node.IsReceiveNode);
}

/// <summary>The numbers are what files store; they must stay what the engine package defines.</summary>
[Fact]
public void Node_types_keep_their_numbers()
{
Assert.Equal(8, (int)NodeType.DropConnection);
Assert.Equal(9, (int)NodeType.WaitForCommunication);
}

/// <summary>
/// Type, value and the failure edge survive saving and loading, as a MessagePack file and as a JSON file (the
/// calls of ScenariosViewModel). The type is stored as a number.
/// </summary>
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Nodes_and_their_failure_edge_survive_saving_and_loading(bool asJson)
{
var drop = ScenarioNodeViewModel.CreateStructural(NodeType.DropConnection, new Point(5, 6));
drop.TransactionName = "1500";
var wait = ScenarioNodeViewModel.CreateStructural(NodeType.WaitForCommunication, new Point(7, 8));
wait.TransactionName = "0";
var end = new ScenarioNodeViewModel { Type = NodeType.End };
var graph = new ScenarioGraph
{
Name = "s",
Nodes = [drop.ToModel(), wait.ToModel(), end.ToModel()],
Edges =
[
new ScenarioEdge { SourceNodeId = drop.Id, TargetNodeId = wait.Id, IsFailurePath = false },
new ScenarioEdge { SourceNodeId = drop.Id, TargetNodeId = end.Id, IsFailurePath = true },
new ScenarioEdge { SourceNodeId = wait.Id, TargetNodeId = end.Id, IsFailurePath = true }
]
};

ScenarioGraph loaded;
if (asJson)
{
var json = JsonSerializer.Serialize(graph, new JsonSerializerOptions { WriteIndented = true });
Assert.Contains("\"Type\": 8", json);
Assert.Contains("\"Type\": 9", json);
loaded = JsonSerializer.Deserialize<ScenarioGraph>(json)!;
}
else
{
var cancellation = TestContext.Current.CancellationToken;
var bytes = MessagePackSerializer.Serialize(ScenarioGraphDto.From(graph), cancellationToken: cancellation);
loaded = MessagePackSerializer.Deserialize<ScenarioGraphDto>(bytes, cancellationToken: cancellation).ToModel();
}

var nodes = loaded.Nodes.Select(ScenarioNodeViewModel.FromModel).ToList();
Assert.Equal([NodeType.DropConnection, NodeType.WaitForCommunication, NodeType.End], nodes.Select(n => n.Type));
Assert.Equal(["1500", "0", null], nodes.Select(n => n.TransactionName));
Assert.Equal(["Drop connection · 1500 ms", "Wait for communication · no timeout", "END"], nodes.Select(n => n.Title));
Assert.Equal([drop.Id, wait.Id, end.Id], nodes.Select(n => n.Id));
Assert.Equal(
[(drop.Id, wait.Id, false), (drop.Id, end.Id, true), (wait.Id, end.Id, true)],
loaded.Edges.Select(edge => (edge.SourceNodeId, edge.TargetNodeId, edge.IsFailurePath)));
}
}
75 changes: 75 additions & 0 deletions EvenBetterFastSim.Tests/NodeMillisecondsViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,80 @@ public void Minimum_decides_whether_zero_is_allowed()
Assert.Contains(" 1 ", new NodeMillisecondsViewModel("t", "l", null, minimum: 1).Error);
}

/// <summary>
/// Drop connection and Wait for communication use the same editor with their own texts (GitLab
/// secsgem/evencoolerfastsim#25). 0 is a value of its own there, so it is allowed and the hint says what it does.
/// </summary>
[Theory]
[InlineData(NodeType.DropConnection, "Drop connection", "Downtime (ms)", "With 0 a new connection is set up right away")]
[InlineData(NodeType.WaitForCommunication, "Wait for communication", "Timeout (ms)", "With 0 the node waits as long as the run may take")]
public void Connection_nodes_get_the_editor_with_their_own_texts_and_zero_allowed(
NodeType type, string header, string label, string note)
{
var node = new ScenarioNodeViewModel { Type = type, TransactionName = "5 s" };
var editor = NodeMillisecondsViewModel.For(node)!;

Assert.Equal(header, editor.HeaderText);
Assert.Equal(label, editor.Label);
Assert.StartsWith("Whole number from 0 to 2147483647. ", editor.Hint);
Assert.Contains(note, editor.Hint);
Assert.Equal("5 s", editor.Text);
Assert.Equal("This is not a whole number from 0 to 2147483647. Use digits only, for example 1500.", editor.Error);
Assert.False(editor.AcceptButtonClickCommand.CanExecute(null));

editor.Text = "0";
Assert.Null(editor.Error);
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.Equal("0", node.TransactionName);
}

/// <summary>What the editor accepts, the engine reads: digits only, also for these two node types.</summary>
[Theory]
[InlineData(NodeType.DropConnection, "+5")]
[InlineData(NodeType.DropConnection, "-1")]
[InlineData(NodeType.DropConnection, "1.5")]
[InlineData(NodeType.WaitForCommunication, "+5")]
[InlineData(NodeType.WaitForCommunication, "2147483648")]
[InlineData(NodeType.WaitForCommunication, "")]
public void Connection_nodes_take_digits_only(NodeType type, string text)
{
var node = new ScenarioNodeViewModel { Type = type, TransactionName = "100" };
var editor = NodeMillisecondsViewModel.For(node)!;

editor.Text = text;
editor.AcceptButtonClickCommand.Execute(null);
editor.ApplyTo(node);

Assert.NotNull(editor.Error);
Assert.False(editor.Accepted);
Assert.Equal("100", node.TransactionName);
}

[Fact]
public void Wait_node_gets_its_editor_and_does_not_allow_zero()
{
var editor = NodeMillisecondsViewModel.For(WaitNode("0"))!;

Assert.Equal("Wait", editor.HeaderText);
Assert.Equal("Wait time (ms)", editor.Label);
Assert.Equal("Whole number from 1 to 2147483647.", editor.Hint);
Assert.NotNull(editor.Error);
}

[Theory]
[InlineData(NodeType.Start)]
[InlineData(NodeType.End)]
[InlineData(NodeType.And)]
[InlineData(NodeType.Send)]
[InlineData(NodeType.SendAndWait)]
[InlineData(NodeType.Receive)]
[InlineData(NodeType.Condition)]
public void Node_types_without_a_time_have_no_such_editor(NodeType type)
{
Assert.Null(NodeMillisecondsViewModel.For(new ScenarioNodeViewModel { Type = type }));
}

private static ScenarioNodeViewModel WaitNode(string? time) => new() { Type = NodeType.Wait, TransactionName = time };
}
114 changes: 114 additions & 0 deletions EvenBetterFastSim.Tests/PortHoldTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using EvenBetterFastSim.Services;
using SecsGemScenarioEngine.Models;
using Xunit;

namespace EvenBetterFastSim.Tests;

/// <summary>
/// While a Drop connection node keeps the port closed, the Connect button is disabled: a click would open the port in
/// the middle of the downtime (GitLab secsgem/evencoolerfastsim#25). The node reports Waiting for that time.
/// </summary>
public class PortHoldTests
{
[Fact]
public void Port_is_held_from_the_waiting_report_until_the_node_is_done()
{
var hold = new PortHold();
var run = hold.Begin();
Assert.False(hold.Active);

hold.Report(run, "drop", ScenarioNodeRunState.Running);
Assert.False(hold.Active);

hold.Report(run, "drop", ScenarioNodeRunState.Waiting);
Assert.True(hold.Active);

hold.Report(run, "drop", ScenarioNodeRunState.Succeeded);
Assert.False(hold.Active);
}

[Theory]
[InlineData(ScenarioNodeRunState.Failed)]
[InlineData(ScenarioNodeRunState.Idle)]
[InlineData(ScenarioNodeRunState.Running)]
public void Any_other_report_for_the_node_ends_the_hold(ScenarioNodeRunState state)
{
var hold = new PortHold();
var run = hold.Begin();
hold.Report(run, "drop", ScenarioNodeRunState.Waiting);

hold.Report(run, "drop", state);

Assert.False(hold.Active);
}

/// <summary>A run that is cancelled during the downtime reports nothing more for the node.</summary>
[Fact]
public void End_of_the_run_ends_the_hold()
{
var hold = new PortHold();
var run = hold.Begin();
hold.Report(run, "drop", ScenarioNodeRunState.Waiting);

hold.End(run);

Assert.False(hold.Active);
}

/// <summary>
/// A report can still be on its way to the UI thread when the run ends. It must not disable the button for good.
/// </summary>
[Fact]
public void Report_after_the_end_of_the_run_is_ignored()
{
var hold = new PortHold();
var run = hold.Begin();
hold.End(run);

hold.Report(run, "drop", ScenarioNodeRunState.Waiting);

Assert.False(hold.Active);
}

/// <summary>Several scenarios can run at once, and all share the one connection.</summary>
[Fact]
public void Each_run_and_each_node_holds_on_its_own()
{
var hold = new PortHold();
var first = hold.Begin();
var second = hold.Begin();
hold.Report(first, "drop", ScenarioNodeRunState.Waiting);
hold.Report(second, "drop", ScenarioNodeRunState.Waiting);
hold.Report(second, "other", ScenarioNodeRunState.Waiting);

hold.End(first);
Assert.True(hold.Active);

hold.Report(second, "other", ScenarioNodeRunState.Succeeded);
// The second run's "drop" is still held: the end of the first run, which held the same id, did not take it.
Assert.True(hold.Active);

hold.Report(second, "drop", ScenarioNodeRunState.Succeeded);
Assert.False(hold.Active);
}

/// <summary>
/// Two runs can hold a node with the same id: a scenario that was imported twice keeps the ids of its file, and
/// both copies can run at once. A report of one run must not end the hold of the other.
/// </summary>
[Fact]
public void Report_of_one_run_does_not_end_the_hold_of_another_run_on_the_same_node_id()
{
var hold = new PortHold();
var first = hold.Begin();
var second = hold.Begin();
hold.Report(first, "drop", ScenarioNodeRunState.Waiting);
hold.Report(second, "drop", ScenarioNodeRunState.Waiting);

hold.Report(second, "drop", ScenarioNodeRunState.Succeeded);
Assert.True(hold.Active);

hold.Report(first, "drop", ScenarioNodeRunState.Succeeded);
Assert.False(hold.Active);
}
}
5 changes: 4 additions & 1 deletion EvenBetterFastSim.Tests/ScenarioRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ public void Copy_keeps_every_property_of_an_edge()

/// <summary>
/// No library transaction is copied into nodes that carry their own, nodes whose TransactionName is a parameter
/// (Wait: ms), and nodes with a name the library does not know (the engine reports those).
/// (Wait, Drop connection, Wait for communication: ms), and nodes with a name the library does not know (the
/// engine reports those).
/// </summary>
[Fact]
public void Other_nodes_keep_what_they_have()
Expand All @@ -241,6 +242,8 @@ public void Other_nodes_keep_what_they_have()
[
new ScenarioNode { Type = NodeType.Send, TransactionName = transaction.PrimaryMessage.Name, TransactionJson = own },
new ScenarioNode { Type = NodeType.Wait, TransactionName = transaction.PrimaryMessage.Name },
new ScenarioNode { Type = NodeType.DropConnection, TransactionName = transaction.PrimaryMessage.Name },
new ScenarioNode { Type = NodeType.WaitForCommunication, TransactionName = transaction.PrimaryMessage.Name },
new ScenarioNode { Type = NodeType.Receive, TransactionName = "not in the library" },
new ScenarioNode { Type = NodeType.Send, TransactionName = " " }
]
Expand Down
Loading