Yokan / rebuild
Course 1 8624561

NovelEmbeddedContentTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelEmbeddedContentTest.kt

test / fixture43 lines
1package eu.friedcutlet.yokan.ui.screens.novel23import kotlinx.coroutines.CompletableDeferred4import kotlinx.coroutines.async5import kotlinx.coroutines.awaitCancellation6import kotlinx.coroutines.cancelAndJoin7import kotlinx.coroutines.test.runTest8import kotlinx.coroutines.yield9import kotlin.test.*1011class NovelEmbeddedContentTest {12    @Test fun embeddedImagesDeduplicateAndKeepExternalImagesAndLinks() = runTest {13        val reads = mutableListOf<String>()14        val bytes = byteArrayOf(1, 2, 3)15        val html = "<p>Before</p><img src='../Images/a.png'><img src='../Images/a.png'><img src='https://img.test/a'><p>After</p>"16        val images = loadNovelEmbeddedImages(html) { reads += it; yield(); bytes }17        val parsed = NovelDocument(html, embeddedImages = images).parse()1819        assertEquals(listOf("../Images/a.png"), reads)20        val blocks = parsed.blocks.filterIsInstance<NovelContentBlock.Image>()21        assertContentEquals(bytes, assertIs<NovelImageSource.Embedded>(blocks[0].source).bytes)22        assertSame(assertIs<NovelImageSource.Embedded>(blocks[0].source).bytes, assertIs<NovelImageSource.Embedded>(blocks[1].source).bytes)23        assertEquals("https://img.test/a", assertIs<NovelImageSource.Remote>(blocks[2].source).url)24        assertTrue(parsed.plainText.startsWith("Before"))25        assertTrue(parsed.plainText.endsWith("After"))26    }2728    @Test fun cancellationWhileAwaitingAnImageStopsBeforeTheNextResource() = runTest {29        val entered = CompletableDeferred<Unit>()30        val reads = mutableListOf<String>()31        var delivered = false32        val work = async {33            loadNovelEmbeddedImages("<img src='one.png'><img src='two.png'>") {34                reads += it; entered.complete(Unit); awaitCancellation()35            }36            delivered = true37        }38        entered.await()39        work.cancelAndJoin()40        assertEquals(listOf("one.png"), reads)41        assertFalse(delivered)42    }43}44

Search chapters, reference and source filenames.