AutoForge/src/tests/AutoForge.IntegrationTests/UiCompositionTests.cs

212 lines
9.1 KiB
C#

using AutoForge.Application.ExecutionTargets;
using AutoForge.Application.Features.Vehicles.Profiles;
using AutoForge.Application.Features.Vehicles.Diagnostics;
using AutoForge.Core.ExecutionTargets;
using AutoForge.UI;
using AutoForge.UI.Features.ExecutionTargets.SelectExecutionTarget;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace AutoForge.IntegrationTests;
public sealed class UiCompositionTests
{
[Fact]
public async Task Routes_HomePageUsesReusableStatusNoticeAndExplainsCurrentScope()
{
using var provider = CreateDiagnosticProvider("http://localhost/");
var html = await RenderRoutesAsync(provider);
Assert.Contains("<h1>AutoForge foundation</h1>", html);
Assert.Contains("Review execution-target selection", html);
Assert.Contains("status-notice--information", html);
Assert.Contains("static synthetic diagnostics", html);
Assert.Contains("fixed-step synthetic dynamics workflow", html);
Assert.Contains("not manufacturer-validated", html);
Assert.Contains("Target selection is metadata only", html);
}
[Fact]
public async Task Routes_SelectionPageResolvesSyntheticMetadataThroughRealApplicationService()
{
var expected = new ExecutionTargetDescriptor(
"synthetic.simulation",
ExecutionTargetKind.Simulation);
using var provider = CreateProvider([expected], "http://localhost/execution-targets");
var selection = provider.GetRequiredService<ExecutionTargetSelectionViewModel>();
selection.ProviderIdentifier = "SYNTHETIC.SIMULATION";
selection.SelectedKind = ExecutionTargetKind.Simulation;
selection.Select();
var html = await RenderRoutesAsync(provider);
Assert.Contains("Select execution-target metadata", html);
Assert.Contains("status-notice--success", html);
Assert.Contains("data-provider=\"synthetic.simulation\"", html);
Assert.Contains("data-kind=\"Simulation\"", html);
Assert.Contains("Resolved registered metadata for synthetic.simulation (Simulation)", html);
Assert.Contains("does not connect to a vehicle or authorize an operation", html);
Assert.DoesNotContain("connected", html, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("write-approved", html, StringComparison.OrdinalIgnoreCase);
Assert.Same(expected, selection.SelectedDescriptor);
}
[Fact]
public async Task Routes_EmptyRegistryAndUnknownProviderRemainVisibleWithoutFallback()
{
using var provider = CreateProvider([], "http://localhost/execution-targets");
var selection = provider.GetRequiredService<ExecutionTargetSelectionViewModel>();
selection.ProviderIdentifier = "synthetic.absent";
selection.SelectedKind = ExecutionTargetKind.PhysicalHardware;
selection.Select();
var html = await RenderRoutesAsync(provider);
Assert.Contains("status-notice--error", html);
Assert.Contains("Provider &#x27;synthetic.absent&#x27; is not registered.", html);
Assert.Null(selection.SelectedDescriptor);
Assert.Contains("does not connect to a vehicle, execute an operation or grant write approval", html);
}
[Fact]
public async Task Routes_RejectsMissingOrInvalidExplicitInputsInRenderedState()
{
using var provider = CreateProvider(
[new ExecutionTargetDescriptor("synthetic.simulation", ExecutionTargetKind.Simulation)],
"http://localhost/execution-targets");
var selection = provider.GetRequiredService<ExecutionTargetSelectionViewModel>();
selection.ProviderIdentifier = " ";
selection.SelectedKind = ExecutionTargetKind.Simulation;
selection.Select();
var blankIdentifierHtml = await RenderRoutesAsync(provider);
Assert.Contains("Enter an explicit provider identifier.", blankIdentifierHtml);
Assert.Null(selection.SelectedDescriptor);
selection.ProviderIdentifier = "synthetic.simulation";
selection.SelectedKind = null;
selection.Select();
var missingKindHtml = await RenderRoutesAsync(provider);
Assert.Contains("Choose an explicit execution-target kind.", missingKindHtml);
Assert.Null(selection.SelectedDescriptor);
selection.ProviderIdentifier = "synthetic.simulation ";
selection.SelectedKind = ExecutionTargetKind.Simulation;
selection.Select();
var invalidIdentifierHtml = await RenderRoutesAsync(provider);
Assert.Contains("status-notice--error", invalidIdentifierHtml);
Assert.Null(selection.SelectedDescriptor);
}
[Fact]
public async Task Routes_RejectsProviderKindMismatchAndDoesNotFallback()
{
using var provider = CreateProvider(
[
new ExecutionTargetDescriptor("synthetic.selected", ExecutionTargetKind.Simulation),
new ExecutionTargetDescriptor("synthetic.other", ExecutionTargetKind.PhysicalHardware)
],
"http://localhost/execution-targets");
var selection = provider.GetRequiredService<ExecutionTargetSelectionViewModel>();
selection.ProviderIdentifier = "synthetic.selected";
selection.SelectedKind = ExecutionTargetKind.PhysicalHardware;
selection.Select();
var html = await RenderRoutesAsync(provider);
Assert.Contains("Provider &#x27;synthetic.selected&#x27; is registered for Simulation", html);
Assert.Contains("not the requested PhysicalHardware", html);
Assert.Contains("no fallback is allowed", html);
Assert.DoesNotContain("data-kind=", html);
Assert.Null(selection.SelectedDescriptor);
}
[Fact]
public async Task Routes_UnknownLocationShowsExplicitNotFoundPageAndNavigation()
{
using var provider = CreateProvider([], "http://localhost/not-a-route");
var html = await RenderRoutesAsync(provider);
Assert.Contains("<h1>Page not found</h1>", html);
Assert.Contains("That AutoForge page does not exist.", html);
Assert.Contains("aria-label=\"Main navigation\"", html);
Assert.Contains("href=\"/execution-targets\"", html);
}
private static ServiceProvider CreateProvider(
IEnumerable<ExecutionTargetDescriptor> registrations,
string uri)
{
var services = new ServiceCollection();
services.AddLogging();
services.AddAutoForgeTargetSelection(registrations);
services.AddAutoForgeVehicleProfiles();
services.AddAutoForgeUi();
services.AddSingleton<AutoForge.Application.Features.DisplayUnits.IDisplayUnitPreferenceStore, IsolatedDisplayUnitPreferenceStore>();
services.AddSingleton<NavigationManager>(new TestNavigationManager(uri));
services.AddSingleton<INavigationInterception, TestNavigationInterception>();
services.AddSingleton<IScrollToLocationHash, TestScrollToLocationHash>();
return services.BuildServiceProvider();
}
private static ServiceProvider CreateDiagnosticProvider(string uri)
{
var services = new ServiceCollection();
services.AddLogging();
services.AddAutoForgeVehicleProfiles();
services.AddAutoForgeDiagnostics(
[new AutoForge.Simulation.SimulatedDiagnosticTransportFactory(
AutoForge.Simulation.SyntheticDiagnosticExamples.CreateDefinitions())]);
services.AddAutoForgeUi();
services.AddSingleton<AutoForge.Application.Features.DisplayUnits.IDisplayUnitPreferenceStore, IsolatedDisplayUnitPreferenceStore>();
services.AddSingleton<NavigationManager>(new TestNavigationManager(uri));
services.AddSingleton<INavigationInterception, TestNavigationInterception>();
services.AddSingleton<IScrollToLocationHash, TestScrollToLocationHash>();
return services.BuildServiceProvider();
}
private static async Task<string> RenderRoutesAsync(IServiceProvider services)
{
await using var renderer = new HtmlRenderer(
services,
services.GetRequiredService<ILoggerFactory>());
return await renderer.Dispatcher.InvokeAsync(async () =>
{
var component = await renderer.RenderComponentAsync<AutoForge.UI.Routes>();
return component.ToHtmlString();
});
}
private sealed class TestNavigationManager : NavigationManager
{
public TestNavigationManager(string uri)
{
Initialize("http://localhost/", uri);
}
protected override void NavigateToCore(string uri, bool forceLoad)
{
Uri = ToAbsoluteUri(uri).ToString();
NotifyLocationChanged(forceLoad);
}
}
private sealed class TestNavigationInterception : INavigationInterception
{
public Task EnableNavigationInterceptionAsync() => Task.CompletedTask;
}
private sealed class TestScrollToLocationHash : IScrollToLocationHash
{
public Task RefreshScrollPositionForHash(string locationAbsolute) => Task.CompletedTask;
}
}