Path: redb-route/tests/redb.Route.Tests.Llm/LlmServiceCollectionExtensionsTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Llm/LlmServiceCollectionExtensionsTests.cs ---- using Microsoft.Extensions.DependencyInjection; using redb.Route.Llm.Engine.Storage; using redb.Route.Llm.Extensions; using redb.Route.Llm.Storage.Redb; namespace redb.Route.Tests.Llm; public sealed class LlmServiceCollectionExtensionsTests { [Fact] public void AddRedbRouteLlm_RegistersComponentEngineAndRegistry() { var services = new ServiceCollection(); var ctx = new RouteContext(); services.AddSingleton(ctx); services.AddRedbRouteLlm(); var sp = services.BuildServiceProvider(); sp.GetRequiredService().Should().BeOfType(); sp.GetRequiredService().Should().BeOfType(); // The startup hook RouteHostedService applies foreach (var configurator in sp.GetServices()) configurator.Configure(ctx); ctx.GetComponent("llm").Should().NotBeNull(); } [Fact] public void AddLlmConnectionFactory_RegistersFactoryByName() { var services = new ServiceCollection(); var ctx = new RouteContext(); services.AddSingleton(ctx); services.AddRedbRouteLlm(); services.AddLlmConnectionFactory("claude", f => { f.Provider = "stub"; f.ModelId = "haiku"; }); var sp = services.BuildServiceProvider(); foreach (var configurator in sp.GetServices()) configurator.Configure(ctx); var factory = ctx.GetFromRegistry("claude"); factory.Should().NotBeNull(); factory!.Name.Should().Be("claude"); factory.Provider.Should().Be("stub"); factory.ModelId.Should().Be("haiku"); } [Fact] public void AddLlmConnectionFactory_NameWinsOverConfigureOverride() { var services = new ServiceCollection(); var ctx = new RouteContext(); services.AddSingleton(ctx); services.AddRedbRouteLlm(); services.AddLlmConnectionFactory("registered-name", f => { f.Name = "evil-rename"; // user attempted override f.Provider = "stub"; }); var sp = services.BuildServiceProvider(); foreach (var configurator in sp.GetServices()) configurator.Configure(ctx); var factory = ctx.GetFromRegistry("registered-name"); factory.Should().NotBeNull(); factory!.Name.Should().Be("registered-name"); ctx.GetFromRegistry("evil-rename").Should().BeNull(); } [Fact] public void AddLlmConnectionFactory_NullName_Throws() { var services = new ServiceCollection(); var act = () => services.AddLlmConnectionFactory(null!, _ => { }); act.Should().Throw(); } [Fact] public void AddLlmConnectionFactory_NullConfigure_Throws() { var services = new ServiceCollection(); var act = () => services.AddLlmConnectionFactory("c", null!); act.Should().Throw(); } // ── The tool idempotency store: an in-memory default that any other registration replaces ── private static ServiceProvider Build(Action register) { var services = new ServiceCollection(); services.AddSingleton(new RouteContext()); register(services); return services.BuildServiceProvider(); } private sealed class HostStore : IToolIdempotencyStore { public Task TryReserveAsync(string conversationId, string toolUseId, IExchange? exchange = null, CancellationToken ct = default) => Task.FromResult(ToolIdempotencyReservation.NewReservation); public Task CompleteAsync(string conversationId, string toolUseId, string outputJson, IExchange? exchange = null, CancellationToken ct = default) => Task.CompletedTask; public Task ReleaseAsync(string conversationId, string toolUseId, IExchange? exchange = null, CancellationToken ct = default) => Task.CompletedTask; } [Fact] public async Task AddRedbRouteLlm_Alone_RegistersAnInMemoryToolIdempotencyStore_ThatDeduplicates() { await using var sp = Build(s => s.AddRedbRouteLlm()); var store = sp.GetRequiredService(); store.Should().BeOfType(); (await store.TryReserveAsync("conv", "tu-1")).IsNew.Should().BeTrue(); await store.CompleteAsync("conv", "tu-1", """{"sent":true}"""); var again = await store.TryReserveAsync("conv", "tu-1"); again.IsNew.Should().BeFalse("a repeated call with the same key gets the stored output"); again.CachedOutputJson.Should().Be("""{"sent":true}"""); } [Fact] public async Task AddRedbLlmStorage_Wins_WhenCalledAfterAddRedbRouteLlm() { await using var sp = Build(s => { s.AddRedbRouteLlm(); s.AddRedbLlmStorage(); }); sp.GetRequiredService().Should().BeOfType(); } [Fact] public async Task AddRedbLlmStorage_Wins_WhenCalledBeforeAddRedbRouteLlm() { await using var sp = Build(s => { s.AddRedbLlmStorage(); s.AddRedbRouteLlm(); }); sp.GetRequiredService().Should().BeOfType(); } [Fact] public async Task AHostsOwnStore_Wins_WhenRegisteredBeforeAddRedbRouteLlm() { await using var sp = Build(s => { s.AddSingleton(); s.AddRedbRouteLlm(); }); sp.GetRequiredService().Should().BeOfType(); } [Fact] public async Task AHostsOwnStore_Wins_WhenRegisteredAfterAddRedbRouteLlm() { await using var sp = Build(s => { s.AddRedbRouteLlm(); s.AddSingleton(); }); sp.GetRequiredService().Should().BeOfType(); } }