package org.terst.feedreader.data import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test /** * The reported bug came with "extractedContent cached: true", which is the half * that a fixed extractor cannot reach: the cookie disclaimer was already written * to `articles.extractedContent`, and every open after that read it straight back * out without ever looking at it again. * * So a poisoned cache has to be recognised and dropped on read, not just kept out * on write. */ class CachedExtractionTest { private val cookieNotice = "

This website uses " + "cookies and other tracking technologies (“Cookies”) to enhance user experience, to " + "display targeted ads, and to analyze performance and user activity on our website. For " + "more information about our use of Cookies and our privacy practices, see our Privacy " + "Policy. By continuing to use this website, you agree to our Terms and Conditions.

" + "
" @Test fun `real cached content is used as-is and the cache is left alone`() = runTest { var cleared = false val content = "

" + "The actual article body. ".repeat(60) + "

" val result = loadCachedExtraction(content) { cleared = true } assertEquals(content, result.content) assertFalse(result.discardedConsentNotice) assertFalse("a good cache must not be cleared", cleared) } @Test fun `a cached consent notice is discarded and deleted`() = runTest { var cleared = false val result = loadCachedExtraction(cookieNotice) { cleared = true } assertNull("the notice must not be shown as the article", result.content) assertTrue(result.discardedConsentNotice) assertTrue("the poisoned row must be cleared so a re-extract can replace it", cleared) } @Test fun `nothing cached touches nothing`() = runTest { var cleared = false val result = loadCachedExtraction(null) { cleared = true } assertNull(result.content) assertFalse(result.discardedConsentNotice) assertFalse(cleared) } // A blank value is as good as nothing cached -- it is not a consent notice, so // reporting it as a discarded one would make the caller re-extract on a false // premise. @Test fun `a blank cached value is not reported as a discarded notice`() = runTest { var cleared = false val result = loadCachedExtraction(" ") { cleared = true } assertFalse(result.discardedConsentNotice) assertFalse(cleared) } }