Files
HashTool/tests/HashTool.App.Tests/UnitTests.cs
T
poli0981andClaude Opus 5.5 4bd17a50f4 feat: hashtool CLI, app tests, release packaging inputs and benchmarks
- src/HashTool.Cli: `hashtool hash | verify | check | algorithms` on top of
  Core, NativeAOT-ready (3.9 MB native binary), coreutils-style output and
  exit codes (0 ok, 1 failed/mismatch, 2 usage or unreadable checksum
  file); Windows paths are written with '/' so lines need no escaping.
- tests/HashTool.Cli.Tests: commands, formats and exit codes.
- tests/HashTool.App.Tests (Avalonia headless): every page in several
  languages including RTL, a folder create-and-verify flow, translation
  integrity (keys, placeholders, Languages.json/resx/project wiring,
  satellite assemblies, unused keys), system language detection, command
  line parsing and helpers.
- tests/Compat/LegacyClient: drives Velopack 0.0.1298 (the library inside
  Hash Tool 1.1.x) against new release files for the upgrade gate.
- build/macos/Info.plist keeps the 1.x bundle identity and adds "Open
  With" document types; build/linux/hashtool.png is the AppImage icon.
- bench/HashTool.Benchmarks (BenchmarkDotNet) and a tool manifest pinning
  vpk 1.2.161, nuget-license and CycloneDX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 13:16:40 +07:00

114 lines
4.0 KiB
C#

using System;
using System.IO;
using HashTool.App.Infrastructure;
using HashTool.App.Infrastructure.Logging;
using HashTool.App.ViewModels;
using HashTool.App.Views.Controls;
using HashTool.Core.Tests;
using Xunit;
namespace HashTool.App.Tests;
public sealed class HighlightTextBlockTests
{
[Fact]
public void Split_EmitsTheTailOnce()
{
Assert.Equal([("rep", true), ("ort.pdf", false)], HighlightTextBlock.Split("report.pdf", "rep"));
}
[Fact]
public void Split_IsCaseInsensitiveAndFindsEveryMatch()
{
Assert.Equal([("a", false), ("X", true), ("a", false), ("x", true)], HighlightTextBlock.Split("aXax", "x"));
}
[Fact]
public void Split_WithoutMatch_ReturnsTheText()
{
Assert.Equal([("report.pdf", false)], HighlightTextBlock.Split("report.pdf", "zip"));
Assert.Equal([("abc", false)], HighlightTextBlock.Split("abc", ""));
}
}
public sealed class AppCommandLineTests
{
[Fact]
public void Parse_VerbAndPaths()
{
var parsed = AppCommandLine.Parse(["--verify", "a.sha256", "sub/b.md5"], workingDirectory: Path.GetTempPath());
Assert.Equal(ActivationVerb.Verify, parsed.Verb);
Assert.Equal([Path.Combine(Path.GetTempPath(), "a.sha256"), Path.GetFullPath(Path.Combine(Path.GetTempPath(), "sub/b.md5"))], parsed.Paths);
Assert.False(parsed.SmokeTest);
}
[Fact]
public void Parse_SmokeTestOptions()
{
var parsed = AppCommandLine.Parse(["--smoke-test", "--smoke-out", "r.json", "--expect-version", "1.2.0", "--wait-pid", "42", "--unknown"]);
Assert.True(parsed.SmokeTest);
Assert.Equal("r.json", parsed.SmokeOutput);
Assert.Equal("1.2.0", parsed.ExpectedVersion);
Assert.Equal(42, parsed.WaitPid);
Assert.Empty(parsed.Paths);
Assert.Null(parsed.ToRequest());
}
[Fact]
public void FilesFrom_DeletesOnlyTheAppsOwnTemporaryList()
{
using var temp = new TempDirectory();
var ownList = Path.Combine(Path.GetTempPath(), $"hashtool-{Guid.NewGuid():N}.txt");
File.WriteAllLines(ownList, [Path.Combine(temp.Path, "a.bin")]);
var userFile = temp.File("important.txt", Path.Combine(temp.Path, "b.bin"));
var fromOwn = AppCommandLine.Parse(["--create", "--files-from", ownList]);
var fromUser = AppCommandLine.Parse(["--files-from", userFile]);
Assert.Equal([Path.Combine(temp.Path, "a.bin")], fromOwn.Paths);
Assert.False(File.Exists(ownList));
Assert.Equal([Path.Combine(temp.Path, "b.bin")], fromUser.Paths);
Assert.True(File.Exists(userFile));
}
}
public sealed class TextHelperTests
{
[Fact]
public void ReleaseNotes_MarkdownBecomesPlainText()
{
var text = UpdateViewModel.ToPlainText("## Added\n\n- **CLI** `hashtool`\n- See [docs](https://example.com)\n\n\n\nEnd");
Assert.Equal("Added\n\n• CLI hashtool\n• See docs\n\nEnd", text);
}
[Fact]
public void PathSanitizer_MasksUserFolders()
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var sanitized = PathSanitizer.Shared.Sanitize($"Could not read {Path.Combine(home, "Documents", "secret.txt")}");
Assert.DoesNotContain(home, sanitized, StringComparison.OrdinalIgnoreCase);
Assert.EndsWith("secret.txt", sanitized);
}
[Theory]
[InlineData("C:/data", "C:/data/a/b.txt", "a/b.txt")]
[InlineData("C:/data", "C:/data/b.txt", "b.txt")]
public void RelativePath_UsesForwardSlashes(string baseDirectory, string file, string expected)
{
if (!OperatingSystem.IsWindows()) return;
Assert.Equal(expected, CreateHashViewModel.RelativePath(baseDirectory, file));
}
[Fact]
public void LogEntry_UsesInvariantTimestamps()
{
var entry = new LogEntry(new DateTimeOffset(2026, 10, 15, 8, 30, 5, 123, TimeSpan.Zero), Microsoft.Extensions.Logging.LogLevel.Warning, "Test", "message");
Assert.Equal("08:30:05.123 [WARN] Test: message", entry.ToString());
}
}