Path: redb/redb.Tests.Integration/Tests/Base/ListItemObjectWalkTestsBase.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/ListItemObjectWalkTestsBase.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// Reading
/// IS a database load, so no reflective walk of a loaded graph may read it: a walk
/// that does fires one load per linked list item on every materialization (the production phantom-load class of
/// 2026-09-09) and, on Pro SQLite, throws through the sync path. The core walkers were closed then; the Pro
/// materialization walkers and the props-cache dirty guard were not.
///
/// Detector: . A read of Object on the live scope loads the object and keeps it
/// (owner decision 2026-09-15), and no walk swallows a failed read any more (plan, item 4). Every test isolates ONE walker
/// and ends with its positive control - one explicit read loads the object - so a road the detector does not cover fails
/// the test instead of hiding.
///
///
/// Free hosts depend on plan part C: until then the Free JSON builders dropped IdObject of list items and the
/// "carries its object link" precondition failed before any walker was judged.
///
///
public abstract class ListItemObjectWalkTestsBase
{
protected abstract void UseProvider(RedbOptionsBuilder options);
protected virtual void Register(IServiceCollection services, Action configure)
=> services.AddRedb(configure);
private ServiceProvider Build(Action configure)
{
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
Register(services, options =>
{
UseProvider(options);
options.Configure(c =>
{
c.EnablePropsCache = false;
c.SkipHashValidationOnCacheCheck = false;
// Own cache domain: this process also holds the shared fixtures on the same database.
c.CacheDomain = "listitem-walk";
configure(c);
});
});
return services.BuildServiceProvider();
}
private sealed record Seed(long RootId, long LinkedObjectId);
private static async Task BootAsync(ServiceProvider sp)
{
var boot = sp.GetRequiredService();
await boot.InitializeAsync(ensureCreated: true);
await boot.SyncSchemeAsync();
await boot.SyncSchemeAsync();
await boot.SyncSchemeAsync();
await boot.SyncSchemeAsync();
await boot.InitializeTypeRegistryAsync();
return boot;
}
private static async Task<(RedbListItem Linked, RedbListItem Plain, long LinkedObjectId)> SeedListAsync(IRedbService redb, string tag)
{
var linkedObjectId = await redb.SaveAsync(new RedbObject
{ name = $"liwalk-linked-{tag}", Props = new SimpleProps { Title = "linked" } });
var list = await redb.ListProvider.SaveListAsync(RedbList.Create($"liwalk-{tag}", "liwalk"));
var linked = await redb.ListProvider.SaveListItemAsync(new RedbListItem { IdList = list.Id, Value = "linked", IdObject = linkedObjectId });
var plain = await redb.ListProvider.SaveListItemAsync(new RedbListItem { IdList = list.Id, Value = "plain" });
return (linked, plain, linkedObjectId);
}
/// Props with references AND list items, one of them linked to an object.
private static async Task SeedWithReferencesAsync(ServiceProvider sp, string tag, bool onlyTrashedReference = false)
{
var redb = await BootAsync(sp);
var (linked, plain, linkedObjectId) = await SeedListAsync(redb, tag);
var c1 = await redb.SaveAsync(new RedbObject { name = $"liwalk-c1-{tag}", Props = new CtProbeChildProps { Tag = "one" } });
var c2 = await redb.SaveAsync(new RedbObject { name = $"liwalk-c2-{tag}", Props = new CtProbeChildProps { Tag = "two" } });
var props = new CtProbeProps { Label = "root", Status = linked, Roles = [linked, plain] };
if (onlyTrashedReference)
{
// The only reference points at an object trashed below: nothing is substituted (the substitution walk
// returns at once on an empty set) and the null-missing walk is the one that runs.
props.Refs = [new RedbObject { id = c2 }];
}
else
{
props.Refs = [new RedbObject { id = c1 }, new RedbObject { id = c2 }];
props.RefDict = new() { ["first"] = new RedbObject { id = c1 } };
}
var rootId = await redb.SaveAsync(new RedbObject { name = $"liwalk-root-{tag}", Props = props });
if (onlyTrashedReference)
await redb.SoftDeleteAsync(new[] { c2 });
return new Seed(rootId, linkedObjectId);
}
/// Props with list items and no references: its first load runs none of the materialization walkers.
private static async Task SeedPersonAsync(ServiceProvider sp, string tag)
{
var redb = await BootAsync(sp);
var (linked, plain, linkedObjectId) = await SeedListAsync(redb, tag);
var rootId = await redb.SaveAsync(new RedbObject
{
name = $"liwalk-person-{tag}",
Props = new PersonProps { Name = "Person", Age = 30, Email = "p@example.com", Status = linked, Roles = [linked, plain] }
});
return new Seed(rootId, linkedObjectId);
}
private static string NewTag() => Guid.NewGuid().ToString("N")[..8];
///
/// No walk read Object: every given instance still carries its link and is not loaded; then one explicit read loads it.
///
private static void AssertNotWoken(string path, long linkedObjectId, params RedbListItem?[] linkedItems)
{
foreach (var item in linkedItems)
{
item.Should().NotBeNull($"{path}: the linked list item must be materialized");
item!.IdObject.Should().Be(linkedObjectId,
$"{path}: precondition - the item carries its object link, or 'not woken' proves nothing");
item.IsObjectLoaded.Should().BeFalse(
$"{path}: no walk may read RedbListItem.Object - reading it IS a database load per linked item");
}
linkedItems[0]!.Object.Should().NotBeNull(
$"{path}: positive control - an explicit read of Object loads it, so the detector is live");
linkedItems[0]!.IsObjectLoaded.Should().BeTrue();
}
[Fact]
public async Task Load_WithReferences_SubstitutionWalk_DoesNotWakeLinkedListItems()
{
await using var sp = Build(_ => { });
var seed = await SeedWithReferencesAsync(sp, NewTag());
await using var scope = sp.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var root = await redb.LoadAsync(seed.RootId, depth: 10);
root!.Props.Refs.Should().HaveCount(2).And.OnlyContain(r => r != null && r.IsPropsLoaded,
"the references are materialized, so the substitution walk ran");
AssertNotWoken("load with references", seed.LinkedObjectId, root.Props.Status, root.Props.Roles![0]);
}
[Fact]
public async Task Load_WithTrashedReferenceTarget_NullMissingWalk_DoesNotWakeLinkedListItems()
{
await using var sp = Build(_ => { });
var seed = await SeedWithReferencesAsync(sp, NewTag(), onlyTrashedReference: true);
await using var scope = sp.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var root = await redb.LoadAsync(seed.RootId, depth: 10);
root!.Props.Refs.Should().ContainSingle().Which.Should().BeNull("the trashed target is nulled, so the null-missing walk ran");
AssertNotWoken("load with a trashed reference target", seed.LinkedObjectId, root.Props.Status, root.Props.Roles![0]);
}
[Fact]
public async Task Load_WithLazyReferences_StubWalk_DoesNotWakeLinkedListItems()
{
await using var sp = Build(c => c.EnableLazyReferences = true);
var seed = await SeedWithReferencesAsync(sp, NewTag());
await using var scope = sp.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var root = await redb.LoadAsync(seed.RootId, depth: 1);
root!.Props.Refs.Should().HaveCount(2).And.OnlyContain(r => r != null && !r.IsPropsLoaded && r.hash != null,
"the references are enriched stubs, so the stub walk ran");
AssertNotWoken("load with lazy references", seed.LinkedObjectId, root.Props.Status, root.Props.Roles![0]);
}
/// First load fills the cache without any walker; the second is served from it.
private async Task CacheHitAsync(Action configure, string path,
Func?>> secondLoad)
{
await using var sp = Build(c => { c.EnablePropsCache = true; configure(c); });
var seed = await SeedPersonAsync(sp, NewTag());
((RedbServiceBase)sp.GetRequiredService()).PropsCache.Instance!.Clear();
await using (var first = sp.CreateAsyncScope())
{
var cached = await first.ServiceProvider.GetRequiredService().LoadAsync(seed.RootId, depth: 10);
cached.Should().NotBeNull();
cached!.Props.Status!.IsObjectLoaded.Should().BeFalse($"{path}: precondition - the load that fills the cache wakes nothing");
}
await using var second = sp.CreateAsyncScope();
var served = await secondLoad(second.ServiceProvider.GetRequiredService(), seed.RootId);
AssertNotWoken(path, seed.LinkedObjectId, served!.Props.Status, served.Props.Roles![0]);
}
[Fact]
public Task CacheHit_PointLoad_DirtyGuard_DoesNotWakeLinkedListItems()
=> CacheHitAsync(_ => { }, "cache hit, point load",
(redb, id) => redb.LoadAsync(id, depth: 10));
[Fact]
public Task CacheHit_WithoutHashValidation_DirtyGuard_DoesNotWakeLinkedListItems()
=> CacheHitAsync(c => c.SkipHashValidationOnCacheCheck = true, "cache hit without hash validation",
(redb, id) => redb.LoadAsync(id, depth: 10));
[Fact]
public Task CacheHit_BulkLoad_DirtyGuard_DoesNotWakeLinkedListItems()
=> CacheHitAsync(_ => { }, "cache hit, bulk load",
async (redb, id) => (RedbObject)(await redb.LoadAsync(new[] { id }, depth: 10))[0]);
[Fact]
public Task CacheHit_SyncPointLoad_DirtyGuard_DoesNotWakeLinkedListItems()
=> CacheHitAsync(_ => { }, "cache hit, sync point load",
(redb, id) => Task.FromResult(redb.Load(id, depth: 10)));
}