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.Recording; using AutoForge.Application.Features.Vehicles.Telemetry.Replay; using AutoForge.Core.ExecutionTargets; using AutoForge.Core.Simulation; using AutoForge.Core.Vehicles; using AutoForge.Diagnostics; using AutoForge.Simulation; using AutoForge.UI; using Microsoft.Extensions.DependencyInjection; namespace AutoForge.IntegrationTests; public sealed partial class TelemetryReplayTests { [Fact] public async Task ReplayCoexistsWithRealLiveSource_WithoutReadsModelCallsOrRecordingWorkspaceRebind() { using var fixture = await ReplayFixture.CreateAsync(); var source = new CountingDynamicsFactory( new DynamicsDiagnosticTransportFactory(SyntheticDiagnosticExamples.CreateDefinitions())); var services = new ServiceCollection(); services.AddAutoForgeVehicleProfiles(); services.AddAutoForgeDiagnostics([source]); services.AddAutoForgeDynamicsConfigurations(); services.AddAutoForgeUi(); services.AddSingleton(fixture.Store); services.AddSingleton(fixture.Clock); services.AddSingleton(fixture.Waiter); services.AddSingleton(); using var provider = services.BuildServiceProvider(); provider.GetRequiredService().Import( File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "synthetic-truck-baseline.json"))); var configuration = provider.GetRequiredService().CreateExample(ProfileId, 1); await using var live = await provider.GetRequiredService().OpenDynamicsAsync( ProfileId, 1, source.Descriptor.Target.ProviderIdentifier, ExecutionTargetKind.Simulation, configuration); await live.DynamicsControl!.StepAsync(7); var workspace = provider.GetRequiredService(); workspace.Start(live); var endpoint = live.ExpectedEndpoints.Single(item => item.ModuleIdentifier == ModuleA); Assert.True((await live.ReadSignalsAsync(endpoint)).IsSuccess); workspace.Stop(); var oldRecording = TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!); var oldTrace = live.Trace.Entries.Count; var oldStatus = live.DynamicsControl.Status; var sourceCalls = source.TransportCalls; var modelCalls = source.ModelCalls; var factory = provider.GetRequiredService(); await using var player = factory.Create(); try { await player.LoadAsync(fixture.CaptureId); Configure(player); player.SeekTime(TimeSpan.FromSeconds(4).Ticks); player.Previous(); player.First(); var preference = provider.GetRequiredService(); Assert.True(preference.TrySet(new DisplayUnitSelection(DisplayUnitSystem.Metric, SpeedDisplayMode.Road))); Assert.True(preference.TrySet(DisplayUnitSelection.Default)); await player.PlayAsync(); var wait = await fixture.Waiter.TakeAsync(); var stop = player.StopAsync(); wait.Release(); await stop.WaitAsync(Deadline); Assert.Equal(sourceCalls, source.TransportCalls); Assert.Equal(modelCalls, source.ModelCalls); Assert.Equal(oldTrace, live.Trace.Entries.Count); Assert.Equal(oldStatus, live.DynamicsControl.Status); Assert.Equal(oldRecording, TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!)); Assert.NotEqual(workspace.CurrentCapture!.Header.CaptureId, player.Snapshot.Context!.CaptureId); Assert.Equal(0, fixture.Store.SaveCalls); Assert.Equal(fixture.Bytes, await File.ReadAllBytesAsync(fixture.ArchivePath)); Assert.True((await live.ReadSignalsAsync(endpoint)).IsSuccess); Assert.Equal(sourceCalls + 1, source.TransportCalls); Assert.Equal(modelCalls, source.ModelCalls); Assert.Equal(oldRecording, TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!)); } finally { fixture.Waiter.ReleaseAll(); await player.StopAsync(); } } private sealed class CountingDynamicsFactory(IDynamicsDiagnosticTransportFactory inner) : IDynamicsDiagnosticTransportFactory { public int TransportCalls { get; private set; } public int ModelCalls { get; private set; } public DiagnosticProviderDescriptor Descriptor => inner.Descriptor; public IDiagnosticTransport OpenTransport(VehicleProfile profile, DiagnosticSessionIdentity identity) => throw new InvalidOperationException("This counting source requires an explicit dynamics configuration."); public DynamicsSessionBinding OpenDynamicsTransport(VehicleProfile profile, DiagnosticSessionIdentity identity, DynamicsConfiguration configuration) { var binding = inner.OpenDynamicsTransport(profile, identity, configuration); return new(new CountingTransport(binding.Transport, () => TransportCalls++), new CountingControl(binding.Control, () => ModelCalls++)); } } private sealed class CountingTransport(IDiagnosticTransport inner, Action count) : IDiagnosticTransport { public IReadOnlyList ExpectedEndpoints => inner.ExpectedEndpoints; public ValueTask SendAsync(ReadOnlyMemory request, CancellationToken cancellationToken) { count(); return inner.SendAsync(request, cancellationToken); } public ValueTask DisposeAsync() => inner.DisposeAsync(); } private sealed class CountingControl(IDynamicsControl inner, Action count) : IDynamicsControl { public DynamicsRunStatus Status => inner.Status; public double PacingFactor => inner.PacingFactor; private Task Invoke(Func> action) { count(); return action(); } public Task StartAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.StartAsync(cancellationToken)); public Task PauseAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.PauseAsync(cancellationToken)); public Task ResumeAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.ResumeAsync(cancellationToken)); public Task StopAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.StopAsync(cancellationToken)); public Task ResetAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.ResetAsync(cancellationToken)); public Task StepAsync(int stepCount = 1, CancellationToken cancellationToken = default) => Invoke(() => inner.StepAsync(stepCount, cancellationToken)); public Task SetPacingFactorAsync(double factor, CancellationToken cancellationToken = default) => Invoke(() => inner.SetPacingFactorAsync(factor, cancellationToken)); public Task RunScenarioAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.RunScenarioAsync(cancellationToken)); public ValueTask DisposeAsync() => inner.DisposeAsync(); } }