@@ -19,13 +19,14 @@ public class ProviderTests
1919 private readonly Mock < TimeProvider > _timeProvider = new ( ) ;
2020 private readonly Mock < ILogger < Provider > > _logger = new ( ) ;
2121 private readonly Mock < IOptions < ReferenceNumberOptions > > _options = new ( ) ;
22+ private readonly MemoryCache _memoryCache = new ( new MemoryCacheOptions ( ) ) ;
2223
2324 private readonly IProvider _sut ;
2425
2526 public ProviderTests ( )
2627 {
2728 _options . Setup ( x => x . Value ) . Returns ( new ReferenceNumberOptions { HmacKeyVersion = 1 , HmacKey = HmacTestSecretKey } ) ;
28- _sut = new Provider ( _options . Object , _bookingReferenceDocumentStore . Object , new MemoryCache ( new MemoryCacheOptions ( ) ) , _logger . Object , _timeProvider . Object ) ;
29+ _sut = new Provider ( _options . Object , _bookingReferenceDocumentStore . Object , _memoryCache , _logger . Object , _timeProvider . Object ) ;
2930 }
3031
3132 [ Fact ]
@@ -79,6 +80,50 @@ public async Task GetReferenceNumber_GeneratesCorrectlyFormattedNumber_Determini
7980 result . Should ( ) . Be ( "3825-69268-6774" ) ;
8081 }
8182
83+ [ Fact ]
84+ public async Task GetReferenceNumber_UsesCachedMultiplierDerivationIfExists ( )
85+ {
86+ _memoryCache . TryGetValue ( "1:3825" , out var _ ) . Should ( ) . BeFalse ( ) ;
87+ _memoryCache . TryGetValue ( "1:4625" , out var _ ) . Should ( ) . BeFalse ( ) ;
88+
89+ _timeProvider . Setup ( x => x . GetUtcNow ( ) ) . Returns ( new DateTime ( 2025 , 5 , 30 , 9 , 0 , 59 ) ) ;
90+ _bookingReferenceDocumentStore . Setup ( x => x . GetNextSequenceNumber ( ) ) . ReturnsAsync ( 2345123 ) ;
91+
92+ var result1 = await _sut . GetReferenceNumber ( ) ;
93+ result1 . Should ( ) . Be ( "3825-69268-6774" ) ;
94+
95+ //prove value is set
96+ _memoryCache . TryGetValue ( "1:3825" , out var multiplier1 ) . Should ( ) . BeTrue ( ) ;
97+ _memoryCache . TryGetValue ( "1:4625" , out var _ ) . Should ( ) . BeFalse ( ) ;
98+
99+ multiplier1 . Should ( ) . Be ( 28980599 ) ;
100+
101+ _timeProvider . Setup ( x => x . GetUtcNow ( ) ) . Returns ( new DateTime ( 2025 , 5 , 31 , 9 , 0 , 59 ) ) ;
102+ _bookingReferenceDocumentStore . Setup ( x => x . GetNextSequenceNumber ( ) ) . ReturnsAsync ( 2345124 ) ;
103+ var result2 = await _sut . GetReferenceNumber ( ) ;
104+ result2 . Should ( ) . Be ( "3825-98249-2768" ) ;
105+
106+ _memoryCache . TryGetValue ( "1:3825" , out var multiplier2 ) . Should ( ) . BeTrue ( ) ;
107+ _memoryCache . TryGetValue ( "1:3725" , out var _ ) . Should ( ) . BeFalse ( ) ;
108+ _memoryCache . TryGetValue ( "1:3925" , out var _ ) . Should ( ) . BeFalse ( ) ;
109+ _memoryCache . TryGetValue ( "1:4625" , out var _ ) . Should ( ) . BeFalse ( ) ;
110+
111+ multiplier2 . Should ( ) . Be ( 28980599 ) ;
112+
113+ _timeProvider . Setup ( x => x . GetUtcNow ( ) ) . Returns ( new DateTime ( 2025 , 6 , 30 , 9 , 0 , 59 ) ) ;
114+ _bookingReferenceDocumentStore . Setup ( x => x . GetNextSequenceNumber ( ) ) . ReturnsAsync ( 2345125 ) ;
115+
116+ var result3 = await _sut . GetReferenceNumber ( ) ;
117+ result3 . Should ( ) . Be ( "4625-68731-6257" ) ;
118+
119+ //prove new value is set
120+ _memoryCache . TryGetValue ( "1:3825" , out var multiplier3 ) . Should ( ) . BeTrue ( ) ;
121+ _memoryCache . TryGetValue ( "1:4625" , out var multiplier4 ) . Should ( ) . BeTrue ( ) ;
122+
123+ multiplier3 . Should ( ) . Be ( 28980599 ) ;
124+ multiplier4 . Should ( ) . Be ( 84432373 ) ;
125+ }
126+
82127 [ Fact ]
83128 public async Task GetReferenceNumber_GeneratesCorrectlyFormattedNumber_MinDate ( )
84129 {
0 commit comments