97 lines
3.1 KiB
C#
97 lines
3.1 KiB
C#
using Microsoft.Extensions.Options;
|
|
using TermRemoteCtl.Agent.Configuration;
|
|
using TermRemoteCtl.Agent.History;
|
|
using TermRemoteCtl.Agent.Sessions;
|
|
using TermRemoteCtl.Agent.Terminal;
|
|
|
|
namespace TermRemoteCtl.Agent.Tests.Terminal;
|
|
|
|
public class ConPtySessionFactoryTests
|
|
{
|
|
[Fact]
|
|
public async Task FactoryBackedHost_Starts_Shell_On_Windows()
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
return;
|
|
}
|
|
|
|
using var harness = HostHarness.Create();
|
|
await using var host = harness.Host;
|
|
var session = harness.Registry.Create("smoke", DateTimeOffset.UtcNow);
|
|
await host.StartAsync(session.SessionId, CancellationToken.None);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FactoryBackedHost_WriteInput_Emits_Output()
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var output = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
using var harness = HostHarness.Create();
|
|
await using var host = harness.Host;
|
|
var session = harness.Registry.Create("smoke", DateTimeOffset.UtcNow);
|
|
host.OutputReceived += (_, args) =>
|
|
{
|
|
if (args.Chunk.Contains("smoke", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
output.TrySetResult(args.Chunk);
|
|
}
|
|
};
|
|
|
|
await host.StartAsync(session.SessionId, CancellationToken.None);
|
|
await Task.Delay(1000);
|
|
await host.WriteInputAsync(session.SessionId, "Write-Output smoke\r\n", CancellationToken.None);
|
|
|
|
var completed = await Task.WhenAny(output.Task, Task.Delay(TimeSpan.FromSeconds(20)));
|
|
Assert.True(ReferenceEquals(output.Task, completed), "Timed out waiting for shell output.");
|
|
Assert.Contains("smoke", await output.Task, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private sealed class HostHarness : IDisposable
|
|
{
|
|
private HostHarness(string dataRoot, SessionRegistry registry, PowerShellSessionHost host)
|
|
{
|
|
DataRoot = dataRoot;
|
|
Registry = registry;
|
|
Host = host;
|
|
}
|
|
|
|
public string DataRoot { get; }
|
|
|
|
public SessionRegistry Registry { get; }
|
|
|
|
public PowerShellSessionHost Host { get; }
|
|
|
|
public static HostHarness Create()
|
|
{
|
|
var dataRoot = Path.Combine(Path.GetTempPath(), "TermRemoteCtl.Tests", Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(dataRoot);
|
|
|
|
var options = Options.Create(new AgentOptions
|
|
{
|
|
DataRoot = dataRoot,
|
|
RingBufferLineLimit = 4000,
|
|
});
|
|
var registry = new SessionRegistry(
|
|
new SessionHistoryStore(dataRoot),
|
|
new SessionIoJournalStore(dataRoot),
|
|
options);
|
|
var host = new PowerShellSessionHost(new ConPtySessionFactory(), registry);
|
|
|
|
return new HostHarness(dataRoot, registry, host);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(DataRoot))
|
|
{
|
|
Directory.Delete(DataRoot, true);
|
|
}
|
|
}
|
|
}
|
|
}
|