From 5017547fcda8f5d1d4f97f85967905a750f6540e Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Mon, 23 Nov 2026 19:43:55 +0200 Subject: [PATCH] test: a confederation proposed as {Atlas} keeps that name and signs its charter (group-name-braces, D10) The guard Phase 1 left out. A confederation's name is signed into its proposal and charter and compared byte for byte, so it must never be made bare once it exists. ConfederationPeople takes the name its proposal is signed under; ConfederationBracedNameJvmTest forms one as {Atlas} and checks every device stores it exactly, the charter built from it is signed on every device, and ConfederationChangeRefusal declines the same charter with the braces taken off. All three fail if membership applies bare. Co-Authored-By: Claude Opus 5.5 --- .../ConfederationBracedNameJvmTest.kt | 149 ++++++++++++++++++ .../compose/managers/ConfederationPeople.kt | 4 +- docs/group-name-braces.md | 12 +- 3 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationBracedNameJvmTest.kt diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationBracedNameJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationBracedNameJvmTest.kt new file mode 100644 index 00000000..b78e979d --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationBracedNameJvmTest.kt @@ -0,0 +1,149 @@ +package mobi.giza.compose.managers + +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher +import com.vitorpamplona.quartz.nip01Core.crypto.Nip01Crypto +import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.time.Clock +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.database.model.Confederation +import mobi.giza.compose.database.model.ConfederationSigningStage +import mobi.giza.compose.extensions.toHex +import mobi.giza.compose.nostr.confederation.ConfederationChangeStatements +import mobi.giza.compose.nostr.confederation.ConfederationCharterEvent + +/** + * A confederation whose name, as proposed, has braces in it: the guard on docs/group-name-braces.md, + * D10. + * + * The app draws a confederation as `{{name}}` and never stores the braces, so `ConfederationName.bare` + * runs in one place only -- where a name is typed, before it is signed. A name another device + * proposed as `{Atlas}` therefore stays `{Atlas}` in the proposal, in every device's + * `Confederation`, in the charter, and in the membership both are checked against. + * `ConfederationSigningManager` and `ConfederationChangeRefusal` compare those copies byte for byte, + * and a change that "tidied" any one of them with `bare` would make this group of groups unable to + * sign its own charter. These tests fail if it does. + * + * `{Atlas}` on purpose: a name with braces is what a device that typed them, or a client that draws + * nothing, can put in a proposal. + */ +class ConfederationBracedNameJvmTest { + private val admins = listOf(listOf(11, 12, 13), listOf(21, 22, 23), listOf(31, 32, 33)) + private val network = ConfederationPeople(admins, tag = 0x72, confederationName = BRACED) + private val groups get() = network.groups + private val devices get() = network.devices.values + + private lateinit var confederation: Confederation + private lateinit var proposer: ConfederationPeople.Device + + @BeforeTest + fun formed() = runBlocking { + network.setUp() + proposer = network.device(11) + network.joinAll() + network.pumpGroups() + confederation = assertNotNull(proposer.db.confederationCeremonyDao().getConfederationForProposal(network.proposalId)) + network.withGroupRooms() + } + + @AfterTest + fun close() { + ConfederationSigningManager.lockedForTest = null + ConfederationSigningManager.forgetSignersForTest() + network.close() + } + + private val now get() = Clock.System.now().epochSeconds + + /** The charter as this device would propose it: its membership's name, groups and threshold, unchanged. */ + private fun charter() = runBlocking { assertNotNull(ConfederationProfileManager.charterTemplate(proposer.db, confederation.id)) } + + /** Agora proposes the charter, and every group endorses it, every device holding all of it. */ + private fun endorsedCharter(tags: Array>, content: String): ConfederationChangeStatements.Change = runBlocking { + val rumor = ConfederationChangeStatements.change( + groups[0], groups.drop(1), confederation.id, 2, null, tags, content, now - 5, now + 1000, ConfederationCharterEvent.KIND, + ) + val change = ConfederationChangeStatements.parseChange(rumor)!! + val answers = listOf(1, 2).map { i -> network.groupKeys[i] to ConfederationChangeStatements.endorsement(groups[i], change, now - 4) } + devices.forEach { d -> + ConfederationProfileManager.recordStatement(d.db, rumor, ConfederationStatementFixture.seal(network.groupKeys[0], rumor), null) + answers.forEach { (key, answer) -> ConfederationProfileManager.recordStatement(d.db, answer, ConfederationStatementFixture.seal(key, answer), null) } + } + change + } + + /** [group]'s voucher for [change], as its quorum signs it. */ + private fun voucher(change: ConfederationChangeStatements.Change, group: Int): Event { + val template = ConfederationChangeStatements.voucherTemplate(change, now - 3) + val id = EventHasher.hashId(groups[group], template.createdAt, template.kind, template.tags, template.content) + val sig = Nip01Crypto.sign(id.hexToByteArray(), network.groupKeys[group].value.toByteArray()).toHex() + return Event(id, groups[group], template.createdAt, template.kind, template.tags, template.content, sig) + } + + @Test + fun `the name is kept exactly as proposed, and drawn in doubled braces`() = runBlocking { + devices.forEach { d -> + val held = assertNotNull(d.db.confederationCeremonyDao().getConfederationForProposal(network.proposalId), d.person.take(6)) + assertEquals(BRACED, held.name, "${d.person.take(6)} stored the name it was proposed under") + assertEquals(BRACED, ConfederationProfileManager.membership(d.db, confederation.id)?.name, d.person.take(6)) + } + assertEquals("{{Atlas}}", ConfederationName.display(confederation.name)) + } + + @Test + fun `its charter names it exactly as proposed, and every device signs it`() { + val (tags, content) = charter() + assertEquals(BRACED, ConfederationCharterEvent.parse(Event("0".repeat(64), confederation.id, now, ConfederationCharterEvent.KIND, tags, content, "0".repeat(128)))?.name) + + val change = endorsedCharter(tags, content) + val vouchers = listOf(0, 1, 2).map { voucher(change, it) } + runBlocking { devices.forEach { d -> vouchers.forEach { ConfederationProfileManager.recordVoucher(d.db, it) } } } + val attempt = runBlocking { + ConfederationSigningManager.propose( + proposer.db, confederation.id, proposer.person, ConfederationCharterEvent.KIND, tags, content, proposer.nsec, change.event.createdAt, vouchers, + ) + } + runBlocking { network.pumpGroups() } + + devices.forEach { d -> + val signed = runBlocking { d.db.confederationCeremonyDao().getAttempt(attempt.id) } + assertEquals(ConfederationSigningStage.COMPLETE, signed?.stage, "${d.person.take(6)}: ${signed?.failureReason}") + } + } + + @Test + fun `a group is not asked to decline its charter over the braces, but is over a tidied name`() = runBlocking { + val (tags, content) = charter() + val asProposed = ConfederationChangeStatements.change( + groups[0], groups.drop(1), confederation.id, 2, null, tags, content, now - 5, now + 1000, ConfederationCharterEvent.KIND, + ) + assertNotEquals( + ConfederationChangeRefusal.UNTRUE_CHARTER, + ConfederationChangeRefusal.of(proposer.db, groups[0], proposer.person, asProposed, emptyList(), ConfederationRefusal.Carried(null, null)), + ) + + // The same charter with only the braces taken off its name, as `bare` would: it no longer + // names the confederation under its name, and is declined. Declining this one also shows + // the first got as far as the charter's check rather than stopping before it. + val tidied = tags.map { tag -> if (tag.size >= 2 && tag[0] == ConfederationCharterEvent.NAME_TAG) arrayOf(tag[0], ConfederationName.bare(tag[1])) else tag }.toTypedArray() + assertEquals(listOf("Atlas"), tidied.filter { it[0] == ConfederationCharterEvent.NAME_TAG }.map { it[1] }) + val asTidied = ConfederationChangeStatements.change( + groups[0], groups.drop(1), confederation.id, 2, null, tidied, content, now - 5, now + 1000, ConfederationCharterEvent.KIND, + ) + assertEquals( + ConfederationChangeRefusal.UNTRUE_CHARTER, + ConfederationChangeRefusal.of(proposer.db, groups[0], proposer.person, asTidied, emptyList(), ConfederationRefusal.Carried(null, null)), + ) + } + + private companion object { + const val BRACED = "{Atlas}" + } +} diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationPeople.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationPeople.kt index a7d2fff5..0a1d75b0 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationPeople.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationPeople.kt @@ -46,6 +46,8 @@ class ConfederationPeople( val adminsByGroup: List>, tag: Int = 0x61, val names: List = listOf("Agora", "Polis", "Ekklesia").take(adminsByGroup.size), + /** What the proposal calls the confederation, exactly as it is signed. */ + val confederationName: String = "Atlas", ) { private val tagByte = tag.toByte() fun secret(n: Int) = PrivateKey(ByteArray(32).also { it[0] = tagByte; it[31] = n.toByte() }) @@ -75,7 +77,7 @@ class ConfederationPeople( suspend fun setUp(statementsFor: (Device) -> List> = { everyStatement() }) { val now = Clock.System.now().epochSeconds proposal = ConfederationStatementFixture.proposal( - groupKeys[0], groups, adminsByGroup[0].map(::npub), createdAt = now - 10, expiresAt = now + 1000, + groupKeys[0], groups, adminsByGroup[0].map(::npub), name = confederationName, createdAt = now - 10, expiresAt = now + 1000, ) acceptances = groupKeys.drop(1).mapIndexed { i, key -> ConfederationStatementFixture.acceptance(key, proposal, adminsByGroup[i + 1].map(::npub), createdAt = now - 5) diff --git a/docs/group-name-braces.md b/docs/group-name-braces.md index c59e9560..7e10acd9 100644 --- a/docs/group-name-braces.md +++ b/docs/group-name-braces.md @@ -23,10 +23,14 @@ The build followed the plan, with these notes: title moved into `strings.xml` as `invite_new_member_to` and `invite_new_member_to_chat`. `MarmotGroupName` and `ConfederationName` share their brace-stripping in an internal `NameMarks.strip`. `ChatRoom.subject`'s KDoc points at `title`. -- **Phase 1 did not add** the test that a charter named `{X}` still matches a record named - `{X}`. The comparisons in `ConfederationSigningManager` and `ConfederationChangeRefusal` - are unchanged, and `bare` runs only in `ProposeConfederationViewModel`. The test is still - worth writing. +- **Phase 1 left out** the test that a charter named `{X}` still matches a record named + `{X}`. It was added after Phase 6 as `ConfederationBracedNameJvmTest`, and + `ConfederationPeople` gained a `confederationName` so that a proposal can arrive as + `{Atlas}`. The test checks that every device stores `{Atlas}`, and that the charter is + signed with that name on every device. It also checks that `ConfederationChangeRefusal` + does not decline the charter as proposed, but does decline the same charter with the + braces taken off. All three tests failed when `membership` was briefly changed to apply + `bare`. - **Phase 2** also took Phase 4's `TYPE_SUBGROUP_CERTIFIED` line, because that line was `of`'s last caller and `of` could not be deleted while it remained. It repointed v53's spec at `bare` in this phase too. D8 was built by comparing names through `bare` in