|
| 1 | +namespace ThreadPilot.Core.Tests |
| 2 | +{ |
| 3 | + using System.Security.Cryptography; |
| 4 | + using System.Text; |
| 5 | + using Microsoft.Extensions.Logging.Abstractions; |
| 6 | + using Moq; |
| 7 | + using ThreadPilot.Services; |
| 8 | + using ThreadPilot.Services.Abstractions; |
| 9 | + |
| 10 | + public sealed class BundledPowerPlanAssetTests |
| 11 | + { |
| 12 | + private static readonly string[] NewPlanFiles = |
| 13 | + [ |
| 14 | + "0 Synez_Public_Power.pow", |
| 15 | + "arsenza low latency (INTEL FIX THREAD-DIRECTOR).pow", |
| 16 | + "arsenza low latency.pow", |
| 17 | + "AutoOS.pow", |
| 18 | + "BEYOND-PERFORMANCE-AMD+INTEL.pow", |
| 19 | + "Bitsum Highest Performance.pow", |
| 20 | + "cactusOS.pow", |
| 21 | + "FPSHEAVEN2026.pow", |
| 22 | + "GALA's ultimate performance(AMD).pow", |
| 23 | + "Gavot Performance.pow", |
| 24 | + "GTweaks Power Plan V3.pow", |
| 25 | + "imribiy2026.pow", |
| 26 | + "IrisFixed.pow", |
| 27 | + "J o k r O S P o w e r P l a n.pow", |
| 28 | + "Jackpot2026.pow", |
| 29 | + "Kizzimo's Extreme Low Latency.pow", |
| 30 | + "KSOS11.pow", |
| 31 | + "melody LowestLatency.pow", |
| 32 | + "Microsoft High performance.pow", |
| 33 | + "Microsoft Ultimate Performance.pow", |
| 34 | + "Mitstas IDLE ENABLED.pow", |
| 35 | + "n1kobg GPU_Booster_Power_Plan.pow", |
| 36 | + "Prodazin Power Plan.pow", |
| 37 | + "Reticle v2.pow", |
| 38 | + "RevisionPowerPlanV2.8.pow", |
| 39 | + "RIP Tweaks Power Plan.pow", |
| 40 | + "Rosca Tweaks v2.pow", |
| 41 | + "Velo's Power Plan.pow", |
| 42 | + "VTRL Optimized.pow", |
| 43 | + "XNRL Pro Plan.pow", |
| 44 | + ]; |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void BundledPlans_AreValidUniqueRegistryHives() |
| 48 | + { |
| 49 | + var directory = GetPowerPlansDirectory(); |
| 50 | + var files = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly); |
| 51 | + |
| 52 | + Assert.All(files, file => Assert.Equal(".pow", Path.GetExtension(file), ignoreCase: true)); |
| 53 | + Assert.Equal(files.Length, files.Select(Path.GetFileNameWithoutExtension).Distinct(StringComparer.OrdinalIgnoreCase).Count()); |
| 54 | + |
| 55 | + foreach (var file in files) |
| 56 | + { |
| 57 | + var info = new FileInfo(file); |
| 58 | + Assert.InRange(info.Length, 4, 10 * 1024 * 1024); |
| 59 | + Assert.Equal("regf", Encoding.ASCII.GetString(File.ReadAllBytes(file), 0, 4)); |
| 60 | + Assert.DoesNotContain(Path.GetFileName(file), character => Path.GetInvalidFileNameChars().Contains(character)); |
| 61 | + Assert.DoesNotContain("#U", Path.GetFileName(file), StringComparison.OrdinalIgnoreCase); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task NewAndExistingPlans_ArePresentDiscoverableAndNotBinaryDuplicates() |
| 67 | + { |
| 68 | + var directory = GetPowerPlansDirectory(); |
| 69 | + var files = Directory.GetFiles(directory, "*.pow", SearchOption.TopDirectoryOnly); |
| 70 | + var newPaths = NewPlanFiles.Select(file => Path.Combine(directory, file)).ToArray(); |
| 71 | + |
| 72 | + Assert.All(newPaths, path => Assert.True(File.Exists(path), $"Missing bundled power plan: {Path.GetFileName(path)}")); |
| 73 | + Assert.All(["Amit.pow", "Beyond.pow", "L1 Final Version.pow"], file => Assert.True(File.Exists(Path.Combine(directory, file)))); |
| 74 | + |
| 75 | + var newHashes = newPaths.ToDictionary(path => path, GetSha256); |
| 76 | + Assert.Equal(newHashes.Count, newHashes.Values.Distinct(StringComparer.Ordinal).Count()); |
| 77 | + Assert.All( |
| 78 | + newHashes, |
| 79 | + entry => Assert.DoesNotContain(files, file => !newPaths.Contains(file, StringComparer.OrdinalIgnoreCase) && GetSha256(file) == entry.Value)); |
| 80 | + |
| 81 | + var logger = new Mock<IEnhancedLoggingService>(MockBehavior.Loose); |
| 82 | + var runner = new Mock<IProcessRunner>(MockBehavior.Strict); |
| 83 | + var service = new PowerPlanService( |
| 84 | + NullLogger<PowerPlanService>.Instance, |
| 85 | + logger.Object, |
| 86 | + runner.Object, |
| 87 | + () => directory); |
| 88 | + |
| 89 | + var discovered = await service.GetCustomPowerPlansAsync(); |
| 90 | + Assert.All(NewPlanFiles, file => Assert.Contains(discovered, plan => plan.Name == Path.GetFileNameWithoutExtension(file))); |
| 91 | + Assert.Equal(files.Length, discovered.Count); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void Project_ConfiguresAllPowerPlansForBuildAndPublish() |
| 96 | + { |
| 97 | + var project = File.ReadAllText(Path.Combine(FindRepositoryRoot(), "ThreadPilot.csproj")); |
| 98 | + |
| 99 | + Assert.Contains(@"assets\Powerplans\**\*.pow", project, StringComparison.Ordinal); |
| 100 | + Assert.Contains(@"<TargetPath>Powerplans\%(RecursiveDir)%(Filename)%(Extension)</TargetPath>", project, StringComparison.Ordinal); |
| 101 | + Assert.Contains("<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>", project, StringComparison.Ordinal); |
| 102 | + Assert.Contains("<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>", project, StringComparison.Ordinal); |
| 103 | + } |
| 104 | + |
| 105 | + private static string GetPowerPlansDirectory() => |
| 106 | + Path.Combine(FindRepositoryRoot(), "assets", "Powerplans"); |
| 107 | + |
| 108 | + private static string GetSha256(string path) => |
| 109 | + Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))); |
| 110 | + |
| 111 | + private static string FindRepositoryRoot() |
| 112 | + { |
| 113 | + var directory = new DirectoryInfo(AppContext.BaseDirectory); |
| 114 | + while (directory != null) |
| 115 | + { |
| 116 | + if (File.Exists(Path.Combine(directory.FullName, "ThreadPilot.csproj"))) |
| 117 | + { |
| 118 | + return directory.FullName; |
| 119 | + } |
| 120 | + |
| 121 | + directory = directory.Parent; |
| 122 | + } |
| 123 | + |
| 124 | + throw new InvalidOperationException("Repository root could not be located."); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments