surveillance-client/managed-client/tests/Surveillance.Client.Application.Tests/LiveViewCoordinatorTests.cs
2026-10-16 15:36:51 +02:00

223 lines
No EOL
8.1 KiB
C#

using System.Runtime.CompilerServices;
using Surveillance.Client.Application;
using Surveillance.Client.Domain;
using Surveillance.Client.Media;
using Surveillance.Client.Protocol;
using Xunit;
namespace Surveillance.Client.Application.Tests;
public sealed class LiveViewCoordinatorTests
{
[Fact]
public async Task ConnectLoadsRememberedCredentialsAndPreviewCanBeStopped()
{
var savedCredentials = new DeviceCredentials("viewer", "secret");
var store = new FakeCredentialStore(savedCredentials);
var preview = new FakePreview();
var session = new FakeSession(preview, AuthenticationResult.Success());
var coordinator = new LiveViewCoordinator(
new FakeSessionFactory(session),
store,
new FakeDecoder());
var result = await coordinator.ConnectAsync(
new DeviceEndpoint("camera.local", 34567),
new DeviceCredentials(string.Empty, string.Empty),
rememberCredentials: true,
TestContext.Current.CancellationToken);
Assert.True(result.IsAuthenticated);
Assert.Same(savedCredentials, session.AuthenticatedCredentials);
Assert.Equal(1, store.ReadCount);
Assert.Equal(1, store.WriteCount);
Assert.True(coordinator.IsConnected);
await coordinator.StartPreviewAsync(new ChannelId(0), TestContext.Current.CancellationToken);
var receivedFrames = new List<DecodedVideoFrame>();
await foreach (var frame in coordinator.ReadFramesAsync(TestContext.Current.CancellationToken))
{
receivedFrames.Add(frame);
}
Assert.Single(receivedFrames);
Assert.True(coordinator.IsPreviewing);
await coordinator.StopAsync(TestContext.Current.CancellationToken);
Assert.False(coordinator.IsConnected);
Assert.False(coordinator.IsPreviewing);
Assert.True(preview.WasStopped);
Assert.True(session.WasDisconnected);
}
[Fact]
public async Task RejectedAuthenticationDoesNotPersistCredentials()
{
var store = new FakeCredentialStore(null);
var session = new FakeSession(
new FakePreview(),
AuthenticationResult.Rejected(AuthenticationFailure.InvalidCredentials));
var coordinator = new LiveViewCoordinator(
new FakeSessionFactory(session),
store,
new FakeDecoder());
var result = await coordinator.ConnectAsync(
new DeviceEndpoint("127.0.0.1", 34567),
new DeviceCredentials("viewer", "wrong"),
rememberCredentials: true,
TestContext.Current.CancellationToken);
Assert.False(result.IsAuthenticated);
Assert.False(coordinator.IsConnected);
Assert.Equal(0, store.WriteCount);
Assert.True(session.WasDisconnected);
await coordinator.DisposeAsync();
}
[Fact]
public async Task SaveCredentialsWritesToTheEndpointCredentialKey()
{
var store = new FakeCredentialStore(null);
var coordinator = new LiveViewCoordinator(
new FakeSessionFactory(new FakeSession(new FakePreview(), AuthenticationResult.Success())),
store,
new FakeDecoder());
var endpoint = new DeviceEndpoint("camera.local", 9000);
var credentials = new DeviceCredentials("viewer", "secret");
await coordinator.SaveCredentialsAsync(endpoint, credentials, TestContext.Current.CancellationToken);
Assert.Equal(endpoint.CredentialKey, store.LastKey);
Assert.Same(credentials, store.LastCredentials);
Assert.Equal(1, store.WriteCount);
await coordinator.DisposeAsync();
}
[Fact]
public async Task ConnectUsesSavedCredentialForBlankField()
{
var savedCredentials = new DeviceCredentials("saved-user", "saved-password");
var store = new FakeCredentialStore(savedCredentials);
var session = new FakeSession(new FakePreview(), AuthenticationResult.Success());
var coordinator = new LiveViewCoordinator(
new FakeSessionFactory(session),
store,
new FakeDecoder());
var result = await coordinator.ConnectAsync(
new DeviceEndpoint("127.0.0.1", 34567),
new DeviceCredentials("typed-user", string.Empty),
rememberCredentials: true,
TestContext.Current.CancellationToken);
Assert.True(result.IsAuthenticated);
Assert.Equal("typed-user", session.AuthenticatedCredentials?.UserName);
Assert.Equal("saved-password", session.AuthenticatedCredentials?.Password);
Assert.Equal(1, store.ReadCount);
Assert.Equal(1, store.WriteCount);
await coordinator.DisposeAsync();
}
private sealed class FakeCredentialStore(DeviceCredentials? credentials) : ICredentialStore
{
public int ReadCount { get; private set; }
public int WriteCount { get; private set; }
public string? LastKey { get; private set; }
public DeviceCredentials? LastCredentials { get; private set; }
public ValueTask<DeviceCredentials?> ReadAsync(string key, CancellationToken cancellationToken = default)
{
ReadCount++;
return ValueTask.FromResult(credentials);
}
public ValueTask WriteAsync(
string key,
DeviceCredentials value,
CancellationToken cancellationToken = default)
{
WriteCount++;
LastKey = key;
LastCredentials = value;
return ValueTask.CompletedTask;
}
}
private sealed class FakeSessionFactory(FakeSession session) : IDeviceSessionFactory
{
public ValueTask<IDeviceSession> ConnectAsync(
DeviceEndpoint endpoint,
CancellationToken cancellationToken = default) =>
ValueTask.FromResult<IDeviceSession>(session);
}
private sealed class FakeSession(FakePreview preview, AuthenticationResult authentication) : IDeviceSession
{
public DeviceCredentials? AuthenticatedCredentials { get; private set; }
public bool WasDisconnected { get; private set; }
public ValueTask<AuthenticationResult> AuthenticateAsync(
DeviceCredentials credentials,
CancellationToken cancellationToken = default)
{
AuthenticatedCredentials = credentials;
return ValueTask.FromResult(authentication);
}
public ValueTask<ILivePreview> StartPreviewAsync(
ChannelId channel,
CancellationToken cancellationToken = default) =>
ValueTask.FromResult<ILivePreview>(preview);
public ValueTask DisconnectAsync(CancellationToken cancellationToken = default)
{
WasDisconnected = true;
return ValueTask.CompletedTask;
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
private sealed class FakePreview : ILivePreview
{
public VideoStreamDescriptor Stream { get; } = new(VideoCodec.H264, 1, 1);
public bool WasStopped { get; private set; }
public async IAsyncEnumerable<EncodedVideoPacket> ReadPacketsAsync(
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
yield return new EncodedVideoPacket(new byte[] { 1 }, null, null, 1, 1, true);
await Task.CompletedTask;
}
public ValueTask StopAsync(CancellationToken cancellationToken = default)
{
WasStopped = true;
return ValueTask.CompletedTask;
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
private sealed class FakeDecoder : ILiveVideoDecoder
{
public async IAsyncEnumerable<DecodedVideoFrame> DecodeAsync(
VideoStreamDescriptor stream,
IAsyncEnumerable<EncodedVideoPacket> packets,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await foreach (var packet in packets.WithCancellation(cancellationToken))
{
yield return new DecodedVideoFrame(1, 1, 4, new byte[] { 0, 0, 0, 255 });
yield break;
}
}
}
}