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

178 lines
6.9 KiB
C#

using System.IO.Pipes;
using System.Threading.Channels;
using CommandForge.Application.Ports;
using CommandForge.Domain;
using CommandForge.Infrastructure.Elevation;
namespace CommandForge.Tests;
/// <summary>
/// Tests the broker's pipe protocol and the elevated <see cref="ElevationServer"/> over an
/// in-process named pipe (no real elevation — the spawn/UAC step is verified manually).
/// </summary>
public sealed class ElevationTests
{
[Fact(Timeout = 30_000)]
public async Task PipeMessageChannel_RoundTrips_Request()
{
using var stream = new MemoryStream();
using var channel = new PipeMessageChannel(stream);
await channel.WriteAsync(
new ElevationRequest { Kind = ElevationRequestKind.Run, CommandId = "dism.checkhealth" },
ElevationJsonContext.Default.ElevationRequest,
TestContext.Current.CancellationToken);
stream.Position = 0;
var read = await channel.ReadAsync(ElevationJsonContext.Default.ElevationRequest, TestContext.Current.CancellationToken);
Assert.NotNull(read);
Assert.Equal(ElevationRequestKind.Run, read.Kind);
Assert.Equal("dism.checkhealth", read.CommandId);
}
[Fact(Timeout = 30_000)]
public async Task PipeMessageChannel_RoundTrips_Message()
{
using var stream = new MemoryStream();
using var channel = new PipeMessageChannel(stream);
await channel.WriteAsync(
ElevationMessage.Output("hello", isError: true),
ElevationJsonContext.Default.ElevationMessage,
TestContext.Current.CancellationToken);
stream.Position = 0;
var read = await channel.ReadAsync(ElevationJsonContext.Default.ElevationMessage, TestContext.Current.CancellationToken);
Assert.NotNull(read);
Assert.Equal(ElevationMessageKind.Output, read.Kind);
Assert.Equal("hello", read.Text);
Assert.True(read.IsError);
}
[Fact(Timeout = 30_000)]
public async Task ElevationServer_RunsAdminCommand_StreamsOutputThenResult()
{
var command = AdminCommand("adm");
var (uiChannel, serverTask, client, server) = await StartServerAsync(
new FakeCatalog(command),
new FakeExecutor([new OutputLine("line-1", false)], Result(exitCode: 0, success: true)));
await using (client)
await using (server)
{
await uiChannel.WriteAsync(
new ElevationRequest { Kind = ElevationRequestKind.Run, CommandId = "adm" },
ElevationJsonContext.Default.ElevationRequest,
TestContext.Current.CancellationToken);
var output = await uiChannel.ReadAsync(ElevationJsonContext.Default.ElevationMessage, TestContext.Current.CancellationToken);
Assert.Equal(ElevationMessageKind.Output, output!.Kind);
Assert.Equal("line-1", output.Text);
var result = await uiChannel.ReadAsync(ElevationJsonContext.Default.ElevationMessage, TestContext.Current.CancellationToken);
Assert.Equal(ElevationMessageKind.Result, result!.Kind);
Assert.True(result.Success);
Assert.Equal(0, result.ExitCode);
uiChannel.Dispose();
await client.DisposeAsync();
await serverTask;
}
}
[Fact(Timeout = 30_000)]
public async Task ElevationServer_RejectsUnknownCommand()
{
var (uiChannel, serverTask, client, server) = await StartServerAsync(
new FakeCatalog(AdminCommand("adm")),
new FakeExecutor([], Result(0, true)));
await using (client)
await using (server)
{
await uiChannel.WriteAsync(
new ElevationRequest { Kind = ElevationRequestKind.Run, CommandId = "does-not-exist" },
ElevationJsonContext.Default.ElevationRequest,
TestContext.Current.CancellationToken);
var rejection = await uiChannel.ReadAsync(ElevationJsonContext.Default.ElevationMessage, TestContext.Current.CancellationToken);
Assert.Equal(ElevationMessageKind.Output, rejection!.Kind);
Assert.True(rejection.IsError);
var result = await uiChannel.ReadAsync(ElevationJsonContext.Default.ElevationMessage, TestContext.Current.CancellationToken);
Assert.Equal(ElevationMessageKind.Result, result!.Kind);
Assert.False(result.Success);
uiChannel.Dispose();
await client.DisposeAsync();
await serverTask;
}
}
private static async Task<(PipeMessageChannel UiChannel, Task ServerTask, NamedPipeClientStream Client, NamedPipeServerStream Server)>
StartServerAsync(ICatalogProvider catalog, ICommandExecutor executor)
{
var pipeName = "CommandForge.Test." + Guid.NewGuid().ToString("N");
var server = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
var waitConnection = server.WaitForConnectionAsync(TestContext.Current.CancellationToken);
await client.ConnectAsync(5000, TestContext.Current.CancellationToken);
await waitConnection;
var serverTask = new ElevationServer(catalog, executor).RunAsync(server, TestContext.Current.CancellationToken);
return (new PipeMessageChannel(client), serverTask, client, server);
}
private static CommandDefinition AdminCommand(string id) => new()
{
Id = id,
CategoryId = "c",
TitleKey = "t",
DescriptionKey = "d",
Executable = "x",
RequiresAdmin = true,
};
private static ExecutionResult Result(int exitCode, bool success)
=> new() { ExitCode = exitCode, Success = success, Duration = TimeSpan.Zero };
private sealed class FakeCatalog : ICatalogProvider
{
private readonly IReadOnlyList<CommandDefinition> _commands;
public FakeCatalog(params CommandDefinition[] commands) => _commands = commands;
public IReadOnlyList<CommandCategory> GetCategories() => [];
public IReadOnlyList<CommandDefinition> GetCommands() => _commands;
public IReadOnlyList<string> ValidationErrors => [];
}
private sealed class FakeExecutor : ICommandExecutor
{
private readonly IReadOnlyList<OutputLine> _lines;
private readonly ExecutionResult _result;
public FakeExecutor(IReadOnlyList<OutputLine> lines, ExecutionResult result)
{
_lines = lines;
_result = result;
}
public async Task<ExecutionResult> ExecuteAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken = default)
{
foreach (var line in _lines)
{
await output.WriteAsync(line, CancellationToken.None);
}
output.TryComplete();
return _result;
}
}
}