From be6e29b2c9b6176bd18087e59d3bc72cddab6862 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 22 Nov 2026 11:58:18 +0200 Subject: [PATCH] test: pin what notifications change, before they change it (notifications, Phase 1) Co-Authored-By: Claude Opus 5.5 --- .../managers/NotificationTodayJvmTest.kt | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/NotificationTodayJvmTest.kt diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/NotificationTodayJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/NotificationTodayJvmTest.kt new file mode 100644 index 00000000..9cc03ba6 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/NotificationTodayJvmTest.kt @@ -0,0 +1,82 @@ +package mobi.giza.compose.managers + +import androidx.room3.Room +import com.vitorpamplona.quartz.nip01Core.core.toHexKey +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair +import java.io.File +import kotlin.test.AfterTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.builder.getRoomDatabase +import mobi.giza.compose.database.repository.DatabaseMarmotRepository + +/** + * What the app does today that notifications change, pinned so each change is made on purpose. + * docs/notifications-and-background-sync.md, Phase 1. + * + * The third pin of that phase -- that going to the background closes every live subscription -- + * is `LiveSubscriptionReconcileTest`'s `backgrounding closes the subscriptions and foregrounding rebuilds them`, and Phase 8 moves it. + */ +class NotificationTodayJvmTest { + + private val db: GizaDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder()) + private val marmotRepository = DatabaseMarmotRepository(db, CoroutineScope(Dispatchers.Default)) + + @AfterTest + fun closeDb() = db.close() + + private val user = KeyPair() + private val userHex = user.pubKey.toHexKey() + private val peer = MarmotClientStandIn() + + /** + * The premise of the background pass (Phase 0, check 5): a Welcome and the first message after + * it are filed by `storeNostrEvent` alone, with no view model running. And nothing records that + * the message has not been read -- the room carries no read state at all. + */ + @Test + fun `a group message is filed with no view model, and nothing marks it unread`() = runBlocking { + MarmotStandIn.seedProfile(db, userHex, "us") + MarmotStandIn.seedProfile(db, peer.hex, "peer") + val roomId = peer.inviteApp(db, marmotRepository, user, peer.createGroup("notify")) + + val bundle = peer.client.buildTextMessage(roomId, "are you there", persistOwn = false) + MarmotStandIn.storeInApp(db, user, bundle.outbound.signedEvent) + + val line = assertNotNull( + db.chatMessageDao().getChatMessagesByChatRoomId(roomId) + .map { it.chatMessage } + .firstOrNull { it.content == "are you there" }, + "the message was not filed without a view model", + ) + assertFalse(line.isUserMessage) + + // Phase 4 flips this: the room will say how far it has been read. + assertNull(assertNotNull(db.chatRoomDao().findChatRoomById(roomId)).chatRoom.viewedAt) + } + + private val source: File = + File("src").takeIf { it.resolve("commonMain").isDirectory } ?: File("composeApp/src") + + /** + * The object graph is built inside a composable, so nothing can run without a UI. Phase 2 moves + * it to `GizaRuntime` and flips this to name that file instead. + */ + @Test + fun `the repositories are constructed in GizaNavHost and nowhere else`() { + val builders = source.resolve("commonMain/kotlin").walkTopDown() + .filter { it.isFile && it.extension == "kt" } + .filter { "DatabaseNostrRepository(" in it.readText().replace("class DatabaseNostrRepository(", "") } + .map { it.nameWithoutExtension } + .toSet() + + assertEquals(setOf("GizaNavHost"), builders) + } +}