diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/database/model/GroupStatementApplyJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/database/model/GroupStatementApplyJvmTest.kt new file mode 100644 index 00000000..bb8e140d --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/database/model/GroupStatementApplyJvmTest.kt @@ -0,0 +1,87 @@ +package mobi.giza.compose.database.model + +import androidx.room3.Room +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.toHexKey +import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair +import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerSync +import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent +import kotlin.test.AfterTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Instant +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.builder.getRoomDatabase + +/** + * What the transcript does with a profile or a post the group signed as itself. + * + * `applyInnerEvent` is reached three ways -- a rumor arriving in the room, a + * signing session completing, a chronicle page -- and these are signed the way a + * completed session's are: by the key whose public half is the room's id. + * docs/group-nostr-profile-and-posts.md. + */ +class GroupStatementApplyJvmTest { + + private val database: GizaDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder()) + + @AfterTest + fun closeDb() = database.close() + + /** The room's own key: its public half is the room's id, as for a derived room. */ + private val room = KeyPair() + private val roomId = room.pubKey.toHexKey() + private val member = KeyPair() + + private fun signedBy(keyPair: KeyPair, kind: Int, content: String): Event = + NostrSignerSync(keyPair).signNormal( + createdAt = 1_700_000_000, + kind = kind, + tags = emptyArray(), + content = content, + ) + + private suspend fun apply(event: Event): ChatMessage? = ChatMessage.applyInnerEvent( + database = database, + groupId = roomId, + event = event, + marmotGroupEventId = null, + marmotInnerEventId = null, + senderPublicKey = member.pubKey.toHexKey(), + isUserMessage = false, + createdAt = Instant.fromEpochSeconds(1_700_000_000), + groupSignedEventId = event.id, + ) + + @Test + fun `a profile the room signed is no line`(): Unit = runBlocking { + val profile = signedBy(room, MetadataEvent.KIND, """{"name":"Translation collective"}""") + + assertNull(apply(profile)) + } + + @Test + fun `a post the room signed is a raw bubble`(): Unit = runBlocking { + val post = signedBy(room, TextNoteEvent.KIND, "We meet on Thursday") + + assertEquals("unsupported", apply(post)?.messageType) + } + + @Test + fun `a member's own post is a raw bubble`(): Unit = runBlocking { + val post = signedBy(member, TextNoteEvent.KIND, "We meet on Thursday") + + assertEquals("unsupported", apply(post)?.messageType) + } + + @Test + fun `lines older builds wrote for the group's profile and posts stay system lines`() { + listOf("groupProfileSigned", "groupPostSigned").forEach { type -> + assertTrue(type in ChatMessage.GROUP_IDENTITY_TYPES, type) + } + } +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SignedGroupKeyStateTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SignedGroupKeyStateTest.kt index 6507e880..1cf6f4c0 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SignedGroupKeyStateTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/SignedGroupKeyStateTest.kt @@ -1253,6 +1253,37 @@ class SignedGroupKeyStateTest { } } + /** + * The premise docs/group-nostr-profile-and-posts.md rests on: what a keyed room + * signs passes `GroupSignedEvent.verifies`, the one gate every reader of the + * group's profile and posts puts an event through. If this fails, the group has + * no nostr identity a reader would accept, whatever screens sit on top. + */ + @Test + fun `a post the group signs verifies as the room's own statement`() = runBlocking { + val creator = device(members[0], signerIndex = 0) + val other = device(members[1], signerIndex = 1) + + val template = TextNoteEvent.build(note = "Sepedi") + val session = FrostSigningManager.proposeSigning( + database = creator.db, + localChatRoom = creator.room, + userPublicKey = creator.publicKey, + kind = template.kind, + tags = template.tags, + content = template.content + ) + pump(creator, other) + FrostSigningManager.approve(other.db, other.room, session.id) + pump(creator, other) + + listOf(creator, other).forEach { device -> + val post = assertNotNull(device.db.groupSignedEventDao().getById(device.item(session.id).eventId)) + assertTrue(post.verifies(), "the room's reader would refuse its own post") + assertEquals(device.room.chatRoom.id, post.chatRoomId) + } + } + @Test fun `a proposer cannot choose the key the group signs as`() = runBlocking { val other = device(members[1], signerIndex = 1)