Cotton code
147 lines · 7.7 KB · KotlinRaw
package com.cottondb.albumsimport android.Manifestimport com.cottondb.albums.library.Accessimport com.cottondb.albums.library.CoreFeedimport com.cottondb.albums.library.ItemStateimport com.cottondb.albums.library.Libraryimport com.cottondb.albums.library.LibraryCacheimport com.cottondb.albums.library.LibraryItemimport com.cottondb.albums.library.MediaAccessimport com.cottondb.albums.library.MediaLibraryimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** The library layer's rules, on invented rows: access, buckets, sums, the Bin's 2,000 cap, the resume cache, words. */class AlbumsLibraryTest {    private fun item(id: Long, bucket: Long = 1, name: String = "Camera", path: String = "DCIM/Camera/", video: Boolean = false, size: Long = 1_000, taken: Long? = id * 1000, pending: Boolean = false) =        LibraryItem(id, video, "external_primary", bucket, name, path, "IMG_$id.jpg", taken, id, id, size, 4000, 3000, if (video) "video/mp4" else "image/jpeg", if (video) 5_000 else null, pending, null, id, false)    @Test    fun access_adds_up_per_android() {        val all = setOf(Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED)        assertEquals(Access.Full, MediaAccess.of(34) { it in all })        // "Select photos" on Android 14: only the user-selected grant.        assertEquals(Access.Partial, MediaAccess.of(34) { it == Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED })        assertEquals(Access.Partial, MediaAccess.of(33) { it == Manifest.permission.READ_MEDIA_IMAGES })        assertEquals(Access.None, MediaAccess.of(34) { false })        // Android 13 has no partial grant: the user-selected one counts for nothing there.        assertEquals(Access.None, MediaAccess.of(33) { it == Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED })        assertEquals(Access.Full, MediaAccess.of(30) { it == Manifest.permission.READ_EXTERNAL_STORAGE })        assertEquals(3, MediaAccess.asks(34).size)        assertEquals(listOf(Manifest.permission.READ_EXTERNAL_STORAGE), MediaAccess.asks(32).toList())    }    @Test    fun buckets_are_counted_sized_and_ordered_biggest_first() {        val rows = (1L..5).map { item(it) } + (6L..7).map { item(it, bucket = 2, name = "Screenshots", path = "Pictures/Screenshots/", size = 300) } +            item(8, bucket = 3, name = "Camera", path = "Download/Camera/", video = true, size = 5_000_000)        val b = Library.buckets(rows)        assertEquals(listOf(1L, 2L, 3L), b.map { it.id })        assertEquals(5, b[0].photos); assertEquals(5_000L, b[0].bytes)        assertEquals(1, b[2].videos); assertEquals(0, b[2].photos)        assertEquals(setOf("Camera"), Library.sharedNames(b))        val sum = Library.sum(rows)        assertEquals(7, sum.photos); assertEquals(1, sum.videos); assertEquals(5_005_600L, sum.bytes)    }    @Test    fun the_bin_never_asks_for_more_than_2000_at_once() {        val parts = Library.requests((1..4_500).toList())        assertEquals(listOf(2_000, 2_000, 500), parts.map { it.size })        assertEquals((1..4_500).toList(), parts.flatten())        assertEquals(2_000, Library.MAX_REQUEST)    }    @Test    fun the_cache_reads_back_what_it_wrote_and_refuses_a_broken_file() {        val file = File.createTempFile("albums", ".tsv").apply { deleteOnExit() }        val stamp = MediaLibrary.Stamp("v1:abc", mapOf("external_primary" to 1234L, "1A2B-3C4D" to 7L))        val rows = listOf(item(1), item(2, video = true, taken = null), item(3, name = "With\ttab", pending = true))        LibraryCache.write(file, stamp, rows)        val (s, back) = LibraryCache.read(file)!!        assertEquals(stamp, s)        assertEquals(rows.map { it.copy(bucket = it.bucket.replace('\t', ' ')) }, back)        file.appendText("not\ta\trow\n")        assertNull(LibraryCache.read(file))        assertNull(LibraryCache.read(File(file.path + ".none")))    }    @Test    fun sizes_and_counts_read_as_people_read_them() {        assertEquals("3,412", Library.count(3_412))        assertEquals("812.4 MB", Library.size(812_400_000))        assertEquals("31.4 GB", Library.size(31_400_000_000))        assertEquals("240 KB", Library.size(240_123))    }    @Test    fun words_fall_back_to_the_placeholders_until_the_core_has_them() {        assertEquals("Moved 2,000 to the Bin.", AlbumsWords.of(null, "moved_after", mapOf("n" to "2,000")))        assertTrue(AlbumsWords.of(null, "cloud.only").contains("Google Photos"))        assertEquals("no.such.key", AlbumsWords.of(null, "no.such.key"))    }    @Test    fun the_core_is_handed_facts_never_pictures() {        val shot = item(9, name = "Screenshots", path = "Pictures/Screenshots/").copy(favourite = true)        val o = CoreFeed.item(shot)        assertEquals("9", o["id"].toString().trim('"'))        assertEquals("\"photo\"", o["kind"].toString())        assertEquals("[\"screenshot\",\"favourite\"]", o["flags"].toString())        assertEquals("\"Pictures/Screenshots/IMG_9.jpg\"", o["path"].toString())        assertEquals("null", CoreFeed.item(item(2, taken = null))["taken_at_ms"].toString())        assertEquals(listOf("limited", "denied", "none", "full"), listOf(            CoreFeed.permission(Access.Partial, false), CoreFeed.permission(Access.None, true), CoreFeed.permission(Access.None, false), CoreFeed.permission(Access.Full, false),        ))        assertEquals(1_000, CoreFeed.BATCH)    }    // ── each item's state, as the read reports it (ALBUMS Android 2; core 1d takes it) ──    private val P = ItemState.Present    private val T = ItemState.Trashed    private val A = ItemState.Absent    @Test    fun a_trashed_row_is_trashed_never_present() {        val rows = listOf(item(1), item(2).copy(trashed = true))        val s = Library.states(emptyMap(), rows, setOf(1L, 2L), complete = true, access = Access.Full)        assertEquals(mapOf(1L to P, 2L to T), s)        // Moved to the phone's trash elsewhere: the next read says trashed, not present and not gone.        assertEquals(T, Library.states(s, listOf(item(1).copy(trashed = true)), setOf(1L, 2L), true, Access.Full)[1L])    }    @Test    fun known_and_not_seen_by_a_finished_read_with_every_photo_allowed_is_absent() {        val before = mapOf(1L to P, 2L to T, 3L to P)        val s = Library.states(before, listOf(item(1)), setOf(1L), complete = true, access = Access.Full)        assertEquals(mapOf(1L to P, 2L to A, 3L to A), s)    }    @Test    fun a_read_that_did_not_finish_makes_nothing_absent() {        val before = mapOf(1L to P, 2L to T, 3L to P)        assertEquals(before, Library.states(before, listOf(item(1)), setOf(1L), complete = false, access = Access.Full))        assertEquals(before, Library.states(before, listOf(item(1)), null, complete = true, access = Access.Full))    }    @Test    fun with_only_chosen_photos_allowed_nothing_is_absent() {        val before = mapOf(1L to P, 2L to P)        assertEquals(before, Library.states(before, listOf(item(1)), setOf(1L), complete = true, access = Access.Partial))        assertEquals(before, Library.states(before, emptyList(), emptySet(), complete = true, access = Access.None))    }    @Test    fun an_absent_item_whose_row_is_read_again_is_back() {        val before = mapOf(1L to A, 2L to A)        val s = Library.states(before, listOf(item(1), item(2).copy(trashed = true)), setOf(1L, 2L), true, Access.Full)        assertEquals(mapOf(1L to P, 2L to T), s)        // Seen by id without its row (a delta read): it stays absent until its row says what it is.        assertEquals(A, Library.states(mapOf(3L to A), emptyList(), setOf(3L), true, Access.Full)[3L])    }}