117 lines · 6.8 KB · KotlinRaw
package com.cottondb.albumsimport com.cottondb.albums.library.Accessimport com.cottondb.albums.library.Binimport com.cottondb.albums.library.CoreFeedimport com.cottondb.albums.library.Libraryimport com.cottondb.albums.library.LibraryCacheimport com.cottondb.albums.library.LibraryItemimport com.cottondb.albums.library.MediaLibraryimport kotlinx.serialization.json.JsonArrayimport kotlinx.serialization.json.jsonArrayimport kotlinx.serialization.json.jsonPrimitiveimport kotlinx.serialization.json.intimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** The coordinator's Albums rules from the core review (27 Nov), A to D, in the library layer. */class AlbumsRulesTest { private fun item( id: Long, name: String = "IMG_$id.jpg", bucket: Long = 1, taken: Long? = 1_000_000 + id * 60_000, mime: String = "image/jpeg", size: Long = 1_000, video: Boolean = false, pending: Boolean = false, trashed: Boolean = false, ) = LibraryItem(id, video, "external_primary", bucket, "Camera", "DCIM/Camera/", name, taken, 0, 0, size, 4000, 3000, mime, null, pending, null, id, false, trashed) // A: absence never removes a choice. @Test fun a_shorter_library_is_gone_only_after_a_complete_read_with_every_photo_allowed() { val kept = setOf(1L, 2L, 3L) assertEquals(setOf(3L), Library.gone(kept, setOf(1L, 2L))) assertEquals(emptySet<Long>(), Library.gone(kept, null)) // a query refused or failed: nothing gone // The trashed are read too (MATCH_INCLUDE): an item moved to the phone's trash is still in the ids, never gone. assertEquals(emptySet<Long>(), Library.gone(kept, setOf(1L, 2L, 3L))) assertTrue(Library.mayReportGone(Access.Full, complete = true)) assertFalse(Library.mayReportGone(Access.Full, complete = false)) assertFalse(Library.mayReportGone(Access.Partial, complete = true)) assertFalse(Library.mayReportGone(Access.None, complete = true)) val body = CoreFeed.allIdsBody(listOf(7L, 9L)) assertEquals(listOf("7", "9"), body.getValue("all_ids").jsonArray.map { it.jsonPrimitive.content }) assertEquals(0, (body.getValue("items") as JsonArray).size) } @Test fun a_trashed_row_is_kept_as_trashed_through_the_cache_and_flagged_to_the_core() { val file = File.createTempFile("albums", ".tsv").apply { deleteOnExit() } val rows = listOf(item(1), item(2, trashed = true)) LibraryCache.write(file, MediaLibrary.Stamp("v", mapOf("external_primary" to 3L)), rows) assertEquals(rows, LibraryCache.read(file)!!.second) assertTrue("trashed" in CoreFeed.flags(rows[1])) assertFalse("trashed" in CoreFeed.flags(rows[0])) } // B: every act carries the ids that were on screen and their count. @Test fun an_act_carries_the_ids_on_screen_and_the_count_it_showed() { val act = CoreFeed.act("bin", listOf(11L, 12L, 13L), 3) assertEquals("bin", act.getValue("act").jsonPrimitive.content) assertEquals(listOf("11", "12", "13"), act.getValue("ids").jsonArray.map { it.jsonPrimitive.content }) assertEquals(3, act.getValue("count").jsonPrimitive.int) val refused = runCatching { CoreFeed.act("bin", listOf(11L, 12L), 3) } assertTrue(refused.isFailure) } // C: nothing is asked of the phone except the items chosen on the screen tapped. @Test fun the_bin_asks_only_for_the_ticked_items_shown_on_that_screen() { val shown = listOf(item(1), item(2), item(3, pending = true), item(4)) // 9 is ticked from another screen; 3 is still being saved; 4 is shown but not ticked. val asked = Bin.chosenOnScreen(shown, setOf(1L, 2L, 3L, 9L)) assertEquals(listOf(1L, 2L), asked.map { it.id }) assertEquals(emptyList<Long>(), Bin.chosenOnScreen(shown, emptySet()).map { it.id }) } // D: RAW + JPEG and edited versions are one item with parts. @Test fun raw_plus_jpeg_and_an_edit_are_one_item_counted_once_sized_whole() { val jpeg = item(1, "PXL_0001.jpg", taken = 5_000, size = 3_000) val raw = item(2, "PXL_0001.dng", taken = 5_400, mime = "image/x-adobe-dng", size = 20_000) val original = item(3, "IMG_0002.jpg", taken = 9_000_000, size = 2_000) val edit = item(4, "IMG_0002-edited.jpg", taken = 9_000_000, size = 2_500) val other = item(5, "IMG_0003.jpg", taken = 9_100_000) val elsewhere = item(6, "PXL_0001.dng", bucket = 2, taken = 5_000, mime = "image/x-adobe-dng") // another folder val later = item(7, "PXL_0001.jpg", taken = 5_000 + 3_600_000) // the same name an hour on: another shot val video = item(8, "PXL_0001.mp4", video = true, taken = 5_000, mime = "video/mp4") val grouped = Library.group(listOf(jpeg, raw, original, edit, other, elsewhere, later, video)).associateBy { it.id } assertEquals(setOf(1L, 4L, 5L, 6L, 7L, 8L), grouped.keys) assertEquals(listOf(2L), grouped.getValue(1).parts.map { it.id }) // the JPEG shows, the RAW is its part assertEquals(23_000L, grouped.getValue(1).totalSize) assertEquals(listOf(3L), grouped.getValue(4).parts.map { it.id }) // the edit shows, the original is its part assertTrue(grouped.getValue(6).parts.isEmpty()) val sum = Library.sum(grouped.values) assertEquals(5, sum.photos); assertEquals(1, sum.videos) assertEquals(listOf(jpeg, raw, original, edit, other, elsewhere, later, video).sumOf { it.size }, sum.bytes) assertTrue(Library.raw(raw)); assertFalse(Library.raw(jpeg)); assertTrue(Library.raw(item(9, "a.CR2", mime = "image/x-canon-cr2"))) assertEquals("img_0002", Library.base(edit)) // The core hears one item with its parts. val said = CoreFeed.item(grouped.getValue(1)) assertEquals(23_000L, said.getValue("bytes").jsonPrimitive.content.toLong()) assertEquals(listOf("2"), said.getValue("parts").jsonArray.map { (it as kotlinx.serialization.json.JsonObject).getValue("id").jsonPrimitive.content }) assertTrue("raw" in CoreFeed.flags(grouped.getValue(1))) } @Test fun a_bin_of_a_raw_pair_takes_both_files_and_the_2000_cap_counts_files() { val pairs = (0 until 1_500).map { k -> item(2L * k + 1, "S_$k.jpg", taken = k * 10_000L).copy(parts = listOf(item(2L * k + 2, "S_$k.dng", taken = k * 10_000L, mime = "image/x-adobe-dng"))) } val files = Bin.files(pairs) assertEquals(3_000, files.size) assertEquals(listOf(2_000, 1_000), Library.requests(files).map { it.size }) assertEquals(listOf(1_000, 500), Bin.itemsPerRequest(pairs)) // A pending part is never asked. val withPending = listOf(item(1).copy(parts = listOf(item(2, pending = true)))) assertEquals(listOf(1L), Bin.files(withPending).map { it.id }) }}