Path: redb/redb.Tests.Integration/Tests/Unit/HashCalculatorDiagTests.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/Unit/HashCalculatorDiagTests.cs
----
using redb.Core.Utils;
namespace redb.Tests.Integration.Tests.Unit;
///
/// DIAGNOSTIC — does RedbHash.ComputeFor actually distinguish a token whose only difference is Status?
/// If Valid and Redeemed hash to the SAME value, the cache's hash-validation can never see the redeem,
/// and LoadAsync returns a stale snapshot — matching the DIAG report (hit=True, dbHash stable).
/// No DB — pure calculator check. Mirrors redb.Identity TokenProps shape.
///
public sealed class HashCalculatorDiagTests
{
// Same shape as redb.Identity.Core TokenProps.
private sealed class TokenLikeProps
{
public long ApplicationObjectId { get; set; }
public long AuthorizationObjectId { get; set; }
public string? Status { get; set; }
public string? Type { get; set; }
public string? ReferenceId { get; set; }
public string? Payload { get; set; }
public Dictionary? Properties { get; set; }
public string? ActorSubject { get; set; }
}
private static TokenLikeProps Token(string status) => new()
{
ApplicationObjectId = 111,
AuthorizationObjectId = 222,
Status = status,
Type = "access_token",
ReferenceId = "ref-abc",
Payload = "{\"jwt\":\"x\"}",
Properties = new Dictionary { ["scope"] = "openid", ["k"] = "v" },
ActorSubject = "sub-1",
};
[Fact]
public void Status_ChangesHash()
{
var valid = RedbHash.ComputeForProps(Token("valid"));
var redeemed = RedbHash.ComputeForProps(Token("redeemed"));
(valid != redeemed).Should().BeTrue(
"changing only Status MUST change the hash — otherwise the cache can never see a redeem");
}
[Fact]
public void SameState_SameHash_Deterministic()
{
// Two identical tokens (incl. same Dictionary) must hash equal across repeated computes —
// otherwise Dictionary enumeration order makes the hash unstable (false cache misses / chaos).
var a = RedbHash.ComputeForProps(Token("valid"));
var b = RedbHash.ComputeForProps(Token("valid"));
(a == b).Should().BeTrue( "identical props (including Dictionary) must hash identically every time");
}
[Fact]
public void DictionaryOrder_DoesNotAffectHash()
{
var t1 = Token("valid");
t1.Properties = new Dictionary { ["a"] = "1", ["b"] = "2" };
var t2 = Token("valid");
t2.Properties = new Dictionary { ["b"] = "2", ["a"] = "1" }; // reversed insert order
(RedbHash.ComputeForProps(t1) == RedbHash.ComputeForProps(t2)).Should().BeTrue(
"Dictionary content, not insertion order, must define the hash");
}
}