Yokan / rebuild
Course 1 8624561

DefaultYokanRepositoryTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/data/DefaultYokanRepositoryTest.kt

test / fixture81 lines
1package eu.friedcutlet.yokan.data23import eu.friedcutlet.yokan.database.DbChapter4import eu.friedcutlet.yokan.database.DbItemType5import eu.friedcutlet.yokan.database.DbManga6import eu.friedcutlet.yokan.database.DbPageUrl7import eu.friedcutlet.yokan.database.InMemoryYokanDatabase8import eu.friedcutlet.yokan.database.seedDatabaseSnapshot9import eu.friedcutlet.yokan.database.DbSource10import eu.friedcutlet.yokan.domain.ItemType11import eu.friedcutlet.yokan.domain.Source12import eu.friedcutlet.yokan.services.extensions.RemoteChapter13import eu.friedcutlet.yokan.services.extensions.RemoteDetail14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertTrue1718class DefaultYokanRepositoryTest {19    @Test20    fun refreshingADetailLeavesUnchangedChaptersUntouched() {21        val database = InMemoryYokanDatabase(22            seedDatabaseSnapshot().copy(23                mangas = listOf(DbManga(id = 1, name = "Title", link = "/title", sourceId = 5, itemType = DbItemType.Manga)),24                chapters = listOf(25                    DbChapter(id = 10, mangaId = 1, name = "Chapter 1", url = "/c1", updatedAt = 1),26                    DbChapter(id = 11, mangaId = 1, name = "Chapter 2", url = "/c2", updatedAt = 1),27                ),28            ),29        )30        val source = Source(id = 5, name = "Source", baseUrl = "https://example.com", lang = "en", itemType = ItemType.Manga, typeSource = "single")3132        DefaultYokanRepository(database).upsertRemoteDetail(33            source,34            RemoteDetail(35                title = "Title",36                url = "/title",37                chapters = listOf(RemoteChapter("Chapter 1", "/c1"), RemoteChapter("Chapter 2 (renamed)", "/c2")),38            ),39        )4041        assertEquals(1L, database.chapter(10)?.updatedAt, "an unchanged chapter is not rewritten")42        assertEquals("Chapter 2 (renamed)", database.chapter(11)?.name)43        assertTrue((database.chapter(11)?.updatedAt ?: 0) > 1, "a changed chapter still gets a fresh sync timestamp")44    }4546    @Test47    fun pageHeadersRoundTripWhetherSharedOrPerPage() {48        val repository = DefaultYokanRepository(InMemoryYokanDatabase(seedDatabaseSnapshot()))49        val shared = mapOf("Referer" to "https://example.com/chapter")50        val sharedPages = (1..3).map { DbPageUrl("https://example.com/$it.jpg", headers = shared) }51        val mixedPages = listOf(52            DbPageUrl("https://example.com/a.jpg", headers = shared),53            DbPageUrl("https://example.com/b.jpg"),54            DbPageUrl("https://example.com/c.jpg", headers = mapOf("Referer" to "other")),55        )5657        repository.cachePageUrls(chapterId = 1, chapterUrl = "/1", pageUrls = sharedPages)58        repository.cachePageUrls(chapterId = 2, chapterUrl = "/2", pageUrls = mixedPages)5960        assertEquals(sharedPages, repository.pageUrls(1))61        assertEquals(mixedPages, repository.pageUrls(2))62    }6364    @Test65    fun savedPageListsNeverKeepCredentials() {66        val database = InMemoryYokanDatabase(seedDatabaseSnapshot())67        val repository = DefaultYokanRepository(database)68        val source = DbSource(id = 5, version = "2.0.0")69        val kept = mapOf("Referer" to "https://example.com/chapter")7071        repository.cachePageUrls(1, "/1", listOf(DbPageUrl("https://example.com/1.jpg", headers = kept + ("cookie" to "session=1"))), source, fetchedAt = 42)72        repository.cachePageUrls(2, "/2", listOf(DbPageUrl("https://example.com/2.jpg", headers = kept + ("AUTHORIZATION" to "Bearer t"))), source, fetchedAt = 42)7374        assertEquals(kept, repository.pageUrls(1).single().headers)75        assertEquals(kept, repository.pageUrls(2).single().headers)76        val (withCookie, withAuthorization) = database.pageCache(1).single() to database.pageCache(2).single()77        assertEquals(42L, withCookie.fetchedAt, "the cookie jar re-adds cookies, so the list stays servable")78        assertEquals(null, withAuthorization.fetchedAt, "nothing re-adds Authorization: the list must be refetched")79        assertEquals(5L to "2.0.0", withAuthorization.sourceId to withAuthorization.sourceVersion)80    }81}82

Search chapters, reference and source filenames.