From f3f66d78dcdd7a2f06ce4854af242cc617364ccb Mon Sep 17 00:00:00 2001
From: elgegeto <341694139+elgegeto@users.noreply.github.com>
Date: Tue, 10 Nov 2026 15:58:00 -0300
Subject: [PATCH 1/4] fix: validate nullable speed inputs across settings
editors (#61)
Clearing a numeric input no longer converts an empty string to int/double. Validate only enabled random ranges and block add, save, apply, and start commands while speed settings are invalid.
---
.../ViewModels/Dialogs/AddTorrentViewModel.cs | 9 +-
.../ViewModels/Dialogs/SettingsViewModel.cs | 11 +-
.../ViewModels/TorrentItemViewModel.cs | 23 +++-
.../ViewModels/TorrentSettingsViewModel.cs | 119 ++++++++++++++----
4 files changed, 134 insertions(+), 28 deletions(-)
diff --git a/src/RatioMaster.App/ViewModels/Dialogs/AddTorrentViewModel.cs b/src/RatioMaster.App/ViewModels/Dialogs/AddTorrentViewModel.cs
index 2f1b7ab..7b65390 100644
--- a/src/RatioMaster.App/ViewModels/Dialogs/AddTorrentViewModel.cs
+++ b/src/RatioMaster.App/ViewModels/Dialogs/AddTorrentViewModel.cs
@@ -52,6 +52,13 @@ public AddTorrentViewModel(
this.files = files;
this.defaults = defaults;
this.startImmediately = startImmediately;
+ this.Settings.PropertyChanged += (_, args) =>
+ {
+ if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
+ {
+ this.AddCommand.NotifyCanExecuteChanged();
+ }
+ };
}
public TorrentSettingsViewModel Settings { get; }
@@ -71,7 +78,7 @@ public AddTorrentViewModel(
_ => string.Format(CultureInfo.CurrentCulture, Strings.AddManyTorrents, this.Torrents.Count),
};
- public bool CanAdd => this.Torrents.Count > 0;
+ public bool CanAdd => this.Torrents.Count > 0 && this.Settings.IsSpeedSettingsValid;
///
/// Gets the reason the chosen tracker cannot be announced to (no tracker, udp://, malformed), or null when it is fine.
diff --git a/src/RatioMaster.App/ViewModels/Dialogs/SettingsViewModel.cs b/src/RatioMaster.App/ViewModels/Dialogs/SettingsViewModel.cs
index c3075b9..8c726d8 100644
--- a/src/RatioMaster.App/ViewModels/Dialogs/SettingsViewModel.cs
+++ b/src/RatioMaster.App/ViewModels/Dialogs/SettingsViewModel.cs
@@ -55,6 +55,13 @@ public SettingsViewModel(AppSettings settings, TorrentSettingsViewModel defaults
this.Title = Strings.Settings;
this.original = settings;
this.Defaults = defaults;
+ this.Defaults.PropertyChanged += (_, args) =>
+ {
+ if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
+ {
+ this.SaveCommand.NotifyCanExecuteChanged();
+ }
+ };
this.theme = settings.Theme;
this.use24HourTime = settings.Use24HourTime;
this.checkForUpdatesOnStartup = settings.CheckForUpdatesOnStartup;
@@ -94,7 +101,9 @@ public SettingsViewModel(AppSettings settings, TorrentSettingsViewModel defaults
public string ClientSummary => string.Format(CultureInfo.CurrentCulture, Strings.ClientsSummary, this.ClientCount);
- [RelayCommand]
+ public bool CanSave => this.Defaults.IsSpeedSettingsValid;
+
+ [RelayCommand(CanExecute = nameof(CanSave))]
private void Save() => this.Close(this.original with
{
Theme = this.Theme,
diff --git a/src/RatioMaster.App/ViewModels/TorrentItemViewModel.cs b/src/RatioMaster.App/ViewModels/TorrentItemViewModel.cs
index c855984..bbe8c07 100644
--- a/src/RatioMaster.App/ViewModels/TorrentItemViewModel.cs
+++ b/src/RatioMaster.App/ViewModels/TorrentItemViewModel.cs
@@ -123,6 +123,14 @@ public TorrentItemViewModel(
this.Log = new LogViewModel(displayName, files, clipboard);
this.Tracker = new TrackerHistoryViewModel();
this.Settings = new TorrentSettingsViewModel(factory.Catalog, factory, settings);
+ this.Settings.PropertyChanged += (_, args) =>
+ {
+ if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
+ {
+ this.StartCommand.NotifyCanExecuteChanged();
+ this.ApplySettingsCommand.NotifyCanExecuteChanged();
+ }
+ };
}
/// Raised when the row wants to be removed from the list.
@@ -154,7 +162,7 @@ public TorrentItemViewModel(
public bool IsRunning => this.State is TorrentSessionState.Starting or TorrentSessionState.Downloading
or TorrentSessionState.Seeding or TorrentSessionState.Updating;
- public bool CanStart => !this.IsRunning && this.State != TorrentSessionState.Stopping;
+ public bool CanStart => !this.IsRunning && this.State != TorrentSessionState.Stopping && this.Settings.IsSpeedSettingsValid;
public string StatusText => this.State switch
{
@@ -234,6 +242,11 @@ public void Refresh()
[RelayCommand(CanExecute = nameof(CanStart))]
public void Start()
{
+ if (!this.CanStart)
+ {
+ return;
+ }
+
// A session that stopped on its own (a stop condition, a tracker error) since the last refresh may have got
// further than that refresh saw, up to complete: carry on from where it really got to.
if (this.session is { } previous)
@@ -280,7 +293,9 @@ public async Task OpenContainingFolderAsync()
public void Remove() => this.RemoveRequested?.Invoke(this, EventArgs.Empty);
/// Pushes the editor's values into the settings, and into a running session where allowed.
- [RelayCommand]
+ public bool CanApplySettings => this.Settings.IsSpeedSettingsValid;
+
+ [RelayCommand(CanExecute = nameof(CanApplySettings))]
public void ApplySettings()
{
this.settings = this.Settings.ToSettings();
@@ -330,13 +345,13 @@ public void SetSpeeds(double? uploadKb, double? downloadKb)
if (uploadKb is { } up)
{
settings = settings with { UploadRateBytes = (long)(up * 1024) };
- this.Settings.UploadKb = up;
+ this.Settings.UploadKb = (decimal)up;
}
if (downloadKb is { } down)
{
settings = settings with { DownloadRateBytes = (long)(down * 1024) };
- this.Settings.DownloadKb = down;
+ this.Settings.DownloadKb = (decimal)down;
}
this.settings = settings;
diff --git a/src/RatioMaster.App/ViewModels/TorrentSettingsViewModel.cs b/src/RatioMaster.App/ViewModels/TorrentSettingsViewModel.cs
index a84bbef..89a7fe9 100644
--- a/src/RatioMaster.App/ViewModels/TorrentSettingsViewModel.cs
+++ b/src/RatioMaster.App/ViewModels/TorrentSettingsViewModel.cs
@@ -22,6 +22,7 @@ public sealed partial class TorrentSettingsViewModel : ViewModelBase
private readonly ClientProfileCatalog catalog;
private readonly TorrentSessionFactory? factory;
private bool suppressVersionReset;
+ private const decimal MaxSpeedKb = 1_000_000_000m;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ClientName))]
@@ -54,46 +55,46 @@ public sealed partial class TorrentSettingsViewModel : ViewModelBase
private bool isGeneratingIdentity;
[ObservableProperty]
- private double uploadKb = 60;
+ private decimal? uploadKb = 60m;
[ObservableProperty]
- private double downloadKb = 30;
+ private decimal? downloadKb = 30m;
[ObservableProperty]
private bool uploadRandomEnabled = true;
[ObservableProperty]
- private int uploadRandomMinKb = 1;
+ private decimal? uploadRandomMinKb = 1m;
[ObservableProperty]
- private int uploadRandomMaxKb = 10;
+ private decimal? uploadRandomMaxKb = 10m;
[ObservableProperty]
private bool downloadRandomEnabled = true;
[ObservableProperty]
- private int downloadRandomMinKb = 1;
+ private decimal? downloadRandomMinKb = 1m;
[ObservableProperty]
- private int downloadRandomMaxKb = 10;
+ private decimal? downloadRandomMaxKb = 10m;
[ObservableProperty]
private bool nextUpdateRandomUpload;
[ObservableProperty]
- private int nextUpdateUploadMinKb = 10;
+ private decimal? nextUpdateUploadMinKb = 10m;
[ObservableProperty]
- private int nextUpdateUploadMaxKb = 50;
+ private decimal? nextUpdateUploadMaxKb = 50m;
[ObservableProperty]
private bool nextUpdateRandomDownload;
[ObservableProperty]
- private int nextUpdateDownloadMinKb = 10;
+ private decimal? nextUpdateDownloadMinKb = 10m;
[ObservableProperty]
- private int nextUpdateDownloadMaxKb = 100;
+ private decimal? nextUpdateDownloadMaxKb = 100m;
[ObservableProperty]
private double finishedPercent;
@@ -183,6 +184,44 @@ public TorrentSettingsViewModel(ClientProfileCatalog catalog, TorrentSessionFact
///
public bool CanEditPortAndPeers => !this.UseAutomaticIdentity && !this.IsLocked;
+ ///
+ /// Cleared numeric fields have a null value. Disabled random ranges are ignored until enabled.
+ ///
+ public bool IsSpeedSettingsValid =>
+ IsValidSpeed(this.UploadKb) &&
+ IsValidSpeed(this.DownloadKb) &&
+ (!this.UploadRandomEnabled || IsValidRange(this.UploadRandomMinKb, this.UploadRandomMaxKb)) &&
+ (!this.DownloadRandomEnabled || IsValidRange(this.DownloadRandomMinKb, this.DownloadRandomMaxKb)) &&
+ (!this.NextUpdateRandomUpload || IsValidRange(this.NextUpdateUploadMinKb, this.NextUpdateUploadMaxKb)) &&
+ (!this.NextUpdateRandomDownload || IsValidRange(this.NextUpdateDownloadMinKb, this.NextUpdateDownloadMaxKb));
+
+ /// Reports friendly input errors without exposing .NET conversion exceptions in the form.
+ public string? SpeedValidationMessage
+ {
+ get
+ {
+ if (this.IsSpeedSettingsValid)
+ {
+ return null;
+ }
+
+ if (!IsValidSpeed(this.UploadKb) || !IsValidSpeed(this.DownloadKb))
+ {
+ return "Enter a non-negative upload and download speed (kB/s).";
+ }
+
+ if ((this.UploadRandomEnabled && (!IsValidRangeNumber(this.UploadRandomMinKb) || !IsValidRangeNumber(this.UploadRandomMaxKb))) ||
+ (this.DownloadRandomEnabled && (!IsValidRangeNumber(this.DownloadRandomMinKb) || !IsValidRangeNumber(this.DownloadRandomMaxKb))) ||
+ (this.NextUpdateRandomUpload && (!IsValidRangeNumber(this.NextUpdateUploadMinKb) || !IsValidRangeNumber(this.NextUpdateUploadMaxKb))) ||
+ (this.NextUpdateRandomDownload && (!IsValidRangeNumber(this.NextUpdateDownloadMinKb) || !IsValidRangeNumber(this.NextUpdateDownloadMaxKb))))
+ {
+ return "Enter whole, non-negative min/max speeds (kB/s) for each enabled option.";
+ }
+
+ return "The minimum speed must not exceed the maximum speed (kB/s).";
+ }
+ }
+
public bool IsProxyConfigured => this.ProxyType != ProxyType.None;
public bool IsStopValueVisible => this.StopType != StopConditionType.Never;
@@ -212,8 +251,8 @@ public void LoadFrom(TorrentSettings settings)
this.Port = settings.CustomPort ?? string.Empty;
this.NumWant = settings.CustomNumWant ?? string.Empty;
- this.UploadKb = settings.UploadRateBytes / 1024d;
- this.DownloadKb = settings.DownloadRateBytes / 1024d;
+ this.UploadKb = settings.UploadRateBytes / 1024m;
+ this.DownloadKb = settings.DownloadRateBytes / 1024m;
this.UploadRandomEnabled = settings.UploadRandomEnabled;
this.UploadRandomMinKb = settings.UploadRandomMinKb;
this.UploadRandomMaxKb = settings.UploadRandomMaxKb;
@@ -256,20 +295,20 @@ public void LoadFrom(TorrentSettings settings)
CustomKey = NullIfBlank(this.Key),
CustomPort = NullIfBlank(this.Port),
CustomNumWant = NullIfBlank(this.NumWant),
- UploadRateBytes = (long)Math.Max(0, this.UploadKb * 1024),
- DownloadRateBytes = (long)Math.Max(0, this.DownloadKb * 1024),
+ UploadRateBytes = (long)(Math.Clamp(this.UploadKb ?? 0m, 0m, MaxSpeedKb) * 1024m),
+ DownloadRateBytes = (long)(Math.Clamp(this.DownloadKb ?? 0m, 0m, MaxSpeedKb) * 1024m),
UploadRandomEnabled = this.UploadRandomEnabled,
- UploadRandomMinKb = this.UploadRandomMinKb,
- UploadRandomMaxKb = this.UploadRandomMaxKb,
+ UploadRandomMinKb = AsRangeInteger(this.UploadRandomMinKb),
+ UploadRandomMaxKb = AsRangeInteger(this.UploadRandomMaxKb),
DownloadRandomEnabled = this.DownloadRandomEnabled,
- DownloadRandomMinKb = this.DownloadRandomMinKb,
- DownloadRandomMaxKb = this.DownloadRandomMaxKb,
+ DownloadRandomMinKb = AsRangeInteger(this.DownloadRandomMinKb),
+ DownloadRandomMaxKb = AsRangeInteger(this.DownloadRandomMaxKb),
NextUpdateRandomUpload = this.NextUpdateRandomUpload,
- NextUpdateUploadMinKb = this.NextUpdateUploadMinKb,
- NextUpdateUploadMaxKb = this.NextUpdateUploadMaxKb,
+ NextUpdateUploadMinKb = AsRangeInteger(this.NextUpdateUploadMinKb),
+ NextUpdateUploadMaxKb = AsRangeInteger(this.NextUpdateUploadMaxKb),
NextUpdateRandomDownload = this.NextUpdateRandomDownload,
- NextUpdateDownloadMinKb = this.NextUpdateDownloadMinKb,
- NextUpdateDownloadMaxKb = this.NextUpdateDownloadMaxKb,
+ NextUpdateDownloadMinKb = AsRangeInteger(this.NextUpdateDownloadMinKb),
+ NextUpdateDownloadMaxKb = AsRangeInteger(this.NextUpdateDownloadMaxKb),
FinishedPercent = Math.Clamp(this.FinishedPercent, 0, 100),
IntervalSeconds = this.IntervalSeconds,
Stop = new StopCondition { Type = this.StopType, Value = this.StopValue },
@@ -289,6 +328,42 @@ public void LoadFrom(TorrentSettings settings)
},
};
+ private static bool IsValidSpeed(decimal? value) => value is >= 0 and <= MaxSpeedKb;
+
+ private static bool IsValidRangeNumber(decimal? value) =>
+ value.HasValue &&
+ value.Value >= 0 &&
+ value.Value <= int.MaxValue &&
+ decimal.Truncate(value.Value) == value.Value;
+
+ private static bool IsValidRange(decimal? min, decimal? max) =>
+ IsValidRangeNumber(min) && IsValidRangeNumber(max) && min!.Value <= max!.Value;
+
+ // Disabled ranges may have unfinished values; never let them overflow during serialization.
+ private static int AsRangeInteger(decimal? value) =>
+ (int)Math.Clamp(decimal.Truncate(value ?? 0m), 0m, int.MaxValue);
+
+ private void NotifySpeedValidationChanged()
+ {
+ this.OnPropertyChanged(nameof(this.IsSpeedSettingsValid));
+ this.OnPropertyChanged(nameof(this.SpeedValidationMessage));
+ }
+
+ partial void OnUploadKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnDownloadKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnUploadRandomEnabledChanged(bool value) => this.NotifySpeedValidationChanged();
+ partial void OnUploadRandomMinKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnUploadRandomMaxKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnDownloadRandomEnabledChanged(bool value) => this.NotifySpeedValidationChanged();
+ partial void OnDownloadRandomMinKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnDownloadRandomMaxKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateRandomUploadChanged(bool value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateUploadMinKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateUploadMaxKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateRandomDownloadChanged(bool value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateDownloadMinKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+ partial void OnNextUpdateDownloadMaxKbChanged(decimal? value) => this.NotifySpeedValidationChanged();
+
private static double DefaultStopValue(StopConditionType type) => type switch
{
StopConditionType.AfterSeconds => 3600,
From 4eecea88d8baae9a928b06ee22e77893e145a341 Mon Sep 17 00:00:00 2001
From: elgegeto <341694139+elgegeto@users.noreply.github.com>
Date: Tue, 10 Nov 2026 16:00:08 -0300
Subject: [PATCH 2/4] feat: use numeric spinners and add speed validation
regression tests (#61)
Replace speed text boxes with nullable decimal NumericUpDown controls. Explain kB/s random ranges and surface friendly validation messages. Test cleared values, reversed ranges, disabled ranges, notifications, and serialization.
---
.../RatioMaster.App.Tests.csproj | 18 +++
.../TorrentSettingsViewModelTests.cs | 116 ++++++++++++++++++
.../Views/Controls/TorrentSettingsView.axaml | 44 ++++---
src/RatioMaster.slnx | 1 +
4 files changed, 160 insertions(+), 19 deletions(-)
create mode 100644 src/RatioMaster.App.Tests/RatioMaster.App.Tests.csproj
create mode 100644 src/RatioMaster.App.Tests/ViewModels/TorrentSettingsViewModelTests.cs
diff --git a/src/RatioMaster.App.Tests/RatioMaster.App.Tests.csproj b/src/RatioMaster.App.Tests/RatioMaster.App.Tests.csproj
new file mode 100644
index 0000000..32453b2
--- /dev/null
+++ b/src/RatioMaster.App.Tests/RatioMaster.App.Tests.csproj
@@ -0,0 +1,18 @@
+
+
+ RatioMaster.App.Tests
+ false
+ true
+ Exe
+ false
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/RatioMaster.App.Tests/ViewModels/TorrentSettingsViewModelTests.cs b/src/RatioMaster.App.Tests/ViewModels/TorrentSettingsViewModelTests.cs
new file mode 100644
index 0000000..2c92b49
--- /dev/null
+++ b/src/RatioMaster.App.Tests/ViewModels/TorrentSettingsViewModelTests.cs
@@ -0,0 +1,116 @@
+namespace RatioMaster.App.Tests.ViewModels
+{
+ using RatioMaster.App.ViewModels;
+ using RatioMaster.Core.Clients;
+ using Xunit;
+
+ public sealed class TorrentSettingsViewModelTests
+ {
+ [Fact]
+ public void ClearingABaseSpeedPreventsSavingUntilCorrected()
+ {
+ var editor = CreateEditor();
+ Assert.True(editor.IsSpeedSettingsValid);
+
+ editor.UploadKb = null;
+ Assert.False(editor.IsSpeedSettingsValid);
+ Assert.NotNull(editor.SpeedValidationMessage);
+
+ editor.UploadKb = 0m;
+ Assert.True(editor.IsSpeedSettingsValid);
+ Assert.Null(editor.SpeedValidationMessage);
+ }
+
+ [Fact]
+ public void ClearingAnEnabledRandomRangePreventsSaving()
+ {
+ var editor = CreateEditor();
+ editor.NextUpdateRandomUpload = true;
+ editor.NextUpdateUploadMinKb = null;
+
+ Assert.False(editor.IsSpeedSettingsValid);
+
+ editor.NextUpdateUploadMinKb = 10m;
+ Assert.True(editor.IsSpeedSettingsValid);
+ }
+
+ [Fact]
+ public void ReversedRangeIsRejectedButIgnoredWhenDisabled()
+ {
+ var editor = CreateEditor();
+ editor.NextUpdateRandomDownload = true;
+ editor.NextUpdateDownloadMinKb = 70m;
+ editor.NextUpdateDownloadMaxKb = 60m;
+
+ Assert.False(editor.IsSpeedSettingsValid);
+ Assert.Contains("minimum", editor.SpeedValidationMessage!);
+
+ editor.NextUpdateRandomDownload = false;
+ Assert.True(editor.IsSpeedSettingsValid);
+ }
+
+ [Fact]
+ public void NonIntegralRandomRangeIsRejected()
+ {
+ var editor = CreateEditor();
+ editor.UploadRandomMinKb = 1.5m;
+
+ Assert.False(editor.IsSpeedSettingsValid);
+
+ editor.UploadRandomMinKb = 2m;
+ Assert.True(editor.IsSpeedSettingsValid);
+ }
+
+ [Fact]
+ public void ChangedValuesAreSerializedWithoutLosingPrecision()
+ {
+ var editor = CreateEditor();
+ editor.UploadKb = 12.5m;
+ editor.DownloadKb = 3.25m;
+ editor.NextUpdateRandomUpload = true;
+ editor.NextUpdateUploadMinKb = 15m;
+ editor.NextUpdateUploadMaxKb = 30m;
+
+ var settings = editor.ToSettings();
+
+ Assert.Equal(12800, settings.UploadRateBytes);
+ Assert.Equal(3328, settings.DownloadRateBytes);
+ Assert.Equal(15, settings.NextUpdateUploadMinKb);
+ Assert.Equal(30, settings.NextUpdateUploadMaxKb);
+ }
+
+ [Fact]
+ public void ModelDoesNotOverflowWhenADisabledRangeContainsAnOutOfRangeValue()
+ {
+ var editor = CreateEditor();
+ editor.UploadRandomEnabled = false;
+ editor.UploadRandomMaxKb = decimal.MaxValue;
+
+ Assert.True(editor.IsSpeedSettingsValid);
+ Assert.Equal(int.MaxValue, editor.ToSettings().UploadRandomMaxKb);
+ }
+
+ [Fact]
+ public void RangeValidationRaisesPropertyChangeNotifications()
+ {
+ var editor = CreateEditor();
+ var raised = 0;
+ editor.PropertyChanged += (_, args) =>
+ {
+ if (args.PropertyName == nameof(TorrentSettingsViewModel.IsSpeedSettingsValid))
+ {
+ raised++;
+ }
+ };
+
+ editor.UploadRandomMaxKb = 0m;
+ Assert.False(editor.IsSpeedSettingsValid);
+
+ editor.UploadRandomMaxKb = 10m;
+ Assert.True(editor.IsSpeedSettingsValid);
+ Assert.Equal(2, raised);
+ }
+
+ private static TorrentSettingsViewModel CreateEditor() => new(ClientProfileCatalog.Load());
+ }
+}
diff --git a/src/RatioMaster.App/Views/Controls/TorrentSettingsView.axaml b/src/RatioMaster.App/Views/Controls/TorrentSettingsView.axaml
index 03ab7d0..c9964ea 100644
--- a/src/RatioMaster.App/Views/Controls/TorrentSettingsView.axaml
+++ b/src/RatioMaster.App/Views/Controls/TorrentSettingsView.axaml
@@ -13,7 +13,7 @@
-