test(tapes): the tapes window, the choice asked before a start, the wheels and the doors from the other cab

- HaslerTapeWindow: with nothing recorded it offers to record and turns on the
  black box and the trains' tapes; a tape of the last launch is summed up with
  its scenario and train, and is no longer followed once the window closes.
- HaslerTapes.Load: a log begun anew over the end that was read is read whole
  once its time goes back; an unreadable tape is none and told in the log; a
  file read only forward is read whole each time. Empty log lines are passed over.
- MessageBox.Choose: the answer clicked, or none on the way back or on closing.
- CabPanel: wheels turning apart from the train are said beside its speed.
- EventList: going to the other cab tells nothing of the doors, still the switches.
- WarehouseEditTests and ConsistOrientationTests put in app-wide services of
  their own: they now wait their turn with the controls that read them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maj00r
2026-11-16 15:58:31 +01:00
parent fa8ae760e1
commit f0fa2d1768
9 changed files with 335 additions and 3 deletions

View File

@@ -222,6 +222,34 @@ public class BlackBoxViewsTests
Assert.Equal(cab.Said, Controls.Peer(cab).GetHelpText());
}
[Fact]
public void Wheels_turning_apart_from_the_train_are_said_beside_its_speed()
{
var f = new BlackBoxFile().Info()
.Vehicle(Head, "EU07-005", "ROJ1")
.User(0, Head, true, 1);
// slipping for the first ten seconds, then the wheels as fast as the train, give or take the rounding
for (double t = 0; t <= 20; t += 0.5)
f.Cab(t, Head, 1150 + t * 10, 40, movement: 1, equippedHead: 0x15, equippedTail: 0x22, engine: 1,
wheelSpeed: t < 10 ? 55.4f : 40.3f);
var cab = new CabPanel
{
Recording = BlackBoxReader.Read(f.End(20).Stream()),
Strings = new DefaultBlackBoxStrings("en"),
Time = 5
};
Controls.Lay(cab, 600, 900);
Controls.Draw(cab);
Assert.Contains("40 km/h", cab.Said);
Assert.Contains("wheels 55", cab.Said);
cab.Time = 15;
Controls.Draw(cab);
Assert.Contains("40 km/h", cab.Said);
Assert.DoesNotContain("wheels", cab.Said);
}
[Fact]
public void The_cab_stopped_under_the_brakes_and_out_of_the_recording()
{

View File

@@ -1,7 +1,10 @@
using Eu07.BlackBox;
using Eu07.BlackBox.Views;
namespace StarterNG.Tests.BlackBox;
// The events are coloured with the views' brushes.
[Collection(AvaloniaControls.Name)]
public class CabDoorsTests
{
private static CabSample Sample(sbyte cab, CabDevices devices) =>
@@ -28,4 +31,19 @@ public class CabDoorsTests
Assert.Equal(sample, sample.AsTheCabSees);
}
[Fact]
public void Going_to_the_other_cab_tells_nothing_of_the_doors_and_still_tells_the_switches()
{
var f = new BlackBoxFile().Info().Vehicle(1, "EN57-1001").User(0, 1, true);
// the left doors permitted from the front cab; in the rear one the permit is still on, and the radio turned off
f.Cab(0, 1, 1000, 0, cabEnd: 1, devices: 0x0007 | (uint)(CabDevices.DoorPermitLeft | CabDevices.Radio));
f.Cab(1, 1, 1000, 0, cabEnd: -1, devices: 0x0007 | (uint)CabDevices.DoorPermitLeft);
var events = EventList.Collect(BlackBoxReader.Read(f.End(1).Stream()), new DefaultBlackBoxStrings("en"))
.Select(e => e.Text).ToList();
Assert.Contains("Radio: off", events);
Assert.DoesNotContain(events, e => e.Contains("doors permitted"));
}
}

View File

@@ -6,6 +6,8 @@ using StarterNG.Tests.Fakes;
namespace StarterNG.Tests;
// Puts in services of its own: not while the controls read theirs.
[Collection(AvaloniaControls.Name)]
public class ConsistOrientationTests
{
private readonly VehicleCatalog _catalog;

View File

@@ -0,0 +1,29 @@
namespace StarterNG.Tests.Fakes;
/// <summary>A stream read from its start to its end and no other way: no length, no seeking.</summary>
internal sealed class ForwardOnlyStream(Stream inner) : Stream
{
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count) => inner.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override void Flush() { }
protected override void Dispose(bool disposing)
{
if (disposing)
inner.Dispose();
base.Dispose(disposing);
}
}

