|
| 1 | +namespace ThreadPilot.Core.Tests |
| 2 | +{ |
| 3 | + using System.Reflection; |
| 4 | + using System.Text.RegularExpressions; |
| 5 | + using System.Xml.Linq; |
| 6 | + using Moq; |
| 7 | + using ThreadPilot.Services; |
| 8 | + using ThreadPilot.ViewModels; |
| 9 | + |
| 10 | + public sealed class MasksViewModelTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void MasksView_SubtitleClarifiesPerProcessUse() |
| 14 | + { |
| 15 | + var document = LoadMasksViewXaml(); |
| 16 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 17 | + |
| 18 | + Assert.Contains("per-process use", serialized, StringComparison.Ordinal); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void MasksView_ContainsEditingOnlyClarification() |
| 23 | + { |
| 24 | + var document = LoadMasksViewXaml(); |
| 25 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 26 | + |
| 27 | + Assert.Contains("does not change CPU affinity", serialized, StringComparison.Ordinal); |
| 28 | + Assert.Contains("until you apply it to a process", serialized, StringComparison.Ordinal); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void MasksView_DefaultPresetTooltipWarnsNoAutoApply() |
| 33 | + { |
| 34 | + var document = LoadMasksViewXaml(); |
| 35 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 36 | + |
| 37 | + Assert.Contains("does not apply CPU affinity automatically", serialized, StringComparison.Ordinal); |
| 38 | + Assert.Contains("Pre-selected when ThreadPilot", serialized, StringComparison.Ordinal); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void MasksView_NoGlobalAffinityControls() |
| 43 | + { |
| 44 | + var document = LoadMasksViewXaml(); |
| 45 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 46 | + |
| 47 | + Assert.DoesNotContain("apply globally", serialized, StringComparison.OrdinalIgnoreCase); |
| 48 | + Assert.DoesNotContain("global affinity", serialized, StringComparison.OrdinalIgnoreCase); |
| 49 | + Assert.DoesNotContain("disable SMT", serialized, StringComparison.OrdinalIgnoreCase); |
| 50 | + Assert.DoesNotContain("HyperThreading", serialized, StringComparison.OrdinalIgnoreCase); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void MasksView_ToggleCpuTextClarifiesNoRunningProcessImpact() |
| 55 | + { |
| 56 | + var document = LoadMasksViewXaml(); |
| 57 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 58 | + |
| 59 | + Assert.Contains("do not affect running processes", serialized, StringComparison.Ordinal); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void MasksView_DeleteWarningRefersToProcessesAndRules_NotGlobal() |
| 64 | + { |
| 65 | + var document = LoadMasksViewXaml(); |
| 66 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 67 | + |
| 68 | + Assert.DoesNotContain("all processes", serialized, StringComparison.OrdinalIgnoreCase); |
| 69 | + Assert.DoesNotContain("system-wide", serialized, StringComparison.OrdinalIgnoreCase); |
| 70 | + Assert.DoesNotContain("globally", serialized, StringComparison.OrdinalIgnoreCase); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void MasksViewModel_ExposesOnlyCrudCommands() |
| 75 | + { |
| 76 | + var commandNames = typeof(MasksViewModel) |
| 77 | + .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) |
| 78 | + .Where(p => p.PropertyType.Name.Contains("ommand", StringComparison.OrdinalIgnoreCase)) |
| 79 | + .Select(p => p.Name) |
| 80 | + .ToList(); |
| 81 | + |
| 82 | + Assert.Contains("CreateMaskCommand", commandNames); |
| 83 | + Assert.Contains("DeleteMaskCommand", commandNames); |
| 84 | + Assert.Contains("DuplicateMaskCommand", commandNames); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void MasksViewModel_HasNoAffinityApplyDependencies() |
| 89 | + { |
| 90 | + var constructorDependencies = typeof(MasksViewModel) |
| 91 | + .GetConstructors() |
| 92 | + .SelectMany(c => c.GetParameters()) |
| 93 | + .Select(p => p.ParameterType.FullName ?? p.ParameterType.Name) |
| 94 | + .ToList(); |
| 95 | + |
| 96 | + Assert.DoesNotContain("AffinityApplyService", constructorDependencies, StringComparer.OrdinalIgnoreCase); |
| 97 | + Assert.DoesNotContain("ProcessAffinityApplyCoordinator", constructorDependencies, StringComparer.OrdinalIgnoreCase); |
| 98 | + Assert.DoesNotContain("IProcessService", constructorDependencies, StringComparer.OrdinalIgnoreCase); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void MasksViewModel_HasNoAffinityApplyMethods() |
| 103 | + { |
| 104 | + var methods = typeof(MasksViewModel) |
| 105 | + .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) |
| 106 | + .Select(m => m.Name) |
| 107 | + .ToList(); |
| 108 | + |
| 109 | + Assert.DoesNotContain(methods, m => Regex.IsMatch(m, "Apply.*Affinity", RegexOptions.IgnoreCase)); |
| 110 | + Assert.DoesNotContain(methods, m => Regex.IsMatch(m, "Set.*Affinity", RegexOptions.IgnoreCase)); |
| 111 | + Assert.DoesNotContain(methods, m => Regex.IsMatch(m, "Apply.*Cpu.*Selection", RegexOptions.IgnoreCase)); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void MasksView_AllCoresProtectedDefaultInText() |
| 116 | + { |
| 117 | + var document = LoadMasksViewXaml(); |
| 118 | + var serialized = document.ToString(SaveOptions.DisableFormatting); |
| 119 | + |
| 120 | + Assert.Contains("All Cores is the protected default preset", serialized, StringComparison.Ordinal); |
| 121 | + } |
| 122 | + |
| 123 | + private static XDocument LoadMasksViewXaml() |
| 124 | + { |
| 125 | + var repoRoot = GetRepositoryRoot(); |
| 126 | + var path = Path.Combine(repoRoot, "Views", "MasksView.xaml"); |
| 127 | + return XDocument.Load(path, LoadOptions.PreserveWhitespace); |
| 128 | + } |
| 129 | + |
| 130 | + private static string GetRepositoryRoot() |
| 131 | + { |
| 132 | + var currentDir = AppContext.BaseDirectory; |
| 133 | + var dir = new DirectoryInfo(currentDir); |
| 134 | + while (dir != null && !File.Exists(Path.Combine(dir.FullName, "ThreadPilot_1.sln"))) |
| 135 | + { |
| 136 | + dir = dir.Parent; |
| 137 | + } |
| 138 | + |
| 139 | + if (dir == null) |
| 140 | + { |
| 141 | + throw new InvalidOperationException("Could not find repository root from " + currentDir); |
| 142 | + } |
| 143 | + |
| 144 | + return dir.FullName; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments