feat: 初始提交——一键运行快捷轮盘工具(WPF + C#)
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using OneClickRun.Models;
|
||||
using OneClickRun.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace OneClickRun.Tests;
|
||||
|
||||
public class ConfigServiceTests
|
||||
{
|
||||
private static string NewTempDir() =>
|
||||
Path.Combine(Path.GetTempPath(), "OneClickRunTests", Guid.NewGuid().ToString("N"));
|
||||
|
||||
[Fact]
|
||||
public void SaveAndLoadRoundTrip()
|
||||
{
|
||||
var dir = NewTempDir();
|
||||
try
|
||||
{
|
||||
var service = new ConfigService(dir);
|
||||
service.Load(); // 首次运行生成默认配置
|
||||
Assert.True(service.WasFirstRun);
|
||||
Assert.Equal(4, service.Current.Items.Count);
|
||||
|
||||
service.Current.General.Theme = ThemeMode.Dark;
|
||||
service.Current.General.TriggerMode = TriggerMode.Hold;
|
||||
service.Current.General.SelectionMode = SelectionMode.Swipe;
|
||||
service.Current.General.Hotkey = new HotkeyBinding { Modifiers = { "Ctrl", "Shift" }, Key = "F9" };
|
||||
service.Current.Items[0].Name = "改名后的指令";
|
||||
service.Save();
|
||||
|
||||
var reloaded = new ConfigService(dir);
|
||||
reloaded.Load();
|
||||
Assert.False(reloaded.WasFirstRun);
|
||||
Assert.Equal(ThemeMode.Dark, reloaded.Current.General.Theme);
|
||||
Assert.Equal(TriggerMode.Hold, reloaded.Current.General.TriggerMode);
|
||||
Assert.Equal(SelectionMode.Swipe, reloaded.Current.General.SelectionMode);
|
||||
Assert.Equal("Ctrl+Shift+F9", reloaded.Current.General.HotkeyText);
|
||||
Assert.Equal("改名后的指令", reloaded.Current.Items[0].Name);
|
||||
Assert.Equal(service.Current.Items.Count, reloaded.Current.Items.Count);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { Directory.Delete(dir, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CorruptConfigIsBackedUpAndRebuilt()
|
||||
{
|
||||
var dir = NewTempDir();
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
var path = Path.Combine(dir, "config.json");
|
||||
File.WriteAllText(path, "{ 这不是合法的 JSON");
|
||||
|
||||
var service = new ConfigService(dir);
|
||||
service.Load();
|
||||
|
||||
Assert.True(File.Exists(path));
|
||||
Assert.Contains("\"General\"", File.ReadAllText(path));
|
||||
Assert.Single(Directory.GetFiles(dir, "config.broken-*.json"));
|
||||
Assert.True(service.WasFirstRun);
|
||||
Assert.NotNull(service.Current.General);
|
||||
Assert.NotNull(service.Current.Items);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { Directory.Delete(dir, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MissingFieldsAreRepaired()
|
||||
{
|
||||
var dir = NewTempDir();
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
// General 缺失、Items 缺失、item 内字段缺失
|
||||
var json = JsonSerializer.Serialize(new { version = 1 });
|
||||
File.WriteAllText(Path.Combine(dir, "config.json"), json);
|
||||
|
||||
var service = new ConfigService(dir);
|
||||
service.Load();
|
||||
|
||||
Assert.False(service.WasFirstRun);
|
||||
Assert.NotNull(service.Current.General);
|
||||
Assert.NotNull(service.Current.General.Hotkey);
|
||||
Assert.Empty(service.Current.Items);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { Directory.Delete(dir, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnumValuesAreSerializedAsStrings()
|
||||
{
|
||||
var service = new ConfigService(NewTempDir());
|
||||
service.Load();
|
||||
service.Current.General.Theme = ThemeMode.System;
|
||||
service.Current.Items.Add(new WheelItem { Type = WheelItemType.Script, Name = "测试", Path = "a.ps1" });
|
||||
service.Save();
|
||||
|
||||
var json = File.ReadAllText(service.ConfigPath);
|
||||
Assert.Contains("\"System\"", json);
|
||||
Assert.Contains("\"Script\"", json);
|
||||
try { Directory.Delete(service.ConfigDirectory, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using OneClickRun.Helpers;
|
||||
using OneClickRun.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace OneClickRun.Tests;
|
||||
|
||||
public class HotkeyFormatTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("Ctrl+Alt+Space")]
|
||||
[InlineData("Ctrl+Shift+F8")]
|
||||
[InlineData("Win+D")]
|
||||
[InlineData("F8")]
|
||||
[InlineData("Alt+F4")]
|
||||
public void FormatAndParseRoundTrip(string text)
|
||||
{
|
||||
var binding = HotkeyFormat.TryParse(text);
|
||||
Assert.NotNull(binding);
|
||||
Assert.Equal(text, HotkeyFormat.Format(binding));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ModifierOrderIsCanonical()
|
||||
{
|
||||
var binding = new HotkeyBinding { Modifiers = { "Alt", "Ctrl", "Shift" }, Key = "Space" };
|
||||
Assert.Equal("Ctrl+Alt+Shift+Space", HotkeyFormat.Format(binding));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseRejectsInvalidInput()
|
||||
{
|
||||
Assert.Null(HotkeyFormat.TryParse("Ctrl")); // 缺少主键
|
||||
Assert.Null(HotkeyFormat.TryParse("Ctrl+Xxx")); // 非法键名
|
||||
Assert.Null(HotkeyFormat.TryParse("Bla+Space")); // 非法修饰键
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyInputReturnsInvalidBinding()
|
||||
{
|
||||
var binding = HotkeyFormat.TryParse(" ");
|
||||
Assert.NotNull(binding);
|
||||
Assert.False(binding.IsValid);
|
||||
Assert.Equal(string.Empty, HotkeyFormat.Format(binding));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VirtualKeyMapping()
|
||||
{
|
||||
Assert.Equal(0x20, HotkeyFormat.VirtualKeyOf("Space")); // VK_SPACE
|
||||
Assert.Equal(0x74, HotkeyFormat.VirtualKeyOf("F5")); // VK_F5
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OneClickRun\OneClickRun.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,35 @@
|
||||
using OneClickRun.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace OneClickRun.Tests;
|
||||
|
||||
public class PowerShellLocatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void BuildScriptArgumentsQuotesPathsWithSpaces()
|
||||
{
|
||||
Assert.Equal(
|
||||
"-NoProfile -ExecutionPolicy Bypass -File \"C:\\my scripts\\hello.ps1\"",
|
||||
PowerShellLocator.BuildScriptArguments(@"C:\my scripts\hello.ps1", null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildScriptArgumentsAppendsExtraArgs()
|
||||
{
|
||||
Assert.Equal(
|
||||
"-NoProfile -ExecutionPolicy Bypass -File \"D:\\a.ps1\" -Name demo",
|
||||
PowerShellLocator.BuildScriptArguments(@"D:\a.ps1", "-Name demo"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindLocatesPwshOnThisMachineOrReturnsNull()
|
||||
{
|
||||
// 开发机已安装 PowerShell 7.6.5;其他环境返回 null 也不算失败
|
||||
var path = PowerShellLocator.Find();
|
||||
if (path != null)
|
||||
{
|
||||
Assert.EndsWith("pwsh.exe", path, System.StringComparison.OrdinalIgnoreCase);
|
||||
Assert.True(System.IO.File.Exists(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
using System.Windows;
|
||||
using OneClickRun.Helpers;
|
||||
using Xunit;
|
||||
|
||||
namespace OneClickRun.Tests;
|
||||
|
||||
public class WheelGeometryTests
|
||||
{
|
||||
private const double InnerR = 70;
|
||||
private const double OuterR = 220;
|
||||
private const double Gap = 10;
|
||||
private static readonly Point Center = new(300, 300);
|
||||
|
||||
/// <summary>
|
||||
/// 需求核心:“不论距离中心多远,扇形之间的间距都相等”。
|
||||
/// 相邻扇形在任意半径处,右边界点与左边界点间的距离应恒等于 Gap。
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void AdjacentSectorGapIsConstantAtEveryRadius()
|
||||
{
|
||||
var radii = new[] { InnerR, 80, 100, 145, 200, OuterR };
|
||||
for (var count = 2; count <= 12; count++)
|
||||
{
|
||||
for (var k = 0; k < count; k++)
|
||||
{
|
||||
var rightEdgeAngle = WheelGeometry.SectorStartAngle(k, count) + WheelGeometry.TwoPi / count;
|
||||
var leftEdgeAngle = WheelGeometry.SectorStartAngle((k + 1) % count, count);
|
||||
// 共享同一条原始径向射线(允许相差 2π 整周期)
|
||||
Assert.Equal(0.0, (rightEdgeAngle - leftEdgeAngle) % WheelGeometry.TwoPi, 12);
|
||||
|
||||
foreach (var rho in radii)
|
||||
{
|
||||
// 扇形 k 的右边界点:角度 - ε(ρ);扇形 k+1 的左边界点:角度 + ε(ρ)
|
||||
var right = WheelGeometry.PointOnCircle(Center, rho, rightEdgeAngle - WheelGeometry.EdgeOffset(rho, Gap));
|
||||
var left = WheelGeometry.PointOnCircle(Center, rho, leftEdgeAngle + WheelGeometry.EdgeOffset(rho, Gap));
|
||||
var distance = (right - left).Length;
|
||||
Assert.True(Math.Abs(distance - Gap) < 1e-9,
|
||||
$"count={count}, sector={k}, rho={rho}: gap={distance}, expected={Gap}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>两个相邻边界点是同一条偏移直线上的点(平行边 ⇒ 处处等距)</summary>
|
||||
[Fact]
|
||||
public void AdjacentEdgePointsLieOnParallelOffsetLines()
|
||||
{
|
||||
const int count = 6;
|
||||
const int sector = 2;
|
||||
var edgeAngle = WheelGeometry.SectorStartAngle(sector, count) + WheelGeometry.TwoPi / count;
|
||||
// 同一偏移线在不同半径上的点应共线:取三点验证叉积为 0
|
||||
var p1 = WheelGeometry.PointOnCircle(Center, 80, edgeAngle + WheelGeometry.EdgeOffset(80, Gap));
|
||||
var p2 = WheelGeometry.PointOnCircle(Center, 145, edgeAngle + WheelGeometry.EdgeOffset(145, Gap));
|
||||
var p3 = WheelGeometry.PointOnCircle(Center, 200, edgeAngle + WheelGeometry.EdgeOffset(200, Gap));
|
||||
var cross = (p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X);
|
||||
Assert.True(Math.Abs(cross) < 1e-9, $"边界点不共线,叉积={cross}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SectorCornersHaveExactRadii()
|
||||
{
|
||||
for (var count = 1; count <= 12; count++)
|
||||
{
|
||||
for (var k = 0; k < count; k++)
|
||||
{
|
||||
var (p1, p2, p3, p4) = WheelGeometry.SectorCorners(Center, InnerR, OuterR, Gap, k, count);
|
||||
Assert.Equal(InnerR, (p1 - Center).Length, 9);
|
||||
Assert.Equal(OuterR, (p2 - Center).Length, 9);
|
||||
Assert.Equal(OuterR, (p3 - Center).Length, 9);
|
||||
Assert.Equal(InnerR, (p4 - Center).Length, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(4)]
|
||||
[InlineData(6)]
|
||||
[InlineData(12)]
|
||||
public void HitTestMapsSectorMidPointsToExpectedSector(int count)
|
||||
{
|
||||
for (var k = 0; k < count; k++)
|
||||
{
|
||||
var mid = WheelGeometry.SectorMidPoint(Center, InnerR, OuterR, k, count);
|
||||
var hit = WheelGeometry.HitTest(mid, Center, InnerR, OuterR, Gap, count);
|
||||
Assert.Equal(k, hit);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HitTestReturnsNullForCenterOutsideAndGap()
|
||||
{
|
||||
// 中心区域
|
||||
Assert.Null(WheelGeometry.HitTest(Center, Center, InnerR, OuterR, Gap, 4));
|
||||
// 圆环外
|
||||
Assert.Null(WheelGeometry.HitTest(new Point(Center.X + OuterR + 5, Center.Y), Center, InnerR, OuterR, Gap, 4));
|
||||
// 间隙正中间:原始径向边界角度处
|
||||
var midRadius = (InnerR + OuterR) / 2;
|
||||
var edge = WheelGeometry.SectorStartAngle(1, 4);
|
||||
var inGap = WheelGeometry.PointOnCircle(Center, midRadius, edge);
|
||||
Assert.Null(WheelGeometry.HitTest(inGap, Center, InnerR, OuterR, Gap, 4));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SingleSectorIsFullRing()
|
||||
{
|
||||
var angles = new[] { -Math.PI / 2, 0.0, Math.PI / 2, Math.PI, Math.PI * 1.5 };
|
||||
foreach (var angle in angles)
|
||||
{
|
||||
var point = WheelGeometry.PointOnCircle(Center, (InnerR + OuterR) / 2, angle);
|
||||
Assert.Equal(0, WheelGeometry.HitTest(point, Center, InnerR, OuterR, Gap, 1));
|
||||
}
|
||||
Assert.NotNull(WheelGeometry.CreateSectorGeometry(Center, InnerR, OuterR, Gap, 0, 1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SectorGeometryIsFrozen()
|
||||
{
|
||||
var geometry = WheelGeometry.CreateSectorGeometry(Center, InnerR, OuterR, Gap, 0, 4);
|
||||
Assert.True(geometry.IsFrozen);
|
||||
Assert.False(geometry.IsEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user