Cotton code
227 lines · 12.4 KB · KotlinRaw
package com.cottondb.albumsimport com.cottondb.albums.library.LibraryItemimport com.cottondb.albums.source.AlbumsActsimport com.cottondb.albums.source.AlbumsDatesimport com.cottondb.albums.source.Answerimport com.cottondb.albums.source.CoreSourceimport com.cottondb.albums.source.FaceMeasureimport com.cottondb.albums.source.Fixtureimport com.cottondb.albums.source.FixtureRulesimport com.cottondb.albums.source.ListKindimport com.cottondb.albums.source.Marksimport com.cottondb.albums.source.Measureimport com.cottondb.albums.source.Measuredimport com.cottondb.albums.source.PhoneTrashimport kotlinx.coroutines.runBlockingimport 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.util.TimeZone/** * THE SCREENS' LOGIC ON THE FIXTURE: Sets and Picks, the coordinator's rule B (every act carries the ids on screen and * their count), rule C and decision G (binning asks nothing of the phone; only Delete asks its trash, for exactly the * ids shown), the Delete sheet's 30 days, faces off hiding People, People's grouping, and the core calls' bodies. */class AlbumsScreensTest {    private val t0 = 1_700_000_000_000L    private fun item(id: Long, taken: Long, video: Boolean = false, size: Long = 1_000_000, path: String = "DCIM/Camera/", name: String = "IMG_$id.jpg") =        LibraryItem(id, video, "external_primary", 1, "Camera", path, name, taken, taken / 1000, taken / 1000, size, 4000, 3000,            if (video) "video/mp4" else "image/jpeg", if (video) 6_000 else null, false, null, id, false)    /** A print in one of [scene]'s looks: scenes differ wholly; [shift] nudges one look a little. */    private fun print(scene: Int, shift: Int = 0) = ByteArray(256) { i -> (((i * (scene * 37 + 11)) % 251) + if (i % 7 == 0) shift else 0).coerceIn(0, 255).toByte() }    private fun measure(id: Long, scene: Int, sharp: Float = 100f, light: Float = 0.5f, shift: Int = 0, shot: Boolean = false) =        Measure(id, if (scene == 1) 0x0F0F0F0F0F0F0F0FL else 0x7A3C1E5B92D4C681L * scene, print(scene, shift), sharp, light, 0.8f, shot, false, 0, null, false)    private val words: (String, Map<String, String>) -> String = { k, f -> AlbumsWords.of(null, k, f) }    /** A library of one moment of five shots (scene 1), a different scene a minute later, and two screenshots. */    private fun fixture(faces: List<FaceMeasure> = emptyList(), facesOn: Boolean = true): Pair<Fixture, List<LibraryItem>> {        val items = (1L..5L).map { item(it, t0 + it * 1_000) } + item(6, t0 + 60_000) +            listOf(item(7, t0 + 120_000, path = "Pictures/Screenshots/", name = "Screenshot_1.png"), item(8, t0 + 130_000, path = "Pictures/Screenshots/", name = "Screenshot_2.png"))        val looks = (1L..5L).map { measure(it, 1, sharp = 100f + it * 40, shift = it.toInt()) } + measure(6, 2) + measure(7, 3, shot = true) + measure(8, 4, shot = true)        val f = Fixture(Marks(null), { Measured(looks, faces) }, words, now = { t0 + 86_400_000 }, facesOn = { facesOn })        runBlocking { f.refresh(items, emptyList(), emptyList()) }        return f to items    }    @Test    fun a_set_is_the_shots_close_in_time_that_look_alike_and_its_pick_is_the_sharpest() {        val (f, _) = fixture()        val sets = f.sets()        assertEquals(1, sets.size)        assertEquals(listOf(1L, 2L, 3L, 4L, 5L), sets[0].members.map { it.id })        // The sharpest first (5 is sharpest); the near-identical rest are not kept beside it.        assertEquals(5L, sets[0].keepers.first())        assertTrue(sets[0].keepers.size < 5)        assertEquals("set:1", sets[0].id)    }    @Test    fun the_same_look_far_apart_in_time_is_two_moments_never_one_set() {        val a = FixtureRules.Looked(item(1, t0), 1L, print(1), 10f, 0.5f, 0.8f, false, false, 0)        val b = a.copy(item = item(2, t0 + 1_000))        val far = a.copy(item = item(3, t0 + 10 * 60_000))        assertEquals(listOf(listOf(1L, 2L)), FixtureRules.sets(listOf(far, b, a)).map { s -> s.map { it.item.id } })    }    @Test    fun tidy_lists_each_item_once_biggest_first_with_screenshots_apart() {        val (f, _) = fixture()        val t = f.tidy(passDone = true)        assertEquals(setOf(ListKind.Sets, ListKind.Screenshots), t.lists.map { it.kind }.toSet())        assertEquals(listOf(7L, 8L).toSet(), f.list(ListKind.Screenshots).items.map { it.id }.toSet())        // The Sets line counts moments; its size is the shots the set does not pick.        val sets = t.lists.first { it.kind == ListKind.Sets }        assertEquals(1, sets.count)        assertEquals(f.sets()[0].frees, sets.bytes)    }    @Test    fun rule_B_an_act_whose_ids_or_count_differ_from_what_is_held_is_refused_changed() {        val (f, _) = fixture()        val s = f.sets()[0]        val members = s.members.map { it.id }        assertTrue(f.setKeep(s.id, members.dropLast(1), members.size - 1, s.keepers) is Answer.Changed)        assertTrue(f.setKeep(s.id, members, members.size + 1, s.keepers) is Answer.Changed)        assertTrue(f.binMove(listOf(7L, 8L), 3, "screenshots") is Answer.Changed)        assertTrue(f.setKeep("set:999", members, members.size, s.keepers) is Answer.Gone)        val done = f.setKeep(s.id, members, members.size, s.keepers)        assertTrue(done is Answer.Done)        assertEquals((members - s.keepers.toSet()).toSet(), f.bin().ids.toSet())        assertEquals(0, f.sets().size)        // Undo puts every mark back as one act.        assertTrue(f.undo((done as Answer.Done).undo!!.token) is Answer.Done)        assertEquals(0, f.bin().count)        assertEquals(1, f.sets().size)    }    // THE HIDDEN PATH (AlbumsBuild.canRemove off in this build): these walk it with the switch on, so it holds when it    // comes back. The look-only build itself is AlbumsLookOnlyTest.    @Test    fun decision_G_binning_keeping_bringing_back_and_undo_make_no_call_to_the_phone() {        val (f, _) = fixture()        val asked = ArrayList<List<Long>>()        val acts = AlbumsActs(f, PhoneTrash { _, ids -> asked += ids }, canRemove = true)        val s = f.sets()[0]        val members = s.members.map { it.id }        val kept = acts.run { it.setKeep(s.id, members, members.size, s.keepers) } as Answer.Done        acts.run { it.binMove(listOf(7L), 1, "screenshots") }        acts.run { it.keep(listOf(8L), 1) }        acts.run { it.bringBack(listOf(7L), 1) }        acts.run { it.undo(kept.undo!!.token) }        acts.run { it.makeAlbum("Goa", listOf(6L), 1) }        assertEquals(emptyList<List<Long>>(), asked)    }    @Test    fun rule_C_delete_asks_the_phones_trash_for_exactly_the_ids_shown_and_only_then() {        val (f, items) = fixture()        val asked = ArrayList<Pair<String, List<Long>>>()        val acts = AlbumsActs(f, PhoneTrash { id, ids -> asked += id to ids }, canRemove = true)        acts.run { it.binMove(listOf(7L, 8L), 2, "screenshots") }        val shown = f.bin().ids        // A Delete that does not carry the Bin as shown asks nothing.        assertTrue(acts.run { it.free(shown.take(1), 1) } is Answer.Changed)        assertTrue(acts.run { it.free(shown + 6L, 3) } is Answer.Changed)        assertEquals(0, asked.size)        val answer = acts.run { it.free(shown, shown.size) } as Answer.Platform        assertEquals("trash", answer.act)        assertEquals(1, asked.size)        assertEquals(shown.toSet(), asked[0].second.toSet())        assertEquals(shown.size, asked[0].second.size)        // The phone answered: the shell says nothing of what moved, and nothing changes until the library is read.        assertTrue(f.platformDone("trash", answer.actId, shown) is Answer.Done)        assertEquals(shown.toSet(), f.bin().ids.toSet())        assertEquals(null, f.settled(answer.actId))        // The same act told twice is refused.        assertTrue(f.platformDone("trash", answer.actId, shown) is Answer.Changed)        // The next read says 7 is in the phone's trash and 8 still in the library: 7 leaves the Bin, 8 stays binned.        val seven = items.single { it.id == 7L }.copy(trashed = true)        runBlocking { f.refresh(items.filter { it.id != 7L }, listOf(seven), emptyList()) }        assertEquals(listOf(8L), f.bin().ids)        assertEquals(listOf(7L), f.bin().inTrash.map { it.id })        val settled = f.settled(answer.actId)!!        assertEquals(1, settled.moved)        assertTrue(settled.after.contains("1"))    }    @Test    fun after_delete_an_item_the_read_finds_absent_stays_binned_and_is_shown_as_no_longer_here() {        val (f, items) = fixture()        val acts = AlbumsActs(f, PhoneTrash { _, _ -> }, canRemove = true)        acts.run { it.binMove(listOf(7L, 8L), 2, "screenshots") }        val shown = f.bin().ids        val answer = acts.run { it.free(shown, shown.size) } as Answer.Platform        f.platformDone("trash", answer.actId, shown)        // Neither in the trash nor in the library: absent (rule A). Nothing moved; both stay his, one shown as gone.        runBlocking { f.refresh(items.filter { it.id != 7L }, emptyList(), emptyList()) }        assertEquals(0, f.settled(answer.actId)!!.moved)        assertEquals(listOf(8L), f.bin().ids)        assertEquals(1, f.bin().gone)    }    /** The hidden path: the Delete sheet's words, for when the switch comes back on. */    @Test    fun the_delete_sheet_says_the_count_the_size_and_that_the_phones_trash_keeps_them_30_days() {        val lines = DeleteSheet.lines(words, 23, 4_100_000, manage = false)        assertTrue(lines.any { "30 days" in it })        assertTrue(lines.first().contains("23") && lines.first().contains("4.1 MB"))        assertTrue(lines.any { "asks you once" in it })        assertFalse(DeleteSheet.lines(words, 23, 4_100_000, manage = true).any { "asks you once" in it })        assertEquals("Delete 23 from this phone?", DeleteSheet.title(words, 23))        assertTrue(lines.none { "at once" in it.lowercase() })    }    @Test    fun faces_off_hides_people_from_the_tabs_and_the_fixture_groups_none() {        assertEquals(listOf("albums", "tidy", "people", "you"), AlbumsTabs.of(true))        assertEquals(listOf("albums", "tidy", "you"), AlbumsTabs.of(false))        val faces = (1L..5L).map { FaceMeasure(it, 0, 0.3f, 0.3f, 0.2f, 0.2f, FloatArray(128) { d -> if (d == 0) 1f else 0.01f * it }) }        assertEquals(1, fixture(faces, facesOn = true).first.people().count)        assertEquals(0, fixture(faces, facesOn = false).first.people().count)    }    @Test    fun people_are_groups_of_five_photos_or_more_and_naming_carries_the_photos_shown() {        val one = (1L..5L).map { FaceMeasure(it, 0, 0.3f, 0.3f, 0.2f, 0.2f, FloatArray(128) { d -> if (d == 0) 1f else 0.02f * it }) }        val other = (6L..8L).map { FaceMeasure(it, 0, 0.3f, 0.3f, 0.2f, 0.2f, FloatArray(128) { d -> if (d == 1) 1f else 0f }) }        val f = fixture(one + other).first        val people = f.people()        assertEquals(1, people.unnamed.size)        assertEquals(5, people.unnamed[0].count)        val id = people.unnamed[0].id        val ids = f.person(id)!!.items.map { it.id }        assertTrue(f.namePerson(id, "Asha", ids.drop(1), ids.size - 1) is Answer.Changed)        assertTrue(f.namePerson(id, "Asha", ids, ids.size) is Answer.Done)        assertEquals("Asha", f.people().named.single().name)    }    @Test    fun the_core_calls_carry_the_ids_and_the_count_by_the_briefs_names() {        val (method, body) = CoreSource.Calls.setKeep("set:1", listOf(1, 2, 3), 3, listOf(2))        assertEquals("albums.set.keep", method)        assertEquals(listOf("1", "2", "3"), body["members"]!!.jsonArray.map { it.jsonPrimitive.content })        assertEquals(3, body["count"]!!.jsonPrimitive.int)        assertEquals(listOf("2"), body["keep"]!!.jsonArray.map { it.jsonPrimitive.content })        assertEquals("albums.bin.delete", CoreSource.Calls.free(listOf(4), 1).first)    }    @Test    fun a_moment_and_a_span_are_said_as_the_core_says_them() {        val utc = TimeZone.getTimeZone("UTC")        val sat = 1_773_511_920_000L // Sat 14 Mar 2026, 18:12 UTC        assertEquals("Sat 14 Mar, 6:12–6:14 pm", AlbumsDates.moment(sat, sat + 120_000, now = sat, zone = utc))        assertEquals("Sat 14 Mar 2026, 6:12 pm", AlbumsDates.whenTaken(sat, now = sat + 400L * 86_400_000, zone = utc))        assertEquals("14–19 Mar", AlbumsDates.span(sat, sat + 5 * 86_400_000L, now = sat, zone = utc))    }}