View File

@@ -20,6 +20,7 @@ public sealed class InMemoryFileSystem : IFileSystem
private readonly HashSet<string> _executable;
private readonly Dictionary<string, string> _links;
private readonly HashSet<string> _unreadable;
private readonly HashSet<string> _unseekable;
private readonly HashSet<string> _unlistable;
private readonly Dictionary<string, DateTime> _written;
@@ -32,6 +33,7 @@ public sealed class InMemoryFileSystem : IFileSystem
_executable = new HashSet<string>(comparer);
_links = new Dictionary<string, string>(comparer);
_unreadable = new HashSet<string>(comparer);
_unseekable = new HashSet<string>(comparer);
_unlistable = new HashSet<string>(comparer);
_written = new Dictionary<string, DateTime>(comparer);
}
@@ -51,6 +53,13 @@ public sealed class InMemoryFileSystem : IFileSystem
return this;
}
/// <summary>The file opens as a stream read only forward, as a pipe or some network shares give it.</summary>
public InMemoryFileSystem OpenedForwardOnly(string path)
{
_unseekable.Add(Normalize(path));
return this;
}
public InMemoryFileSystem AllowReading(string path)
{
_unreadable.Remove(Normalize(path));
@@ -115,7 +124,11 @@ public sealed class InMemoryFileSystem : IFileSystem
public byte[] ReadAllBytes(string path) => Bytes(path);
public Stream OpenRead(string path) => new MemoryStream(Bytes(path), writable: false);
public Stream OpenRead(string path)
{
var stream = new MemoryStream(Bytes(path), writable: false);
return _unseekable.Contains(Normalize(path)) ? new ForwardOnlyStream(stream) : stream;
}
public void WriteAllText(string path, string contents) => WithFile(path, contents);

View File

@@ -6,12 +6,12 @@ namespace StarterNG.Tests;
public class HaslerTapeTests
{
private const string Header =
internal const string Header =
"Time[s] Velocity[m/s] Acceleration[m/ss] Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] " +
"BrakeForce[kN] BrakePress[MPa] PipePress[MPa] MotorCurrent[A] MCP SCP BCP LBP Direction Command CVal1 CVal2 " +
"Security Wheelslip EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg]\r\n";
private static string Line(double t, double v, double brake, double current, string command = "none",
internal static string Line(double t, double v, double brake, double current, string command = "none",
int security = 0) =>
string.Create(CultureInfo.InvariantCulture,
$"{t:0.0000} {v:0.0000} 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 {brake:0.0000} 0.5000 {current:0.0000} ") +
@@ -182,6 +182,63 @@ public class HaslerTapeTests
Assert.Equal(18, Assert.Single(tapes.Load(file, follow: true)!.Samples).SpeedKmh, 6);
}
[Fact]
public void A_log_begun_anew_over_the_end_that_was_read_is_read_whole_once_its_time_goes_back()
{
// the command is free text, here long enough that the end kept of the read part holds no time
string command = new('x', 400);
string path = TestInstallation.At("physicslog", "EU07-424.dat");
var files = new InMemoryFileSystem().WithFile(path, Header + Line(3, 36, 0, 0, command));
var tapes = new TestInstallation(files).Tapes;
var file = Assert.Single(tapes.List());
Assert.Equal(3, Assert.Single(tapes.Load(file, follow: false)!.Samples).Time, 6);
// begun anew: where the read part ended it reads as it did, but the lines added go back to the start
files.WithFile(path, Header + Line(0, 36, 0, 0, command) + Line(1, 36, 0, 0, command) + Line(2, 36, 0, 0, command));
var tape = tapes.Load(file, follow: true)!;
Assert.Equal([0.0, 1.0, 2.0], tape.Samples.Select(s => s.Time));
}
[Fact]
public void A_tape_that_cannot_be_read_is_none_and_told_and_is_read_whole_once_it_can()
{
string path = TestInstallation.At("physicslog", "EU07-424.dat");
var files = new InMemoryFileSystem().WithFile(path, Header + Line(0, 36, 0, 0));
var installation = new TestInstallation(files);
var tapes = installation.Tapes;
var file = Assert.Single(tapes.List());
tapes.Load(file, follow: false);
files.FailReading(path);
Assert.Null(tapes.Load(file, follow: true));
Assert.Contains("EU07-424.dat", installation.Log.Text);
files.AllowReading(path).WithFile(path, Header + Line(0, 72, 0, 0) + Line(1, 72, 0, 0));
Assert.All(tapes.Load(file, follow: true)!.Samples, s => Assert.Equal(72, s.SpeedKmh, 6));
}
[Fact]
public void A_file_read_only_forward_is_read_whole_each_time()
{
string path = TestInstallation.At("physicslog", "EU07-424.dat");
var files = new InMemoryFileSystem().WithFile(path, Header + Line(0, 36, 0, 0)).OpenedForwardOnly(path);
var tapes = new TestInstallation(files).Tapes;
var file = Assert.Single(tapes.List());
Assert.Single(tapes.Load(file, follow: true)!.Samples);
files.WithFile(path, Header + Line(0, 36, 0, 0) + Line(1, 36, 0, 0));
Assert.Equal(2, tapes.Load(file, follow: true)!.Samples.Count);
}
[Fact]
public void Empty_lines_in_a_log_are_passed_over()
{
var tape = HaslerTape.Parse(Header + "\r\n" + Line(0, 36, 0, 0) + " \r\n" + Line(1, 36, 0, 0));
Assert.Equal(2, tape.Samples.Count);
}
[Fact]
public void Forgetting_another_file_keeps_the_followed_tape_and_its_own_lets_it_go()
{

View File

@@ -0,0 +1,96 @@
using Avalonia.Controls;
using Avalonia.LogicalTree;
using StarterNG.Application;
using StarterNG.Domain.Tapes;
using StarterNG.Tests.Fakes;
using StarterNG.Views.Tapes;
namespace StarterNG.Tests;
/// <summary>The tapes' window over the tapes of an installation of its own, read as it is opened.</summary>
[Collection(AvaloniaControls.Name)]
public class HaslerTapeWindowTests
{
private static readonly DateTime Launched = new(2025, 12, 31, 23, 0, 0, DateTimeKind.Utc);
static HaslerTapeWindowTests() => HeadlessDrawing.Ensure();
private static AppServices Services(InMemoryFileSystem files)
{
var installation = new TestInstallation(files);
installation.Clock.Now = Launched;
return AppServices.Initialize(installation.Paths, installation.Files, installation.Clock, installation.Environment);
}
// The window reads the tapes off the UI thread and shows them on it: what it shows is waited for.
private static void Until(Func<bool> shown) =>
Assert.True(SpinWait.SpinUntil(() =>
{
Controls.Settle();
return shown();
}, TimeSpan.FromSeconds(5)));
private static Button Button(Window window, string key) =>
window.GetLogicalDescendants().OfType<Button>().Single(b => b.Content as string == App.Loc[key]);
[Fact]
public void With_nothing_recorded_it_offers_to_record_and_turns_on_the_black_box_and_the_trains_tapes()
{
var services = Services(new InMemoryFileSystem());
var window = new HaslerTapeWindow();
bool told = false;
window.LogEnabled += () => told = true;
window.Show();
try
{
var strip = (Control)Button(window, "HaslerRecordingEnable").Parent!;
Until(() => strip.IsVisible);
Button(window, "HaslerRecordingEnable").RaiseEvent(Controls.Click());
Assert.True(services.Settings.BlackBox);
Assert.True(services.Settings.PhysicsLogTrains);
Assert.False(services.Settings.PhysicsLog);
Assert.True(told);
Assert.False(strip.IsVisible);
// the simulator's own log is on as it comes: nothing more to offer
Assert.False(((Control)Button(window, "HaslerLogEnable").Parent!).IsVisible);
}
finally
{
window.Close();
}
}
[Fact]
public void A_tape_of_the_last_launch_is_summed_up_with_its_scenario_and_train()
{
string path = TestInstallation.At("physicslog", "EP07-424.dat");
var files = new InMemoryFileSystem()
.WithFile(path, HaslerTapeTests.Header + HaslerTapeTests.Line(0, 36, 0, 0) + HaslerTapeTests.Line(60, 36, 0, 0));
var services = Services(files);
services.Tapes.RecordLaunch("td.scn", TimeSpan.Zero, Array.Empty<TapeVehicle>(), "EP07-424", new[] { "EP07-424" },
"Tor doświadczalny", "EIE1531");
var window = new HaslerTapeWindow();
window.Show();
try
{
string scenario = string.Format(App.Loc["HaslerScenario"], "Tor doświadczalny");
var texts = window.GetLogicalDescendants().OfType<TextBlock>();
Until(() => texts.Any(t => t.Text?.Contains(scenario) == true));
string summary = texts.First(t => t.Text?.Contains(scenario) == true).Text!;
Assert.StartsWith("EP07-424", summary);
Assert.Contains(string.Format(App.Loc["HaslerTrain"], "EIE1531"), summary);
}
finally
{
window.Close();
}
// closed, the tape it read is no longer followed: begun anew and longer, it is read whole
files.WithFile(path, HaslerTapeTests.Header + HaslerTapeTests.Line(0, 72, 0, 0) + HaslerTapeTests.Line(1, 72, 0, 0)
+ HaslerTapeTests.Line(2, 72, 0, 0));
var tape = services.Tapes.Load(services.Tapes.List().Single(), follow: true)!;
Assert.All(tape.Samples, s => Assert.Equal(72, s.SpeedKmh, 6));
}
}

View File

@@ -0,0 +1,87 @@
using Avalonia.Controls;
using Avalonia.LogicalTree;
using StarterNG.Application;
using StarterNG.Tests.Fakes;
using StarterNG.Views;
namespace StarterNG.Tests;
/// <summary>The starter's questions over its window, answered with the mouse or not at all.</summary>
[Collection(AvaloniaControls.Name)]
public class MessageBoxTests
{
// The buttons are labelled in the starter's language.
static MessageBoxTests()
{
HeadlessDrawing.Ensure();
if (AppServices.CurrentOrNull is null)
{
var installation = new TestInstallation(new InMemoryFileSystem());
AppServices.Initialize(installation.Paths, installation.Files, installation.Clock, installation.Environment);
}
}
private static (Window Owner, Window Dialog, Task<bool?> Answer) Choose()
{
var owner = new Window();
owner.Show();
var answer = MessageBox.Choose(owner, "Zapisać zmiany przed startem?", "Niezapisane zmiany", "Zapisz", "Nie zapisuj");
Controls.Settle();
return (owner, Assert.Single(owner.OwnedWindows), answer);
}
// The answer is given once the dialog's closing has gone through the dispatcher.
private static bool? Answered(Task<bool?> answer)
{
Assert.True(SpinWait.SpinUntil(() =>
{
Controls.Settle();
return answer.IsCompleted;
}, TimeSpan.FromSeconds(5)));
return answer.Result;
}
private static Button[] Buttons(Window dialog) => dialog.GetLogicalDescendants().OfType<Button>().ToArray();
[Theory]
[InlineData(0, true)]
[InlineData(1, false)]
[InlineData(2, null)]
public void A_choice_gives_the_answer_clicked_and_the_way_back_gives_none(int clicked, bool? expected)
{
var (owner, dialog, answer) = Choose();
try
{
Assert.Equal("Niezapisane zmiany", dialog.Title);
Assert.Contains(dialog.GetLogicalDescendants().OfType<TextBlock>(), t => t.Text == "Zapisać zmiany przed startem?");
var buttons = Buttons(dialog);
Assert.Equal(["Zapisz", "Nie zapisuj", App.Loc["Cancel"]], buttons.Select(b => b.Content as string));
Assert.False(answer.IsCompleted);
buttons[clicked].RaiseEvent(Controls.Click());
Assert.Equal(expected, Answered(answer));
Assert.Empty(owner.OwnedWindows);
}
finally
{
owner.Close();
}
}
[Fact]
public void Closing_the_window_of_a_choice_picks_neither_answer()
{
var (owner, dialog, answer) = Choose();
try
{
dialog.Close();
Assert.Null(Answered(answer));
}
finally
{
owner.Close();
}
}
}

View File

@@ -6,6 +6,8 @@ namespace StarterNG.Tests;
// PresetStore keeps the disk of the first test that reaches it, a fake one as every
// composition root in the tests is built on fakes. So each test has a name to itself.
// Puts in services of its own: not while the controls read theirs.
[Collection(AvaloniaControls.Name)]
public class WarehouseEditTests
{
private const string Text =