package eu.friedcutlet.yokan.data import eu.friedcutlet.yokan.database.DbChapter import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbPageUrl import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.seedDatabaseSnapshot import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.domain.Source import eu.friedcutlet.yokan.services.extensions.RemoteChapter import eu.friedcutlet.yokan.services.extensions.RemoteDetail import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue class DefaultYokanRepositoryTest { @Test fun refreshingADetailLeavesUnchangedChaptersUntouched() { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( mangas = listOf(DbManga(id = 1, name = "Title", link = "/title", sourceId = 5, itemType = DbItemType.Manga)), chapters = listOf( DbChapter(id = 10, mangaId = 1, name = "Chapter 1", url = "/c1", updatedAt = 1), DbChapter(id = 11, mangaId = 1, name = "Chapter 2", url = "/c2", updatedAt = 1), ), ), ) val source = Source(id = 5, name = "Source", baseUrl = "https://example.com", lang = "en", itemType = ItemType.Manga, typeSource = "single") DefaultYokanRepository(database).upsertRemoteDetail( source, RemoteDetail( title = "Title", url = "/title", chapters = listOf(RemoteChapter("Chapter 1", "/c1"), RemoteChapter("Chapter 2 (renamed)", "/c2")), ), ) assertEquals(1L, database.chapter(10)?.updatedAt, "an unchanged chapter is not rewritten") assertEquals("Chapter 2 (renamed)", database.chapter(11)?.name) assertTrue((database.chapter(11)?.updatedAt ?: 0) > 1, "a changed chapter still gets a fresh sync timestamp") } @Test fun pageHeadersRoundTripWhetherSharedOrPerPage() { val repository = DefaultYokanRepository(InMemoryYokanDatabase(seedDatabaseSnapshot())) val shared = mapOf("Referer" to "https://example.com/chapter") val sharedPages = (1..3).map { DbPageUrl("https://example.com/$it.jpg", headers = shared) } val mixedPages = listOf( DbPageUrl("https://example.com/a.jpg", headers = shared), DbPageUrl("https://example.com/b.jpg"), DbPageUrl("https://example.com/c.jpg", headers = mapOf("Referer" to "other")), ) repository.cachePageUrls(chapterId = 1, chapterUrl = "/1", pageUrls = sharedPages) repository.cachePageUrls(chapterId = 2, chapterUrl = "/2", pageUrls = mixedPages) assertEquals(sharedPages, repository.pageUrls(1)) assertEquals(mixedPages, repository.pageUrls(2)) } @Test fun savedPageListsNeverKeepCredentials() { val database = InMemoryYokanDatabase(seedDatabaseSnapshot()) val repository = DefaultYokanRepository(database) val source = DbSource(id = 5, version = "2.0.0") val kept = mapOf("Referer" to "https://example.com/chapter") repository.cachePageUrls(1, "/1", listOf(DbPageUrl("https://example.com/1.jpg", headers = kept + ("cookie" to "session=1"))), source, fetchedAt = 42) repository.cachePageUrls(2, "/2", listOf(DbPageUrl("https://example.com/2.jpg", headers = kept + ("AUTHORIZATION" to "Bearer t"))), source, fetchedAt = 42) assertEquals(kept, repository.pageUrls(1).single().headers) assertEquals(kept, repository.pageUrls(2).single().headers) val (withCookie, withAuthorization) = database.pageCache(1).single() to database.pageCache(2).single() assertEquals(42L, withCookie.fetchedAt, "the cookie jar re-adds cookies, so the list stays servable") assertEquals(null, withAuthorization.fetchedAt, "nothing re-adds Authorization: the list must be refetched") assertEquals(5L to "2.0.0", withAuthorization.sourceId to withAuthorization.sourceVersion) } }