using System.Collections.Specialized;
using SpdUp.Core;
using SpdUp.ViewModels;
namespace SpdUp.Tests;
///
/// Tests for DebugLogViewModel.
/// Uses a helper to run on STA thread for WPF Dispatcher compatibility.
///
public class DebugLogViewModelTests
{
private static void RunOnSta(Action action)
{
Exception? caught = null;
var thread = new Thread(() =>
{
try { action(); }
catch (Exception ex) { caught = ex; }
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
if (caught is not null)
throw new Xunit.Sdk.XunitException($"STA test failed: {caught.Message}", caught);
}
[Fact]
public void Initial_LogLines_IsEmpty()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
Assert.Empty(vm.LogLines);
});
}
[Fact]
public void AutoScroll_DefaultTrue()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
Assert.True(vm.AutoScroll);
});
}
[Fact]
public void AutoScroll_Settable()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
vm.AutoScroll = false;
Assert.False(vm.AutoScroll);
});
}
[Fact]
public void FilterText_DefaultEmpty()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
Assert.Equal(string.Empty, vm.FilterText);
});
}
[Fact]
public void FilterText_RaisesPropertyChanged()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
var changed = new List();
vm.PropertyChanged += (_, e) => changed.Add(e.PropertyName!);
vm.FilterText = "error";
Assert.Contains("FilterText", changed);
Assert.Contains("FilteredLogLines", changed);
});
}
[Fact]
public void ClearCommand_ClearsLogLines()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
vm.LogLines.Add("test line 1");
vm.LogLines.Add("test line 2");
vm.ClearCommand.Execute(null);
Assert.Empty(vm.LogLines);
});
}
[Fact]
public void FilteredLogLines_NoFilter_ReturnsAll()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
vm.LogLines.Add("[INFO] started");
vm.LogLines.Add("[ERROR] failed");
vm.LogLines.Add("[WARN] low memory");
var filtered = vm.FilteredLogLines.ToList();
Assert.Equal(3, filtered.Count);
});
}
[Fact]
public void FilteredLogLines_WithFilter_ReturnsMatches()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
vm.LogLines.Add("[INFO] started");
vm.LogLines.Add("[ERROR] something failed");
vm.LogLines.Add("[WARN] low memory");
vm.FilterText = "error";
var filtered = vm.FilteredLogLines.ToList();
Assert.Single(filtered);
Assert.Contains("ERROR", filtered[0]);
});
}
[Fact]
public void FilteredLogLines_CaseInsensitive()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
vm.LogLines.Add("[INFO] Test Message");
vm.FilterText = "test message";
Assert.Single(vm.FilteredLogLines);
});
}
[Fact]
public void Commands_AreNotNull()
{
RunOnSta(() =>
{
var vm = new DebugLogViewModel();
Assert.NotNull(vm.ClearCommand);
Assert.NotNull(vm.CopyAllCommand);
});
}
}