package eu.friedcutlet.yokan.services.files import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File import java.net.URI import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue class AndroidSafLocalArchiveServiceTest { @Test fun fullRescanRefreshesStagedBooksWithoutProviderTimestamps() { val root = testDirectory() var title = "before" var opens = 0 val book = "content://test/document/book.epub" try { val service = AndroidSafLocalArchiveService(root.absolutePath, contentStreamOpener = { opens++ ByteArrayInputStream(epub(title)) }) assertEquals("before", service.listEpubChapters(book).single().title) title = "after" assertEquals("before", service.listEpubChapters(book).single().title) assertEquals(1, opens) service.invalidateIndexCache() assertEquals("after", service.listEpubChapters(book).single().title) assertEquals(2, opens) } finally { root.deleteRecursively() } } @Test fun stagesEachDocumentOnceAndReusesTheCopy() { val root = testDirectory() val book = "content://test/document/book.epub" var opens = 0 try { val service = AndroidSafLocalArchiveService( cacheRoot = root.absolutePath, contentStreamOpener = { uri -> opens++ ByteArrayInputStream(epub(uri.substringAfterLast('/'))) }, ) assertEquals("book.epub", service.listEpubChapters(book).single().title) assertTrue(service.readEpubChapter(book, 0).contains("book.epub")) assertTrue(service.readEpubResource(book, "chapter.xhtml") != null) assertEquals(1, opens) assertEquals(1, stagedCopies(root).size) } finally { root.deleteRecursively() } } @Test fun keepsOnlyTheMostRecentlyUsedStagedCopies() { val root = testDirectory() val books = listOf("a", "b", "c", "d").associateWith { epub(it) } val opens = mutableListOf() fun uri(book: String) = "content://test/document/$book" try { val service = AndroidSafLocalArchiveService( cacheRoot = root.absolutePath, contentStreamOpener = { uri -> val book = uri.substringAfterLast('/') opens += book books[book]?.let(::ByteArrayInputStream) }, ) listOf("a", "b", "c").forEach { assertTrue(service.readEpubResource(uri(it), "chapter.xhtml") != null) } // Pin the LRU order explicitly: filesystem mtimes can tie within one test. stagedCopies(root).forEach { copy -> val book = books.entries.first { it.value.contentEquals(copy.readBytes()) }.key copy.setLastModified(1_000L * (1 + "abc".indexOf(book))) } assertTrue(service.readEpubResource(uri("a"), "chapter.xhtml") != null) // reuse marks a as used assertTrue(service.readEpubResource(uri("d"), "chapter.xhtml") != null) // evicts b, the oldest val remaining = stagedCopies(root).map { copy -> books.entries.first { it.value.contentEquals(copy.readBytes()) }.key } assertEquals(setOf("a", "c", "d"), remaining.toSet()) assertTrue(service.readEpubResource(uri("b"), "chapter.xhtml") != null) assertEquals(listOf("a", "b", "c", "d", "b"), opens) } finally { root.deleteRecursively() } } @Test fun providerFailureReturnsNullAndDoesNotLeaveAStagedFile() { val root = testDirectory() try { val service = AndroidSafLocalArchiveService( cacheRoot = root.absolutePath, contentStreamOpener = { error("provider unavailable") }, ) assertEquals(null, service.readEpubResource("content://test/document/book.epub", "chapter.xhtml")) assertTrue(stagedCopies(root).isEmpty()) } finally { root.deleteRecursively() } } @Test fun treeChildUrisStageTheChildArchiveInsteadOfTheTreeRoot() { val root = testDirectory() val requestedUris = mutableListOf() // The Android SDK's DocumentsContract builders are not implemented in JVM tests. // Keep the SAF-shaped fixture literal; the service still receives document URIs. val rootEpub = "content://test/tree/root/document/root.epub" val childEpub = "content://test/tree/root/document/child.epub" val rootCbz = "content://test/tree/root/document/root.cbz" val childCbz = "content://test/tree/root/document/child.cbz" val streams = mapOf( rootEpub to epub("root"), childEpub to epub("child"), rootCbz to cbz("root"), childCbz to cbz("child"), ) try { val service = AndroidSafLocalArchiveService( cacheRoot = root.absolutePath, contentStreamOpener = { uri -> requestedUris += uri streams[uri]?.let(::ByteArrayInputStream) }, ) assertEquals("child", service.listEpubChapters(childEpub).single().title) val pages = service.listPages(childCbz) assertEquals(1, pages.size) assertEquals("child", File(URI(pages.single().url)).readText()) assertTrue(childEpub in requestedUris) assertTrue(childCbz in requestedUris) assertTrue(rootEpub !in requestedUris) assertTrue(rootCbz !in requestedUris) } finally { root.deleteRecursively() } } private fun epub(title: String): ByteArray = zipOf( "META-INF/container.xml" to """ """.trimIndent(), "content.opf" to """ """.trimIndent(), "chapter.xhtml" to "$title$title", ) private fun cbz(page: String): ByteArray = zipOf("$page.jpg" to page) private fun zipOf(vararg entries: Pair): ByteArray = ByteArrayOutputStream().also { bytes -> ZipOutputStream(bytes).use { zip -> entries.forEach { (name, content) -> zip.putNextEntry(ZipEntry(name)) zip.write(content.encodeToByteArray()) zip.closeEntry() } } }.toByteArray() private fun stagedCopies(root: File): List = File(root, "archive-staging").listFiles().orEmpty().toList() private fun testDirectory(): File = File(System.getProperty("java.io.tmpdir"), "yokan-archive-test-${System.nanoTime()}").also { it.mkdirs() } }