@@ -259,7 +259,7 @@ class BlocktankRepoTest : BaseUnitTest() {
259259 }
260260
261261 @Test
262- fun `getCjitEntry falls back to cached entries when refresh fails ` () = test {
262+ fun `getCjitEntry returns cached entry without refreshing when already associated ` () = test {
263263 sut = createSut()
264264 val fundingTxId = " cached-funding-tx"
265265 val channel = mock<IBtChannel >()
@@ -271,11 +271,40 @@ class BlocktankRepoTest : BaseUnitTest() {
271271 BlocktankBackupV1 (createdAt = 0L , orders = emptyList(), cjitEntries = listOf (cachedEntry)),
272272 )
273273
274+ val channelDetails = mock<ChannelDetails >()
275+ whenever(channelDetails.fundingTxo).thenReturn(OutPoint (txid = fundingTxId, vout = 0u ))
276+
277+ // A server refresh would return no entries (setUp default), so a non-null result can only come
278+ // from the cached state short-circuit, proving the server is not hit when the entry is already known.
279+ assertEquals(cachedEntry, sut.getCjitEntry(channelDetails))
280+ }
281+
282+ @Test
283+ fun `getCjitEntry returns null when cache misses and refresh fails` () = test {
284+ sut = createSut()
274285 whenever(coreService.blocktank.cjitEntries(refresh = true )).thenThrow(RuntimeException (" Network error" ))
275286
287+ val channelDetails = mock<ChannelDetails >()
288+ whenever(channelDetails.fundingTxo).thenReturn(OutPoint (txid = " missing-funding-tx" , vout = 0u ))
289+
290+ assertNull(sut.getCjitEntry(channelDetails))
291+ }
292+
293+ @Test
294+ fun `getCjitEntry retries the refresh and matches after a transient failure` () = test {
295+ sut = createSut()
296+ val fundingTxId = " cjit-funding-tx"
297+ val channel = mock<IBtChannel >()
298+ whenever(channel.fundingTx).thenReturn(FundingTx (id = fundingTxId, vout = 0u ))
299+ val entry = mock<IcJitEntry >()
300+ whenever(entry.channel).thenReturn(channel)
301+ whenever(coreService.blocktank.cjitEntries(refresh = true ))
302+ .thenThrow(RuntimeException (" transient" ))
303+ .thenReturn(listOf (entry))
304+
276305 val channelDetails = mock<ChannelDetails >()
277306 whenever(channelDetails.fundingTxo).thenReturn(OutPoint (txid = fundingTxId, vout = 0u ))
278307
279- assertEquals(cachedEntry , sut.getCjitEntry(channelDetails))
308+ assertEquals(entry , sut.getCjitEntry(channelDetails))
280309 }
281310}
0 commit comments