Files
poli0981andClaude Opus 5.5 f781690b5f test: cover decoding, path resolution, elevation environment, storage
New tests for the output decoder (UTF-8, UTF-16LE with and without BOM,
OEM fallback, chunk boundaries), executable resolution, argument tokens,
the elevated child environment (including a real PowerShell run showing
it loads modules only from admin-only folders), crash-safe JSON files and
v1.5.1 settings, accent-insensitive search, recipe confirmations, the
update dialog, the console buffer and crash codes.

Run faults and cancellation must complete the output channel; elevation
tests have time limits; the DI test uses a scratch folder instead of the
real %AppData%; culture-switching tests run on their own; Domain and
Application may reference only the runtime (and Domain).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:21:25 +07:00

117 lines
4.2 KiB
C#

using CommandForge.Application.Search;
using CommandForge.Domain;
namespace CommandForge.Tests;
/// <summary>Tests accent-insensitive matching, precise description/tag matching and highlighting.</summary>
public sealed class SearchFoldingTests
{
[Theory]
[InlineData("Dọn dẹp ổ đĩa", "don dep o dia")]
[InlineData("Đặt lại mạng", "dat lai mang")]
[InlineData("CỬA SỔ", "cua so")]
[InlineData("Configuración", "configuracion")]
public void Fold_RemovesAccentsAndCase(string text, string expected)
=> Assert.Equal(expected, TextFolding.Fold(text));
[Fact]
public void Fold_KeepsEveryIndex()
{
// Highlight indices are computed on the folded text and drawn on the original.
const string text = "Xóa bộ nhớ đệm DNS — Ổn định";
Assert.Equal(text.Length, TextFolding.Fold(text).Length);
}
[Fact]
public void Search_IgnoresVietnameseAccents()
{
var hits = SearchCommandsUseCase.Search([Make("vi", "Dọn dẹp ổ đĩa", "Xóa tệp tạm")], "don dep");
Assert.Equal("vi", Assert.Single(hits).Command.Id);
}
[Fact]
public void Search_DescriptionNeedsTheWordItself_NotScatteredLetters()
{
// "dns" is a subsequence of "Delete ... and system caches"; that is not a match.
var items = new[]
{
Make("cleanup", "Disk Cleanup", "Delete temporary files and system caches"),
Make("flush", "Flush resolver", "Clear the DNS client cache"),
};
var hits = SearchCommandsUseCase.Search(items, "dns");
Assert.Equal("flush", Assert.Single(hits).Command.Id);
}
[Fact]
public void Search_TagsMatchByWordPrefix()
{
var items = new[]
{
Make("wifi", "Connection details", "x", tags: ["wifi", "signal"]),
Make("other", "Other", "x", tags: ["swift"]),
};
Assert.Equal("wifi", Assert.Single(SearchCommandsUseCase.Search(items, "wi")).Command.Id);
}
[Fact]
public void Search_HighlightsTheMatchedTitleCharacters()
{
var hit = Assert.Single(SearchCommandsUseCase.Search([Make("f", "Flush DNS cache", "x")], "dns"));
Assert.Equal([6, 7, 8], hit.TitleMatches);
}
[Fact]
public void Search_PrefersAWordStartMatch()
{
var items = new[] { Make("inside", "Autoflush", "x"), Make("start", "Flush DNS", "x") };
var hits = SearchCommandsUseCase.Search(items, "flush");
Assert.Equal(["start", "inside"], hits.Select(h => h.Command.Id));
}
[Fact]
public void Search_EqualScores_KeepCatalogOrder()
{
var items = new[] { Make("b", "Restart service B", "x"), Make("a", "Restart service A", "x") };
Assert.Equal(["b", "a"], SearchCommandsUseCase.Search(items, "restart").Select(h => h.Command.Id));
}
[Fact]
public void RequiresRestartFilter_MeansAlways()
{
var always = new SearchableCommand(Command("always") with { Restart = RestartPolicy.Always }, "A", "x", []);
var sometimes = new SearchableCommand(Command("sometimes") with { Restart = RestartPolicy.FromExitCode }, "B", "x", []);
var hits = SearchCommandsUseCase.Search([always, sometimes], null, new CommandSearchFilter { RequiresRestart = true });
Assert.Equal("always", Assert.Single(hits).Command.Id);
}
[Theory]
[InlineData("ping google")]
[InlineData("typing")]
[InlineData("shipping")]
public void NaturalLanguage_MatchesWholeWordsOnly(string query)
=> Assert.Null(NaturalLanguageSearch.Suggest(query)); // "pin" (battery) hides inside each of these
[Theory]
[InlineData("pin laptop yếu", "battery")]
[InlineData("MẠNG", "network")]
[InlineData("Cap nhat", "update")]
public void NaturalLanguage_IgnoresAccentsAndCase(string query, string expected)
=> Assert.Equal(expected, NaturalLanguageSearch.Suggest(query));
private static CommandDefinition Command(string id)
=> new() { Id = id, CategoryId = "cat", TitleKey = "t", DescriptionKey = "d", Executable = "x" };
private static SearchableCommand Make(string id, string title, string description, IReadOnlyList<string>? tags = null)
=> new(Command(id), title, description, tags ?? []);
}