From f0fa2d176816770855db36adf64d03e4442a2531 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 15:58:31 +0100 Subject: [PATCH] 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 --- .../BlackBox/BlackBoxViewsTests.cs | 28 ++++++ StarterNG.Tests/BlackBox/CabDoorsTests.cs | 18 ++++ StarterNG.Tests/ConsistOrientationTests.cs | 2 + StarterNG.Tests/Fakes/ForwardOnlyStream.cs | 29 ++++++ StarterNG.Tests/Fakes/InMemoryFileSystem.cs | 15 ++- StarterNG.Tests/HaslerTapeTests.cs | 61 +++++++++++- StarterNG.Tests/HaslerTapeWindowTests.cs | 96 +++++++++++++++++++ StarterNG.Tests/MessageBoxTests.cs | 87 +++++++++++++++++ StarterNG.Tests/WarehouseEditTests.cs | 2 + 9 files changed, 335 insertions(+), 3 deletions(-) create mode 100644 StarterNG.Tests/Fakes/ForwardOnlyStream.cs create mode 100644 StarterNG.Tests/HaslerTapeWindowTests.cs create mode 100644 StarterNG.Tests/MessageBoxTests.cs diff --git a/StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs b/StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs index 6102433..09659a1 100644 --- a/StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs +++ b/StarterNG.Tests/BlackBox/BlackBoxViewsTests.cs @@ -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() { diff --git a/StarterNG.Tests/BlackBox/CabDoorsTests.cs b/StarterNG.Tests/BlackBox/CabDoorsTests.cs index 7907e73..d801d0c 100644 --- a/StarterNG.Tests/BlackBox/CabDoorsTests.cs +++ b/StarterNG.Tests/BlackBox/CabDoorsTests.cs @@ -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")); + } } diff --git a/StarterNG.Tests/ConsistOrientationTests.cs b/StarterNG.Tests/ConsistOrientationTests.cs index 558cd37..505d41a 100644 --- a/StarterNG.Tests/ConsistOrientationTests.cs +++ b/StarterNG.Tests/ConsistOrientationTests.cs @@ -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; diff --git a/StarterNG.Tests/Fakes/ForwardOnlyStream.cs b/StarterNG.Tests/Fakes/ForwardOnlyStream.cs new file mode 100644 index 0000000..509afa6 --- /dev/null +++ b/StarterNG.Tests/Fakes/ForwardOnlyStream.cs @@ -0,0 +1,29 @@ +namespace StarterNG.Tests.Fakes; + +/// A stream read from its start to its end and no other way: no length, no seeking. +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); + } +} diff --git a/StarterNG.Tests/Fakes/InMemoryFileSystem.cs b/StarterNG.Tests/Fakes/InMemoryFileSystem.cs index 22c2868..a6fbba9 100644 --- a/StarterNG.Tests/Fakes/InMemoryFileSystem.cs +++ b/StarterNG.Tests/Fakes/InMemoryFileSystem.cs @@ -20,6 +20,7 @@ public sealed class InMemoryFileSystem : IFileSystem private readonly HashSet _executable; private readonly Dictionary _links; private readonly HashSet _unreadable; + private readonly HashSet _unseekable; private readonly HashSet _unlistable; private readonly Dictionary _written; @@ -32,6 +33,7 @@ public sealed class InMemoryFileSystem : IFileSystem _executable = new HashSet(comparer); _links = new Dictionary(comparer); _unreadable = new HashSet(comparer); + _unseekable = new HashSet(comparer); _unlistable = new HashSet(comparer); _written = new Dictionary(comparer); } @@ -51,6 +53,13 @@ public sealed class InMemoryFileSystem : IFileSystem return this; } + /// The file opens as a stream read only forward, as a pipe or some network shares give it. + 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); diff --git a/StarterNG.Tests/HaslerTapeTests.cs b/StarterNG.Tests/HaslerTapeTests.cs index 43bc9c3..18108eb 100644 --- a/StarterNG.Tests/HaslerTapeTests.cs +++ b/StarterNG.Tests/HaslerTapeTests.cs @@ -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() { diff --git a/StarterNG.Tests/HaslerTapeWindowTests.cs b/StarterNG.Tests/HaslerTapeWindowTests.cs new file mode 100644 index 0000000..ac93640 --- /dev/null +++ b/StarterNG.Tests/HaslerTapeWindowTests.cs @@ -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; + +/// The tapes' window over the tapes of an installation of its own, read as it is opened. +[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 shown) => + Assert.True(SpinWait.SpinUntil(() => + { + Controls.Settle(); + return shown(); + }, TimeSpan.FromSeconds(5))); + + private static Button Button(Window window, string key) => + window.GetLogicalDescendants().OfType