using System.Text; using System.Text.Json; using System.Text.Json.Nodes; using System.Threading.Channels; using AutoForge.Application.Features.DisplayUnits; using AutoForge.Application.Features.Vehicles.Diagnostics; using AutoForge.Application.Features.Vehicles.Profiles; using AutoForge.Application.Features.Vehicles.Simulation; using AutoForge.Application.Features.Vehicles.Telemetry.LiveDashboard; using AutoForge.Application.Features.Vehicles.Telemetry.Recording; using AutoForge.Application.Features.Vehicles.Telemetry.Replay; using AutoForge.Core.ExecutionTargets; using AutoForge.Diagnostics; using AutoForge.Simulation; using AutoForge.UI; using Microsoft.Extensions.DependencyInjection; namespace AutoForge.IntegrationTests; public sealed partial class TelemetryReplayTests { private const string ProfileId = "autoforge.synthetic.test-truck"; private const string ModuleA = "module.engine.primary"; private const string ModuleB = "module.cabin.body"; private const string SignalId = "recorded.measurement"; private static readonly TimeSpan Deadline = TimeSpan.FromSeconds(10); [Fact] public async Task OfflineLoad_PreindexesOnlyMetadataAndReconstructsTheExactFourRecordPrefix() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); var source = player.Snapshot.Archive!; var context = player.Snapshot.Context!; Assert.Equal(ExecutionTargetKind.RecordedReplay, context.Kind); Assert.Equal(fixture.CaptureId, context.CaptureId); Assert.Equal(ExecutionTargetKind.Simulation, source.Header.TargetKind); Assert.Equal(ReplayState.Stopped, player.Snapshot.State); Assert.Equal(0, player.Snapshot.AppliedCount); Assert.Equal(0, player.Snapshot.PositionTicks); Assert.Equal(4 * TimeSpan.TicksPerSecond, player.Snapshot.DurationTicks); Assert.Equal(2, player.Snapshot.AvailableSignals.Count); Configure(player); Assert.All(player.Snapshot.Values, value => Assert.Null(value.Signal)); player.SeekTime(TimeSpan.FromSeconds(1.5).Ticks); AssertPrefix(player, 1, 1.5, 850, DashboardReadingState.Good, null); player.SeekTime(TimeSpan.FromSeconds(2).Ticks); AssertPrefix(player, 3, 2, 1000, DashboardReadingState.Uncertain, 12.6); Assert.Equal(DashboardReadingState.OutOfRangeHigh, player.Snapshot.Values[0].RangeState); Assert.Equal(100, player.Snapshot.Values[0].BarPercent); player.SeekTime(TimeSpan.FromSeconds(4).Ticks); AssertPrefix(player, 4, 4, 850, DashboardReadingState.Stale, 12.6); var stale = player.Snapshot.Values[0]; Assert.Equal(source.Observations[0].SignalSnapshot!.Signals[0].LogicalTimeTicks, stale.Signal!.LogicalTimeTicks); Assert.Equal(1, player.Snapshot.SampleContexts[stale.Definition.GaugeIdentifier].Sequence); Assert.Equal(TimeSpan.TicksPerSecond, player.Snapshot.SampleContexts[stale.Definition.GaugeIdentifier].CaptureElapsedTicks); Assert.Contains("Timeout", stale.Reason); Assert.Equal(ReplayState.Completed, player.Snapshot.State); Assert.Equal(850d / 900 * 100, player.Snapshot.Values[0].BarPercent); player.Previous(); AssertPrefix(player, 3, 2, 1000, DashboardReadingState.Uncertain, 12.6); player.SeekOrdinal(1); AssertPrefix(player, 1, 1, 850, DashboardReadingState.Good, null); Assert.DoesNotContain(player.Snapshot.Modules, status => status.ModuleIdentifier == ModuleB); player.First(); Assert.Equal(0, player.Snapshot.AppliedCount); Assert.All(player.Snapshot.Values, value => Assert.Null(value.NumericValue)); Assert.Empty(player.Snapshot.Modules); Assert.Equal(2, player.Snapshot.Definitions.Count); Assert.Same(context, player.Snapshot.Context); Assert.Equal(0, fixture.Store.SaveCalls); Assert.Equal(fixture.Bytes, await File.ReadAllBytesAsync(fixture.ArchivePath)); } [Fact] public async Task EqualAndZeroTimes_ManualNextIsOneButTimeSeekAndPlayApplyCompleteGroups() { using var fixture = await ReplayFixture.CreateAsync(allZero: true); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Configure(player); Assert.Equal(0, player.Snapshot.DurationTicks); player.Next(); Assert.Equal(1, player.Snapshot.AppliedCount); Assert.Equal(ReplayState.Paused, player.Snapshot.State); player.Next(); Assert.Equal(2, player.Snapshot.AppliedCount); player.SeekTime(0); Assert.Equal(4, player.Snapshot.AppliedCount); Assert.Equal(ReplayState.Completed, player.Snapshot.State); player.First(); await player.PlayAsync(); Assert.Equal(4, player.Snapshot.AppliedCount); Assert.Equal(ReplayState.Completed, player.Snapshot.State); Assert.Equal(0, fixture.Waiter.WaitCount); await player.StopAsync(); Assert.Equal(0, player.Snapshot.AppliedCount); Assert.All(player.Snapshot.Values, value => Assert.Null(value.Signal)); } [Fact] public async Task EmptyAndNonSignalArchives_NeverFabricateMeasurementsOrUseUtcDuration() { using var fixture = await ReplayFixture.CreateAsync(empty: true); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Assert.Empty(player.Snapshot.AvailableSignals); Assert.Equal(0, player.Snapshot.DurationTicks); await player.PlayAsync(); Assert.Equal(ReplayState.Completed, player.Snapshot.State); Assert.Equal(0, fixture.Waiter.WaitCount); player.SeekTime(0); Assert.Equal(ReplayState.Stopped, player.Snapshot.State); AssertRejected(player, () => player.Next(), ReplayFailure.NoRecords); AssertRejected(player, () => player.Previous(), ReplayFailure.NoRecords); await player.RestartAsync(); Assert.Equal(ReplayState.Completed, player.Snapshot.State); using var nonSignal = await ReplayFixture.CreateAsync(nonSignal: true); await using var other = nonSignal.CreatePlayer(); await other.LoadAsync(nonSignal.CaptureId); other.Next(); Assert.Equal(DiagnosticServices.ReadTroubleCodes, other.Snapshot.CurrentObservation!.Service); Assert.Empty(other.Snapshot.Values); Assert.Empty(other.Snapshot.Modules); Assert.Equal(1, other.Snapshot.AppliedCount); } [Theory] [InlineData(0.25)] [InlineData(1)] [InlineData(1000)] public async Task ControlledPacing_ScalesCaptureIntervalsWithoutChangingRecordedResults(double rate) { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Configure(player); player.SetRate(rate); await player.PlayAsync(); var firstWait = await fixture.Waiter.TakeAsync(); try { Assert.Equal(TimeSpan.FromSeconds(1 / rate), firstWait.Interval); fixture.Clock.Advance(firstWait.Interval); firstWait.Release(); var secondWait = await fixture.Waiter.TakeAsync(); Assert.Equal(1, player.Snapshot.AppliedCount); Assert.Equal(TimeSpan.FromSeconds(1 / rate), secondWait.Interval); fixture.Clock.Advance(secondWait.Interval); secondWait.Release(); var thirdWait = await fixture.Waiter.TakeAsync(); AssertPrefix(player, 3, 2, 1000, DashboardReadingState.Uncertain, 12.6); Assert.Equal(TimeSpan.FromSeconds(2 / rate), thirdWait.Interval); fixture.Clock.Advance(thirdWait.Interval); thirdWait.Release(); await WaitForStateAsync(player, ReplayState.Completed); AssertPrefix(player, 4, 4, 850, DashboardReadingState.Stale, 12.6); } finally { fixture.Waiter.ReleaseAll(); await player.StopAsync(); } } [Fact] public async Task MidGapPause_DrainsOneOwnerAndResumeWaitsOnlyTheRemainingInterval() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Configure(player); player.SeekTime(TimeSpan.FromSeconds(1.5).Ticks); player.SetRate(2); await player.PlayAsync(); var wait = await fixture.Waiter.TakeAsync(); try { Assert.Equal(TimeSpan.FromSeconds(0.25), wait.Interval); await player.PlayAsync(); Assert.Equal(1, fixture.Waiter.WaitCount); fixture.Clock.Advance(TimeSpan.FromSeconds(0.10)); var pause = player.PauseAsync(); Assert.False(pause.IsCompleted); Assert.True(player.Snapshot.IsDraining); Assert.Equal(TimeSpan.FromSeconds(1.7).Ticks, player.Snapshot.PositionTicks); Assert.Equal(1, player.Snapshot.AppliedCount); var state = player.Snapshot; AssertRejected(player, () => player.Next(), ReplayFailure.Busy); AssertRejected(player, () => player.SetRate(10), ReplayFailure.Busy); await Assert.ThrowsAsync(() => player.PlayAsync()); await Assert.ThrowsAsync(() => player.LoadAsync(fixture.CaptureId)); Assert.Equal(state.Context, player.Snapshot.Context); fixture.Clock.Advance(TimeSpan.FromSeconds(3)); wait.Release(); await pause.WaitAsync(Deadline); Assert.Equal(TimeSpan.FromSeconds(1.7).Ticks, player.Snapshot.PositionTicks); Assert.False(player.Snapshot.IsDraining); await player.PlayAsync(); var resumed = await fixture.Waiter.TakeAsync(); Assert.Equal(TimeSpan.FromSeconds(0.15), resumed.Interval); fixture.Clock.Advance(resumed.Interval); resumed.Release(); var next = await fixture.Waiter.TakeAsync(); AssertPrefix(player, 3, 2, 1000, DashboardReadingState.Uncertain, 12.6); var stop = player.StopAsync(); Assert.Same(stop, player.StopAsync()); Assert.False(stop.IsCompleted); AssertRejected(player, () => player.First(), ReplayFailure.Busy); next.Release(); await stop.WaitAsync(Deadline); Assert.Equal(ReplayState.Stopped, player.Snapshot.State); Assert.All(player.Snapshot.Values, value => Assert.Null(value.Signal)); } finally { fixture.Waiter.ReleaseAll(); await player.StopAsync(); } } [Fact] public async Task WaitFault_IsVisibleAndKeepsCommittedPrefixUntilExplicitRecovery() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Configure(player); player.SeekOrdinal(1); await player.PlayAsync(); var wait = await fixture.Waiter.TakeAsync(); wait.Fail(new IOException("controlled replay wait failure")); await WaitForStateAsync(player, ReplayState.Faulted); Assert.Equal(1, player.Snapshot.AppliedCount); Assert.Equal(850, player.Snapshot.Values[0].NumericValue); Assert.Equal(DashboardReadingState.Good, player.Snapshot.Values[0].State); Assert.Contains("controlled replay wait failure", player.Snapshot.Error); await Assert.ThrowsAsync(() => player.PlayAsync()); AssertRejected(player, () => player.Next(), ReplayFailure.Faulted); AssertRejected(player, () => player.SeekTime(0), ReplayFailure.Faulted); AssertRejected(player, () => player.SeekOrdinal(0), ReplayFailure.Faulted); await Assert.ThrowsAsync(() => player.RestartAsync()); await player.StopAsync(); Assert.Equal(ReplayState.Stopped, player.Snapshot.State); Assert.Equal(0, player.Snapshot.AppliedCount); } [Fact] public async Task InvalidRequests_AreTypedVisibleAndAtomicIncludingClosedAndUnloadedPlayers() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); AssertRejected(player, () => player.Next(), ReplayFailure.NotLoaded); await player.LoadAsync(fixture.CaptureId); Configure(player); player.SeekOrdinal(1); foreach (var rate in new[] { double.NaN, double.PositiveInfinity, double.NegativeInfinity, 0.249, 1000.001 }) { AssertRejected(player, () => player.SetRate(rate), ReplayFailure.InvalidRate); } AssertRejected(player, () => player.SeekTime(-1), ReplayFailure.InvalidPosition); AssertRejected(player, () => player.SeekTime(player.Snapshot.DurationTicks + 1), ReplayFailure.InvalidPosition); AssertRejected(player, () => player.SeekOrdinal(5), ReplayFailure.InvalidPosition); AssertRejected(player, () => player.SeekOrdinal(-1), ReplayFailure.InvalidPosition); var original = player.Snapshot.Definitions; var invalid = original[0] with { Unit = "RPM" }; Assert.Throws(() => player.ReplaceDefinitions([invalid])); Assert.Equal(original, player.Snapshot.Definitions); Assert.Throws(() => player.ReplaceDefinitions( Enumerable.Range(0, 9).Select(index => original[0] with { GaugeIdentifier = $"g{index}" }))); Assert.Equal(original, player.Snapshot.Definitions); foreach (var definition in new[] { original[0] with { Label = " " }, original[0] with { Kind = (DashboardGaugeKind)99 }, original[0] with { Minimum = double.NaN }, original[0] with { Minimum = -double.MaxValue, Maximum = double.MaxValue }, original[0] with { Minimum = 1, Maximum = 1 }, original[0] with { DecimalPrecision = 4 } }) { var before = player.Snapshot; Assert.Throws(() => player.ReplaceDefinitions([definition])); AssertUnchanged(before, player.Snapshot); } Assert.Throws(() => player.ReplaceDefinitions([original[0], original[0] with { GaugeIdentifier = "A" }])); player.ReplaceDefinitions(Enumerable.Range(0, 8).Select(index => original[0] with { GaugeIdentifier = $"g{index}" })); Assert.Equal(8, player.Snapshot.Values.Count); player.ReplaceDefinitions([]); Assert.Empty(player.Snapshot.Values); Assert.Equal(1, player.Snapshot.AppliedCount); await player.DisposeAsync(); AssertRejected(player, () => player.Next(), ReplayFailure.Closed); Assert.Equal(0, fixture.Store.SaveCalls); } [Theory] [InlineData(TelemetryCaptureState.Incomplete)] [InlineData(TelemetryCaptureState.Error)] public async Task PartialAcknowledgement_IsPerLoadAndEndNeverRewritesOriginalCompletion(TelemetryCaptureState sourceState) { using var fixture = await ReplayFixture.CreateAsync(partial: sourceState); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); AssertRejected(player, () => player.Next(), ReplayFailure.AcknowledgementRequired); AssertRejected(player, () => player.SeekTime(0), ReplayFailure.AcknowledgementRequired); AssertRejected(player, () => player.SeekOrdinal(0), ReplayFailure.AcknowledgementRequired); await Assert.ThrowsAsync(() => player.PlayAsync()); player.First(); await player.StopAsync(); player.AcknowledgePartial(); player.SeekOrdinal(4); Assert.Equal(sourceState, player.Snapshot.Archive!.Completion!.State); Assert.Equal(ReplayState.Completed, player.Snapshot.State); await player.StopAsync(); Assert.True(player.Snapshot.PartialAcknowledged); player.SetRate(0.25); var identity = player.Snapshot.Context; await player.LoadAsync(fixture.CaptureId); Assert.NotEqual(identity, player.Snapshot.Context); Assert.False(player.Snapshot.PartialAcknowledged); Assert.Equal(0.25, player.Snapshot.Rate); Assert.Empty(player.Snapshot.Definitions); } [Fact] public async Task FailedCancelledAndLateLoads_PreserveValidatedContextAndCloseDrainsWithoutStalePublication() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); Configure(player); player.SeekOrdinal(1); var before = player.Snapshot; await Assert.ThrowsAsync(() => player.LoadAsync(new string('f', 32))); AssertUnchanged(before, player.Snapshot); Assert.NotNull(player.Snapshot.Error); using var cancellation = new CancellationTokenSource(); fixture.Store.HoldLoads = true; var load = player.LoadAsync(fixture.CaptureId, cancellation.Token); await fixture.Store.LoadEntered.Task.WaitAsync(Deadline); AssertRejected(player, () => player.Next(), ReplayFailure.Busy); cancellation.Cancel(); fixture.Store.ReleaseLoad(); await Assert.ThrowsAnyAsync(() => load); AssertUnchanged(before, player.Snapshot); fixture.Store.ResetLoadBarrier(); var late = player.LoadAsync(fixture.CaptureId); await fixture.Store.LoadEntered.Task.WaitAsync(Deadline); var close = player.DisposeAsync().AsTask(); Assert.Same(close, player.DisposeAsync().AsTask()); Assert.False(close.IsCompleted); fixture.Store.ReleaseLoad(); await Assert.ThrowsAnyAsync(() => late); await close.WaitAsync(Deadline); Assert.Equal(ReplayState.Closed, player.Snapshot.State); Assert.Same(before.Context, player.Snapshot.Context); Assert.Same(before.Archive, player.Snapshot.Archive); } [Fact] public async Task CloseWithDelayedOldWake_NeverPublishesIntoClosedOrFreshFactorySession() { using var fixture = await ReplayFixture.CreateAsync(); await using var player = fixture.CreatePlayer(); await player.LoadAsync(fixture.CaptureId); await player.PlayAsync(); var wait = await fixture.Waiter.TakeAsync(); try { var close = player.DisposeAsync().AsTask(); Assert.False(close.IsCompleted); fixture.Clock.Advance(TimeSpan.FromSeconds(20)); await using var fresh = fixture.CreatePlayer(); await fresh.LoadAsync(fixture.CaptureId); wait.Release(); await close.WaitAsync(Deadline); Assert.Equal(ReplayState.Closed, player.Snapshot.State); Assert.Equal(0, player.Snapshot.AppliedCount); Assert.Equal(ReplayState.Stopped, fresh.Snapshot.State); Assert.Equal(0, fresh.Snapshot.AppliedCount); Assert.NotEqual(player.Snapshot.Context, fresh.Snapshot.Context); } finally { fixture.Waiter.ReleaseAll(); } } private static void Configure(TelemetryReplaySession player) => player.ReplaceDefinitions( [ new("a", "Recorded A", DashboardGaugeKind.HorizontalBar, ModuleA, SignalId, "rpm", 0, 900, 0), new("b", "Recorded B", DashboardGaugeKind.Numeric, ModuleB, SignalId, "V", 0, 15, 1) ]); private static void AssertPrefix(TelemetryReplaySession player, int count, double seconds, double a, DashboardReadingState state, double? b) { var snapshot = player.Snapshot; Assert.Equal(count, snapshot.AppliedCount); Assert.Equal(TimeSpan.FromSeconds(seconds).Ticks, snapshot.PositionTicks); Assert.Equal(a, snapshot.Values[0].NumericValue); Assert.Equal(state, snapshot.Values[0].State); Assert.Equal(b, snapshot.Values[1].NumericValue); if (b is null) { Assert.Null(snapshot.Values[1].Signal); } } private static void AssertRejected(TelemetryReplaySession player, Action command, ReplayFailure failure) { var previous = player.Snapshot; var exception = Assert.Throws(command); Assert.Equal(failure, exception.Failure); AssertUnchanged(previous, player.Snapshot); Assert.Equal(exception.Message, player.Snapshot.Error); } private static void AssertUnchanged(TelemetryReplaySnapshot previous, TelemetryReplaySnapshot current) { Assert.Same(previous.Context, current.Context); Assert.Same(previous.Archive, current.Archive); Assert.Equal(previous.State, current.State); Assert.Equal(previous.PositionTicks, current.PositionTicks); Assert.Equal(previous.AppliedCount, current.AppliedCount); Assert.Equal(previous.Rate, current.Rate); Assert.Equal(previous.Definitions, current.Definitions); Assert.Equal(previous.Values, current.Values); Assert.Equal(previous.IsDraining, current.IsDraining); } private static async Task WaitForStateAsync(TelemetryReplaySession player, ReplayState state) { var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); void Check() { if (player.Snapshot.State == state && !player.Snapshot.IsDraining) { completion.TrySetResult(); } } player.Changed += Check; try { Check(); await completion.Task.WaitAsync(Deadline); } finally { player.Changed -= Check; } } private sealed class ManualReplayClock : TimeProvider { private long _ticks; public Exception? Failure { get; set; } public override long TimestampFrequency => TimeSpan.TicksPerSecond; public override long GetTimestamp() => Failure is { } failure ? throw failure : Interlocked.Read(ref _ticks); public override DateTimeOffset GetUtcNow() => DateTimeOffset.UnixEpoch.AddTicks(GetTimestamp()); public void Advance(TimeSpan amount) => Interlocked.Add(ref _ticks, amount.Ticks); } private sealed class ControlledReplayWaiter : IReplayWaiter { private readonly Channel _requests = Channel.CreateUnbounded(); private readonly List _all = []; private bool _releasedAll; public int WaitCount { get; private set; } public async Task WaitAsync(TimeSpan interval, CancellationToken cancellationToken) { var request = new WaitRequest(interval, cancellationToken); lock (_all) { _all.Add(request); WaitCount++; if (_releasedAll) request.Release(); } using var registration = cancellationToken.Register(() => request.CancellationRequested.TrySetResult()); _requests.Writer.TryWrite(request); await request.Completion.Task; } public async Task TakeAsync() => await _requests.Reader.ReadAsync().AsTask().WaitAsync(Deadline); public void ReleaseAll() { lock (_all) { _releasedAll = true; foreach (var request in _all) { request.Release(); } } } } private sealed record WaitRequest(TimeSpan Interval, CancellationToken Cancellation) { public TaskCompletionSource Completion { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public TaskCompletionSource CancellationRequested { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public void Release() => Completion.TrySetResult(); public void Fail(Exception exception) => Completion.TrySetException(exception); } private sealed class ReadOnlySpyStore(ITelemetryArchiveStore inner) : ITelemetryArchiveStore { private TaskCompletionSource _release = NewBarrier(); public TaskCompletionSource LoadEntered { get; private set; } = NewBarrier(); public bool HoldLoads { get; set; } public int SaveCalls { get; private set; } public Task SaveAsync(TelemetryCaptureSnapshot capture, CancellationToken cancellationToken = default) { SaveCalls++; throw new InvalidOperationException("Replay attempted an archive write."); } public Task> ListAsync(CancellationToken cancellationToken = default) => inner.ListAsync(cancellationToken); public async Task LoadAsync(string captureId, CancellationToken cancellationToken = default) { if (HoldLoads) { LoadEntered.TrySetResult(); await _release.Task; } return await inner.LoadAsync(captureId, CancellationToken.None); } public void ReleaseLoad() => _release.TrySetResult(); public void ResetLoadBarrier() { _release = NewBarrier(); LoadEntered = NewBarrier(); } private static TaskCompletionSource NewBarrier() => new(TaskCreationOptions.RunContinuationsAsynchronously); } private sealed class ReplayFixture : IDisposable { public string DirectoryPath { get; } = Path.Combine(Path.GetTempPath(), $"autoforge-step08-{Guid.NewGuid():N}"); public string CaptureId { get; private set; } = string.Empty; public string ArchivePath => Path.Combine(DirectoryPath, $"{CaptureId}.json"); public byte[] Bytes { get; private set; } = []; public ManualReplayClock Clock { get; } = new(); public ControlledReplayWaiter Waiter { get; } = new(); public ReadOnlySpyStore Store { get; private set; } = null!; public TelemetryReplaySession CreatePlayer() => new(Store, Clock, Waiter); public static async Task CreateAsync(bool allZero = false, bool empty = false, bool nonSignal = false, TelemetryCaptureState? partial = null, bool dynamic = false, bool resetModel = false) { var fixture = new ReplayFixture(); try { Directory.CreateDirectory(fixture.DirectoryPath); var services = new ServiceCollection(); services.AddAutoForgeVehicleProfiles(); var call = 0; var original = new SimulatedDiagnosticTransportFactory(SyntheticDiagnosticExamples.CreateDefinitions()); var transport = new RewritingDiagnosticTransportFactory(original, async (inner, request, bytes, token) => { if (request.Service != DiagnosticServices.ReadSignals) { return await inner.SendAsync(bytes, token); } call++; if (call == 4) { return new TransportReply(TransportOutcome.Timeout, ReadOnlyMemory.Empty, 1, "Timeout fixture"); } var reply = await inner.SendAsync(bytes, token); var node = JsonNode.Parse(reply.Payload.Span)!.AsObject(); node["data"]!["signals"] = new JsonArray(new JsonObject { ["signalIdentifier"] = SignalId, ["value"] = call == 2 ? JsonValue.Create(12.6) : JsonValue.Create(call == 3 ? 1000 : 850), ["unit"] = call == 2 ? "V" : "rpm", ["quality"] = call == 3 ? "Uncertain" : "Good" }); return new TransportReply(TransportOutcome.Response, Encoding.UTF8.GetBytes(node.ToJsonString()), reply.ElapsedTicks); }); services.AddAutoForgeDiagnostics([transport, new DynamicsDiagnosticTransportFactory(SyntheticDiagnosticExamples.CreateDefinitions())]); services.AddAutoForgeDynamicsConfigurations(); services.AddAutoForgeTelemetryRecording(fixture.DirectoryPath); services.AddSingleton(fixture.Clock); services.AddAutoForgeTelemetryRecordingFallback(); using (var provider = services.BuildServiceProvider()) { provider.GetRequiredService().Import( File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "synthetic-truck-baseline.json"))); var sessionService = provider.GetRequiredService(); var session = dynamic ? await sessionService.OpenDynamicsAsync(ProfileId, 1, DynamicsDiagnosticTransportFactory.ProviderIdentifier, ExecutionTargetKind.Simulation, provider.GetRequiredService().CreateExample(ProfileId, 1)) : await sessionService.OpenAsync(ProfileId, 1, original.Descriptor.Target.ProviderIdentifier, ExecutionTargetKind.Simulation); await using (session) { if (resetModel) await session.DynamicsControl!.StepAsync(3); var recording = provider.GetRequiredService(); recording.Start(session); if (!empty) { var a = session.ExpectedEndpoints.Single(endpoint => endpoint.ModuleIdentifier == ModuleA); var b = session.ExpectedEndpoints.Single(endpoint => endpoint.ModuleIdentifier == (dynamic ? "module.transmission.primary" : ModuleB)); if (nonSignal) { fixture.Clock.Advance(TimeSpan.FromSeconds(1)); Assert.True((await session.ReadTroubleCodesAsync(a)).IsSuccess); } else { if (!allZero) fixture.Clock.Advance(TimeSpan.FromSeconds(1)); Assert.True((await session.ReadSignalsAsync(a)).IsSuccess); if (resetModel) await session.DynamicsControl!.ResetAsync(); if (!allZero) fixture.Clock.Advance(TimeSpan.FromSeconds(1)); Assert.True((await session.ReadSignalsAsync(b)).IsSuccess); Assert.True((await session.ReadSignalsAsync(a)).IsSuccess); if (!allZero) fixture.Clock.Advance(TimeSpan.FromSeconds(2)); var last = await session.ReadSignalsAsync(a); Assert.Equal(dynamic ? DiagnosticOutcome.PositiveResponse : DiagnosticOutcome.Timeout, last.Outcome); } } recording.Stop(); var capture = recording.CurrentCapture!; if (partial is { } state) { capture = new TelemetryCaptureSnapshot(capture.Header, capture.Observations, capture.Completion! with { State = state, StopReason = state == TelemetryCaptureState.Incomplete ? TelemetryCaptureStopReason.LimitExceeded : TelemetryCaptureStopReason.ObservationFailure, Error = state == TelemetryCaptureState.Error ? "Recorded source error" : null }); } fixture.CaptureId = capture.Header.CaptureId; await provider.GetRequiredService().SaveAsync(capture); } } fixture.Bytes = await File.ReadAllBytesAsync(fixture.ArchivePath); fixture.Store = new ReadOnlySpyStore(new FileTelemetryArchiveStore(fixture.DirectoryPath)); return fixture; } catch { fixture.Dispose(); throw; } } public void Dispose() { Waiter.ReleaseAll(); Store?.ReleaseLoad(); if (Directory.Exists(DirectoryPath)) { Directory.Delete(DirectoryPath, recursive: true); } } } }