513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
139 lines
6.3 KiB
C#
139 lines
6.3 KiB
C#
using Akka.TestKit.Xunit2;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Grpc.Core;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.Communication.Grpc;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Communication.Tests;
|
|
|
|
/// <summary>
|
|
/// Flow Debugger (design 2026-09-05 §5.4): <see cref="SiteStreamGrpcServer.PullTraceEvents"/>
|
|
/// — request validation, the queue point-read, DTO projection, and the guarantee that it is
|
|
/// NOT a reconciliation path (no cursor flip). Also pins that the RPC sits under the
|
|
/// PSK-gated <c>/sitestream.SiteStreamService/</c> prefix.
|
|
/// </summary>
|
|
public class SiteStreamPullTraceEventsTests : TestKit
|
|
{
|
|
private readonly ISiteStreamSubscriber _subscriber = Substitute.For<ISiteStreamSubscriber>();
|
|
|
|
private SiteStreamGrpcServer CreateServer() => new(_subscriber, NullLogger<SiteStreamGrpcServer>.Instance);
|
|
|
|
private static ServerCallContext NewContext()
|
|
{
|
|
var context = Substitute.For<ServerCallContext>();
|
|
context.CancellationToken.Returns(CancellationToken.None);
|
|
return context;
|
|
}
|
|
|
|
private static AuditEvent Row(Guid executionId) => ScadaBridgeAuditEventFactory.Create(
|
|
AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
|
|
occurredAtUtc: new DateTime(2026, 9, 5, 12, 0, 0, DateTimeKind.Utc), executionId: executionId, sourceSiteId: "site-a");
|
|
|
|
/// <summary>Verifies that the pull-trace-events RPC sits under the preshared-key-gated service prefix.</summary>
|
|
[Fact]
|
|
public void PullTraceEvents_IsUnderThePskGatedServicePrefix()
|
|
{
|
|
var method = SiteStreamService.Descriptor.FindMethodByName("PullTraceEvents");
|
|
Assert.NotNull(method);
|
|
Assert.Equal("sitestream.SiteStreamService", SiteStreamService.Descriptor.FullName);
|
|
}
|
|
|
|
/// <summary>Verifies that the RPC returns an empty result when no queue is wired.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task NoQueueWired_ReturnsEmpty()
|
|
{
|
|
var response = await CreateServer().PullTraceEvents(new PullTraceEventsRequest
|
|
{
|
|
ExecutionIds = { Guid.NewGuid().ToString("D") }, MaxEvents = 10,
|
|
}, NewContext());
|
|
Assert.Empty(response.Events);
|
|
}
|
|
|
|
/// <summary>Verifies that the RPC reads events by id, projects them to DTOs and never changes their state.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ReadsByIds_ProjectsDtos_AndNeverFlipsState()
|
|
{
|
|
var queue = Substitute.For<ISiteAuditQueue>();
|
|
var id = Guid.NewGuid();
|
|
var rows = new List<AuditEvent> { Row(id), Row(id) };
|
|
IReadOnlyCollection<Guid>? seenIds = null;
|
|
DateTime seenSince = default;
|
|
int seenLimit = 0;
|
|
queue.ReadByExecutionIdsAsync(
|
|
Arg.Do<IReadOnlyCollection<Guid>>(x => seenIds = x), Arg.Do<DateTime>(x => seenSince = x),
|
|
Arg.Do<int>(x => seenLimit = x), Arg.Any<CancellationToken>())
|
|
.Returns((IReadOnlyList<AuditEvent>)rows);
|
|
var server = CreateServer();
|
|
server.SetSiteAuditQueue(queue);
|
|
var since = new DateTime(2026, 9, 5, 11, 0, 0, DateTimeKind.Utc);
|
|
|
|
var response = await server.PullTraceEvents(new PullTraceEventsRequest
|
|
{
|
|
ExecutionIds = { id.ToString("D") }, SinceUtc = Timestamp.FromDateTime(since), MaxEvents = 500,
|
|
}, NewContext());
|
|
|
|
Assert.Equal(2, response.Events.Count);
|
|
Assert.Equal(rows[0].EventId.ToString(), response.Events[0].EventId);
|
|
Assert.Equal(new[] { id }, seenIds);
|
|
Assert.Equal(since, seenSince);
|
|
Assert.Equal(500, seenLimit);
|
|
await queue.DidNotReceiveWithAnyArgs().MarkReconciledUpToAsync(default, default, default);
|
|
await queue.DidNotReceiveWithAnyArgs().MarkReconciledAsync(default!, default);
|
|
}
|
|
|
|
/// <summary>Verifies that invalid arguments are rejected.</summary>
|
|
/// <param name="maxEvents">The requested maximum number of events.</param>
|
|
/// <param name="idCount">The number of ids supplied in the request.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Theory]
|
|
[InlineData(0, 1)]
|
|
[InlineData(2001, 1)]
|
|
[InlineData(10, 0)]
|
|
[InlineData(10, 65)]
|
|
public async Task InvalidArguments_AreRejected(int maxEvents, int idCount)
|
|
{
|
|
var server = CreateServer();
|
|
server.SetSiteAuditQueue(Substitute.For<ISiteAuditQueue>());
|
|
var request = new PullTraceEventsRequest { MaxEvents = maxEvents };
|
|
for (var i = 0; i < idCount; i++) request.ExecutionIds.Add(Guid.NewGuid().ToString("D"));
|
|
|
|
var ex = await Assert.ThrowsAsync<RpcException>(() => server.PullTraceEvents(request, NewContext()));
|
|
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
}
|
|
|
|
/// <summary>Verifies that an id that is not a GUID is rejected.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task NonGuidId_IsRejected()
|
|
{
|
|
var server = CreateServer();
|
|
server.SetSiteAuditQueue(Substitute.For<ISiteAuditQueue>());
|
|
var ex = await Assert.ThrowsAsync<RpcException>(() => server.PullTraceEvents(
|
|
new PullTraceEventsRequest { ExecutionIds = { "nope" }, MaxEvents = 10 }, NewContext()));
|
|
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
}
|
|
|
|
/// <summary>Verifies that a queue fault collapses to an empty result.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task QueueFault_CollapsesToEmpty()
|
|
{
|
|
var queue = Substitute.For<ISiteAuditQueue>();
|
|
queue.ReadByExecutionIdsAsync(default!, default, default, default)
|
|
.ReturnsForAnyArgs<IReadOnlyList<AuditEvent>>(_ => throw new InvalidOperationException("boom"));
|
|
var server = CreateServer();
|
|
server.SetSiteAuditQueue(queue);
|
|
|
|
var response = await server.PullTraceEvents(
|
|
new PullTraceEventsRequest { ExecutionIds = { Guid.NewGuid().ToString("D") }, MaxEvents = 10 }, NewContext());
|
|
Assert.Empty(response.Events);
|
|
}
|
|
}
|